-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawCash.java
More file actions
217 lines (186 loc) · 7.67 KB
/
Copy pathWithdrawCash.java
File metadata and controls
217 lines (186 loc) · 7.67 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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.AudioClip;
import javafx.scene.media.MediaException;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
class WithdrawCash implements EventHandler<ActionEvent> {
private final Account acc;
private final Controller controller = ATM.getController();
WithdrawCash(Account acc) {
this.acc = acc;
}
/**
* Checks if the withdraw amount is possible or not, by checking the user's account and how much
* money is left in the ATM, and what denominations are available.
*
* @param acc The user's account
* @param withdrawAmt The withdraw amount
* @return Whether or not the transaction can be completed
*/
private WithdrawResult checkIfPossibleToWithdraw(Account acc, float withdrawAmt) {
WithdrawResult result = new WithdrawResult();
if (withdrawAmt != (int) withdrawAmt) {
result.setErrorMessage(String
.format("You cannot withdraw coins. However, you can withdraw %f instead.", withdrawAmt));
return result;
} else if (controller.getVault().getTotal() < withdrawAmt) {
result.setErrorMessage("The ATM does not have enough money to service your request.");
return result;
} else if (withdrawAmt > acc.getRemainingDailyWithdrawLimit()) {
result.setErrorMessage("This withdraw would exceed your daily withdraw limit.");
return result;
} else
return uncheckedWithdraw(withdrawAmt);
}
/**
* Checks if it is possible to withdraw, but does not check the status of the vault, the user's
* cash limit, or if the amount is valid
*
* @param withdrawAmt The amount to withdraw
* @return Whether or not the value can be withdrawn, and if it can, returns the bills that need
* to be withdrawn from the vault
*/
private WithdrawResult uncheckedWithdraw(float withdrawAmt) {
WithdrawResult result = new WithdrawResult();
// get the data from the vault and see how many of each denomination
// we have
Map<Integer, Integer> denominationsAvailable = new HashMap<>();
denominationsAvailable.put(50, controller.getVault().getNumOfFifties());
denominationsAvailable.put(20, controller.getVault().getNumOfTwenties());
denominationsAvailable.put(10, controller.getVault().getNumOfTens());
denominationsAvailable.put(5, controller.getVault().getNumOfFives());
int runningWithdraw = (int) withdrawAmt;
Map<Integer, Integer> withdrawDenominations = new HashMap<>();
/*
* Iterate through all of the bills, and subtract the largest possible bill as many times as
* possible from the user's withdraw amount.
*/
for (int i = 0; i < Utilities.stdDenominations.size(); i++) {
int currBillVal = Utilities.stdDenominations.get(i);
int numOfNeededBills = runningWithdraw / currBillVal;
if (numOfNeededBills <= denominationsAvailable.get(currBillVal)) {
withdrawDenominations.put(currBillVal, numOfNeededBills);
runningWithdraw = runningWithdraw - (numOfNeededBills * currBillVal);
} else {
withdrawDenominations.put(currBillVal, 0);
}
}
// if the amount that remains is zero, then we can make a withdraw
if (runningWithdraw == 0) {
// user can withdraw money because enough denominations exist
// calculate how many bills they should withdraw
result.setWithdrawAmount(withdrawDenominations);
System.out.println("$50 x " + withdrawDenominations.get(50));
System.out.println("$20 x " + withdrawDenominations.get(20));
System.out.println("$10 x " + withdrawDenominations.get(10));
System.out.println("$5 x " + withdrawDenominations.get(5));
result.setDidSucceed(true);
return result;
} else if (runningWithdraw > 0) {
/*
* There was a remainder left from calculating the bills, so the remainder cannot be
* withdrawn. Subtract the remainder from the user's withdraw amount and ask if they'd like to
* have that out instead.
*/
result.setErrorMessage("You can't withdraw that amount. Would you like to withdraw $"
+ (withdrawAmt - runningWithdraw) + " instead?");
return result;
} else {
// there's no money left in the ATM, or a fatal calculation
// error occurred
result.setErrorMessage("An unknown error occurred.");
return result;
}
}
@Override
public void handle(ActionEvent e) {
Stage primaryStage = View.primaryStage;
primaryStage.setTitle(Utilities.ATMName + " - Withdrawal");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 600, 400);
Text scenetitle = new Text("How much would you like to withdraw?");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label withdrawAmount = new Label("Amount in dollars");
grid.add(withdrawAmount, 0, 1);
final TextField withdrawAmountField = new TextField();
grid.add(withdrawAmountField, 1, 1);
Button withdrawButton = new Button("Go");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(withdrawButton);
grid.add(hbBtn, 1, 4);
Button returnCard = new Button("Return Card");
grid.add(returnCard, 2, 0);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
returnCard.setOnAction(new ExitScreen());
withdrawButton.setOnAction(f -> {
try {
withdrawFunds(Float.valueOf(withdrawAmountField.getText()), acc, withdrawAmount);
} catch (NumberFormatException e1) {
System.out.println("The number was formatted incorrectly.");
e1.printStackTrace();
}
});
returnCard.setOnAction(h -> System.exit(0));
primaryStage.setScene(scene);
primaryStage.show();
}
private void withdrawFunds(float withdrawAmt, Account acc,
Label withdrawAmount) {
if (acc != null) {
WithdrawResult result = checkIfPossibleToWithdraw(acc, withdrawAmt);
if (result.didSucceed())
dispenseCash(withdrawAmt, result);
else
withdrawAmount.setText(result.getErrorMessage());
}
}
private void dispenseCash(float withdrawAmt, WithdrawResult result) {
this.acc.setBalance(this.acc.getBalance() - withdrawAmt);
// http://stackoverflow.com/questions/24097059/
for (int i : result.getWithdrawAmount().keySet()) {
for (Iterator<Bill> billsIter = controller.getVault().getDenominationValue(i); billsIter.hasNext(); ) {
Bill item = billsIter.next();
System.out.println(item.toString() + " bill being dispensed...");
billsIter.remove();
}
}
// play withdraw sound
try {
playAudioFromURL("withdraw.mp3");
} catch (Exception e) {
// in case the file gets moved, or is deleted for some weird reason
System.out.println("The audio file could not be played.");
}
System.out.println("Money dispensed.");
View.primaryStage.setScene(MainMenu.mainMenu);
}
private void playAudioFromURL(String clipURL) {
try {
AudioClip audio = new AudioClip(clipURL);
audio.play();
} catch (MediaException e) {
System.out.println("The audio could not be played because " + e.getMessage());
}
}
}