randomly generated 10-digit number. The scheduleId is a unique identifier for ca
ID: 3860297 • Letter: R
Question
randomly generated 10-digit number. The scheduleId is a unique identifier for cach train schedule in the linked list and repeats are not allowed. A string variable to hold the date that the schedule will take effect. The date must be in the form mm/dd/yyyy. Where mm is the two- digit month, dd is the two-digit day, and yyyy is a four-digit year Input Validation Required: The program must re-prompt the user to re-enter the date if it is not the valid format provided above. scheduleDate An integer variable to hold the number of days that the schedule will be valid. Input Validation Required: The program must re-prompt the user to re-enter the number if it is not greater than 0 . scheduleDays . trainNumber - An integer variable to hold the train number A string variable to hold the name of the station of origin. Note that spaccs . originStation - must be allowed in the string. A string variable that will hold the time that the train will depart from the station of origin. This variable must be in the form HHMM where HH is a number from 00-23 and MM is a number from 00- 59 Input Validation Required: The program must re-prompt the user to re-enter the time if it is not the valid format provided above .origDepartureTime - destinationStation - A string variable to hold the name of the destination station. Note that spaces must be allowed in the string. A string variable that will hold the time that the train will arrive that the destination station. Input validation is recommended but is not required. This variable should be in the form HHMM. . arrivalTime A string variable that will hold the time that the train will depart from the station. Again, input validation is recommended but not required here. This variable should be in the form HHMM . departureTime - Provide the appropriate methods to set and get the data for each of these class variables. For example setscheduleDate(string scheduleDate) and string getscheduleDate ). The Book Inventory provides an example on how this can be done. In addition, the main program must provide the following functionality: 1. When the program is first started, it must read a data file called schedule.dat. The program will not prompt the user for the name of the data file. The name of the file must be hard-coded in the program. If the file exists, the program will load the data for each schedule into the global linked list. If the file does not exist, the program will start with an empty linked list. The program will provide a simple text-based user interface that manages the all of the train schedules within a linked list. Each schedule must be placed in the linked list as an object that holds all of the attributes associated with it as mentioned above. The user interface will allow the user to perform the 2.Explanation / Answer
#include<iostream>
#include <string.h>
#include<list>
#include<algorithm>
#include<fstream>
using namespace std;
class TrainSchedule
{
private:
int scheduleId; //An integer variable to hold the schedule identifier
string scheduleDate; //A string variable to hold the date that the schedule will take effect mm/dd/yyyy
int scheduleDays; // An integer variable to hold the number of days that the schedule will be valid.
int trainNumber;
string originStation ;
string origDepartureTime; // HHMM
string destinationStation ;
string arrivalTime; // HHMM
public:
TrainSchedule()
{
}
TrainSchedule(string scheduleDate,int scheduleDays,int trainNumber,string originStation,string origDepartureTime,string destinationStation,string arrivalTime)
{
int id = 1000000000 + ( std::rand() % ( 100000000000 - 1000000000 + 1 ) );
scheduleId = id;
scheduleDate = scheduleDate;
scheduleDays = scheduleDays;
trainNumber = trainNumber;
originStation = originStation;
origDepartureTime = origDepartureTime;
destinationStation =destinationStation;
arrivalTime = arrivalTime;
}
void setscheduleId()
{
int id = 1000000000 + ( std::rand() % ( 100000000000 - 1000000000 + 1 ) );
scheduleId = id;
}
int getscheduleId()
{
return scheduleId;
}
void setscheduleDate(string date)
{
scheduleDate = date;
}
string getscheduleDate()
{
return scheduleDate;
}
void setscheduleDays(int days)
{
scheduleDays = days;
}
int getscheduleDays()
{
return scheduleDays;
}
void settrainNumber(int no)
{
trainNumber = no;
}
int gettrainNumber()
{
return trainNumber;
}
void setOriginStation(string originStation) { this->originStation = originStation; }
string getOriginStation() { return this->originStation; }
void setOrigDepartureTime(string origDepartureTime) { this->origDepartureTime = origDepartureTime; }
string getOrigDepartureTime() { return this->origDepartureTime; }
void setDestinationStation(string destinationStation) { this->destinationStation = destinationStation; }
string getDestinationStation() { return this->destinationStation; }
void setArrivalTime(string arrivalTime) { this->arrivalTime = arrivalTime; }
string getArrivalTime() { return this->arrivalTime; }
};
list<TrainSchedule> mylist;
void EnterSchedule()
{
string scheduleDate; //A string variable to hold the date that the schedule will take effect mm/dd/yyyy
cout << "Enter scheduleDate: ";
getline(cin,scheduleDate);
int scheduleDays; // An integer variable to hold the number of days that the schedule will be valid.
cout << "Enter scheduleDays: ";
cin >> scheduleDays;
int trainNumber;
cout << "Enter trainNumber: ";
cin >> trainNumber;
string originStation ;
cout << "Enter originStation: ";
getline(cin,originStation);
string origDepartureTime; // HHMM
cout << "Enter origDepartureTime: ";
getline(cin,origDepartureTime);
string destinationStation ;
cout << "Enter destinationStation: ";
getline(cin,destinationStation);
string arrivalTime; // HHMM
cout << "Enter arrivalTime: ";
getline(cin,arrivalTime);
TrainSchedule tobj(scheduleDate,scheduleDays,trainNumber,originStation,origDepartureTime,destinationStation,arrivalTime);
mylist.push_back(tobj);
}
//display schedule
void DisplaySchedule()
{
for(auto it=mylist.begin();it!=mylist.end();it++)
{
TrainSchedule t = *it;
cout << t.getscheduleId()<< endl;; //An integer variable to hold the schedule identifier
cout << t.getscheduleDate()<< endl;; //A string variable to hold the date that the schedule will take effect mm/dd/yyyy
cout << t.getscheduleDays()<< endl;; // An integer variable to hold the number of days that the schedule will be valid.
cout << t.gettrainNumber()<< endl;;
cout << t.getOriginStation()<< endl; ;
cout << t.getOrigDepartureTime()<< endl;; // HHMM
cout << t.getDestinationStation()<< endl; ;
cout << t.getArrivalTime()<< endl; // HHMM
}
}
bool SearchSchedule(int id)
{
for(auto it=mylist.begin();it!=mylist.end();it++)
{
TrainSchedule t = *it;
if(t.getscheduleId() == id)
return true;
}
return false;
}
bool EditSchedule(int id)
{
if(SearchSchedule(id) == false)
return false;
else
{
for(auto it=mylist.begin();it!=mylist.end();it++)
{
TrainSchedule t = *it;
if(t.getscheduleId() == id)
break;
}
cout <<"enter field want to change" << endl;
//here we can set all field that we want to edit
//with the help of setter function in our class
return true;
}
}
void DeleteSchedule(int id)
{
if(SearchSchedule(id) == false)
return;
else
{
auto it = mylist.begin();
for(it=mylist.begin();it!=mylist.end();it++)
{
TrainSchedule t = *it;
if(t.getscheduleId() == id)
break;
}
mylist.erase(it);
}
}
int main()
{
TrainSchedule t;
ifstream inFile;
long size = sizeof(t);
inFile.open("schedule.dat",ios::binary | ios::ate);
if (!inFile)
cout << "Error opening data file. ";
else
{
inFile.read(reinterpret_cast<char*> (&t), sizeof(t));
}
//enterSchedule
EnterSchedule();
DisplaySchedule();
int id;
cin >> id;
SearchSchedule(id);
while(EditSchedule(id)== false)
{
cout << "enter schedule id again to edit" << endl;
cin >> id;
}
cout <<"enter id to delete"<< endl;
cin >> id;
DeleteSchedule(id);
return 0;
}
//due to time constraint i am not able to store back data in schedule.dat file.