Skip to content

Instantly share code, notes, and snippets.

@jailuthra
Last active August 29, 2015 14:05
Show Gist options
  • Save jailuthra/211ff6c5f18221ac9547 to your computer and use it in GitHub Desktop.
Save jailuthra/211ff6c5f18221ac9547 to your computer and use it in GitHub Desktop.
Class 12 Project (needs the worst C++ compiler in the world to compile) (licensed under GPLv3.0)
/*
PROJECT: RAILWAY TICKET RESERVATION
Jai Luthra Ayush Bisht
12-C 12-C
4 2
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
/* GLOBAL CONSTANTS */
const char * TRAINFILE = "train.dat";
const char * TICKETFILE = "ticket.dat";
/*
CUSTOM DATA TYPES
Basic structures and classes not aware of file handling,
they hold info about respective data types
*/
struct Time
{
int hours;
int minutes;
};
struct Date
{
int day; //DD
int month; //MM
int year; //YYYY
};
struct Passenger
{
char name[25];
int age;
char gender;
};
class Train
{
private:
int trainno;
char from[4];
char to[4];
Time depart, arrival;
public:
int getTrainNo() { return trainno; }
char * getFrom() { return from; }
char * getTo() { return to; }
void create();
void show();
};
class Ticket
{
private:
unsigned int pnr;
Train m_train;
Passenger m_passenger;
Date journey_date;
public:
void book(Train, Passenger p, Date);
void show();
int getPNR() { return pnr; }
};
/* CTORS and METHODS */
void Train::create ()
{
cout << endl << endl;
cout << "Train No: ";
cin >> trainno;
cout << "Source and Destination codes (--- ---): ";
cin >> from >> to;
cout << "Departure Time (HH MM): ";
cin >> depart.hours >> depart.minutes;
cout << "Arrival Time (HH MM): ";
cin >> arrival.hours >> arrival.minutes;
}
void Train::show ()
{
cout << "Train No: " << trainno << endl;
cout << "From: " << from << endl;
cout << "To: " << to << endl;
cout << "Departure Time: " << depart.hours << ":" << depart.minutes << endl;
cout << "Arrival Time: " << arrival.hours << ":" << arrival.minutes << endl;
}
void Ticket::book (Train t, Passenger p, Date d)
{
m_train = t;
m_passenger = p;
journey_date = d;
srand (time(NULL));
pnr = rand() % 100000;
}
void Ticket::show ()
{
cout << "PNR No: " << pnr << endl << endl;
cout << "Train Info" << endl;
cout << "----------" << endl;
m_train.show();
cout << endl;
cout << "Name: " << m_passenger.name << endl;
cout << "Age: " << m_passenger.age << endl;
cout << "Gender: " << m_passenger.gender << endl;
cout << "Date: " << journey_date.day << '/' << journey_date.month
<< '/' << journey_date.year << endl;
}
/*
FUNCTIONS
All the File Handling and creation of Train, Ticket objects take place here
User Interaction also happens through these
*/
/* USER MENU FUNCTIONS */
void search_train()
{
Train t;
char from[4], to[4];
cout << "Enter the source station code: ";
cin >> from;
cout << "Enter the destination station code: ";
cin >> to;
fstream file (TRAINFILE, ios::in|ios::binary);
if (!file) {
return;
}
while (!file.eof()) {
file.read((char *) &t, sizeof(Train));
if (file.eof()) {
break;
}
if (strcmp(from, t.getFrom()) == 0 && strcmp(to, t.getTo()) == 0) {
cout << "\n\n";
t.show();
getch();
}
}
file.close();
}
void book_ticket()
{
int trainno;
Train t;
cout << endl << "Train No.: ";
cin >> trainno;
int found = 0;
fstream tfile (TRAINFILE, ios::in|ios::binary);
if (!tfile) {
return;
}
while (tfile) {
tfile.read((char *) &t, sizeof(Train));
if (t.getTrainNo() == trainno) {
found = 1;
break;
}
}
tfile.close();
if (!found) {
cout << endl;
cout << "Train Not Found. Please search train from Main Menu";
getch();
return;
}
Passenger p;
cout << endl << "Please enter passenger details" << endl;
cout << "Name: ";
gets(p.name);
cout << "Age: ";
cin >> p.age;
cout << "Gender (M/F/O): ";
cin >> p.gender;
Date d;
cout << "Date (DD MM YYYY): ";
cin >> d.day >> d.month >> d.year;
Ticket ti;
ti.book(t, p, d);
tfile.open(TICKETFILE, ios::app|ios::binary);
tfile.write((char *) &ti, sizeof(Ticket));
tfile.close();
cout << endl << "Ticket booked. Your PNR no. is: " << ti.getPNR();
getch();
}
void check_pnr()
{
int pnr;
clrscr();
cout << "PNR: ";
cin >> pnr;
Ticket ti;
ifstream file (TICKETFILE, ios::in|ios::binary);
if (!file)
return;
while (!file.eof()) {
file.read((char *) &ti, sizeof(Ticket));
if (file.eof()) {
break;
}
if (ti.getPNR() == pnr) {
ti.show();
}
}
file.close();
getch();
}
void admin_dashboard();
void login()
{
char * password;
cout << "Enter the administrator password: ";
gets(password);
if (strcmp(password, "nimda") == 0) {
cout << "Login Succesful!";
admin_dashboard();
return;
} else {
cout << "Wrong Password!";
getch();
}
}
/* ADMIN MENU FUNCTIONS */
void create_train()
{
Train t;
t.create();
ofstream file (TRAINFILE, ios::binary|ios::app);
file.write((char *) &t, sizeof(Train));
file.close();
cout << "\nCREATED TRAIN!";
cout << "\n\n";
t.show();
getch();
}
void show_trains()
{
ifstream file (TRAINFILE, ios::binary|ios::in);
Train t;
while (file) {
file.read((char *) &t, sizeof(Train));
if (file.eof())
break;
cout << endl << endl;
t.show();
cout << endl;
getch();
}
file.close();
}
void show_tickets()
{
ifstream file (TICKETFILE, ios::binary|ios::in);
Ticket t;
while (file) {
file.read((char *) &t, sizeof(Ticket));
if (file.eof())
break;
cout << endl << endl;
t.show();
cout << endl;
getch();
}
file.close();
}
void admin_dashboard()
{
while (1) {
clrscr();
int opt;
cout << "ADMIN DASHBOARD\n";
cout << "---------------\n\n";
cout << "1. Add Train" << endl;
cout << "2. Show Trains" << endl;
cout << "3. Show Tickets" << endl;
cout << "4. Logout" << endl;
cout << endl << "Enter Option: ";
cin >> opt;
cout << endl;
switch (opt) {
case 1:
create_train();
break;
case 2:
show_trains();
break;
case 3:
show_tickets();
break;
case 4:
return;
default:
cout << "\nUNKNOWN OPTION";
}
}
}
void main_menu()
{
int opt;
clrscr();
cout << "*=*=*=*= RAILWAY TICKET RESERVATION =*=*=*=*\n\n";
cout << "1. Search for trains" << endl;
cout << "2. Book a ticket" << endl;
cout << "3. Check PNR status" << endl;
cout << "4. Staff Login" << endl;
cout << "5. Exit" << endl;
cout << endl << "Enter Option: ";
cin >> opt;
switch (opt) {
case 1:
search_train();
break;
case 2:
book_ticket();
break;
case 3:
check_pnr();
break;
case 4:
login();
break;
case 5:
exit(0);
default:
cout << "\nUNKNOWN OPTION\n";
getch();
}
}
void main() {
while (1) {
main_menu();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment