Train Information Portal Project in C++ for the CBSE Students of XII Computer Science - Working Code Download Available

/*
                 TRAIN INFORMATION PORTAL

 I have created this CPP project for my students of XII Computer Science for their reference. In this project the File Handling System is used to store the Train Data. You need to initially enter the data and file will automatically will be created.

 This project is about Train Information only, which takes the input for the Train No, Station From, Station To, Capacity of Passengers, Mail or Express, Fare will be auto assigned by the functions used.

 You may add as many trains into it. Display, Modify, Delete, Restore the deleted items, view the deleted items, Search in different ways. All are perfectly working and ready to run project.

 Just copy entire code into Notepad and save it as Trains.cpp in your bin folder.

 Enjoy, All the best to you.....
*/


#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>

//User Defined Function Prototypes
void menu();
void options();
void drawLine(int,int);
void add();
void displayall();
void searchFrom();
void searchByNo();
void Modify();
void Delete();
void Trash();
void displayE(); //Display the list of Express Trains only.
void displayM(); //Display the list of Mail Trains only.
void intro();    //Starting screen of the project.
void password(); //setting user id and password to start the project.
void conclude(); //At the end of program, message to be displayed...

//Definition of Class...
class Train
{
long Tno;
char From[20];
char To[20];
int Capacity;
char Type; //E for Express, M for Mail
int Distance;
float Fare;
public:
  long getTno(){return Tno;}
  char *GetFrom(){return From;}
  char *GetTo(){return To;}
  char getType(){return Type;}
  void Input()
  {
cout<<"Enter Train No.    : ";
cin>>Tno;
cout<<"Station From       : ";
cin>>From;
cout<<"Station To         : ";
cin>>To;
cout<<"Passenger Capacity : ";
cin>>Capacity;
cout<<"Train Type (M or E): ";
cin>>Type;
cout<<"Enter Distance : ";
cin>>Distance;
if(Type=='E')
{
   if(Distance>1000) Fare=Distance*0.70;
   else if(Distance>800) Fare=Distance*0.75;
   else if(Distance>600) Fare=Distance*0.80;
   else if(Distance>400) Fare=Distance*0.85;
   else if(Distance>200) Fare=Distance*0.90;
   else if(Distance>100) Fare=Distance*0.95;
   else if(Distance>0) Fare=Distance*1.00;
}
else if(Type=='M')
{
   if(Distance>1000) Fare=Distance*0.40;
   else if(Distance>800) Fare=Distance*0.45;
   else if(Distance>600) Fare=Distance*0.50;
   else if(Distance>400) Fare=Distance*0.55;
   else if(Distance>200) Fare=Distance*0.60;
   else if(Distance>100) Fare=Distance*0.65;
   else if(Distance>0) Fare=Distance*0.70;
}
  }
  void Output()
  {
cout<<"\nTrain No. : "<<Tno<<"\tStation From "<<From
    <<" To " <<To<<endl;
cout<<"Type of Train  : "<<Type<<"\twith passenger capacity "
    <<Capacity<<"\n";
cout<<"Distance in Kms: "<<Distance<<"\tFare Rs. : "<<Fare<<"\n";
drawLine(60,14);
  }
}T;

//Displaying Menu Items
void options()
{
clrscr();
cout<<"\n\n";
cout<<"\t==========TRAIN DATABASE==========\n";
cout<<"\t0. Exit from Program...\n";
cout<<"\t1. Add Train Data...\n";
cout<<"\t2. Display Trains Data...\n";
cout<<"\t3. Modify Train Info...\n";
cout<<"\t4. Delete Train Info...\n";
cout<<"\t5. View Deleted Trains Info...\n";
cout<<"\t6. Search by Station From...\n";
cout<<"\t7. Search by Train No....\n";
cout<<"\t8. List of Express Trains....\n";
cout<<"\t9. List of Mail Trains....\n";
cout<<"\n   Enter your Choice : ";
}

