-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
182 lines (147 loc) · 5.88 KB
/
ATM.java
File metadata and controls
182 lines (147 loc) · 5.88 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
import java.util.HashMap;
import java.util.Scanner;
import java.io.*;
public class ATM {
private HashMap<String, Double> accounts;
public ATM() {
this.accounts = new HashMap<String, Double>();
}
public void openAccount(String userID, double ammount) {
try {
if (!accounts.containsKey(userID)) {
if (ammount >= 1) {
accounts.put(userID, ammount);
System.out.println(userID + ", we made your account with " + ammount + " dollars");
} else {
System.out.println("BROKE BOI GET YO MONEY UP! GET YO FUNNY UP! NO HOPS GET YO BUNNIES UP!");
}
} else {
System.out.println("An Account with said user name exists. BOZO!!!! Make a new name!");
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Something bugging bro bro...");
}
}
public void closeAccount(String userID) {
try {
if (accounts.get(userID) == 0) {
System.out.println(userID + ", we are closing your account");
accounts.remove(userID);
} else if (accounts.get(userID) > 0) {
System.out.println(userID
+ ", you need to withdraw money and have 0 dollars in your account before you can close it.");
} else {
System.out.println(
"YOU IN DEBT BOIIIIII.... get out of the negative and then you can close your account.");
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Something bugging bro bro...");
}
}
public double checkBalance(String userID) {
try {
if (accounts.containsKey(userID)) {
double output = accounts.get(userID);
return output;
} else {
System.out.println("BROKE BOI..make a valid account first...");
return 0.0;
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Something bugging bro bro...");
return 0.0;
}
}
public double depositMoney(String userID, double ammount) {
try {
if (accounts.containsKey(userID)) {
if (ammount > 0) {
double currentMoney = accounts.get(userID);
double output = currentMoney + ammount;
accounts.replace(userID, output);
return output;
} else {
System.out.println("can't deposit negative money...");
return 0.0;
}
} else {
System.out.println(userID + " is not a valid account.");
return 0.0;
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Something bugging bro bro...");
return 0.0;
}
}
public double withdrawMoney(String userID, double ammount) {
try {
if (accounts.containsKey(userID)) {
double currentMoney = accounts.get(userID);
if (currentMoney >= ammount && ammount > 0) {
double output = currentMoney - ammount;
accounts.replace(userID, output);
return output;
} else {
System.out.println("INSUFFECIENT FUNDS BROKE BOI!");
System.out.println(userID + " only has " + currentMoney);
return 0.0;
}
} else {
System.out.println(userID + " is not a valid account.");
return 0.0;
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Something bugging bro bro...");
return 0.0;
}
}
public boolean transferMoney(String fromAccount, String toAccount, double ammount) {
try {
if (accounts.containsKey(fromAccount) && accounts.containsKey(toAccount)) {
if (ammount > 0) {
if (accounts.get(fromAccount) >= ammount) {
double fromMoney = accounts.get(fromAccount);
double toMoney = accounts.get(fromAccount);
fromMoney -= ammount;
toMoney += ammount;
accounts.replace(fromAccount, fromMoney);
accounts.replace(toAccount, toMoney);
System.out.println("Successful tranfer!");
return true;
} else {
System.out.println(fromAccount + " does not have enough dinero to transfer.");
return false;
}
} else {
System.out.println("Money transferred must be positive.");
return false;
}
} else {
System.out.println("Either one or both accounts are invalid.");
return false;
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Something's bugging bro bro...");
return false;
}
}
public void audit() throws FileNotFoundException { // credit for sentry for teaching me how to do a for each loop
// with a Hashmap
try {
PrintWriter pw = new PrintWriter("AccountAudit.txt");
String s = "";
for (HashMap.Entry<String, Double> entry : accounts.entrySet()) {
s += entry.getKey() + " " + entry.getValue() + "\n";
}
pw.print(s);
pw.close();
} catch (Exception e) {
}
}
}