-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculations.java
76 lines (70 loc) · 1.94 KB
/
Calculations.java
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
/**
* Calculations.java
* @description Handles calculation operations
* @author R Samman
* @version 2.0 04-04-2022
*/
/**
* Calculations class represents a the operations handling for a calculator.
*/
public class Calculations {
private String currentOperation;
private Integer storedValue = 0;
private boolean multipleEquals = false;
/**
* Constructor.
*/
public Calculations() {}
/**
* Setting operation handling.
*/
public void handleOperation(String number, String op) {
storedValue = Integer.parseInt(number);
currentOperation = op;
multipleEquals = false;
}
/**
* Sets the calculations algorihtim/logic.
*/
public String performCalculation(String secondNumber) {
int secondNum = Integer.parseInt(secondNumber);
int remainder = 0;
int result;
switch (currentOperation) {
case "+":
result = storedValue + secondNum;
break;
case "-":
result = storedValue - secondNum;
break;
case "\u00D7":
result = storedValue * secondNum;
break;
case "\u00F7":
remainder = storedValue % secondNum;
result = storedValue / secondNum;
break;
default:
return "";
}
if (!multipleEquals) {
storedValue = secondNum;
}
multipleEquals = true;
return (remainder == 0) ? Integer.toString(result) : Integer.toString(result) + " R" + remainder;
}
/**
* Gets the stored value in memory.
*/
public Integer getStoredValue() {
return storedValue;
}
/**
* Main method.
*/
public static void main(String[] args) {
Calculations testCalculator = new Calculations();
testCalculator.handleOperation("23", "*");
System.out.println(testCalculator.performCalculation("4"));
}
}