Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV4;
- public abstract class Account {
- private String accountOwner;
- private int id; //se odnesuva na momentalna smetka
- private static int idSeed = 10000;
- private double currentAmount;
- private AccountType accountType;
- public Account(String accountOwner, double currentAmount) {
- this.accountOwner = accountOwner;
- this.currentAmount = currentAmount;
- this.id = idSeed++;
- }
- public double getCurrentAmount() {
- return currentAmount;
- }
- public void addAmount(double amount) {
- this.currentAmount += amount;
- }
- public void withdrawAmount(double amount) throws CannotWithdrawMoneyException {
- if (currentAmount <= amount) {
- throw new CannotWithdrawMoneyException(currentAmount, amount);
- }
- this.currentAmount -= amount;
- }
- public abstract AccountType accountType();
- @Override
- public String toString() {
- return String.format("%d: %.2f", id, currentAmount);
- }
- }
- package AV4;
- public enum AccountType {
- INTEREST,
- NON_INTEREST
- }
- package AV4;
- import java.util.Arrays;
- public class Bank {
- private Account[] accounts;
- private int totalAccounts;
- private int maxAccounts;
- public Bank(int maxAccounts) {
- this.maxAccounts = maxAccounts;
- this.accounts = new Account[maxAccounts];
- this.totalAccounts = 0;
- }
- public void addAccount(Account account) {
- if (totalAccounts == maxAccounts) {
- accounts = Arrays.copyOf(accounts, maxAccounts * 2);
- maxAccounts *= 2;
- }
- accounts[totalAccounts++] = account;
- }
- public double totalAssets() {
- double total = 0;
- for (Account account : accounts) {
- total += account.getCurrentAmount();
- }
- return total;
- }
- public void addInterestToAllAccounts() {
- for (Account account : accounts) {
- if(account.accountType().equals(AccountType.INTEREST))
- {
- InterestBearingAccount interestBearingAccount=(InterestBearingAccount) account;
- interestBearingAccount.addInterest();
- }
- }
- }
- }
- package AV4;
- public class CannotWithdrawMoneyException extends Exception {
- public CannotWithdrawMoneyException(double currentAmount, double amount) {
- super(String.format("Your current amount is : %.2f, and you can not withdraw this amount %.2f",
- currentAmount, amount));
- }
- }
- package AV4;
- public class InteresrCheckingAccount extends Account implements InterestBearingAccount {
- public static final double INTEREST_RATE=0.03;
- public InteresrCheckingAccount(String accountOwner, double currentAmount) {
- super(accountOwner, currentAmount);
- }
- @Override
- public AccountType accountType() {
- return AccountType.INTEREST;
- }
- @Override
- public void addInterest() {
- addAmount(getCurrentAmount() * INTEREST_RATE);
- }
- }
- package AV4;
- public interface InterestBearingAccount {
- void addInterest();
- }
- package AV4;
- public class NonInterestCheckingAccount extends Account{
- public NonInterestCheckingAccount(String accountOwner, double currentAmount) {
- super(accountOwner, currentAmount);
- }
- @Override
- public AccountType accountType() {
- return AccountType.NON_INTEREST;
- }
- }
- package AV4;
- public class PlatinumCheckingAccount extends InteresrCheckingAccount{
- public PlatinumCheckingAccount(String accountOwner, double currentAmount) {
- super(accountOwner, currentAmount);
- }
- @Override
- public void addInterest() {
- addAmount(getCurrentAmount() * INTEREST_RATE *2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement