-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton
More file actions
65 lines (51 loc) · 1.55 KB
/
Singleton
File metadata and controls
65 lines (51 loc) · 1.55 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
package org.example;
public class AVAX extends Currency {
private static AVAX instance;
private AVAX() {}
public static AVAX getInstance() {
if (instance == null) {
instance = new AVAX();
}
return instance;
}
@Override
public String toString() {
return super.toString();
}
}
package org.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Wallet {
private HashMap<Currency, Double> balances = new HashMap<>();
private List<History> histories = new ArrayList<>();
public void deposit(Currency c, Double quantity) {
balances.put(c, balances.getOrDefault(c, 0.0) + quantity);
histories.add(new History(c, quantity, TransferType.DEPOSIT));
}
public void send(Currency c, Double quantity, Wallet targetWallet) {
double balance = balances.getOrDefault(c, 0.0) - quantity;
if (balance < 0.0) {
throw new RuntimeException("Insufficient balance");
}
balances.put(c, balance);
histories.add(new History(c, quantity, TransferType.SEND));
targetWallet.deposit(c, quantity);
}
/**
* To make it work, need to add a few toString methods
*/
public void printBalance() {
for (Map.Entry<Currency, Double> entry : balances.entrySet()) {
}
}
/**
* To make it work, need to add a few toString methods
*/
public void printHistory() {
for (History history : histories) {
}
}
}