-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsuranceCalc.java
More file actions
91 lines (72 loc) · 2.64 KB
/
InsuranceCalc.java
File metadata and controls
91 lines (72 loc) · 2.64 KB
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
package ie.atu.sw;
public class InsuranceCalc {
final int BASIC_CHARGE = 500;
final int AGE_CHARGE = 100;
int Run(int newAge, int newAccidents) { // Allowing the passing of age and accident parameters makes this method
// testable by our test suite
int total = calcCost(newAge, newAccidents);
return total; // Although total is not strictly needed by the calling class, it is useful to pass back for the sake of testing
}
int calcCost(int age, int accidents) {
int total = 0;
int ageCharge = getAgeSurcharge(age); // get extra cost due to age
int accidentCharge = getAccidentSurcharge(accidents); // get extra costs due to number of accidents
if (accidentCharge < 0) { // if the result of the accident surcharge check was -1, the user is uninsurable
System.out.println("No Insurance!");
return -1;
} else { //Otherwise, return the total cost of insurance
total = ageCharge + accidentCharge + BASIC_CHARGE;
System.out.println("Total amount to pay: " + total);
return total;
}
}
int getAgeSurcharge(int myAge) throws IllegalArgumentException {
int result = 0;
if (myAge < 0) { // negative integers are not accepted
throw new IllegalArgumentException("Only positive arguments accepted");
} else {
if (myAge >= 25) { // no charge for >= 25 yo
System.out.println("No age surcharge");
result = 0;
} else {
result = AGE_CHARGE; // +100 for under 25 yo
System.out.println("Additional surcharge for age: " + result);
}
}
return (result);
}
int getAccidentSurcharge(int myAccidents) throws IllegalArgumentException {
int result = 0;
if (myAccidents < 0) { // negative integers are not accepted
throw new IllegalArgumentException("Only positive arguments accepted");
} else {
switch (myAccidents) { // Do not need to handle negative input as we know the input is not negative
case 0:
System.out.println("No surcharge for accident(s)");
result = 0; // no accidents.. no charge
break;
case 1:
result = 50; // following charges fall in line with that defined in provided program
break; // in future iterations, would be better to create an enumeration to hold the values
case 2:
result = 125;
break;
case 3:
result = 225;
break;
case 4:
result = 375;
break;
case 5:
result = 575;
break;
default:
result = -1;
}
if (result > 0) {
System.out.println("Additional surcharge for accident(s): " + result);
}
}
return (result);
}
}