summaryrefslogtreecommitdiff
path: root/java/06-simple-objects-better/AccountManager.java
blob: 5a3368c0c2a71dd8beb8deadd080a0b39c03ceb1 (plain)
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
import java.util.HashMap;
import java.util.Map;

public class AccountManager {

    private Map<Integer, Account> accounts = new HashMap<>();

	/*
		This is a called a constructor. It gets executed when an object of this class is
		created.	
	*/
    public AccountManager() {
		// For testing, create some account objects.
		// In a real systems, account data will be in a storage system like a database.

		Account account = new Account(1, 10.0);
        accounts.put(account.getAccountNumber(), account);

		account = new Account(2, 20.0);
        accounts.put(account.getAccountNumber(), account);
        
		account = new Account(3, 10.0);
        accounts.put(account.getAccountNumber(), account);
    }

	public void withdraw(int accountId, double amount) {
		Account account = accounts.get(accountId);
		account.setBalance(account.getBalance() - amount);
	}

	public void deposit(int accountId, double amount) {
		Account account = accounts.get(accountId);
		account.setBalance(account.getBalance() + amount);
	}

	public double getBalance(int accountId) {
		return accounts.get(accountId).getBalance();
	}
}