import DataStructures.*; import java.io.*; import java.util.StringTokenizer; public class TrainDemo { public static void reportSideTrack(String heading, Stack track) { System.out.println(heading); while (! track.isEmpty()) { TrainCar car = (TrainCar) track.pop(); System.out.println(car.as_string()); } System.out.println(""); } public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader( new FileReader(args[0]) ); String line; Stack trenton = new StackArray(); Stack charlotte = new StackArray(); Stack baltimore = new StackArray(); Stack elsewhere = new StackArray(); while ((line = in.readLine()) != null) { StringTokenizer st = new StringTokenizer(line); String id = st.nextToken(); String cargo = st.nextToken(); String src = st.nextToken(); String dest = st.nextToken(); TrainCar car = new TrainCar(id, cargo, src, dest); if (dest.compareTo("Trenton") == 0) { trenton.push(car); } else if (dest.compareTo("Charlotte") == 0) { charlotte.push(car); } else if (dest.compareTo("Baltimore") == 0) { baltimore.push(car); } else { elsewhere.push(car); } } reportSideTrack("cars bound for Trenton:", trenton); reportSideTrack("cars bound for Charlotte:", charlotte); reportSideTrack("cars bound for Baltimore:", baltimore); reportSideTrack("cars bound for elsewhere:", elsewhere); } }