-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathATM.java
109 lines (91 loc) · 3.36 KB
/
ATM.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
public class ATM{
private boolean userAuthenticated;
private int currentAccountNumber;
private Screen screen;
private Keypad keypad;
private CashDispenser cashDispenser;
private DepositSlot depositSlot;
private BankDatabase bankDatabase;
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;
public ATM(){
userAuthenticated = false;
currentAccountNumber = 0;
screen = new Screen();
keypad = new Keypad();
cashDispenser = new CashDispenser();
depositSlot = new DepositSlot();
bankDatabase = new BankDatabase();
}
public void run(){
while(true){
while(!userAuthenticated){
screen.displayMessageLine(" \n Welcome");
authenticateUser();
}
performTransactions();
userAuthenticated = false;
currentAccountNumber = 0;
screen.displayMessageLine("\n Thank you! Goodbye!");
}
}
private void authenticateUser(){
screen.displayMessage("\n Please enter your account number:");
int accountNumber = keypad.getInput();
screen.displayMessage("\n Enter your PIN:");
int pin = keypad.getInput();
userAuthenticated = bankDatabase.authenticateUser(accountNumber, pin);
if(userAuthenticated){
currentAccountNumber = accountNumber;
}else{
screen.displayMessageLine("Invalid account number or PIN. Please try again.");
}
}
private void performTransactions(){
Transaction currentTransaction = null;
boolean userExited = false;
while(!userExited){
int mainMenuSelection = displayMainMenu();
switch(mainMenuSelection){
case BALANCE_INQUIRY:
case WITHDRAWAL:
case DEPOSIT:
currentTransaction = createTransaction(mainMenuSelection);
currentTransaction.execute();
break;
case EXIT:
screen.displayMessageLine("\n Exiting the system...");
userExited = true;
break;
default:
screen.displayMessageLine("\n You did not enter a valid selection. Try again.");
}
}
}
private int displayMainMenu(){
screen.displayMessageLine("\n Main Menu");
screen.displayMessageLine("1 - View my balance");
screen.displayMessageLine("2 - Withdraw Cash");
screen.displayMessageLine("3- Deposit Funds");
screen.displayMessageLine("Exit \n");
return keypad.getInput();
}
private Transaction createTransaction(int type)
{
Transaction temp = null;
switch(type){
case BALANCE_INQUIRY:
temp = new BalanceInquiry(currentAccountNumber, screen, bankDatabase);
break;
case WITHDRAWAL:
temp = new Withdrawal(currentAccountNumber, screen, bankDatabase, keypad, cashDispenser);
break;
case DEPOSIT:
temp = new Deposit(currentAccountNumber, screen, bankDatabase, keypad, depositSlot);
break;
}
return temp;
}
}