我的计划是列车时刻表,显示城市的到达和离开时间。在程序中,“延迟”输入会提示您键入城市名称和分钟数,以便在该城市的到达时间内添加延迟(以分钟为单位)。此信息全部存储在ArrayList
中这是我的延迟方法:
public void delay(String station, int minutes) {
// negative delay in minutes are allowed but no guarantee of the
// consistency of schedules of previous stations
boolean startDelay = false;
ListIterator<Station> sIt = schedule.listIterator();
while (sIt.hasNext()) {
// first find out the index of station
Station currStation = sIt.next();
if (!startDelay && currStation.getCity().equals(station)) {
startDelay = true;
} else {
currStation.delay(minutes);
}
}
startDelay = false;
}
但是,正是在这一点上,我必须在列表的其余时间添加相同的分钟,但是在实施延迟之前没有将这些更改应用到城市。
例如,如果我输入“Edmonton”和“30”,输出应如下所示:
Link to what output should look like
这是我的主要方法:
public static void main(String[] args) throws ParseException {
ArrayList<Station> schedule = new ArrayList<Station>();
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();
}
}
答案 0 :(得分:0)
稍作修改应该可以解决您的问题。当站名匹配时,将startDelay设置为true,并将dd设置为此站和所有后续站的延迟。
public void delay(String station, int minutes) {
boolean startDelay = false;
ListIterator<Station> sIt = schedule.listIterator();
while (sIt.hasNext()) {
Station currStation = sIt.next();
if (!startDelay && currStation.getCity().equals(station)) {
startDelay = true;
}
if( startDelay ){
currStation.delay(minutes);
}
}
}
编辑,遵循David的建议:简化方法体,
boolean startDelay = false;
for( Station currStation: schedule ){
if ...
if ...
}