COMP2396
Loading...
Searching...
No Matches
Question1.java
Go to the documentation of this file.
1package assignment1;
2import java.io.*;
3
4class Route {
5 String route_name;
6 String company_code;
7 int full_fare;
8 Route(String[] route) {
9 this.route_name = route[0];
10 this.company_code = route[1];
11 this.full_fare = Integer.parseInt(route[2]);
12 }
13
14 // Compare the fare of two routes
15 public boolean expensive_than(Route route) {
16 return this.full_fare > route.full_fare;
17 }
18 public int fare_difference(Route route) {
19 return Math.abs(this.full_fare - route.full_fare);
20 }
21 public boolean operatedByBNB() {
22 return this.company_code.equals("BNB");
23 }
24 public boolean routeNameStartsWith(String prefix) {
25 return this.route_name.startsWith(prefix);
26 }
27}
28
29class Question1 {
30 public static void main(String[] args) throws IOException {
31 InputStreamReader isr = new InputStreamReader(System.in);
32 BufferedReader br = new BufferedReader(isr);
33 int remaining_balance = Integer.parseInt(br.readLine());
34 Route first = new Route(br.readLine().split(" "));
35 Route second = new Route(br.readLine().split(" "));
36
37 if (remaining_balance >= 1) {
38 // Check for (3) "does not apply" first
39 if (!first.operatedByBNB() || second.routeNameStartsWith("A")) {
40 // Full fare
41 remaining_balance -= second.full_fare;
42 } else if (!first.routeNameStartsWith("P") & second.expensive_than(first)) {
43 remaining_balance -= first.fare_difference(second);
44 }
45 }
46 System.out.println("The remaining balance is " + remaining_balance + ".");
47 }
48}