Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bank Exercise
- ============================================================
- Account class
- =============
- package myPkg;
- public class Account {
- // class attributes,
- // automatic id for the next new account
- private static int newAccountId = 1;
- // instance attributes
- // customer personal details
- protected String name; // customer name
- protected int personId; // customer id number
- // account details
- protected int accountId; // account id, auto unique generated number
- protected double balance = 0; // account balance, default value is 0
- protected double loan = 0; // loan amount, default value is 0
- protected int minBalance = 0; // minimum balance allowed, by default
- // overdraft not allowed
- protected double commission = -1; // commission, -1 for default commission
- // create a new account
- public Account(String name, int personId) {
- // set auto account id
- this.accountId = newAccountId;
- newAccountId += 1;
- // set customer details
- this.name = name;
- this.personId = personId;
- // all other attributes initialized to default values
- }
- // getters for all attributes
- public String getName() {
- return this.name;
- }
- public int getPersonId() {
- return this.personId;
- }
- public int getAccountId() {
- return this.accountId;
- }
- public double getBalance() {
- return this.balance;
- }
- public int getMinBalance() {
- return this.minBalance;
- }
- public double getLoan() {
- return this.loan;
- }
- // get actual commission
- public double getCommission() {
- return this.commission == -1 ? getDefaultCommission() : this.commission;
- }
- // get default value of commission
- // should be overridden in all derived classes
- public double getDefaultCommission() {
- return Utils.getAccountCommission();
- }
- // setters with no commission for attributes
- public boolean setName(String name) {
- if (name.trim().length() < 5)
- return false;
- this.name = name.trim();
- return true;
- }
- public boolean setMinBalance(int minBalance) {
- if (this.balance < minBalance)
- return false;
- this.minBalance = minBalance;
- return true;
- }
- public boolean setCommission(double commission) {
- if (commission == -1 || commission >= 0
- && commission <= getDefaultCommission()) {
- this.commission = commission;
- return true;
- }
- return false;
- }
- // operations with commission
- // put more money in account, return actual money added to account
- // on error return 0
- public double deposit(double amount) {
- if (amount <= 0)
- return 0;// rejected operation
- double actualMoney = amount * (1 - getCommission());
- this.balance += actualMoney;
- return actualMoney;
- }
- // get money out from account, return actual money taken out from account
- // on error return 0
- public double withdraw(double amount) {
- if (amount <= 0)
- return 0;// rejected operation
- double actualMoney = amount * (1 + getCommission());
- if (this.balance - actualMoney >= minBalance) {
- this.balance -= actualMoney;
- return actualMoney;
- }
- return 0; // rejected operation
- }
- // give a new loan to customer, return the actual money of the loan (with
- // commission)
- public double giveLoan(double loan) {
- if (this.loan != 0 || loan <= 0) {
- // reject the loan
- return 0;
- }
- // give loan amount with the bank take commission
- double actualMoney = loan * (1 + getCommission());
- this.loan = actualMoney;
- return actualMoney;
- }
- @Override
- public String toString() {
- return String
- .format("%s [accountId=%d, name=%s, personId=%d, balance=%.2f, minBalance=%d, loan=%.2f, commission=%s]",
- this.getClass().getSimpleName(), accountId, name,
- personId, balance, minBalance, loan,
- commission == -1 ? getDefaultCommission() + "(default)"
- : getCommission());
- }
- }
- Regular class
- ==================
- package myPkg;
- public class Regular extends Account {
- public Regular(String name, int personId) {
- super(name, personId);
- }
- // get default commission for regular account
- public double getDefaultCommission() {
- return Utils.getRegularCommission();
- }
- }
- Business class
- =====================
- package myPkg;
- public class Business extends Account {
- public Business(String name, int personId) {
- super(name, personId);
- }
- // get default commission for business account
- public double getDefaultCommission() {
- return Utils.getBusinessCommission();
- }
- }
- Teeanage class
- =======================
- package myPkg;
- public class Teenage extends Account {
- protected int age;
- public Teenage(String name, int personId, int age) {
- super(name, personId);
- this.age = age;
- }
- // setter and getter for age attribute
- public int getAge() {
- return this.age;
- }
- public boolean setAge(int age) {
- if (age < 0 || age > Utils.MAX_TEENAGE_AGE)
- return false;
- this.age = age;
- return true;
- }
- @Override
- public double getDefaultCommission() {
- return Utils.getTeenageCommission();
- }
- @Override
- public boolean setMinBalance(int minBalance) {
- if (minBalance != 0)
- return false;
- this.minBalance = 0;
- return true;
- }
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return super.toString().replace("]", ", age=" + this.age + "]");
- }
- }
- Stusent class
- ==================
- package myPkg;
- public class Student extends Teenage {
- public Student(String name, int personId, int age) {
- super(name, personId, age);
- }
- // get amount of money do do loan exit
- public double getExitAmount() {
- return this.loan * (1 + getCommission());
- }
- // pay all amount for loan
- public double exit(double amount) {
- // if its the right amount (including commission) then
- if (amount == getExitAmount()) {
- // no loan any more
- this.loan = 0;
- return amount;
- }
- // return the current loan, 0 for success
- return 0;
- }
- @Override
- public double getDefaultCommission() {
- // TODO Auto-generated method stub
- return Utils.getStudentCommission();
- }
- }
- Utils class
- ================
- package myPkg;
- public class Utils {
- public static final double MIN_COMMISSION = 0.0;
- public static final double MAX_COMMISSION = 0.35;
- public static final int MAX_TEENAGE_AGE = 18;
- private static double accountCommission = MAX_COMMISSION;
- private static double teenageCommission = 0.0;
- private static double studentCommission = 0.04;
- private static double businessCommission = 0.08;
- private static double regularCommission = 0.14;
- private static boolean isValidCommission(double commission) {
- return commission >= Utils.MIN_COMMISSION
- && commission <= Utils.MAX_COMMISSION;
- }
- public static double getAccountCommission() {
- return Utils.accountCommission;
- }
- public static boolean setAccountCommission(double accountCommission) {
- if (Utils.isValidCommission(accountCommission)) {
- Utils.accountCommission = accountCommission;
- return true;
- }
- return false;
- }
- public static double getRegularCommission() {
- return Utils.regularCommission;
- }
- public static boolean setRegularCommission(double regularCommission) {
- if (Utils.isValidCommission(regularCommission)) {
- Utils.regularCommission = regularCommission;
- return true;
- }
- return false;
- }
- public static double getStudentCommission() {
- return Utils.studentCommission;
- }
- public static boolean setStudentCommission(double studentCommission) {
- if (Utils.isValidCommission(studentCommission)) {
- Utils.studentCommission = studentCommission;
- return true;
- }
- return false;
- }
- public static double getTeenageCommission() {
- return Utils.teenageCommission;
- }
- public static boolean setTeenageCommission(double teenageCommission) {
- if (Utils.isValidCommission(teenageCommission)) {
- Utils.teenageCommission = teenageCommission;
- return true;
- }
- return false;
- }
- public static double getBusinessCommission() {
- return Utils.businessCommission;
- }
- public static boolean setbusinessCommission(double businessCommission) {
- if (Utils.isValidCommission(businessCommission)) {
- Utils.businessCommission = businessCommission;
- return true;
- }
- return false;
- }
- }
- Bank class
- =====================
- package myPkg;
- import java.util.Arrays;
- public class Bank {
- private Account[] accounts; // array of bank account
- public Bank() {
- accounts = new Account[0];
- }
- // add new accounts to the bank,
- // return true if successfully created, and false otherwise
- public boolean createRegularAccount(String name, int personId) {
- return addAccount(new Regular(name, personId));
- }
- public boolean createBusinessAccount(String name, int personId) {
- return addAccount(new Business(name, personId));
- }
- public boolean createTeenageAccount(String name, int personId, int age) {
- return addAccount(new Teenage(name, personId, age));
- }
- public boolean createStudentAccount(String name, int personId, int age) {
- return addAccount(new Student(name, personId, age));
- }
- // add an account to the accounts array
- private boolean addAccount(Account newAccount) {
- if (getAccountIndex(newAccount.getAccountId()) != -1) {
- return false;
- }
- accounts = Arrays.copyOf(accounts, accounts.length + 1);
- accounts[accounts.length - 1] = newAccount;
- return true;
- }
- // close an account
- public boolean closeAccounut(int accountIndex) {
- if (accountIndex >= 0 && accountIndex < accounts.length) {
- if (accounts[accountIndex].getBalance() == 0
- && accounts[accountIndex].getLoan() == 0) {
- for (int index = accountIndex; index < accounts.length - 1; index += 1) {
- accounts[index] = accounts[index + 1];
- }
- accounts = Arrays.copyOf(accounts, accounts.length - 1);
- return true;
- }
- }
- return false;
- }
- // return an account by its index
- public Account getAccount(int accountIndex) {
- if (accountIndex >= 0 && accountIndex < accounts.length) {
- return accounts[accountIndex];
- }
- return null;
- }
- // search for an account, and return its index,
- // return -1 if not found
- public int getAccountIndex(int accountId) {
- int index;
- for (index = 0; index < accounts.length; index += 1) {
- if ((accounts[index].getAccountId() == accountId))
- return index;
- }
- return -1;
- }
- // search by personId
- // return accounts of person array
- public Account[] getPersonAccounts(int personId) {
- Account[] resultArray = new Account[0];
- int index = this.getAccountIndex(personId, 0);
- while (index != -1) {
- resultArray = Arrays.copyOf(resultArray, resultArray.length + 1);
- resultArray[resultArray.length - 1] = accounts[index];
- index = this.getAccountIndex(personId, index + 1);
- }
- return resultArray;
- }
- public int getAccountIndex(int personId, int startIndex) {
- int index;
- for (index = startIndex; index < accounts.length; index += 1) {
- if ((accounts[index].getPersonId() == personId))
- return index;
- }
- return -1;
- }
- // search by person name
- // return accounts of person array
- public Account[] getPersonAccounts(String personName) {
- Account[] resultArray = new Account[0];
- int index = this.getAccountIndex(personName, 0);
- while (index != -1) {
- resultArray = Arrays.copyOf(resultArray, resultArray.length + 1);
- resultArray[resultArray.length - 1] = accounts[index];
- index = this.getAccountIndex(personName, index + 1);
- }
- return resultArray;
- }
- public int getAccountIndex(String personName, int startIndex) {
- int index;
- for (index = startIndex; index < accounts.length; index += 1) {
- if (accounts[index].getName().compareToIgnoreCase(personName) == 0)
- return index;
- }
- return -1;
- }
- public String toString(String title, Account[] accountsArray) {
- String result = title + " (" + accountsArray.length + "):\n";
- for (Account anAccount : accountsArray) {
- result += anAccount + "\n";
- }
- return result;
- }
- @Override
- public String toString() {
- return toString("All Bank Accounts", this.accounts);
- }
- }
- main program - Testrer class
- =================================
- package testerPkg;
- import myPkg.Account;
- import myPkg.Bank;
- import myPkg.Utils;
- public class Tester {
- public static void main(String[] args) {
- Bank myBank = new Bank();
- myBank.createRegularAccount("Ali", 111111111);
- myBank.createStudentAccount("Usif", 222222222, 12);
- myBank.createBusinessAccount("Hadil", 333333333);
- myBank.createBusinessAccount("Ali", 111111111);
- myBank.createTeenageAccount("Ali", 111111111, 15);
- myBank.createStudentAccount("Usif", 222222222, 12);
- myBank.createBusinessAccount("Gal", 555555555);
- myBank.createStudentAccount("Ali", 111111111, 15);
- System.out.println(myBank);
- // for all accounts of ali
- // set commission to 0.06
- // deposit 1000
- // and set minBalance to -500
- Account[] aliallAccounts = myBank.getPersonAccounts("ali");
- System.out.println(myBank.toString("Ali Accounts", aliallAccounts));
- for (Account aliAccount : aliallAccounts) {
- aliAccount.setCommission(0.06);
- aliAccount.deposit(1000);
- aliAccount.setMinBalance(-500);
- }
- // show updated accounts
- System.out.println(myBank.toString("Ali Accounts after update",
- aliallAccounts));
- // increase default commission by 1%
- Utils.setRegularCommission(Utils.getRegularCommission() + 0.01);
- Utils.setbusinessCommission(Utils.getBusinessCommission() + 0.01);
- Utils.setTeenageCommission(Utils.getTeenageCommission() + 0.01);
- Utils.setStudentCommission(Utils.getStudentCommission() + 0.01);
- // show updated accounts
- System.out.println(myBank.toString(
- "Ali Accounts after commision changed", aliallAccounts));
- // withdraw 1000 from each account
- for (Account aliAccount : aliallAccounts) {
- aliAccount.withdraw(1000);
- }
- // show updated accounts
- System.out.println(myBank.toString("Ali Accounts after withdraw 1000",
- aliallAccounts));
- // show all accounts
- System.out.println(myBank);
- }
- }
Add Comment
Please, Sign In to add comment