forked from ErdemOzgen/Java-Learning-Archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
executable file
·156 lines (134 loc) · 6.54 KB
/
ATM.java
File metadata and controls
executable file
·156 lines (134 loc) · 6.54 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
// ATM.java
// Represents an automated teller machine
public class ATM {
private boolean userAuthenticated; // whether user is authenticated
private int currentAccountNumber; // current user's account number
private Screen screen; // ATM's screen
private Keypad keypad; // ATM's keypad
private CashDispenser cashDispenser; // ATM's cash dispenser
private DepositSlot depositSlot; // ATM's deposit slot
private BankDatabase bankDatabase; // account information database
// constants corresponding to main menu options
private static final int BALANCE_INQUIRY = 1;
private static final int WITHDRAWAL = 2;
private static final int DEPOSIT = 3;
private static final int EXIT = 4;
// no-argument ATM constructor initializes instance variables
public ATM() {
userAuthenticated = false; // user is not authenticated to start
currentAccountNumber = 0; // no current account number to start
screen = new Screen(); // create screen
keypad = new Keypad(); // create keypad
cashDispenser = new CashDispenser(); // create cash dispenser
depositSlot = new DepositSlot(); // create deposit slot
bankDatabase = new BankDatabase(); // create acct info database
}
// start ATM
public void run() {
// welcome and authenticate user; perform transactions
while (true) {
// loop while user is not yet authenticated
while (!userAuthenticated) {
screen.displayMessageLine("\nWelcome!");
authenticateUser(); // authenticate user
}
performTransactions(); // user is now authenticated
userAuthenticated = false; // reset before next ATM session
currentAccountNumber = 0; // reset before next ATM session
screen.displayMessageLine("\nThank you! Goodbye!");
}
}
// attempts to authenticate user against database
private void authenticateUser() {
screen.displayMessage("\nPlease enter your account number: ");
int accountNumber = keypad.getInput(); // input account number
screen.displayMessage("\nEnter your PIN: "); // prompt for PIN
int pin = keypad.getInput(); // input PIN
// set userAuthenticated to boolean value returned by database
userAuthenticated =
bankDatabase.authenticateUser(accountNumber, pin);
// check whether authentication succeeded
if (userAuthenticated) {
currentAccountNumber = accountNumber; // save user's account #
}
else {
screen.displayMessageLine(
"Invalid account number or PIN. Please try again.");
}
}
// display the main menu and perform transactions
private void performTransactions() {
// local variable to store transaction currently being processed
Transaction currentTransaction = null;
boolean userExited = false; // user has not chosen to exit
// loop while user has not chosen option to exit system
while (!userExited) {
// show main menu and get user selection
int mainMenuSelection = displayMainMenu();
// decide how to proceed based on user's menu selection
switch (mainMenuSelection) {
// user chose to perform one of three transaction types
case BALANCE_INQUIRY:
case WITHDRAWAL:
case DEPOSIT:
// initialize as new object of chosen type
currentTransaction =
createTransaction(mainMenuSelection);
currentTransaction.execute(); // execute transaction
break;
case EXIT: // user chose to terminate session
screen.displayMessageLine("\nExiting the system...");
userExited = true; // this ATM session should end
break;
default: // user did not enter an integer from 1-4
screen.displayMessageLine(
"\nYou did not enter a valid selection. Try again.");
break;
}
}
}
// display the main menu and return an input selection
private int displayMainMenu() {
screen.displayMessageLine("\nMain menu:");
screen.displayMessageLine("1 - View my balance");
screen.displayMessageLine("2 - Withdraw cash");
screen.displayMessageLine("3 - Deposit funds");
screen.displayMessageLine("4 - Exit\n");
screen.displayMessage("Enter a choice: ");
return keypad.getInput(); // return user's selection
}
// return object of specified Transaction subclass
private Transaction createTransaction(int type) {
Transaction temp = null; // temporary Transaction variable
// determine which type of Transaction to create
switch (type) {
case BALANCE_INQUIRY: // create new BalanceInquiry transaction
temp = new BalanceInquiry(
currentAccountNumber, screen, bankDatabase);
break;
case WITHDRAWAL: // create new Withdrawal transaction
temp = new Withdrawal(currentAccountNumber, screen,
bankDatabase, keypad, cashDispenser);
break;
case DEPOSIT: // create new Deposit transaction
temp = new Deposit(currentAccountNumber, screen,
bankDatabase, keypad, depositSlot);
break;
}
return temp; // return the newly created object
}
}
/**************************************************************************
* (C) Copyright 1992-2018 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. *
*************************************************************************/