Advertisement
ivana_andreevska

AV4 Bank

Aug 9th, 2022
1,832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package AV4;
  2.  
  3. public abstract class Account {
  4.     private String accountOwner;
  5.     private int id; //se odnesuva na momentalna smetka
  6.     private static int idSeed = 10000;
  7.     private double currentAmount;
  8.     private AccountType accountType;
  9.  
  10.     public Account(String accountOwner, double currentAmount) {
  11.         this.accountOwner = accountOwner;
  12.         this.currentAmount = currentAmount;
  13.         this.id = idSeed++;
  14.  
  15.     }
  16.  
  17.     public double getCurrentAmount() {
  18.         return currentAmount;
  19.     }
  20.  
  21.     public void addAmount(double amount) {
  22.         this.currentAmount += amount;
  23.     }
  24.  
  25.     public void withdrawAmount(double amount) throws CannotWithdrawMoneyException {
  26.         if (currentAmount <= amount) {
  27.             throw new CannotWithdrawMoneyException(currentAmount, amount);
  28.         }
  29.         this.currentAmount -= amount;
  30.  
  31.     }
  32.  
  33.     public abstract AccountType accountType();
  34.  
  35.     @Override
  36.     public String toString() {
  37.         return String.format("%d: %.2f", id, currentAmount);
  38.     }
  39. }
  40. package AV4;
  41.  
  42. public enum AccountType {
  43.     INTEREST,
  44.     NON_INTEREST
  45. }
  46.  
  47. package AV4;
  48.  
  49. import java.util.Arrays;
  50.  
  51. public class Bank {
  52.     private Account[] accounts;
  53.     private int totalAccounts;
  54.     private int maxAccounts;
  55.  
  56.     public Bank(int maxAccounts) {
  57.         this.maxAccounts = maxAccounts;
  58.         this.accounts = new Account[maxAccounts];
  59.         this.totalAccounts = 0;
  60.     }
  61.  
  62.     public void addAccount(Account account) {
  63.         if (totalAccounts == maxAccounts) {
  64.             accounts = Arrays.copyOf(accounts, maxAccounts * 2);
  65.             maxAccounts *= 2;
  66.         }
  67.         accounts[totalAccounts++] = account;
  68.     }
  69.  
  70.     public double totalAssets() {
  71.         double total = 0;
  72.         for (Account account : accounts) {
  73.             total += account.getCurrentAmount();
  74.         }
  75.         return total;
  76.     }
  77.  
  78.     public void addInterestToAllAccounts() {
  79.         for (Account account : accounts) {
  80.             if(account.accountType().equals(AccountType.INTEREST))
  81.             {
  82.                 InterestBearingAccount interestBearingAccount=(InterestBearingAccount) account;
  83.                 interestBearingAccount.addInterest();
  84.             }
  85.         }
  86.     }
  87. }
  88. package AV4;
  89.  
  90. public class CannotWithdrawMoneyException extends Exception {
  91.     public CannotWithdrawMoneyException(double currentAmount, double amount) {
  92.         super(String.format("Your current amount is : %.2f, and you can not withdraw this amount %.2f",
  93.                 currentAmount, amount));
  94.     }
  95. }
  96. package AV4;
  97.  
  98. public class InteresrCheckingAccount extends Account implements InterestBearingAccount {
  99.     public static final double INTEREST_RATE=0.03;
  100.     public InteresrCheckingAccount(String accountOwner, double currentAmount) {
  101.         super(accountOwner, currentAmount);
  102.     }
  103.  
  104.     @Override
  105.     public AccountType accountType() {
  106.         return AccountType.INTEREST;
  107.     }
  108.  
  109.     @Override
  110.     public void addInterest() {
  111.         addAmount(getCurrentAmount() * INTEREST_RATE);
  112.     }
  113. }
  114. package AV4;
  115.  
  116. public interface InterestBearingAccount {
  117.     void addInterest();
  118. }
  119. package AV4;
  120.  
  121. public class NonInterestCheckingAccount extends Account{
  122.     public NonInterestCheckingAccount(String accountOwner, double currentAmount) {
  123.         super(accountOwner, currentAmount);
  124.     }
  125.  
  126.     @Override
  127.     public AccountType accountType() {
  128.         return AccountType.NON_INTEREST;
  129.     }
  130. }
  131. package AV4;
  132.  
  133. public class PlatinumCheckingAccount extends InteresrCheckingAccount{
  134.     public PlatinumCheckingAccount(String accountOwner, double currentAmount) {
  135.         super(accountOwner, currentAmount);
  136.     }
  137.  
  138.     @Override
  139.     public void addInterest() {
  140.         addAmount(getCurrentAmount() * INTEREST_RATE *2);
  141.     }
  142. }
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement