-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
88 lines (61 loc) · 2.5 KB
/
ATM.java
File metadata and controls
88 lines (61 loc) · 2.5 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
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* ATM
*/
public class ATM {
HashMap <String, Double> accountMap;
public ATM () {
accountMap = new HashMap<String, Double> ();
}
public void openAccount(String userId, double amount) throws Exception {
if (accountMap.containsKey(userId))
throw new Exception("Error: Account already exists.");
accountMap.put(userId, amount);
}
public void closeAccount (String userId) throws Exception{
if (!accountMap.containsKey(userId))
throw new Exception("Error: User ID does not exist.");
if (accountMap.get(userId) <= 0) {
accountMap.remove(userId);
} else {
throw new Exception("Error: Must withdraw money before closing.");
}
}
public double checkBalance (String userId) throws Exception {
if (!accountMap.containsKey(userId))
throw new Exception("Error: User ID does not exist.");
return accountMap.get(userId);
}
public double depositMoney (String userId, double amount) throws Exception {
if (!accountMap.containsKey(userId))
throw new Exception("Error: You're broke AF.");
accountMap.put(userId, accountMap.get(userId) + amount);
return amount;
}
public double withdrawMoney (String userId, double amount) throws Exception {
if (!accountMap.containsKey(userId) || accountMap.get(userId) < amount)
throw new Exception("Error: You're broke AF.");
accountMap.put(userId, accountMap.get(userId) - amount);
return amount;
}
public boolean transferMoney (String fromAccount, String toAccount, double amount) throws Exception{
if (!accountMap.containsKey(fromAccount) || !accountMap.containsKey(toAccount))
throw new Exception("Error: An inputted account does not exist.");
withdrawMoney(fromAccount, amount);
depositMoney(toAccount, amount);
return true;
}
public void audit () throws IOException {
PrintWriter printWriter = new PrintWriter(new FileWriter("AccountAudit.txt"));
for (Entry<String, Double> acc: accountMap.entrySet()) {
printWriter.println("AccountEmail: " + acc.getKey() + ", AccountValue: " + acc.getValue());
}
printWriter.close();
}
}