-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathATM.java
More file actions
125 lines (105 loc) · 3.54 KB
/
Copy pathATM.java
File metadata and controls
125 lines (105 loc) · 3.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
/* Challenge Activity 1 - ATM PIN
Let's re-write the ATM example to first prompt for a pin before it shows the menu.
It should loop until you enter a valid pin or you've attempted to use the pin 3 times.
Here's the pseudocode
make a new BankAccount object with pin "1234" and an opening balance of 500
attempts = 0
loop
prompt to enter pin
accept input for pin
add one to attempts
if pin was entered is the same as BankAccount pin then
exit loop with success
if attempts >=3 then
exit loop with failure
end loop
if success
show main atm menu
else
show error message "too many invalid pins"
NOTE: be sure to use proper blocking while writing your code. neatness counts!
SAMPLE RUN (Successful pin)
*********************************
*** Welcome to FudgeBank ATM! ***
*********************************
Enter PIN # ==> 1234
*********************************
*** Main Menu ***
*********************************
[d=Deposit,w=Withdrawl,b=Balance,q=Quit] ? q
You selected: Quit
SAMPLE RUN (three invalid attempts)
*********************************
*** Welcome to FudgeBank ATM! ***
*********************************
Enter PIN # ==> 1112
Enter PIN # ==> 3313
Enter PIN # ==> 0023
You entered an invalid pin 3 times!
*/
package fudgebanking;
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
System.out.printf("*********************************\n");
System.out.printf("*** Welcome to FudgeBank ATM! ***\n");
System.out.printf("*********************************\n\n");
Scanner input = new Scanner(System.in);
//TODO: Write your code here.
BankAccount myacct = new BankAccount("1234", 500);
int attempts = 0;
boolean success= false;
while(true) {
System.out.print("Enter PIN # ==> ");
String pin = input.nextLine();
attempts++;
if (pin.equals(myacct.Pin())) {
success = true;
break;
}
if (attempts >= 3) {
success = false;
break;
}
}
if (success) {
menu();
} else {
System.out.println("You entered an invalid pin 3 times!");
}
}
/** main menu of ATM */
public static void menu() {
Scanner input = new Scanner(System.in);
ATMMenu option;
System.out.printf("*********************************\n");
System.out.printf("*** Main Menu ***\n");
System.out.printf("*********************************\n\n");
System.out.printf("[%s=%s,%s=%s,%s=%s,%s=%s] ? ",
ATMMenu.Deposit.Value(),ATMMenu.Deposit,
ATMMenu.Withdrawl.Value(),ATMMenu.Withdrawl,
ATMMenu.Balance.Value(),ATMMenu.Balance,
ATMMenu.Quit.Value(),ATMMenu.Quit
);
String choice = input.nextLine();
switch (choice)
{
case "d":
option = ATMMenu.Deposit;
break;
case "w":
option = ATMMenu.Withdrawl;
break;
case "b":
option = ATMMenu.Balance;
break;
case "q":
option = ATMMenu.Quit;
break;
default:
option = ATMMenu.Quit;
break;
}
System.out.printf("You selected: %s\n", option);
}
}