-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2Advanced.java
More file actions
110 lines (91 loc) · 3.72 KB
/
Question2Advanced.java
File metadata and controls
110 lines (91 loc) · 3.72 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package Examples.demos;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.Scanner;
public class Question2Advanced {
public static void main(String[] args) {
double result = 0.0;
Scanner scanner = new Scanner(System.in);
UserInput[] questions = defineQuestions();
for (int index = 0; index < questions.length; ++index) {
questions[index].setValue(validateInput(scanner, questions[index].getClazz(), questions[index].getMessage()));
}
switch (questions[1].getValue().toString()) {
case "+":
result = (Double.parseDouble(questions[0].getValue().toString()) + Double.parseDouble(questions[2].getValue().toString()));
break;
case "-":
result = (Double.parseDouble(questions[0].getValue().toString()) - Double.parseDouble(questions[2].getValue().toString()));
break;
case "/":
result = (Double.parseDouble(questions[0].getValue().toString()) / Double.parseDouble(questions[2].getValue().toString()));
break;
case "x":
case "*":
result = (Double.parseDouble(questions[0].getValue().toString()) * Double.parseDouble(questions[2].getValue().toString()));
break;
default:
break;
}
System.out.println(questions[0].getValue().toString() + " " + questions[1].getValue().toString() + " " + questions[2].getValue().toString() + " = " + result);
}
private static UserInput[] defineQuestions() {
UserInput[] values = new UserInput[3];
values[0] = new UserInput<>(Double.class, "Please enter a number.");
values[1] = new UserInput<>(String.class, "Please enter a mathematical operation.");
values[2] = new UserInput<>(Double.class, "Please enter a second number.");
return values;
}
private static class UserInput<T> {
private final Class<T> clazz;
private final String message;
private T value;
public UserInput(Class<T> clazz, String message) {
this.clazz = clazz;
this.message = message;
}
public String getMessage() {
return message;
}
public Class getClazz() {
return this.clazz;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
@Override
public String toString() {
return "UserInput{" +
"clazz=" + clazz +
", message='" + message + '\'' +
", value=" + value +
'}';
}
}
private static <T> T validateInput(Scanner scanner, Class<T> type, String message) {
T value = null;
String input;
String[] acceptedOperations = new String[]{"+", "-", "/", "*", "x"};
System.out.println(message);
while (true) {
input = scanner.nextLine();
try {
if (type == Double.class) {
return (T) Double.valueOf(input);
} else {
if (Arrays.stream(acceptedOperations).anyMatch(input::equals)) {
return (T) input;
}
throw new InvalidParameterException();
}
} catch (NumberFormatException exception) {
System.out.println("Incorrect value, please enter a number.");
} catch (InvalidParameterException exception) {
System.out.println("Incorrect value, please enter a mathematical operation.");
}
}
}
}