summaryrefslogtreecommitdiff
path: root/src/section-4/BanffMarathonRunner.java
blob: afc04726700e501de2e50c085615404e730c3b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ca.mokhan.assignment1;

import java.util.ArrayList;
import java.util.Arrays;

public class BanffMarathonRunner extends AddressBook {
  private int time;
  private int years;

  public BanffMarathonRunner(String firstName, String lastName, int time, int years) {
    super(firstName, "", lastName);
    this.time = time;
    this.years = years;
  }

  public int compareTo(AddressBook other) {
    BanffMarathonRunner runner = (BanffMarathonRunner) other;
    return Integer.compare(this.time, runner.time);
  }

  @Override
  public String toString() {
    return super.getFirstName() + " " + this.years;
  }

  public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
    Arrays.sort(runners);
    return runners[0];
  }

  public static BanffMarathonRunner getSecondFastestRunner(BanffMarathonRunner[] runners) {
    Arrays.sort(runners);
    return runners[1];
  }

  public static int getAverageTime(BanffMarathonRunner[] runners) {
    int sum = 0;
    for (BanffMarathonRunner runner : runners) sum += runner.time;
    return sum / runners.length;
  }

  public static String getAboveAverageRunners(BanffMarathonRunner[] runners) {
    int average = getAverageTime(runners);
    ArrayList<String> winners = new ArrayList<String>();

    for (BanffMarathonRunner runner : runners)
      if (runner.time >= average) winners.add(runner.toString());

    return String.join(System.lineSeparator(), winners);
  }
}