-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathWithdrawal.java
165 lines (139 loc) · 5.85 KB
/
Withdrawal.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// 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
// perform transaction
@Override
public void execute()
{
// amount available for withdrawal
// get references to bank database and screen
// loop until cash is dispensed or the user cancels
displayMenuOfAmounts();
}
public void transaction(int amount){
BankDatabase bankDatabase = getBankDatabase();
Screen screen = getScreen();
boolean cashDispensed = false; // cash was not dispensed yet
double availableBalance;
// check whether user chose a withdrawal amount or 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 the withdrawal
bankDatabase.debit(getAccountNumber(), amount);
cashDispenser.dispenseCash(amount); // dispense cash
cashDispensed = true; // cash was dispensed
// instruct user to take cash
screen.messageJLabel7.setText("\nYour cash has been" +
" dispensed. Please take your cash now.");
} // end if
else // cash dispenser does not have enough cash
screen.messageJLabel7.setText(
"\nInsufficient cash available in the ATM." +
"\n\nPlease choose a smaller amount.");
} // end if
else // not enough money available in user's account
{
screen.messageJLabel7.setText(
"\nInsufficient funds in your account." +
"\n\nPlease choose a smaller amount.");
} // end else
} // end if
// end else
// end method execute
// display a menu of withdrawal amounts and the option to cancel;
// return the chosen amount or 0 if the user chooses to cancel
private void displayMenuOfAmounts()
{
int userChoice = 0; // local variable to store return value
Screen screen = getScreen(); // get screen reference
screen.createWithdrawGUI();
screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER);
withdraw1 check1 = new withdraw1();
withdraw2 check2 = new withdraw2();
withdraw3 check3 = new withdraw3();
withdraw4 check4 = new withdraw4();
withdraw5 check5 = new withdraw5();
Keypad.B1.addActionListener(check1);
Keypad.B2.addActionListener(check2);
Keypad.B3.addActionListener(check3);
Keypad.B4.addActionListener(check4);
Keypad.B5.addActionListener(check5);
screen.Mainframe.revalidate();
}
public class withdraw1 implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
transaction(20);
}
}
public class withdraw2 implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
transaction(40);
}
}
public class withdraw3 implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
transaction(60);
}
}
public class withdraw4 implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
transaction(100);
}
}
public class withdraw5 implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
transaction(200);
}
}
}
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/