void menu()
{
int ch;
options();
cin>>ch;
while(ch)
{
switch(ch)
{
case 1: add(); break;
case 2: displayall(); break;
case 3: Modify(); break;
case 4: Delete(); break;
case 5: Trash(); break;
case 6: searchFrom(); break;
case 7: searchByNo(); break;
case 8: displayE(); break;
case 9: displayM(); break;
default: cout<<"\nWrong Input...\n";
}
cout<<endl<<"\t";
system("pause");
options();
cin>>ch;
}
}

//Adding the records for Train
void add()
{
ofstream fout("Trains.dat",ios::binary|ios::out|ios::app);
T.Input();
fout.write((char*)&T,sizeof(T));
fout.close();
}

//Displaying all the records from Train File.
void displayall()
{
ifstream fin("Trains.dat",ios::binary|ios::in);
while(fin.read((char*)&T,sizeof(T)))
{
T.Output();
}
}

//Drawing the line
void drawLine(int x, int y=14)
{
textcolor(y);
for(int i=0;i<x;i++)
cprintf("=");
cout<<endl;
textcolor(15);
}

//Searching of Train by Station From
void searchFrom()
{
   char *city;
   char found='n';
   cout<<"Enter the city name to be searched from : ";
   cin>>city;
   ifstream fin("Trains.dat",ios::in|ios::binary);
   while(fin.read((char*)&T,sizeof(T)))
   {
if(strcmp(T.GetFrom(),city)==0)
{
T.Output();
found='y';
}
   }
   if(found=='n')
cout<<"\nNo Train is departing from "<<city<<" as base station.";
}

void searchByNo()
{
   int no;
   char found='n';
   cout<<"Enter the Train No. to be searched : ";
   cin>>no;
   ifstream fin("Trains.dat",ios::in|ios::binary);
   while(fin.read((char*)&T,sizeof(T)))
   {
if(T.getTno()==no)
{
T.Output();
found='y';
}
   }
   if(found=='n')
cout<<"\nTrain No. does not match....";
   fin.close();
}

void Modify()
{
fstream fio("Trains.dat",ios::in|ios::out|ios::binary);
int tno, loc;
char found='n';
fio.seekg(0);
cout<<"\nEnter the Train No. whose record is to be modified:";
cin>>tno;

while(fio)
{
  loc=fio.tellg();
  fio.read((char*)&T,sizeof(T));
  if(T.getTno()==tno)
  {
T.Input();
found='y';
fio.seekg(loc);
fio.write((char*)&T,sizeof(T));
cout<<"\nYour record of Train No. "<<tno
    <<" has been updated....";
    break;
  }
}
if(found=='n') cout<<"\nThe Train "<<tno<<" is not in the file...\n";
fio.close();
}

//Deleting the Record from File.
void Delete()
{
int tno;
char found='n';
fstream fio("Trains.dat",ios::in|ios::out|ios::binary);
ofstream fout("TrnTemp.dat",ios::out|ios::binary|ios::app);
ofstream out("Trntrash.dat",ios::out|ios::binary|ios::app);

cout<<"\nEnter the Train No. whose record is to be deleted:";
cin>>tno;

while(fio.read((char*)&T,sizeof(T)))
{
if(T.getTno()==tno)
{
out.write((char*)&T,sizeof(T));
found='y';
}
else
{
fout.write((char*)&T,sizeof(T));
}
}
if(found=='n') cout<<"\nThe Train No."<<tno<<" is not in the file...\n";
fio.close();
fout.close();
remove("Trains.dat");
rename("TrnTemp.dat","Trains.dat");
}

//Displays the deleted data from Trntrah.dat file.
void Trash()
{
ifstream fin("Trntrash.dat",ios::binary|ios::in);
while(fin.read((char*)&T,sizeof(T)))
{
T.Output();
}
fin.close();
}

//Function for displaying the Express Trains List
void displayE()
{
ifstream fin("Trains.dat",ios::in|ios::binary);
char found='n';
while(fin.read((char*)&T,sizeof(T)))
{
  if(T.getType()=='E'||T.getType()=='e')
  {
T.Output();
found='y';
  }
}
if(found=='n')
cout<<"\nThere no Express Train the list of file....";
fin.close();
}

