-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsuranceProgram.java
More file actions
71 lines (49 loc) · 1.92 KB
/
InsuranceProgram.java
File metadata and controls
71 lines (49 loc) · 1.92 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
package ie.atu.sw;
import java.util.Scanner;
public class InsuranceProgram {
// This program has been re-written with testing primarily in mind.
// Many of the design choices were made to help demonstrate the requirements in the testing exercise.
// Program entry point
public static void main(String[] args) {
InsuranceCalc myCalc = new InsuranceCalc();
Scanner myScanner = getScanner();
int age = getAge(myScanner);
int accidents = getAccidents(myScanner);
myCalc.Run(age, accidents);
myScanner.close();
}
// getScanner method created to be able to test for issues with Scanner creation
static Scanner getScanner() {
Scanner newScanner;
newScanner = new Scanner(System.in);
return newScanner;
}
// getAge method will throw exception in the event it is passed a null argument
static int getAge(Scanner myScanner) throws NullPointerException {
int result = -1;
if (myScanner != null) { // Testing the following block code only adds value if testing the Scanner
// utility for I/O which is not in scope for this project
while (result < 0) { // Ensures result is not negative
System.out.print("Enter your age: ");
result = myScanner.nextInt();
}
} else {
throw new NullPointerException();
}
return (result);
}
// getAccidents method will throw exception in the event it is passed a null argument
static int getAccidents(Scanner myScanner) throws NullPointerException {
int result = -1;
if (myScanner != null) { // Testing the following block code only adds value if testing the Scanner
// utility for I/O which is not in scope for this project
while (result < 0) { // Ensures result is not negative
System.out.print("How many accidents did you have? ");
result = myScanner.nextInt();
}
} else {
throw new NullPointerException();
}
return (result);
}
}