-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
249 lines (148 loc) · 6.59 KB
/
ATM.java
File metadata and controls
249 lines (148 loc) · 6.59 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* This program simulates a simple ATM environment.
* It is quite barebones, but functional.
* ATM supports deposits, withdrawals, transfers, and balance inquiries.
* ATM does not currently validate user input in this release.
* All currency is in USD $
* @version 0.1
*
*/
import java.text.NumberFormat; // Helps with formatting doubles as currency
import java.util.Scanner; // Will be used to get input from the user
public class ATM {
public static void main(String[] args) {
// Create and instantiate two Account objects
Account checking = new Account();
checking.setType("Checking");
checking.setBalance(0.00);
checking.setRate(0.00);
Account savings = new Account();
savings.setType("Savings");
savings.setBalance(0.00);
savings.setRate(2.00);
NumberFormat formatter = NumberFormat.getCurrencyInstance(); // Creates the formatter object for currency
Scanner sc = new Scanner(System.in); // Creates the sc object to read user input
boolean session = true; // This variable will break the (while) loop when false
while (session) {
// Present the user with a menu of 5 options
System.out.print("========================\n"
+"ATM Menu: \n \n"
+ "1. Deposit Money \n"
+ "2. Withdraw Money \n"
+ "3. Transfer Funds \n"
+ "4. Check Account Balance\n"
+ "5. End Session\n"
+ "========================\n"
+ "\nEnter selection: ");
int selection = sc.nextInt(); // assign the user's input to the selection variable
// This switch block will handle one of five selections and deal with them appropriately
switch (selection) {
// case 1 handles the depositing of money
case 1:
System.out.print("Enter (1) for Savings or (2) for Checking: ");
int depAccount = sc.nextInt(); // Keeps track of which account to deposit money to
if (depAccount == 1) {
System.out.println("\nYour current Savings balance is: " + formatter.format(savings.getBalance()) + "\n");
System.out.println("How much money would you like to deposit?");
double deposit = sc.nextDouble();
savings.deposit(deposit);
System.out.println("\nYour Savings balance is now: " + formatter.format(savings.getBalance()) + "\n");
}
else if (depAccount == 2) {
System.out.println("\nYour current Checking balance is: " + formatter.format(checking.getBalance()) + "\n");
System.out.println("How much money would you like to deposit?");
double deposit = sc.nextDouble();
checking.deposit(deposit);
System.out.println("\nChecking balance is now: " + formatter.format(checking.getBalance()) + "\n");
}
break;
// case 2 handles the withdrawal of money
case 2:
System.out.print("\nEnter (1) for Savings or (2) for Checking: ");
int witAccount = sc.nextInt(); // Keeps track of which account to withdraw from
if (witAccount == 1) {
System.out.println("\nYour current Savings balance is: " + formatter.format(savings.getBalance()) + "\n");
System.out.println("How much money would you like to withdraw?");
double withdraw = sc.nextDouble();
savings.withdraw(withdraw);
System.out.println("\nYour Savings balance is now: " + formatter.format(savings.getBalance()) + "\n");
}
else if (witAccount == 2) {
System.out.println("\nYour current Checking balance is: " + formatter.format(checking.getBalance()) + "\n");
System.out.println("How much money would you like to withdraw?");
double withdraw = sc.nextDouble();
checking.withdraw(withdraw);
System.out.println("\nYour Checking balance is now: " + formatter.format(checking.getBalance()) + "\n");
}
break;
// case 3 handles the transfer of funds
case 3:
System.out.print("\nFrom which account do you wish to transfer funds from?, (1) for Savings or (2) for Checking: ");
int tranAccount = sc.nextInt();
if (tranAccount == 1) {
System.out.println("\nYour current Savings balance is: " + formatter.format(savings.getBalance()) + "\n");
System.out.print("How much money do you wish to transfer from Savings to Checking?: ");
double tranAmount = sc.nextDouble();
savings.withdraw(tranAmount);
checking.deposit(tranAmount);
System.out.println("\nYou successfully transferred " + formatter.format(tranAmount) + " from Savings to Checking");
System.out.println("\nChecking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + "\n");
}
else if (tranAccount == 2) {
System.out.println("\nYour current Checking balance is: " + formatter.format(checking.getBalance()) + "\n");
System.out.print("How much money do you wish to transfer from Checking to Saving?: ");
double tranAmount = sc.nextDouble();
checking.withdraw(tranAmount);
savings.deposit(tranAmount);
System.out.println("\nYou successfully transferred " + formatter.format(tranAmount) + " from Checking to Savings");
System.out.println("\nChecking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + "\n");
}
break;
// case 4 simply outputs the balances of both Checking and Savings accounts
case 4:
System.out.println("\nChecking Balance: " + formatter.format(checking.getBalance()));
System.out.println("Savings Balance: " + formatter.format(savings.getBalance()) + "\n");
break;
// case 5 breaks out of the (while) loop when the user is finished using the ATM
case 5:
session = false;
break;
}
}
System.out.println("\nThank you for banking with us!\n");
}
}
class Account {
// Here we declare some variables that a typical bank account will have
String type;
double balance;
double rate;
// The following methods are a combination of getter/setter methods as well
// as two special deposit() and withdraw() methods
void setType(String accType) {
type = accType;
}
void setBalance(double accBal) {
balance = accBal;
}
void setRate(double accRate) {
rate = accRate;
}
void deposit(double dep) {
balance += dep; // Take the Account object's balance and add to it the current deposit
}
void withdraw(double wit) {
balance -= wit; // Take the Account object's balance and subtract from it the current withdrawal
}
String getType() {
return type;
}
double getBalance() {
return balance;
}
double getRate() {
return rate;
}
}