-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
83 lines (74 loc) · 2.49 KB
/
ATM.java
File metadata and controls
83 lines (74 loc) · 2.49 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
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
public class ATM{
private 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 java.lang.Error ("An account with the email \"" + userId + "\" already exists.");
}
accountMap.put (userId, amount);
}
public void closeAccount (String userId) throws Exception
{
if (accountMap.get (userId) != 0)
{
throw new java.lang.Error ("Balance must be withdrawn prior to account closure.");
}
accountMap.remove (userId);
}
public double checkBalance (String userId) throws Exception
{
if (!accountMap.containsKey (userId))
{
throw new java.lang.Error ("No account found with this email.");
}
return accountMap.get (userId);
}
public double depositMoney (String userId, double amount) throws Exception
{
if (!accountMap.containsKey (userId))
{
throw new java.lang.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 java.lang.Error ("You're broke AF.");
}
accountMap.put (userId, accountMap.get (userId) - amount);
return amount;
}
public boolean transferMoney (String fromAccount, String toAccount, double amount)
{
if (!accountMap.containsKey (fromAccount) || !accountMap.containsKey (toAccount) || accountMap.get (fromAccount) < amount)
{
return false;
}
accountMap.put (fromAccount, accountMap.get (fromAccount) - amount);
accountMap.put (toAccount, accountMap.get (toAccount) + amount);
return true;
}
public void audit () throws IOException
{
FileWriter writer = new FileWriter("AccountAudit.txt",false);
PrintWriter out = new PrintWriter(writer);
for (String key : accountMap.keySet ())
{
out.println (key + ": " + accountMap.get (key));
}
writer.close ();
out.close ();
}
}