-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.java
146 lines (118 loc) · 3.55 KB
/
Controller.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
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
package com.project.demo3;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
public class Controller {
@FXML
private TextField textField;
@FXML
private Text savedNumbers;
private String firstNumber = "";
private String currentNumber = "";
private String calculationType;
@FXML
void addAction(ActionEvent event) {
calculationSetup("+");
}
@FXML
void minusAction(ActionEvent event) {
calculationSetup("-");
}
@FXML
void divideAction(ActionEvent event) {
calculationSetup("/");
}
@FXML
void multiplicationAction(ActionEvent event) {
calculationSetup("*");
}
public void calculationSetup(String calculationType){
this.calculationType = calculationType;
firstNumber = currentNumber;
currentNumber = "";
savedNumbers.setText(firstNumber + " " + calculationType);
}
@FXML
void calculate(ActionEvent event) {
int firstNumberInt = Integer.parseInt(firstNumber);
int secondNumberInt = Integer.parseInt(currentNumber);
switch (calculationType) {
case "+" -> {
int calculatedNumber = firstNumberInt + secondNumberInt;
savedNumbers.setText(firstNumber + " + " + currentNumber + " = " + calculatedNumber);
textField.setText(String.valueOf(calculatedNumber));
}
case "-" -> {
int calculatedNumber = firstNumberInt - secondNumberInt;
savedNumbers.setText(firstNumber + " - " + currentNumber + " = " + calculatedNumber);
textField.setText(String.valueOf(calculatedNumber));
}
case "/" -> {
double calculatedNumber = firstNumberInt / (double)secondNumberInt;
savedNumbers.setText(firstNumber + " / " + currentNumber + " = " + calculatedNumber);
textField.setText(String.valueOf(calculatedNumber));
}
case "*" -> {
int calculatedNumber = firstNumberInt * secondNumberInt;
savedNumbers.setText(firstNumber + " * " + currentNumber + " = " + calculatedNumber);
textField.setText(String.valueOf(calculatedNumber));
}
}
}
@FXML
void clearTextField(ActionEvent event) {
currentNumber = "";
textField.setText("");
savedNumbers.setText("");
}
@FXML
void button0Clicked(ActionEvent event) {
if(!currentNumber.equals("")){
addNumber("0");
}
}
@FXML
void button1Clicked(ActionEvent event) {
addNumber("1");
}
@FXML
void button2Clicked(ActionEvent event) {
addNumber("2");
}
@FXML
void button3Clicked(ActionEvent event) {
addNumber("3");
}
@FXML
void button4Clicked(ActionEvent event) {
addNumber("4");
}
@FXML
void button5Clicked(ActionEvent event) {
addNumber("5");
}
@FXML
void button6Clicked(ActionEvent event) {
addNumber("6");
}
@FXML
void button7Clicked(ActionEvent event) {
addNumber("7");
}
@FXML
void button8Clicked(ActionEvent event) {
addNumber("8");
}
@FXML
void button9Clicked(ActionEvent event) {
addNumber("9");
}
public void updateTextField(){
textField.setText(currentNumber);
}
public void addNumber(String number){
currentNumber += number;
updateTextField();
}
}