//Function for displaying the Mail Trains List
void displayM()
{
ifstream fin("Trains.dat",ios::in|ios::binary);
char found='n';
while(fin.read((char*)&T,sizeof(T)))
{
  if(T.getType()=='M'||T.getType()=='m')
  {
T.Output();
found='y';
  }
}
if(found=='n')
cout<<"\nThere no Mail Train the list of file....";
fin.close();
}

//Setting the introduction page
void intro()
{
clrscr();
textcolor(14+BLINK);
gotoxy(20,5);
cprintf("=========== TRAIN INFORMATION PORTAL ===========");
cout<<endl;
textcolor(13);
gotoxy(20,10);
cprintf("MADE BY : NARENDRA ALIANI");
cout<<endl;
gotoxy(20,12);
cprintf("SCHOOL  : APS INTERNATIONAL");
cout<<endl;
gotoxy(20,14);
cprintf("CLASS   : XII - COMMERCE");
cout<<endl;
gotoxy(20,16);
cprintf("ROLL NO : 17");
cout<<endl;
gotoxy(50,22);
textcolor(15);
system("pause");
password();
}

//Setting the userid and password
void password()
{
char *uid="admin", *pwd="admin";
int i=1,k=1;
start:
clrscr();
textcolor(11);
cprintf("Enter the Username: ");
cin>>uid;
if(strcmp(uid,"admin")==0)
{
int j=0;
cprintf("Enter the Password: ");
while(pwd[j]!='\0')
{
pwd[j++]=getch(); cout<<"*";
}

if(strcmp(pwd,"admin")==0)
{
cout<<"\n\nPassword matched...";
for(int p=0;p<1;p++)
{
gotoxy(15,20);
cout<<"Loading";
for(int q=0;q<=100;q++)
{
delay(50);
gotoxy(25,20);
cout<<q<<"%";
}
}
cout<<"\nWelcome User : "<<uid<<endl;
system("pause");
}
else
{
cout<<"\nWrong password....";
cout<<"\nPlease enter the correct password...";
cout<<"\nOnly 3 chances will be given to enter the correct password...";
cout<<"\nYou have used "<<k++<<" chance of 3 chances...\n";
getch();

if(k<=3)
goto start;
else
{
cout<<"\nYou have used 3 chances...";
cout<<"\nYou are not authorized person to login...";
delay(2000);
exit(0);
}
}
}
else
{
cout<<"\nWrong Username....";
cout<<"\nPlease enter the correct username...";
cout<<"\nOnly 3 chances will be given to enter the correct Id & password...";
cout<<"\nYou have used "<<i++<<" chance of 3 chances...\n";
getch();
if(i<=3)
goto start;
else
{
cout<<"\nYou have used 3 chances...";
cout<<"\nYou are not authorized person to login...\n";
delay(2000);
exit(0);
}
}
}

//Concluding Message at the end of program
void conclude()
{
clrscr();
textcolor(14+BLINK);
gotoxy(20,5);
cprintf("=========== TRAIN INFORMATION PORTAL ===========");
cout<<endl;
textcolor(13);
gotoxy(20,10);
cprintf("MADE BY : NARENDRA ALIANI");
cout<<endl;
gotoxy(20,12);
cprintf("SCHOOL  : APS INTERNATIONAL");
cout<<endl;
gotoxy(20,14);
cprintf("CLASS   : XII - COMMERCE");
cout<<endl;
gotoxy(20,16);
cprintf("ROLL NO : 17");
cout<<endl;
textcolor(11+BLINK);
gotoxy(20,20);
cprintf("THANKS FOR USING....");
cout<<endl;
gotoxy(20,24);
cout<<"Press any key to exit...";
}

//Main Program
void main()
{
clrscr();
intro();
menu();
conclude();
getch();
}


/*
You may download the working cpp file from the following link. 
Share it with your friends if you like the project.
Write in comments if you have any query or requirements related to project. 


*/

No comments

Post your comments

Powered by Blogger.