CS201 assignment solution of 5th

#include <iostream>
#include <stdio.h>
#include <sstream> // Stream building
#include <string> // String Manipulation
#include <fstream> // read/ write file
#include <windows.h> //for Sleep function
#include <time.h> //to get current system time -> Departure time

using namespace std;

class schedule{
string choice, tripp, DoD, DoR, city1, city2, DestinationCity, OriginCity, DTime, ATime;
int lapse, flight;
bool trip; // just declared all the required stuff
public:
schedule(); // constructor of class schedule
void GetData(); // member function
void Allocate(); // member function
void WriteData(); // member function
};
schedule::schedule(){
trip = true; //setting trip to true which can be set to false later by the user through menu
lapse = 2; // Default 2 hours gap between departure and arrival time
};
void schedule::GetData(){
system("CLS"); // clear the garbage
cout <<"Welcome to PIA\n"; // greet the customer
cout <<"Please Select... \n";
menu1:
cout <<"1- Make a Booking 2- Exit\nYour Choice: ";
getline(cin, choice);
if(choice=="1"){ // ok so the customer wana use software
cout << "Are you planning a Round Trip?\nEnter 0 for No or any other value if you mean it.\nYour Choice. ";
getline(cin,tripp); // doesn't the customer want a round trip? we already declared trip to true above.
if(tripp=="0")
{
trip=false; //ok if he doesn't want, let set the trip to false
}
cout<<"Please Provide Details:\n"; // lets move ahead and gather details
menu2:
cout<<"City of Origin:\nPlease Choose Between\n";
cout<<"1- Lahore 2- Karachi 3- Islamabad 4- Peshawar 5- Quetta";
cout<<"\nYour choice: ";
getline(cin,city1);
if(city1=="1")
{
OriginCity="Lahore";
}
else if(city1=="2")
{
OriginCity="Karachi";
}else if(city1=="3")
{
OriginCity="Islamabad";
}else if(city1=="4")
{
OriginCity="Peshawar";
}else if(city1=="5")
{
OriginCity="Quetta";
}
else
{
cout <<"\nhmmm! Invalid Choice. Please try again.\n"; // raise exception, can use catch & throw if was asked in assignment
goto menu2;
}
menu3:
cout<<"\nDestination City:\nPlease Choose Between\n";
cout<<"1- Lahore 2- Karachi 3- Islamabad 4- Peshawar 5- Quetta";
cout<<"\nYour choice: ";
getline(cin,city2);
if(city2==city1)
{
cout <<"\nhmmm! Same Inbound and Outbound City. Please try again.\n"; // raise exception, can use catch & throw if was asked in assignment
goto menu2;
}
else if(city2=="1")
{
DestinationCity="Lahore";
}
else if(city2=="2")
{
DestinationCity="Karachi";
}else if(city2=="3")
{
DestinationCity="Islamabad";
}else if(city2=="4")
{
DestinationCity="Peshawar";
}else if(city2=="5")
{
DestinationCity="Quetta";
}
else
{
cout <<"\nhmmm! Invalid Choice. Please try again.\n"; // raise exception, can use catch & throw if was asked in assignment
goto menu3;
}
menu4:
cout<<"Date of Departure (DD/MM/YY): ";
getline(cin,DoD);
if(trip) // OK, if its a round trip, let's also ask customer about the date of return.
{
cout<<"Date of Return (DD/MM/YY): ";
getline(cin,DoR);
if(DoR==DoD)
{
cout <<"\nhmmm! Same Travel Dates. Please try again.\n"; // raise exception, can use catch & throw if was asked in assignment
goto menu4;
}
}

}
else if(choice=="2"){
cout << "\nThank you for using PIA."; // Let's thank the customer and exit! after a wait of 3000 ms.
Sleep(3000);
exit(1);
}
else{
cout <<"\nhmmm! Invalid Choice. Please try again.\n"; // raise exception, can use catch & throw if was asked in assignment
goto menu1;
}
};
void schedule::Allocate(){
string Cur_Time;
int hours, minutes, seconds, temp_minutes,ATime_Hours, ATime_Minutes;
stringstream temp_ATime,temp_DTime;
char timebuff[9];
flight = rand()%201;
Cur_Time = _strtime(timebuff); // get current system time <time.h>
sscanf(Cur_Time.c_str(), "%d:%d:%d", &hours, &minutes, &seconds); //seperate hours and minutes.
temp_minutes = (hours*60)+minutes+(lapse*60); // make total minutes adding the 2 hours lapse as well
ATime_Hours = temp_minutes/60; //make hours from minutes back.
ATime_Minutes = temp_minutes%60; // get extra minutes after making hours
temp_DTime << hours <<":" <<minutes; // build stream containing temp departure time
DTime = temp_DTime.str(); // build string containing departure time
temp_ATime << ATime_Hours <<":" <<ATime_Minutes;// build stream containing temp arrival time
ATime = temp_ATime.str(); // build string containing arrival time
};
void schedule::WriteData(){
ofstream record("schedule.txt", ios:ut | ios::binary);
if(!record)
{
cout << "Cannot write to file.\n"; // raise exception, can use catch & throw if was asked in assignment
return;
}
else //lets finish this and write every thing to schedule.txt
{
record <<"Flight No: "<< flight << endl;
record <<"Origin: "<< OriginCity << endl;
record <<"Destination: "<< DestinationCity << endl;
record <<"Date of Departure: "<< DoD << endl;
record <<"Date of Return: "<< DoR << endl;
record <<"Departure Time: "<< DTime << endl;
record <<"Arrival Time: "<< ATime << endl;
record.close();
cout <<"\n\nYour reservation has been successfully completed.\n\nPlease wait while we take you back...";
}
};

void main ()
{
Boot:
schedule new_schedule; //new object of class schedule
new_schedule.GetData(); // Prompt costumer
new_schedule.Allocate(); //Generate Flight Number, Departure and Arrival Time
new_schedule.WriteData(); // Write the data to schedule.txt
new_schedule.~schedule(); // destroy the object
Sleep(3000); //take some rest before starting again
goto Boot; //No! a long day ahead again.
}

Sponsored Links