Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Trains {
- private Map<String, TrainInfo> trains;
- public Trains() {
- trains = new HashMap<>();
- }
- public void process() throws IOException {
- BufferedReader in = null;
- try {
- InputStream stream = Trains.class.getResourceAsStream("trainlog.txt");
- in = new BufferedReader(new InputStreamReader(stream));
- String line;
- while ((line = in.readLine()) != null) {
- String[] data = line.split(";");
- boolean arrival = data[1].trim().equals("arrival");
- String id = data[0].trim();
- TrainInfo train = trains.get(id);
- if (train == null) {
- if (arrival) {
- System.err.println("There is no arrival for the train " + id);
- System.exit(-1);
- }
- train = new TrainInfo(id);
- trains.put(id, train);
- }
- int time = Integer.parseInt(data[2].trim());
- if (arrival)
- train.arrived(time);
- else
- train.departed(time);
- } // end while()
- print();
- } finally {
- if (in != null)
- in.close();
- }
- }
- private void print() {
- Set<TrainInfo> set = new TreeSet<>();
- set.addAll(trains.values());
- for (TrainInfo t: set)
- System.out.println(t);
- }
- public static void main(String[] args) throws IOException {
- new Trains().process();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement