-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawal.java
More file actions
153 lines (131 loc) · 6.97 KB
/
Withdrawal.java
File metadata and controls
153 lines (131 loc) · 6.97 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
// Withdrawal.java
// Represents a withdrawal ATM transaction
public class Withdrawal extends Transaction {
private int amount; // amount to withdraw
private Keypad keypad; // reference to keypad
private CashDispenser cashDispenser; // reference to cash dispenser
// constant corresponding to menu option to cancel
private final static int CANCELED = 6;
// Withdrawal constructor
public Withdrawal(int userAccountNumber, Screen atmScreen,
BankDatabase atmBankDatabase, Keypad atmKeypad,
CashDispenser atmCashDispenser) {
// initialize superclass variables
super(userAccountNumber, atmScreen, atmBankDatabase);
// initialize references to keypad and cash dispenser
keypad = atmKeypad;
cashDispenser = atmCashDispenser;
} // end Withdrawal constructor
// Execute withdrawal for GUI
public boolean executeGUI(int selectedAmount) {
BankDatabase bankDatabase = getBankDatabase();
Screen screen = getScreen();
// Check if the selected amount is valid
if (selectedAmount != CANCELED) {
double availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
if (selectedAmount <= availableBalance) {
if (cashDispenser.isSufficientCashAvailable(selectedAmount)) {
bankDatabase.debit(getAccountNumber(), selectedAmount);
cashDispenser.dispenseCash(selectedAmount);
screen.displayMessageLine("\nPlease take your cash now.");
return true; // Withdrawal successful
} else {
screen.displayMessageLine("\nInsufficient cash available in the ATM.");
return false; // Insufficient cash in ATM
}
} else {
screen.displayMessageLine("\nInsufficient funds in your account.");
return false; // Insufficient funds in account
}
} else {
screen.displayMessageLine("\nTransaction canceled.");
return false; // Transaction canceled
}
}
// perform transaction
public void execute() {
boolean cashDispensed = false; // cash was not dispensed yet
double availableBalance; // amount available for withdrawal
// get references to bank database and screen
BankDatabase bankDatabase = getBankDatabase();
Screen screen = getScreen();
// loop until cash is dispensed or the user cancels
do {
// obtain a chosen withdrawal amount from the user
amount = displayMenuOfAmounts();
// check whether user chose a withdrawal amount or canceled
if (amount != CANCELED) {
// get available balance of account involved
availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
// check whether the user has enough money in the account
if (amount <= availableBalance) {
// check whether the cash dispenser has enough money
if (cashDispenser.isSufficientCashAvailable(amount)) {
// update the account involved to reflect withdrawal
bankDatabase.debit(getAccountNumber(), amount);
cashDispenser.dispenseCash(amount); // dispense cash
cashDispensed = true; // cash was dispensed
// instruct user to take cash
screen.displayMessageLine("\nPlease take your cash now.");
} // end if
else // cash dispenser does not have enough cash
screen.displayMessageLine("\nInsufficient cash available in the ATM." +
"\n\nPlease choose a smaller amount.");
} // end if
else // not enough money available in user's account
{
screen.displayMessageLine("\nInsufficient funds in your account." +
"\n\nPlease choose a smaller amount.");
} // end else
} // end if
else // user chose cancel menu option
{
screen.displayMessageLine("\nCanceling transaction...");
return; // return to main menu because user canceled
} // end else
} while (!cashDispensed);
} // end method execute
// display a menu of withdrawal amounts and the option to cancel;
// return the chosen amount or CANCELED if the user chooses to cancel
private int displayMenuOfAmounts() {
int userChoice = 0; // local variable to store return value
Screen screen = getScreen(); // get screen reference
// array of amounts to correspond to menu numbers
int amounts[] = { 0, 100, 500, 1000 };
// loop while no valid choice has been made
while (userChoice == 0) {
// display the menu
screen.displayMessageLine("\nWithdrawal Menu:");
screen.displayMessageLine("1 - HK$ 100");
screen.displayMessageLine("2 - HK$ 500");
screen.displayMessageLine("3 - HK$ 1000");
screen.displayMessageLine("4 - Enter your amount");
screen.displayMessageLine("5 - Cancel transaction");
screen.displayMessage("\nChoose a withdrawal amount: ");
int input = keypad.getInput(); // get user input through keypad
// determine how to proceed based on the input value
switch (input) {
case 1: // if the user chose a withdrawal amount
case 2: // (i.e., chose option 1, 2, or 3), return the
case 3: // corresponding amount from amounts array
userChoice = amounts[input]; // save user's choice
break;
case 4: // Allow user to enter a custom amount
screen.displayMessage("\nEnter your amount (must be a multiple of HK$100 or HK$1000): ");
int customAmount = keypad.getInput(); // get custom amount from keypad
if (customAmount >= 100 && (customAmount % 100 == 0 || customAmount % 1000 == 0)) {
userChoice = customAmount; // save custom amount
} else {
screen.displayMessageLine("\nInvalid amount. Please enter a valid amount.");
}
break;
case 5:
userChoice = CANCELED; // save user's choice to cancel
break;
default: // the user did not enter a valid value
screen.displayMessageLine("\nInvalid selection. Try again.");
} // end switch
} // end while
return userChoice; // return withdrawal amount or CANCELED
} // end method displayMenuOfAmounts
} // end class Withdrawal