Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your program must compile and you must provide all of the source code files so t

ID: 3884916 • Letter: Y

Question

Your program must compile and you must provide all of the source code files so that I can also compile and run your program (i.e. provide me with all files ending in .h and .cpp that are necessary to compile your program)

***Can .h and .cpp please be shown which is which? ***

TrainSchedule.com has contracted you to implement a light rail scheduling system. The company will consider your program as a proof-of-concept prototype for future work on a larger system. The program will provide a simple text based interface that will allow the user to enter attributes associated with a given train schedule. The program will manage these attributes in an object, which will consist of an individual node within a single linked list stored in memory. The linked list can be a global variable for simplicity, as done in the Book Inventory sample program. The program must use the “list” API in the C++ standard template library (STL). The program must implement at least one class that will hold the following variables (input validation is only required where indicated): Note: Again, you may use outside resources or books, but you are not to post questions, comments, or source code related to this assignment in any public open forum other than the ones specified for this course in Canvas (e.g. Ask the Instructor or Student Lounge). Also, note that pay for websites for programming solutions are not an acceptable method for completing this assignment. If you have a question, don’t sit on it for too long; ask me in the Ask the Instructor forum or via e-mail. I am also available by appointment via Skype.

scheduleId — An integer variable to hold the schedule identifier. This must be a randomly generated 10-digit number. The scheduleId is a unique identifier for each train schedule in the linked list and repeats are not allowed.

scheduleDate - – 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.

scheduleDays –   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.

trainNumber – An integer variable to hold the train numbe

originStation —  A string variable to hold the name of the station of origin. Note that spaces must be allowed in the string.

origDepartureTime – 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.

destinationStation — A string variable to hold the name of the destination station. Note that spaces must be allowed in the string.

arrivalTime – . 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.

departureTime - 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.

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.

2. 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 following:

(a) EnterSchedule–allowstheusertoenterallofthefieldsassociatedwithagiventrainschedule, except for the scheduleId, which will be automatically generated by the program as previously mentioned. After the fields are entered, the program will place the schedule object in the global linked list.

(b) Display all Schedules – displays all of the schedules within the linked list along with their associated fields. In addition, this option must print the total number of schedules in the linked list.

(c) SearchforSchedule–allowstheusertofindaschedulebyitsscheduleidentifier.Theprogram will prompt the user to enter the scheduleId and will display all of the fields associated with the given schedule, if it is found. The program must display an error message if the schedule is not found in the linked list.

(d) Edit Schedule – allows the user to edit the fields for a given schedule that is in the linked list. The program must prompt the user to enter the scheduleId as the key to find the schedule to edit. Print a message if the schedule is not found in the linked list. For simplicity, the program may re-prompt the user to re-enter all of the fields associated with the given schedule; however, it must reuse the scheduleId value.

(e) DeleteSchedule–allowstheusertodeleteaschedulefromthelinkedlistusingthescheduleId as the key. The program must display a message if the provided scheduleId does not find an associated schedule in the linked list.

(f) Exit System – before the program exits, it must save all of the data in the linked list to the data file. I recommend using a standard text file with one field in the object per line. At this point, if the file does not exist, the program will create it.

Explanation / Answer

package com.itcuties.java.snippets;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* Sample Train Schedule implementation.
*
* @author itcuties
*
*/
public class TrainSchedule {

// Train schedule
private List<Date> schedule;

/**
* Initialize object
*
* @param trainFrequency - in minutes
* @param firstTrain - date of the first train
* @param lastTrain - date of the last train
*/
public TrainSchedule(short trainFrequency, Date firstTrain, Date lastTrain) {
schedule = new ArrayList<Date>();

// Train frequency in milliseconds
long trainFrequencyMillis = trainFrequency*60*1000;

// First and last train date in milliseconds
long firstTrainMillis = firstTrain.getTime();
long lastTrainMillis = lastTrain.getTime();


// Iterate from the first train date to the last train date to initialize the schedule
for (long i=firstTrainMillis; i < lastTrainMillis; i += trainFrequencyMillis) {
schedule.add(new Date(i));
}

// Add the last train literally
schedule.add(lastTrain);
}

/**
* Return time to next train in milliseconds
* @param time
* @return
*/
public long timeToNextTrain(Date time) {
// Current time in milliseconds
long currentTime = time.getTime();

// Check if the trains are riding
if (currentTime <= schedule.get(0).getTime()) // You are before the first train
return schedule.get(0).getTime() - currentTime; // Time to the first train

if (currentTime >= schedule.get(schedule.size()-1).getTime()) { // You are after the last train
// You need to take the first train from the next day schedule
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(schedule.get(0).getTime() + 24*60*60*1000); // Set the next schedule first train time using this schedule first train time + one day in milliseconds

return calendar.getTimeInMillis() - currentTime;
}

// We are in the schedule - go through it
for (int i=0; i < schedule.size(); i++) {
if (i == schedule.size() - 1) // We are at the last element, need to brake the loop
break;
// Time of the previous and next trains
long previous = schedule.get(i).getTime();
long next = schedule.get(i+1).getTime();

if (currentTime > previous && currentTime < next)
return next - currentTime;

}

// Should never happen :)
return -1;
}

/**
* Get the train schedule.
* @return
*/
public List<Date> getSchedule() {
return schedule;
}
}

TrainSchedule class usage

// Set the calendar to 06:00 AM today

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 6);

calendar.set(Calendar.MINUTE, 0);

calendar.set(Calendar.SECOND, 0);

calendar.set(Calendar.MILLISECOND, 0);

// The time of the first train

Date firstTrain = calendar.getTime();

// Let's change the hour

calendar.set(Calendar.HOUR_OF_DAY, 23);

Date lastTrain = calendar.getTime();

// Create the TrainSchedule object

TrainSchedule ts = new TrainSchedule((short)7, firstTrain, lastTrain);

// Display the schedule

System.out.println("Today's schedule:");

for (Date d: ts.getSchedule())

System.out.println(d);

System.out.println("=========================================");

calendar = Calendar.getInstance();

// Let's check how much time we have to the next train

System.out.println("It's " + calendar.getTime() + " you have " + ts.timeToNextTrain(calendar.getTime()) / 1000 + " seconds to the next train");

// Let's check how it would behave when we are before the schedule

calendar.set(Calendar.HOUR_OF_DAY, 5);

System.out.println("It's " + calendar.getTime() + " you have " + ts.timeToNextTrain(calendar.getTime()) / 1000 + " seconds to the first train from today's schedule");

// Let's check how it would behave when we are before the schedule

calendar.set(Calendar.HOUR_OF_DAY, 23);

System.out.println("It's " + calendar.getTime() + " you have " + ts.timeToNextTrain(calendar.getTime()) / 1000 + " seconds to the first train from the next schedule");