Skip to content

Commit de755f2

Browse files
committed
More Updates
1 parent dd07e30 commit de755f2

File tree

4 files changed

+176
-3
lines changed

4 files changed

+176
-3
lines changed

13 ATM Case Study/src/ATM.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public void run()
5353
// attempts to authenticate user against database
5454
private void authenticateUser()
5555
{
56-
screen.diplayMessage("\nPlease enter your account number: ");
56+
screen.displayMessage("\nPlease enter your account number: ");
5757
int accountNumber = keypad.getInput(); // input account number
58-
screen.diplayMessage("\nEnter your PIN: "); // prompt for PIN
58+
screen.displayMessage("\nEnter your PIN: "); // prompt for PIN
5959
int pin = keypad.getInput(); // input PIN
6060

6161
// set userAuthentigated to boolean value returned by the database

13 ATM Case Study/src/Screen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
public class Screen
55
{
66
// display a message without a carriage return
7-
public void diplayMessage(String message)
7+
public void displayMessage(String message)
88
{
99
System.out.print(message);
1010
} // end method displayMessage
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Transaction.java
2+
// Abstract superclass Transaction represents an ATM transaction
3+
4+
public abstract class Transaction
5+
{
6+
private int accountNumber; // indicates account involved
7+
private Screen screen; // ATM's screen
8+
private BankDatabase bankDatabase; // account info database
9+
10+
// Transaction constructor invoke by subclasses using super()
11+
public Transaction(int userAccountNumber, Screen atmScreen,
12+
BankDatabase atmBankDatabase)
13+
{
14+
accountNumber = userAccountNumber;
15+
screen = atmScreen;
16+
bankDatabase = atmBankDatabase;
17+
} // end Transaction constructor
18+
19+
// return account number
20+
public int getAccountNumber()
21+
{
22+
return accountNumber;
23+
} // end method getAccountNumber
24+
25+
// return reference to screen
26+
public Screen getScreen()
27+
{
28+
return screen;
29+
} // end method getScreen
30+
31+
// return reference to bank database
32+
public BankDatabase getBankDatabase()
33+
{
34+
return bankDatabase;
35+
} // end method getBankDatabase
36+
37+
// perform the transaction (overridden by each subclass)
38+
abstract public void execute();
39+
}

13 ATM Case Study/src/Withdrawal.java

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Withdrawal.java
2+
// Represents a withdrawal ATM transaction
3+
4+
public class Withdrawal extends Transaction
5+
{
6+
private int amount; // amount to withdraw
7+
private Keypad keypad; // reference to keypad
8+
private CashDispenser cashDispenser; // reference to cash dispenser
9+
10+
// constant corresponding to menu option to cancel
11+
private final static int CANCELED = 6;
12+
13+
// Withdrawal constructor
14+
public Withdrawal(int userAccountNumber, Screen atmScreen,
15+
BankDatabase atmBankDatabase, Keypad atmKeypad,
16+
CashDispenser atmCashDispenser)
17+
{
18+
// initialize superclass variables
19+
super(userAccountNumber, atmScreen, atmBankDatabase);
20+
21+
// initialize references to keypad and cash dispenser
22+
keypad = atmKeypad;
23+
cashDispenser = atmCashDispenser;
24+
} // end Withdrawal constructor
25+
26+
// perform transaction
27+
@Override
28+
public void execute()
29+
{
30+
boolean cashDispensed = false; // cash was not dispensed yet
31+
double availableBalance; // amount available for withdrawal
32+
33+
// get references to bank database and screen
34+
BankDatabase bankDatabase = getBankDatabase();
35+
Screen screen = getScreen();
36+
37+
// loop until cash is dispensed or the user cancels
38+
do
39+
{
40+
// obtain a chosen withdrawal amount from the user
41+
amount = displayMenuOfAmounts();
42+
43+
// check whether user chose a withdrawal amount or canceled
44+
if (amount != CANCELED)
45+
{
46+
// get available balance of account involved
47+
availableBalance =
48+
bankDatabase.getAvailableBalance(getAccountNumber());
49+
50+
// check whether the user has enough money in the account
51+
if (amount <= availableBalance)
52+
{
53+
// check whethur the cash dispenser has enough money
54+
if(cashDispenser.isSufficientCashAvailable(amount))
55+
{
56+
// update the account involved to reflect the withdrawal
57+
bankDatabase.debit(getAccountNumber(), amount);
58+
59+
cashDispenser.dispenseCash(amount); // dispense cash
60+
cashDispensed = true; // cash was dispensed
61+
62+
// instruct user to take cash
63+
screen.displayMessageLine("\nYour cash has been" +
64+
" dispensed. Please take your cash now.");
65+
} // end if
66+
else // not enough money available in user's accountscreen.displayMessageLine(
67+
screen.displayMessageLine(
68+
"\nInsufficient cash available in the ATM." +
69+
"\n\nPlease choose a smaller amount.");
70+
} // end if
71+
else // not enough money available in user's account
72+
{
73+
screen.displayMessageLine(
74+
"\nInsufficient funds in your account." +
75+
"\n\nPlease choose a smaller amount.");
76+
} // end else
77+
} // end if
78+
else // user chose cancel menu option
79+
{
80+
screen.displayMessageLine("\nCanceling transaction...");
81+
return; // return to main menu because user canceled
82+
} // end else
83+
} while(!cashDispensed);
84+
85+
} // end method execute
86+
87+
// display a menu of withdrawal amounts and the options to cancel;
88+
// return the chosen amount or 0 if the user chooses to cancel
89+
private int displayMenuOfAmounts()
90+
{
91+
int userChoice = 0; // local variable to store return value
92+
93+
Screen screen = getScreen(); // get screen reference
94+
95+
// array of amounts to correspond to menu numbers
96+
int[] amounts = { 0, 20, 40, 60, 100, 200 };
97+
98+
// loop while no valid choice has been made
99+
while (userChoice == 0)
100+
{
101+
// display the withdrawal menu
102+
screen.displayMessageLine("\nWithdrawal Menu:");
103+
screen.displayMessageLine("1 - $20");
104+
screen.displayMessageLine("2 - $40");
105+
screen.displayMessageLine("3 - $60");
106+
screen.displayMessageLine("4 - $100");
107+
screen.displayMessageLine("5 - $200");
108+
screen.displayMessageLine("6 - Cancel transaction");
109+
screen.displayMessage("\nChoose a withdrawal amount: ");
110+
111+
int input = keypad.getInput(); // get user input through keypad
112+
113+
// determine how to proceed based on the input value
114+
switch (input)
115+
{
116+
case 1: // if the user chose a withdrawal amount
117+
case 2: // (i.e., chose option 1, 2, 3, 4, or 5), return the
118+
case 3: // corresponding amount from amounts array
119+
case 4:
120+
case 5:
121+
userChoice = amounts[input]; // save user's choice
122+
break;
123+
case CANCELED: // the user chose to cancel
124+
userChoice = CANCELED; // save user's choice
125+
break;
126+
default: // the user did not enter a value from 1-6
127+
screen.displayMessageLine(
128+
"\nInvalid selection. Try again.");
129+
} // end switch
130+
} // end while
131+
132+
return userChoice; // return withdrawal amount or CANCELED
133+
} // end method displayMenuOfAmounts
134+
} // end class Withdrawal

0 commit comments

Comments
 (0)