DamSi

Untitled

Jul 16th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. //@author: Emilija Gjorgjevska
  4.  
  5. class Driver implements Comparable<Driver> {
  6.     private String name;
  7.     private String best_time;
  8.  
  9.     public Driver(String name, String best_time) {
  10.         this.name = name;
  11.         this.best_time = best_time;
  12.     }
  13.  
  14.     @Override
  15.     public int compareTo(Driver o) {
  16.         return (best_time.compareTo(o.best_time));
  17.     }
  18.  
  19.     public String toString(int i) {
  20.         return String.format("%d. %-10s%10s\n", i, name, best_time);
  21.     }
  22. }
  23.  
  24. class F1Race {
  25.     private ArrayList<Driver> drivers;
  26.  
  27.     public F1Race() {
  28.         drivers = new ArrayList<Driver>();
  29.     }
  30.  
  31.     public void readResults(InputStream inputStream) {
  32.         Scanner in = new Scanner(inputStream);
  33.         while (in.hasNextLine()) {
  34.             String read = in.nextLine();
  35.             String[] parts = read.split("\\s++");
  36.             String minimum = parts[1];
  37.             for (int i = 2; i < parts.length; i++) {
  38.                 if (minimum.compareTo(parts[i]) > 0)
  39.                     minimum = parts[i];
  40.             }
  41.             drivers.add(new Driver(parts[0], minimum));
  42.         }
  43.         in.close();
  44.     }
  45.  
  46.     public void printSorted(OutputStream outputStream) {
  47.         Collections.sort(drivers);
  48.         PrintWriter out = new PrintWriter(outputStream);
  49.         for (int i = 0; i < drivers.size(); i++)
  50.             System.out.print(drivers.get(i).toString(i + 1));
  51.         out.close();
  52.     }
  53. }
  54.  
  55. class F1Test {
  56.     public static void main(String[] args)
  57.             throws ArrayIndexOutOfBoundsException {
  58.         F1Race f1Race = new F1Race();
  59.         f1Race.readResults(System.in);
  60.         f1Race.printSorted(System.out);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment