-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
97 lines (95 loc) · 3.18 KB
/
ATM.java
File metadata and controls
97 lines (95 loc) · 3.18 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
public class ATM{
public HashMap <String,Double> account = new HashMap<>();
public void openAccount (String userId, double amount) throws Exception
{
if (account.containsKey(userId))
{
throw new Exception("bro you already registered something under " + userId);
}
account.put(userId, amount);
}
public void closeAccount (String userId) throws Exception
{
if (account.get(userId) <= 0 )
{
account.remove(userId);
}
throw new Exception("take yo money out before you withdraw, ya got " + account.get(userId) + " dollas");
}
public double checkBalance (String userId) throws Exception
{
if (!account.containsKey(userId))
{
throw new Exception("no account found");
}
return account.get(userId);
}
public double depositMoney (String userId, double amount) throws Exception
{
if (account.containsKey(userId))
{
double currentAmount = checkBalance(userId);
account.put(userId, currentAmount + amount);
}
else
{
throw new Exception("you're a broke boi");
}
return amount;
}
public double withdrawMoney(String userId, double amount) throws Exception
{
if (!account.containsKey(userId))
{
throw new Exception("no account found");
}
if (account.get(userId) > amount)
{
double currentAmount = checkBalance(userId);
account.put(userId, currentAmount - amount);
return account.get(userId);
}
else
{
throw new Exception("you're a broke boi");
}
}
public boolean transferMoney (String fromAccount, String toAccount, double amount) throws Exception
{
withdrawMoney(fromAccount,amount);
depositMoney (toAccount, amount);
return true;
}
public void audit () throws IOException
{
FileWriter fw = new FileWriter ("AccountAudit.txt", false);
PrintWriter pw = new PrintWriter (fw);
for (String email : account.keySet())
{
pw.write (email + " -> " + account.get(email) + "\n");
}
fw.close();
pw.close();
}
public static void main(String[] args) throws Exception {
ATM atm = new ATM();
atm.openAccount ("markyma",3400);
atm.openAccount ("george",0);
System.out.println(atm.checkBalance("markyma"));
System.out.println(atm.depositMoney("markyma", 200));
System.out.println(atm.depositMoney("markyma", 600));
System.out.println(atm.transferMoney("markyma", "george", 2000));
System.out.println(atm.checkBalance("george"));
System.out.println(atm.transferMoney("markyma", "george", 2000));
System.out.println(atm.checkBalance("george"));
System.out.println(atm.transferMoney("markyma", "george", 2000));
System.out.println(atm.checkBalance("george"));
atm.audit();
}
}