summaryrefslogtreecommitdiff
path: root/java/09-packages/src/lk/ac/pdn/ceit/fullstack/AccountManager.java
blob: 429e0e7521902d56714f4fd03d4e082ccb63a35a (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
40
41
package lk.ac.pdn.ceit.fullstack;

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();
	}
}