mmayoub

Bank, Account abstarct class

Aug 10th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.65 KB | None | 0 0
  1. /*
  2.  * Account abstract class
  3.  * All Types of bank accounts should be derived from this class
  4.  */
  5. package BankAccounts;
  6.  
  7. import java.time.LocalDate;
  8.  
  9. import BankClients.Client;
  10. import BankExceptions.BankAgeException;
  11. import BankExceptions.BankException;
  12. import BankExceptions.BankNoCreditException;
  13. import BankInterfaces.Accountable;
  14. import BankPkg.AccountType;
  15. import BankPkg.BankUtils;
  16.  
  17. // no loan , no age
  18. public abstract class Account implements Accountable {
  19.     /*
  20.      * static class attributes
  21.      */
  22.  
  23.     // counter for all accounts created
  24.     protected static int totalCounter = 0;
  25.  
  26.     // number of accounts created from each type
  27.     // each counter initialized to zero
  28.     protected static int[] typeCounter = new int[AccountType.values().length];
  29.  
  30.     /*
  31.      * instance attributes
  32.      */
  33.     protected Client owner; // account owner
  34.  
  35.     private AccountType accountType; // as defined in AccountType.java
  36.  
  37.     protected int accountId; // account id, auto unique generated number
  38.     protected double accountBalance; // account balance
  39.     protected double accountFee; // amount of account fee
  40.     protected int accountCredit; // credit for the account
  41.  
  42.     protected boolean isDefaultFee; // use default fee as account type
  43.     protected boolean isDefaultCredit; // use default credit as account type
  44.  
  45.     // all derived classes should call this constructor first
  46.     public Account(String personName, int personId, LocalDate birthDate,
  47.             AccountType type) throws Exception {
  48.         // set fields by arguments
  49.         this(new Client(personName, personId, birthDate), type);
  50.     }
  51.  
  52.     public Account(Client owner, AccountType type) throws Exception {
  53.  
  54.         this.owner = owner;
  55.         this.setAccountType(type);
  56.  
  57.         // fields to default values
  58.         this.accountBalance = 0;
  59.         this.isDefaultFee = true;
  60.         this.isDefaultCredit = true;
  61.         this.accountFee = type.getDefaultFee();
  62.         this.accountCredit = type.getDefaultCredit();
  63.  
  64.         // update total and type counters
  65.         Account.totalCounter += 1;
  66.         Account.typeCounter[type.ordinal()] += 1;
  67.  
  68.         // set auto generated account id
  69.         this.accountId = Integer.parseInt(type.getPrefixIdDigit() + ""
  70.                 + Account.totalCounter);
  71.     }
  72.  
  73.     public Account(Account other) {
  74.         this.owner = new Client(other.owner);
  75.         this.accountId = other.accountId;
  76.         this.accountBalance = other.accountBalance;
  77.         this.accountCredit = other.accountCredit;
  78.         this.accountFee = other.accountFee;
  79.         this.accountType = other.accountType;
  80.         this.isDefaultCredit = other.isDefaultCredit;
  81.         this.isDefaultFee = other.isDefaultFee;
  82.     }
  83.  
  84.     public static Account copyOf(Account src) {
  85.         if (src.getAccountType() == AccountType.regular) {
  86.             return new Regular((Regular) src);
  87.         } else if (src.getAccountType() == AccountType.teenage) {
  88.             return new Teenage((Teenage) src);
  89.         } else if (src.getAccountType() == AccountType.business) {
  90.             return new Business((Business) src);
  91.         } else if (src.getAccountType() == AccountType.student) {
  92.             return new Student((Student) src);
  93.         } else if (src.getAccountType() == AccountType.soldier) {
  94.             return new Soldier((Soldier) src);
  95.         } else {
  96.             return null;
  97.         }
  98.     }
  99.  
  100.     /*
  101.      * getters for fields
  102.      */
  103.     // return amount of all accounts created
  104.     public static int getAllAccountsCounter() {
  105.         return Account.totalCounter;
  106.     }
  107.  
  108.     // return amount of accounts created from a given type
  109.     public static int getTypeAccountsCounter(AccountType type) {
  110.         return Account.typeCounter[type.ordinal()];
  111.     }
  112.  
  113.     // return owner
  114.     public Client getOwner() {
  115.         return this.owner;
  116.     }
  117.  
  118.     private void setAccountType(AccountType type) throws BankAgeException {
  119.         if (!BankUtils.isBetween(owner.getAge(), type.getMinAge(),
  120.                 type.getMaxAge())) {
  121.  
  122.             throw new BankAgeException(type, owner.getAge());
  123.         } else
  124.             this.accountType = type;
  125.     }
  126.  
  127.     // return account type, as defined in AccountType.java
  128.     public AccountType getAccountType() {
  129.         return this.accountType;
  130.     }
  131.  
  132.     // return account id
  133.     public int getAccountId() {
  134.         return this.accountId;
  135.     }
  136.  
  137.     // return account balance
  138.     public double getAccountBalance() {
  139.         return this.accountBalance;
  140.     }
  141.  
  142.     // return account fee
  143.     // also required for interface implementation
  144.     public double getAccountFee() {
  145.         return this.accountFee;
  146.     }
  147.  
  148.     // return account credit
  149.     public int getAccountCredit() {
  150.         return this.accountCredit;
  151.     }
  152.  
  153.     // calculate fee amount for an amount of money
  154.     protected double calcFee(double amount) {
  155.         return amount * (this.accountFee / 100.0);
  156.     }
  157.  
  158.     public boolean isDefaultFee() {
  159.         return this.isDefaultFee;
  160.     }
  161.  
  162.     public boolean isDefaultCredit() {
  163.         return this.isDefaultCredit;
  164.     }
  165.  
  166.     /*
  167.      * setters for fields
  168.      */
  169.  
  170.     // also required for interface implementation
  171.     public boolean setAccountFee(double customFee) {
  172.         double defaultFee = this.accountType.getDefaultFee();
  173.         if (customFee == -1) {
  174.             this.isDefaultFee = true;
  175.             this.accountFee = defaultFee;
  176.             return true;
  177.         } else if (customFee <= defaultFee) {
  178.             this.isDefaultFee = false;
  179.             this.accountFee = customFee;
  180.             return true;
  181.         } else {
  182.             try {
  183.                 throw new BankException("Error! Fee account(" + customFee
  184.                         + ") is greater than default fee!(" + defaultFee + ")");
  185.             } catch (BankException e) {
  186.                 // TODO Auto-generated catch block
  187.                 e.printStackTrace();
  188.             }
  189.             return false;
  190.         }
  191.     }
  192.  
  193.     public boolean setAccountCredit(int customCredit) {
  194.         if ((this instanceof Teenage) || (this instanceof Student)) {
  195.             // error
  196.             try {
  197.                 throw new BankException(
  198.                         "Error setting a credit for Teenage/Student account!");
  199.             } catch (Exception e) {
  200.                 e.printStackTrace();
  201.             }
  202.             return false;
  203.         } else {
  204.             this.isDefaultCredit = false;
  205.             this.accountCredit = customCredit;
  206.             return true;
  207.         }
  208.     }
  209.  
  210.     public boolean setAccountCredit() {
  211.         this.isDefaultCredit = true;
  212.         this.accountCredit = this.accountType.getDefaultCredit();
  213.         return true;
  214.     }
  215.  
  216.     /*
  217.      * implement interface methods defined in Accountable.java
  218.      */
  219.     @Override
  220.     public int getTotalAccount() {
  221.         return Account.totalCounter;
  222.     }
  223.  
  224.     @Override
  225.     public int getTotalAccountByType(AccountType type) {
  226.         // TODO Auto-generated method stub
  227.         return Account.typeCounter[type.ordinal()];
  228.     }
  229.  
  230.     @Override
  231.     public double getBalance() {
  232.         return this.getAccountBalance();
  233.     }
  234.  
  235.     @Override
  236.     public boolean withDraw(double amount) {
  237.         boolean res = false;
  238.  
  239.         double amountWithFee = amount + calcFee(amount);
  240.         if (this.accountBalance - amountWithFee >= this.accountCredit) {
  241.             this.accountBalance -= amountWithFee;
  242.             res = true;
  243.         } else {
  244.             try {
  245.                 throw new BankNoCreditException(this, amount);
  246.             } catch (BankNoCreditException e) {
  247.                 // TODO Auto-generated catch block
  248.                 e.printStackTrace();
  249.             }
  250.         }
  251.         return res;
  252.  
  253.     }
  254.  
  255.     @Override
  256.     public void deposit(double amount) {
  257.         this.accountBalance += amount - calcFee(amount);
  258.     }
  259.  
  260.     @Override
  261.     public int getAccountNumber() {
  262.         return this.getAccountId();
  263.     }
  264.  
  265.     @Override
  266.     public String toString() {
  267.         return this.getClass().getSimpleName() + " [" + this.feildsAsString()
  268.                 + "]";
  269.     }
  270.  
  271.     protected String feildsAsString() {
  272.         return String
  273.                 .format("accountId=%d, , accountType=%s, personName=%s, personId=%d, age=%d, accountBalance=%.2f, , accountFee=%f%s, accountCredit=%d%s",
  274.                         accountId, accountType, owner.getName(), owner.getId(),
  275.                         owner.getAge(), accountBalance, accountFee,
  276.                         isDefaultFee ? "(default)" : "", accountCredit,
  277.                         isDefaultCredit ? "(default)" : "");
  278.  
  279.     }
  280.  
  281.     public boolean canCloseAccount() {
  282.         double total = this.accountBalance;
  283.         if (total < 0)
  284.             return false;
  285.  
  286.         if (this instanceof LoanableAccount) {
  287.             double loan = ((LoanableAccount) this).loanAmount;
  288.             total += loan + this.calcFee(loan);
  289.         }
  290.  
  291.         return total >= 0;
  292.  
  293.     }
  294.  
  295. }
Add Comment
Please, Sign In to add comment