-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCalculator.java
More file actions
63 lines (58 loc) · 1.66 KB
/
TestCalculator.java
File metadata and controls
63 lines (58 loc) · 1.66 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
package com.bham.pij.assignments.calculator;
import java.util.Scanner;
public class TestCalculator {
public static void main(String[] args) {
Calculator calc = new Calculator();
String input = "";
Scanner s = new Scanner(System.in);
// Loops program until the user enters "quit"
while(!input.equals("quit")) {
System.out.println("Enter expression to evaluate: (Enter 'quit' to close)");
input = s.nextLine();
// As long as the last result isn't invalid it will save it in memory, otherwise saves 0
if(input.equals("m")) {
if(calc.getCurrentValue() != Float.MIN_VALUE) {
calc.setMemoryValue(calc.getCurrentValue());
}
else {
calc.setMemoryValue(0);
}
}
// Returns current value in memory
else if(input.equals("mr")) {
System.out.println(calc.getMemoryValue());
}
// Clears memory to 0
else if(input.equals("c")) {
calc.setMemoryValue(0);
}
// Outputs the history as the numbers separated by a space
else if(input.equals("h")) {
for(int i = 0; i < calc.getHistory().size(); i++) {
if(i != calc.getHistory().size() - 1) {
System.out.print(calc.getHistoryValue(i) + " ");
}
else {
System.out.println(calc.getHistoryValue(i));
}
}
}
// Exits the program
else if(input.equals("quit")) {
System.out.println("Exiting...");
}
// If none of the following apply it feeds it to the calculator
else {
float result = calc.evaluate(input);
if(result == Float.MIN_VALUE) {
System.out.println("Invalid Input");
}
else {
System.out.println(result);
}
}
}
// After the loop finishes it closes the Scanner
s.close();
}
}