summaryrefslogtreecommitdiff
path: root/src/Q4/BanffMarathonRunner.java
blob: d9f56179e952e01a81df09de6e5c2f23f35c372b (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * Assignment 1, COMP268 Class: BanffMarathonRunner.java
 *
 * @description Represents a marathon runner
 * @author: mo khan Student ID: 3431709
 * @date May 8, 2019
 * @version 1.0
 */
package Q4;

import Q1.*;
import java.util.ArrayList;
import java.util.Arrays;

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

  /**
   * Constructs a BanffMarathonRunner object.
   *
   * @param firstName the first name of the runner
   * @param lastName the last name of the runner
   * @param time the time it took the runner to complete the marathon
   * @param years the number of years they participated
   */
  public BanffMarathonRunner(String firstName, String lastName, int time, int years) {
    super(firstName, "", lastName);
    this.time = time;
    this.years = years;
  }

  /**
   * Performs a comparison of this runner with another based on the time it took them to complete
   * the marathon. Because java generics does not allow implementing a generic interface with
   * different type parameters, this code assumes the AddressBook is an instance of a Runner.
   *
   * @param other the other runner to compare against
   * @return a negative integer, zero, or a positive integer as the first argument is less than,
   *     equal to, or greater than the second.
   */
  public int compareTo(AddressBook other) {
    BanffMarathonRunner runner = (BanffMarathonRunner) other;
    return Integer.compare(this.time, runner.time);
  }

  /**
   * Return the time take to complete the race.
   *
   * @return the time taken to complete the race
   */
  public int getTime() {
    return this.time;
  }

  /**
   * Returns a string representation of the runner.
   *
   * @return The first name + the # of years that they participated.
   */
  @Override
  public String toString() {
    return super.getFirstName() + " " + this.years;
  }

  /**
   * Sorts the list of runners based on time, then returns the fastest runner.
   *
   * @param runners the list of runners.
   * @return the fastest runner
   */
  public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
    Arrays.sort(runners);
    return runners[0];
  }

  /**
   * Sorts the list of runners based on time, then returns the second fastest runner.
   *
   * @param runners the list of runners.
   * @return the second fastest runner
   */
  public static BanffMarathonRunner getSecondFastestRunner(BanffMarathonRunner[] runners) {
    Arrays.sort(runners);
    return runners[1];
  }

  /**
   * Calculates the average time that it took the runners to complete the marathon.
   *
   * @param runners the array of runners.
   * @return the average time taken to complete the marathon
   */
  public static int getAverageTime(BanffMarathonRunner[] runners) {
    int sum = 0;
    for (BanffMarathonRunner runner : runners) sum += runner.time;
    return sum / runners.length;
  }

  /**
   * Returns the runners that finished the marathon in above or equal to average time.
   *
   * @param runners the list of runners
   * @return the list of runners that finished the marathon in above average time.
   */
  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);
  }

  public static void main(String[] args) {
    BanffMarathonRunner[] runners = {
      new BanffMarathonRunner("Elena", "Brandon", 341, 1),
      new BanffMarathonRunner("Thomas", "Molson", 273, 2),
      new BanffMarathonRunner("Hamilton", "Winn", 278, 5),
      new BanffMarathonRunner("Suzie", "Sarandin", 329, 7),
      new BanffMarathonRunner("Philip", "Winne", 445, 9),
      new BanffMarathonRunner("Alex", "Trebok", 275, 3),
      new BanffMarathonRunner("Emma", "Pivoto", 275, 4),
      new BanffMarathonRunner("John", "Lenthen", 243, 1),
      new BanffMarathonRunner("James", "Lean", 334, 1),
      new BanffMarathonRunner("Jane", "Ostin", 412, 1),
      new BanffMarathonRunner("Emily", "Car", 393, 4),
      new BanffMarathonRunner("Daniel", "Hamshire", 299, 4),
      new BanffMarathonRunner("Neda", "Bazdar", 343, 3),
      new BanffMarathonRunner("Aaron", "Smith", 317, 6),
      new BanffMarathonRunner("Kate", "Hen", 265, 8)
    };

    BanffMarathonRunner fastestRunner = BanffMarathonRunner.getFastestRunner(runners);
    System.out.println(fastestRunner.getFirstName());
    System.out.println(fastestRunner.getHomeAddress());
    System.out.println(fastestRunner.getTime());

    BanffMarathonRunner secondFastestRunner = BanffMarathonRunner.getSecondFastestRunner(runners);
    System.out.println(secondFastestRunner.getFirstName());
    System.out.println(secondFastestRunner.getHomeAddress());
    System.out.println(secondFastestRunner.getTime());
    System.out.println(secondFastestRunner.getTime() - fastestRunner.getTime());

    System.out.print(BanffMarathonRunner.getAboveAverageRunners(runners));
  }
}