Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here are my codes: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.ListIterator; import java.util.Scanner; public class TrainTimeTable { private ArrayList schedule; public void delay(String station,

image text in transcribedimage text in transcribedimage text in transcribed

Here are my codes:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.ListIterator;

import java.util.Scanner;

public class TrainTimeTable {

private ArrayList schedule;

public void delay(String station, int minutes) {

boolean startDelay = false;

ListIterator sIt = schedule.listIterator();

while (sIt.hasNext()) {

Station currStation = sIt.next();

if (!startDelay && currStation.getCity().equals(station)) {

startDelay = true;

}

if( startDelay ){

currStation.delay(minutes);

}

}

}

public String getCities() {

String cities = "";

for (Station ast : schedule) {

cities += ast.getCity() + " ";

}

return cities;

}

public void displaySchedule() {

System.out.printf("%16s\t%10s\t%10s\t%s ", "Station", "Arrival", "Departure", "Day");

for (int i = 0; i

schedule.get(i).displaySchedule();

}

}

public TrainTimeTable(ArrayList schedule) {

super();

this.schedule = schedule;

}

public static void main(String[] args) throws ParseException {

ArrayList schedule = new ArrayList();

SimpleDateFormat sdf = new SimpleDateFormat("d HH:mm");

schedule.add(new Station(null, sdf.parse("1 20:30"), "Vancouver"));

schedule.add(new Station(sdf.parse("2 06:00"), sdf.parse("2 06:35"), "Kamloops"));

schedule.add(new Station(sdf.parse("2 16:00"), sdf.parse("2 17:30"), "Jasper"));

schedule.add(new Station(sdf.parse("2 23:00"), sdf.parse("2 23:59"), "Edmonton"));

schedule.add(new Station(sdf.parse("3 08:00"), sdf.parse("3 08:25"), "Saskatoon"));

schedule.add(new Station(sdf.parse("3 20:45"), sdf.parse("3 22:30"), "Winnipeg"));

schedule.add(new Station(sdf.parse("4 05:02"), sdf.parse("4 05:42"), "Sioux Lookout"));

schedule.add(new Station(sdf.parse("4 15:35"), sdf.parse("4 16:10"), "Hornepayne"));

schedule.add(new Station(sdf.parse("5 00:18"), sdf.parse("5 00:48"), "Capreol"));

schedule.add(new Station(sdf.parse("5 09:30"), null, "Toronto"));

TrainTimeTable ttt = new TrainTimeTable(schedule);

Scanner inp = new Scanner(System.in);

String cmd = "";

System.out.println(("Timetable for a train travelling between Vancouver and Toronto").toUpperCase());

while (!cmd.equalsIgnoreCase("Quit")) {

System.out.print("Input [Quit | Delay | Show] timetable: ");

cmd = inp.next();

if (cmd.equalsIgnoreCase("Show")) {

System.out.println();

System.out.println("\t\tSHOWING TRAIN SCHEDULE: ");

ttt.displaySchedule();

} else if (cmd.equalsIgnoreCase("Delay")) {

System.out.print("Please enter the station where train is delayed: ");

String station = inp.next();

System.out.print("Please enter the delay time in minutes: ");

int minutes = inp.nextInt();

ttt.delay(station, minutes);

System.out.println();

System.out.println("\t\tSHOWING NEW SCHEDULE: ");

ttt.displaySchedule();

}

System.out.println();

}

inp.close();

}

}

------------------------------------

import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar;

public class Station { private Date arrival; private Date departure; private String city;

/** * @param arrival * @param departure * @param city */ public Station(Date arrival, Date departure, String city) { super(); this.arrival = arrival; this.departure = departure; this.city = city; }

public String getArrival() { if (arrival == null) return ""; return new SimpleDateFormat("HH:mm").format(arrival); }

/** * @param arrival * the arrival to set */ public void setArrival(Date arrival) { this.arrival = arrival; }

public int getDate() { Calendar cal = new GregorianCalendar(); if (departure == null) cal.setTime(arrival); else cal.setTime(departure); // potential bug here. If delay/pull-up over a year return cal.get(Calendar.DAY_OF_YEAR); }

public void displaySchedule() { System.out.printf("%16s\t%10s\t%8s\t%10d ", getCity(), getArrival(), getDeparture(), getDate()); }

public void delay(int minutes) { // negative delay is allowed but no guarantee on the date part Calendar cal = new GregorianCalendar(); if (arrival != null) { cal.setTime(arrival); cal.add(Calendar.MINUTE, minutes); arrival = cal.getTime(); } if (departure != null ) { cal.setTime(departure); cal.add(Calendar.MINUTE, minutes); departure = cal.getTime(); } }

public String getDeparture() { if (departure == null) return ""; return new SimpleDateFormat("HH:mm").format(departure); }

/** * @param departure * the departure to set */ public void setDeparture(Date departure) { this.departure = departure; }

/** * @return the city */ public String getCity() { return city; }

/** * @param city * the city to set */ public void setCity(String city) { this.city = city; }

}

------------------------------

Here is my question: I would like to display the new schedule showing the delayed arrival, departure, and day at the delayed station. How would I do that?

10. [40 marks] A train timetable for a train travelling between Vancouver and Toronto is given below Station Vancouver Kamloops Jasper Edmonton Saskatoon Winnipeg Sioux Lookout 05:0205:42 Hornepayne 15:35 16:10 Capreol Toronto ArrivaDeparture Da 20:30 1 06:00 06:35 16:0017:30 23:00 23:59 08:00 08:25 20:45 22:30 4 4 00:18 00:48 09:30 Store the information from each row of the table in an object. Then, arrange the objects in an ArrayList structure Your program should now take the following commands in a continuous loop I. Show-shows the full table II. Delay -the arrival of the train is delayed by at station ; that is, add the delay to the corresponding station entry. For example, Delay Edmonton 30 implies that the train would arrive 30 minutes later than the expected time of arrival in Edmonton. The new entry would be "Edmonton 23:30 00:29 3". All stations following Edmonton will also update their arrival and departure by +30 minutes, and consequently the day of arrival and departure as well. The result of this Delay command is shown below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

state what is meant by the term performance management

Answered: 1 week ago