-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
229 lines (174 loc) · 6.57 KB
/
Copy pathMain.java
File metadata and controls
229 lines (174 loc) · 6.57 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package application;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
public class Main extends Application {
private double width = 700;
private double height = 600;
ArrayList<Person> personList = new ArrayList<Person>();
ArrayList<Person> payPerson = new ArrayList<Person>();
ArrayList<Person> receivePerson = new ArrayList<Person>();
public Scene solutionPage(Stage stage) {
double transferAmount = 0.0;
for(int i = 0 ; i<payPerson.size() ; i++) {
payPerson.get(i).balance = MainActivity.average - payPerson.get(i).totalPaid;
System.out.println("pay: " + payPerson.get(i).name + " balance " + payPerson.get(i).balance);
}
for(int i = 0 ; i<receivePerson.size() ; i++) {
receivePerson.get(i).balance = receivePerson.get(i).totalPaid - MainActivity.average;
System.out.println("receive: " + receivePerson.get(i).name + " balance " + receivePerson.get(i).balance);
transferAmount += receivePerson.get(i).balance;
}
System.out.println(transferAmount);
ArrayList<Text> solution = new ArrayList<Text>();
int i = 0;
int j = 0;
while(transferAmount>0) {
if(payPerson.get(i).balance == receivePerson.get(j).balance) {
Text t = new Text(payPerson.get(i).name + " need to pay " + receivePerson.get(j).name + " RM" + receivePerson.get(j).balance);
solution.add(t);
transferAmount -= receivePerson.get(j).balance;
i++;
j++;
}
else if(payPerson.get(i).balance > receivePerson.get(j).balance) {
Text t = new Text(payPerson.get(i).name + " need to pay " + receivePerson.get(j).name + " RM" + receivePerson.get(j).balance);
payPerson.get(i).balance -= receivePerson.get(j).balance;
System.out.println(payPerson.get(i).balance);
transferAmount -= receivePerson.get(j).balance;
solution.add(t);
j++;
}
else if(payPerson.get(i).balance < receivePerson.get(j).balance) {
Text t = new Text(payPerson.get(i).name + " need to pay " + receivePerson.get(j).name + " RM" + payPerson.get(j).balance);
receivePerson.get(i).balance -= payPerson.get(j).balance;
transferAmount -= payPerson.get(j).balance;
solution.add(t);
i++;
}
}
GridPane grid = new GridPane();
for(int k = 0 ; k< solution.size() ; k++) {
grid.add(solution.get(k), 0, k);
}
BorderPane pane = new BorderPane();
pane.setCenter(grid);
Scene scene = new Scene(pane, width, height);
return scene;
}
public Scene totalSpentPage(Stage stage) {
HBox header = new HBox();
GridPane grid = new GridPane();
ArrayList<TextField> field = new ArrayList<TextField>();
for(int i = 0 ; i<personList.size() ; i++) {
Text label = new Text("How much " + personList.get(i).name + " pay: ");
TextField fields = new TextField();
field.add(fields);
grid.add(label, 0, i);
grid.add(fields, 1, i);
}
Button submit = new Button("submit");
grid.add(submit, 1, personList.size());
submit.addEventHandler(MouseEvent.MOUSE_CLICKED, (e)->{
for(int i = 0 ; i<personList.size() ; i++) {
personList.get(i).totalPaid = Double.parseDouble(field.get(i).getText());
MainActivity.totalSpent += Double.parseDouble(field.get(i).getText());
}
MainActivity.average = MainActivity.totalSpent/MainActivity.peopleCount;
for(int i = 0 ; i<personList.size() ; i++) {
if(personList.get(i).totalPaid == MainActivity.average) {
continue;
}
else if(personList.get(i).totalPaid > MainActivity.average) {
receivePerson.add(personList.get(i));
}
else {
payPerson.add(personList.get(i));
}
}
for(int i = 0 ; i<payPerson.size() ; i++) {
System.out.println("pay: " + payPerson.get(i).name);
}
for(int i = 0 ; i<receivePerson.size() ; i++) {
System.out.println("receive" + receivePerson.get(i).name);
}
stage.setScene(solutionPage(stage));
});
BorderPane pane = new BorderPane();
pane.setTop(header);
pane.setCenter(grid);
Scene scene = new Scene(pane, width, height);
return scene;
}
public Scene addNamePage(Stage stage) {
HBox header = new HBox();
header.getChildren().add(new Text("Add name"));
GridPane grid = new GridPane();
ArrayList<TextField> field = new ArrayList<TextField>();
for(int i = 0 ; i<personList.size() ; i++) {
Text labelName = new Text("Name " + (i+1));
TextField nameField = new TextField();
field.add(nameField);
grid.add(labelName, 0, i);
grid.add(nameField, 1, i);
}
Button submit = new Button("submit");
grid.add(submit, 1, personList.size());
submit.addEventHandler(MouseEvent.MOUSE_CLICKED, (e)->{
for(int i = 0 ; i<personList.size() ; i++) {
personList.get(i).name = field.get(i).getText();
System.out.println(personList.get(i).name);
}
stage.setScene(totalSpentPage(stage));
});
BorderPane pane = new BorderPane();
pane.setTop(header);
pane.setCenter(grid);
Scene scene = new Scene(pane, width, height);
return scene;
}
public Scene mainPage(Stage stage) {
Text personText = new Text("How many people: ");
TextField numPersonField = new TextField();
Button submitButton = new Button("submit");
submitButton.addEventHandler(MouseEvent.MOUSE_CLICKED, (e)->{
MainActivity.peopleCount = Integer.parseInt(numPersonField.getText());
System.out.println(MainActivity.peopleCount);
for(int i = 0 ; i<MainActivity.peopleCount ; i++) {
Person p = new Person();
personList.add(p);
}
stage.setScene(addNamePage(stage));
});
HBox header = new HBox();
header.getChildren().add(new Text("Home"));
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.add(personText, 0, 0);
grid.add(numPersonField, 0, 1);
grid.add(submitButton, 0, 2);
BorderPane pane = new BorderPane();
pane.setTop(header);
pane.setCenter(grid);
Scene scene = new Scene(pane, width, height);
return scene;
}
@Override
public void start(Stage stage) {
stage.setScene(mainPage(stage));
stage.setTitle("bill calculator");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}