This repository was archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.java
More file actions
177 lines (138 loc) · 6.32 KB
/
Interface.java
File metadata and controls
177 lines (138 loc) · 6.32 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.io.IOException;
import java.util.Objects;
public class Interface {
Interface() {
}
Operations operations = new Operations();
Temps temps = new Temps();
Temps tempsYN = new Temps(); // For input at "Do you want to continue [y/N]?".
Save save = new Save();
Utilities utilities = new Utilities();
// For getting, setting and saving input.
public void printInpSave(String message, String message1) {
save.save(utilities.print(message));
temps.setFirst(temps.scanner.nextDouble());
temps.scanner.nextLine();
save.saveInp(temps.getFirst());
save.save(utilities.print(message1));
temps.setSecond(temps.scanner.nextDouble());
temps.scanner.nextLine();
save.saveInp(temps.getSecond());
}
public void printInpSave(String message) {
save.save(utilities.print(message));
temps.setFirst(temps.scanner.nextDouble());
temps.scanner.nextLine();
save.saveInp(temps.getFirst());
}
// Gets, validates, sets input and saves it.
public void interOnStart() {
onstart:
while (true) {
String message = "Enter '1' for '+',\n'2' for '-',\n'3' for '*',\n'4' for '**' (the exponent),\n'5' for 'v-' (the square root)\nor '0' to exit.";
save.save(utilities.print(message));
temps.setInput(temps.scanner.nextLine());
save.saveInp(temps.getInput());
if (Objects.equals(temps.getInput(), null)) {
} else if (Objects.equals(temps.getInput(), "")) {
} else if (Objects.equals(temps.getInput(), "0")) {
break;
} else if (Objects.equals(temps.getInput(), "1")) {
temps.setOperator("1");
break;
} else if (Objects.equals(temps.getInput(), "2")) {
temps.setOperator("2");
break;
} else if (Objects.equals(temps.getInput(), "3")) {
temps.setOperator("3");
break;
} else if (Objects.equals(temps.getInput(), "4")) {
temps.setOperator("4");
break;
} else if (Objects.equals(temps.getInput(), "5")) {
temps.setOperator("5");
break;
} else {
String message1 = "Try again.\nEnter a valid input.";
save.save(utilities.print(message1));
continue onstart;
}
}
}
// Gets user input based on the selected operation.
public void interOnRepeat() {
if (!Objects.equals(temps.getOperator(), null)) {
// After the latests numerical inputs there must be a newline character inserted in the stream by using """temps.scanner.nextLine();""" in order to allow the next String input.
if (Objects.equals(temps.getOperator(), "1")) {
String message = "Enter first Addend.";
String message1 = "Enter second Addend.";
printInpSave(message, message1);
} else if (Objects.equals(temps.getOperator(), "2")) {
String message = "Enter subtrahend.";
String message1 = "Enter subtractor.";
printInpSave(message, message1);
} else if (Objects.equals(temps.getOperator(), "3")) {
String message = "Enter multiplicand.";
String message1 = "Enter multiplier.";
printInpSave(message, message1);
} else if (Objects.equals(temps.getOperator(), "4")) {
String message = "Enter base.";
String message1 = "Enter exponent.";
printInpSave(message, message1);
}
// For the square root only one input is required because the second would be '2' as the index of the root.
else if (Objects.equals(temps.getOperator(), "5")) {
String message = "Enter base of square root.";
printInpSave(message);
}
}
}
public void interOnEnd() {
String message = "Program Stopped.";
System.out.println(message);
save.save(message);
}
// Main Loop.
public void inter() throws IOException {
mainLoop:
do {
interOnStart();
if (!(Objects.equals(temps.getInput(), "0"))) {
interOnRepeat();
// Passes the 'operator' value across different instances of 'Temps' class.
operations.temps.setOperator(temps.getOperator());
// Passes the 'first' value across different instances of 'Temps' class.
operations.temps.setFirst(temps.getFirst());
// Passes the 'second' value across different instances of 'Temps' class.
operations.temps.setSecond(temps.getSecond());
save.save(utilities.print(operations.calculations()));
}
// Exit / Continue?.
if (!(Objects.equals(temps.getInput(), "0"))) {
// Continue?
do {
String message = "Do you want to continue [y/N]?";
save.save(utilities.print(message));
tempsYN.setInput(tempsYN.scanner.nextLine());
save.saveInp(tempsYN.getInput());
System.out.println("\n");
} while (!(Objects.equals(tempsYN.getInput(), "Y") || Objects.equals(tempsYN.getInput(), "YES") || Objects.equals(tempsYN.getInput(), "y") || Objects.equals(tempsYN.getInput(), "yes")));
} else {
// Exit?
do {
String message = "Are you sure [y/N]?";
save.save(utilities.print(message));
tempsYN.setInput(tempsYN.scanner.nextLine());
save.saveInp(tempsYN.getInput());
System.out.println("\n");
} while (!(Objects.equals(tempsYN.getInput(), "Y") || Objects.equals(tempsYN.getInput(), "YES") || Objects.equals(tempsYN.getInput(), "y") || Objects.equals(tempsYN.getInput(), "yes")));
}
} while (!(Objects.equals(temps.getInput(), "0")));
interOnEnd();
save.saveHistory();
}
public static void main(String[] args) throws IOException {
Interface interf = new Interface();
interf.inter();
}
}