Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- //@author: Emilija Gjorgjevska
- class Driver implements Comparable<Driver> {
- private String name;
- private String best_time;
- public Driver(String name, String best_time) {
- this.name = name;
- this.best_time = best_time;
- }
- @Override
- public int compareTo(Driver o) {
- return (best_time.compareTo(o.best_time));
- }
- public String toString(int i) {
- return String.format("%d. %-10s%10s\n", i, name, best_time);
- }
- }
- class F1Race {
- private ArrayList<Driver> drivers;
- public F1Race() {
- drivers = new ArrayList<Driver>();
- }
- public void readResults(InputStream inputStream) {
- Scanner in = new Scanner(inputStream);
- while (in.hasNextLine()) {
- String read = in.nextLine();
- String[] parts = read.split("\\s++");
- String minimum = parts[1];
- for (int i = 2; i < parts.length; i++) {
- if (minimum.compareTo(parts[i]) > 0)
- minimum = parts[i];
- }
- drivers.add(new Driver(parts[0], minimum));
- }
- in.close();
- }
- public void printSorted(OutputStream outputStream) {
- Collections.sort(drivers);
- PrintWriter out = new PrintWriter(outputStream);
- for (int i = 0; i < drivers.size(); i++)
- System.out.print(drivers.get(i).toString(i + 1));
- out.close();
- }
- }
- class F1Test {
- public static void main(String[] args)
- throws ArrayIndexOutOfBoundsException {
- F1Race f1Race = new F1Race();
- f1Race.readResults(System.in);
- f1Race.printSorted(System.out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment