Filip_Markoski

[NP] Ф1 Трка

Nov 11th, 2017
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.PrintWriter;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Scanner;
  7.  
  8. class Lap implements Comparable<Lap> {
  9.     int minutes;
  10.     int seconds;
  11.     int nanoSeconds;
  12.  
  13.     public Lap(int minutes, int seconds, int nanoSeconds) {
  14.         this.minutes = minutes;
  15.         this.seconds = seconds;
  16.         this.nanoSeconds = nanoSeconds;
  17.     }
  18.  
  19.     @Override
  20.     public String toString() {
  21.         return String.format("%d:%02d:%03d", minutes, seconds, nanoSeconds);
  22.     }
  23.  
  24.     @Override
  25.     public int compareTo(Lap that) {
  26.         if (this.minutes < that.minutes) {
  27.             return -1;
  28.         } else if (this.minutes > that.minutes) {
  29.             return 1;
  30.         } else if (this.minutes == that.minutes) {
  31.             if (this.seconds < that.seconds) {
  32.                 return -1;
  33.             } else if (this.seconds > that.seconds) {
  34.                 return 1;
  35.             } else if (this.seconds == that.seconds) {
  36.                 if (this.nanoSeconds < that.nanoSeconds) {
  37.                     return -1;
  38.                 } else if (this.nanoSeconds > that.nanoSeconds) {
  39.                     return 1;
  40.                 } else if (this.nanoSeconds == that.nanoSeconds) {
  41.                     return 0;
  42.                 }
  43.             }
  44.         }
  45.         return 0;
  46.     }
  47. }
  48.  
  49. class Driver implements Comparable<Driver> {
  50.     String name;
  51.     ArrayList<Lap> laps;
  52.  
  53.     private Lap makeLap(String lap) {
  54.         String numbers[] = lap.split(":");
  55.         int minutes = Integer.parseInt(numbers[0]);
  56.         int seconds = Integer.parseInt(numbers[1]);
  57.         int nanoSeconds = Integer.parseInt(numbers[2]);
  58.         return new Lap(minutes, seconds, nanoSeconds);
  59.     }
  60.  
  61.     public Driver(String name, String... lap) {
  62.         this.name = name;
  63.         laps = new ArrayList<>();
  64.         for (int i = 0; i < lap.length; i++) {
  65.             laps.add(makeLap(lap[i]));
  66.         }
  67.     }
  68.  
  69.     /*public Lap bestLap() {
  70.         Lap max = new Lap(0, 0, 0);
  71.         int maxIndex = 0;
  72.         for (int i = 0; i < laps.size(); i++) {
  73.             if (max.compareTo(laps.get(i)) < 0) {
  74.                 max = laps.get(i);
  75.                 maxIndex = i;
  76.             }
  77.         }
  78.         return laps.get(maxIndex);
  79.     }*/
  80.  
  81.     public Lap bestLap() {
  82.         Collections.sort(laps);
  83.         return laps.get(0);
  84.     }
  85.  
  86.     @Override
  87.     public String toString() {
  88.         return String.format("%-10s%10s", name, bestLap().toString());
  89.     }
  90.  
  91.     @Override
  92.     public int compareTo(Driver that) {
  93.         return this.bestLap().compareTo(that.bestLap());
  94.     }
  95. }
  96.  
  97. class F1Race {
  98.     ArrayList<Driver> drivers;
  99.  
  100.     public F1Race() {
  101.         drivers = new ArrayList<>();
  102.     }
  103.  
  104.     public void readResults(InputStream inputStream) {
  105.         Scanner input = new Scanner(inputStream);
  106.         while (input.hasNextLine()) {
  107.             String row[] = input.nextLine().split(" ");
  108.             String name = row[0];
  109.             String lapOne = row[1];
  110.             String lapTwo = row[2];
  111.             String lapThree = row[3];
  112.             Driver driver = new Driver(name, lapOne, lapTwo, lapThree);
  113.             drivers.add(driver);
  114.         }
  115.     }
  116.  
  117.     public void printSorted(OutputStream outputStream) {
  118.         PrintWriter pw = new PrintWriter(outputStream);
  119.         Collections.sort(drivers);
  120.         for (int i = 0; i < drivers.size(); i++) {
  121.             pw.println(i + 1 + ". " + drivers.get(i));
  122.         }
  123.         pw.flush();
  124.     }
  125. }
  126.  
  127. public class F1Test {
  128.  
  129.     public static void main(String[] args) {
  130.         F1Race f1Race = new F1Race();
  131.         f1Race.readResults(System.in);
  132.         f1Race.printSorted(System.out);
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment