Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- class Lap implements Comparable<Lap> {
- int minutes;
- int seconds;
- int nanoSeconds;
- public Lap(int minutes, int seconds, int nanoSeconds) {
- this.minutes = minutes;
- this.seconds = seconds;
- this.nanoSeconds = nanoSeconds;
- }
- @Override
- public String toString() {
- return String.format("%d:%02d:%03d", minutes, seconds, nanoSeconds);
- }
- @Override
- public int compareTo(Lap that) {
- if (this.minutes < that.minutes) {
- return -1;
- } else if (this.minutes > that.minutes) {
- return 1;
- } else if (this.minutes == that.minutes) {
- if (this.seconds < that.seconds) {
- return -1;
- } else if (this.seconds > that.seconds) {
- return 1;
- } else if (this.seconds == that.seconds) {
- if (this.nanoSeconds < that.nanoSeconds) {
- return -1;
- } else if (this.nanoSeconds > that.nanoSeconds) {
- return 1;
- } else if (this.nanoSeconds == that.nanoSeconds) {
- return 0;
- }
- }
- }
- return 0;
- }
- }
- class Driver implements Comparable<Driver> {
- String name;
- ArrayList<Lap> laps;
- private Lap makeLap(String lap) {
- String numbers[] = lap.split(":");
- int minutes = Integer.parseInt(numbers[0]);
- int seconds = Integer.parseInt(numbers[1]);
- int nanoSeconds = Integer.parseInt(numbers[2]);
- return new Lap(minutes, seconds, nanoSeconds);
- }
- public Driver(String name, String... lap) {
- this.name = name;
- laps = new ArrayList<>();
- for (int i = 0; i < lap.length; i++) {
- laps.add(makeLap(lap[i]));
- }
- }
- /*public Lap bestLap() {
- Lap max = new Lap(0, 0, 0);
- int maxIndex = 0;
- for (int i = 0; i < laps.size(); i++) {
- if (max.compareTo(laps.get(i)) < 0) {
- max = laps.get(i);
- maxIndex = i;
- }
- }
- return laps.get(maxIndex);
- }*/
- public Lap bestLap() {
- Collections.sort(laps);
- return laps.get(0);
- }
- @Override
- public String toString() {
- return String.format("%-10s%10s", name, bestLap().toString());
- }
- @Override
- public int compareTo(Driver that) {
- return this.bestLap().compareTo(that.bestLap());
- }
- }
- class F1Race {
- ArrayList<Driver> drivers;
- public F1Race() {
- drivers = new ArrayList<>();
- }
- public void readResults(InputStream inputStream) {
- Scanner input = new Scanner(inputStream);
- while (input.hasNextLine()) {
- String row[] = input.nextLine().split(" ");
- String name = row[0];
- String lapOne = row[1];
- String lapTwo = row[2];
- String lapThree = row[3];
- Driver driver = new Driver(name, lapOne, lapTwo, lapThree);
- drivers.add(driver);
- }
- }
- public void printSorted(OutputStream outputStream) {
- PrintWriter pw = new PrintWriter(outputStream);
- Collections.sort(drivers);
- for (int i = 0; i < drivers.size(); i++) {
- pw.println(i + 1 + ". " + drivers.get(i));
- }
- pw.flush();
- }
- }
- public class F1Test {
- public static void main(String[] args) {
- F1Race f1Race = new F1Race();
- f1Race.readResults(System.in);
- f1Race.printSorted(System.out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment