Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Account abstract class
- * All Types of bank accounts should be derived from this class
- */
- package BankAccounts;
- import java.time.LocalDate;
- import BankClients.Client;
- import BankExceptions.BankAgeException;
- import BankExceptions.BankException;
- import BankExceptions.BankNoCreditException;
- import BankInterfaces.Accountable;
- import BankPkg.AccountType;
- import BankPkg.BankUtils;
- // no loan , no age
- public abstract class Account implements Accountable {
- /*
- * static class attributes
- */
- // counter for all accounts created
- protected static int totalCounter = 0;
- // number of accounts created from each type
- // each counter initialized to zero
- protected static int[] typeCounter = new int[AccountType.values().length];
- /*
- * instance attributes
- */
- protected Client owner; // account owner
- private AccountType accountType; // as defined in AccountType.java
- protected int accountId; // account id, auto unique generated number
- protected double accountBalance; // account balance
- protected double accountFee; // amount of account fee
- protected int accountCredit; // credit for the account
- protected boolean isDefaultFee; // use default fee as account type
- protected boolean isDefaultCredit; // use default credit as account type
- // all derived classes should call this constructor first
- public Account(String personName, int personId, LocalDate birthDate,
- AccountType type) throws Exception {
- // set fields by arguments
- this(new Client(personName, personId, birthDate), type);
- }
- public Account(Client owner, AccountType type) throws Exception {
- this.owner = owner;
- this.setAccountType(type);
- // fields to default values
- this.accountBalance = 0;
- this.isDefaultFee = true;
- this.isDefaultCredit = true;
- this.accountFee = type.getDefaultFee();
- this.accountCredit = type.getDefaultCredit();
- // update total and type counters
- Account.totalCounter += 1;
- Account.typeCounter[type.ordinal()] += 1;
- // set auto generated account id
- this.accountId = Integer.parseInt(type.getPrefixIdDigit() + ""
- + Account.totalCounter);
- }
- public Account(Account other) {
- this.owner = new Client(other.owner);
- this.accountId = other.accountId;
- this.accountBalance = other.accountBalance;
- this.accountCredit = other.accountCredit;
- this.accountFee = other.accountFee;
- this.accountType = other.accountType;
- this.isDefaultCredit = other.isDefaultCredit;
- this.isDefaultFee = other.isDefaultFee;
- }
- public static Account copyOf(Account src) {
- if (src.getAccountType() == AccountType.regular) {
- return new Regular((Regular) src);
- } else if (src.getAccountType() == AccountType.teenage) {
- return new Teenage((Teenage) src);
- } else if (src.getAccountType() == AccountType.business) {
- return new Business((Business) src);
- } else if (src.getAccountType() == AccountType.student) {
- return new Student((Student) src);
- } else if (src.getAccountType() == AccountType.soldier) {
- return new Soldier((Soldier) src);
- } else {
- return null;
- }
- }
- /*
- * getters for fields
- */
- // return amount of all accounts created
- public static int getAllAccountsCounter() {
- return Account.totalCounter;
- }
- // return amount of accounts created from a given type
- public static int getTypeAccountsCounter(AccountType type) {
- return Account.typeCounter[type.ordinal()];
- }
- // return owner
- public Client getOwner() {
- return this.owner;
- }
- private void setAccountType(AccountType type) throws BankAgeException {
- if (!BankUtils.isBetween(owner.getAge(), type.getMinAge(),
- type.getMaxAge())) {
- throw new BankAgeException(type, owner.getAge());
- } else
- this.accountType = type;
- }
- // return account type, as defined in AccountType.java
- public AccountType getAccountType() {
- return this.accountType;
- }
- // return account id
- public int getAccountId() {
- return this.accountId;
- }
- // return account balance
- public double getAccountBalance() {
- return this.accountBalance;
- }
- // return account fee
- // also required for interface implementation
- public double getAccountFee() {
- return this.accountFee;
- }
- // return account credit
- public int getAccountCredit() {
- return this.accountCredit;
- }
- // calculate fee amount for an amount of money
- protected double calcFee(double amount) {
- return amount * (this.accountFee / 100.0);
- }
- public boolean isDefaultFee() {
- return this.isDefaultFee;
- }
- public boolean isDefaultCredit() {
- return this.isDefaultCredit;
- }
- /*
- * setters for fields
- */
- // also required for interface implementation
- public boolean setAccountFee(double customFee) {
- double defaultFee = this.accountType.getDefaultFee();
- if (customFee == -1) {
- this.isDefaultFee = true;
- this.accountFee = defaultFee;
- return true;
- } else if (customFee <= defaultFee) {
- this.isDefaultFee = false;
- this.accountFee = customFee;
- return true;
- } else {
- try {
- throw new BankException("Error! Fee account(" + customFee
- + ") is greater than default fee!(" + defaultFee + ")");
- } catch (BankException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- }
- public boolean setAccountCredit(int customCredit) {
- if ((this instanceof Teenage) || (this instanceof Student)) {
- // error
- try {
- throw new BankException(
- "Error setting a credit for Teenage/Student account!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- } else {
- this.isDefaultCredit = false;
- this.accountCredit = customCredit;
- return true;
- }
- }
- public boolean setAccountCredit() {
- this.isDefaultCredit = true;
- this.accountCredit = this.accountType.getDefaultCredit();
- return true;
- }
- /*
- * implement interface methods defined in Accountable.java
- */
- @Override
- public int getTotalAccount() {
- return Account.totalCounter;
- }
- @Override
- public int getTotalAccountByType(AccountType type) {
- // TODO Auto-generated method stub
- return Account.typeCounter[type.ordinal()];
- }
- @Override
- public double getBalance() {
- return this.getAccountBalance();
- }
- @Override
- public boolean withDraw(double amount) {
- boolean res = false;
- double amountWithFee = amount + calcFee(amount);
- if (this.accountBalance - amountWithFee >= this.accountCredit) {
- this.accountBalance -= amountWithFee;
- res = true;
- } else {
- try {
- throw new BankNoCreditException(this, amount);
- } catch (BankNoCreditException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return res;
- }
- @Override
- public void deposit(double amount) {
- this.accountBalance += amount - calcFee(amount);
- }
- @Override
- public int getAccountNumber() {
- return this.getAccountId();
- }
- @Override
- public String toString() {
- return this.getClass().getSimpleName() + " [" + this.feildsAsString()
- + "]";
- }
- protected String feildsAsString() {
- return String
- .format("accountId=%d, , accountType=%s, personName=%s, personId=%d, age=%d, accountBalance=%.2f, , accountFee=%f%s, accountCredit=%d%s",
- accountId, accountType, owner.getName(), owner.getId(),
- owner.getAge(), accountBalance, accountFee,
- isDefaultFee ? "(default)" : "", accountCredit,
- isDefaultCredit ? "(default)" : "");
- }
- public boolean canCloseAccount() {
- double total = this.accountBalance;
- if (total < 0)
- return false;
- if (this instanceof LoanableAccount) {
- double loan = ((LoanableAccount) this).loanAmount;
- total += loan + this.calcFee(loan);
- }
- return total >= 0;
- }
- }
Add Comment
Please, Sign In to add comment