Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. package bank;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class Bank {
  6.     private static Bank bank;
  7.     HashMap<String, Account> Accounts = new HashMap<String, Account>();
  8.     int accountNumber;
  9.    
  10.     private Bank() {}
  11.    
  12.     public static Bank getBank() {
  13.         if(bank == null) bank = new Bank();
  14.         return bank;
  15.     }
  16.    
  17.     public Account createAccount() {
  18.         Accounts.put(++accountNumber + "", new Account(accountNumber + ""));
  19.         return Accounts.get(accountNumber + "");
  20.     }
  21.    
  22.     public Account lookupAccount(String number) {
  23.         return Accounts.get(number);
  24.     }
  25.    
  26.     public int getBalance(String accountNo) throws AccountException {
  27.         if (!Accounts.containsKey(accountNo)) throw new AccountException("Account " + accountNo + " doesn't exist");
  28.         else return Accounts.get(accountNo).getBalance();
  29.     }
  30.    
  31.     public void deposit(String accountNo, int howMuch) throws AccountException {
  32.         if (!Accounts.containsKey(accountNo)) throw new AccountException("Account " + accountNo + " doesn't exist");
  33.         else Accounts.get(accountNo).deposit(howMuch);
  34.     }
  35.    
  36.     public void withdraw(String accountNo, int howMuch) throws AccountException {
  37.         if (!Accounts.containsKey(accountNo)) throw new AccountException("Account " + accountNo + " doesn't exist");
  38.         else Accounts.get(accountNo).withdraw(howMuch);
  39.     }
  40.    
  41.     public void transfer(String sourceAccount, int howMuch, String targetAccount) throws AccountException {
  42.         if (!Accounts.containsKey(sourceAccount)) throw new AccountException("Account " + sourceAccount + " doesn't exist");
  43.         else if (!Accounts.containsKey(targetAccount)) throw new AccountException("Account " + targetAccount + " doesn't exist");        
  44.         else Accounts.get(sourceAccount).transfer(howMuch, Accounts.get(targetAccount));
  45.     }
  46.    
  47.     public void closeAccount(String accountNo) throws AccountException {
  48.         if (!Accounts.containsKey(accountNo)) throw new AccountException("Account " + accountNo + " doesn't exist");
  49.         else Accounts.get(accountNo).closeAccount();
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement