mmayoub

Bank exercise - oop

Jul 24th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.91 KB | None | 0 0
  1. Bank Exercise
  2. ============================================================
  3.  
  4. Account class
  5. =============
  6. package myPkg;
  7.  
  8. public class Account {
  9.     // class attributes,
  10.     // automatic id for the next new account
  11.     private static int newAccountId = 1;
  12.  
  13.     // instance attributes
  14.     // customer personal details
  15.     protected String name; // customer name
  16.     protected int personId; // customer id number
  17.  
  18.     // account details
  19.     protected int accountId; // account id, auto unique generated number
  20.     protected double balance = 0; // account balance, default value is 0
  21.     protected double loan = 0; // loan amount, default value is 0
  22.  
  23.     protected int minBalance = 0; // minimum balance allowed, by default
  24.                                     // overdraft not allowed
  25.     protected double commission = -1; // commission, -1 for default commission
  26.  
  27.     // create a new account
  28.     public Account(String name, int personId) {
  29.         // set auto account id
  30.         this.accountId = newAccountId;
  31.         newAccountId += 1;
  32.  
  33.         // set customer details
  34.         this.name = name;
  35.         this.personId = personId;
  36.  
  37.         // all other attributes initialized to default values
  38.     }
  39.  
  40.     // getters for all attributes
  41.     public String getName() {
  42.         return this.name;
  43.     }
  44.  
  45.     public int getPersonId() {
  46.         return this.personId;
  47.     }
  48.  
  49.     public int getAccountId() {
  50.         return this.accountId;
  51.     }
  52.  
  53.     public double getBalance() {
  54.         return this.balance;
  55.     }
  56.  
  57.     public int getMinBalance() {
  58.         return this.minBalance;
  59.     }
  60.  
  61.     public double getLoan() {
  62.         return this.loan;
  63.     }
  64.  
  65.     // get actual commission
  66.     public double getCommission() {
  67.         return this.commission == -1 ? getDefaultCommission() : this.commission;
  68.     }
  69.  
  70.     // get default value of commission
  71.     // should be overridden in all derived classes
  72.     public double getDefaultCommission() {
  73.         return Utils.getAccountCommission();
  74.     }
  75.  
  76.     // setters with no commission for attributes
  77.     public boolean setName(String name) {
  78.         if (name.trim().length() < 5)
  79.             return false;
  80.  
  81.         this.name = name.trim();
  82.         return true;
  83.     }
  84.  
  85.     public boolean setMinBalance(int minBalance) {
  86.         if (this.balance < minBalance)
  87.             return false;
  88.  
  89.         this.minBalance = minBalance;
  90.         return true;
  91.     }
  92.  
  93.     public boolean setCommission(double commission) {
  94.         if (commission == -1 || commission >= 0
  95.                 && commission <= getDefaultCommission()) {
  96.             this.commission = commission;
  97.             return true;
  98.         }
  99.  
  100.         return false;
  101.     }
  102.  
  103.     // operations with commission
  104.     // put more money in account, return actual money added to account
  105.     // on error return 0
  106.     public double deposit(double amount) {
  107.         if (amount <= 0)
  108.             return 0;// rejected operation
  109.  
  110.         double actualMoney = amount * (1 - getCommission());
  111.         this.balance += actualMoney;
  112.  
  113.         return actualMoney;
  114.     }
  115.  
  116.     // get money out from account, return actual money taken out from account
  117.     // on error return 0
  118.     public double withdraw(double amount) {
  119.         if (amount <= 0)
  120.             return 0;// rejected operation
  121.  
  122.         double actualMoney = amount * (1 + getCommission());
  123.         if (this.balance - actualMoney >= minBalance) {
  124.             this.balance -= actualMoney;
  125.             return actualMoney;
  126.         }
  127.  
  128.         return 0; // rejected operation
  129.     }
  130.  
  131.     // give a new loan to customer, return the actual money of the loan (with
  132.     // commission)
  133.     public double giveLoan(double loan) {
  134.         if (this.loan != 0 || loan <= 0) {
  135.             // reject the loan
  136.             return 0;
  137.         }
  138.  
  139.         // give loan amount with the bank take commission
  140.         double actualMoney = loan * (1 + getCommission());
  141.         this.loan = actualMoney;
  142.  
  143.         return actualMoney;
  144.     }
  145.  
  146.     @Override
  147.     public String toString() {
  148.         return String
  149.                 .format("%s [accountId=%d, name=%s, personId=%d, balance=%.2f, minBalance=%d, loan=%.2f, commission=%s]",
  150.                         this.getClass().getSimpleName(), accountId, name,
  151.                         personId, balance, minBalance, loan,
  152.                         commission == -1 ? getDefaultCommission() + "(default)"
  153.                                 : getCommission());
  154.     }
  155.  
  156. }
  157.  
  158.  
  159. Regular class
  160. ==================
  161. package myPkg;
  162.  
  163. public class Regular extends Account {
  164.  
  165.     public Regular(String name, int personId) {
  166.         super(name, personId);
  167.     }
  168.  
  169.     // get default commission for regular account
  170.     public double getDefaultCommission() {
  171.         return Utils.getRegularCommission();
  172.     }
  173. }
  174.  
  175.  
  176. Business class
  177. =====================
  178. package myPkg;
  179.  
  180. public class Business extends Account {
  181.  
  182.     public Business(String name, int personId) {
  183.         super(name, personId);
  184.     }
  185.  
  186.     // get default commission for business account
  187.     public double getDefaultCommission() {
  188.         return Utils.getBusinessCommission();
  189.     }
  190. }
  191.  
  192.  
  193. Teeanage class
  194. =======================
  195. package myPkg;
  196.  
  197. public class Teenage extends Account {
  198.     protected int age;
  199.  
  200.     public Teenage(String name, int personId, int age) {
  201.         super(name, personId);
  202.         this.age = age;
  203.     }
  204.  
  205.     // setter and getter for age attribute
  206.     public int getAge() {
  207.         return this.age;
  208.     }
  209.  
  210.     public boolean setAge(int age) {
  211.         if (age < 0 || age > Utils.MAX_TEENAGE_AGE)
  212.             return false;
  213.  
  214.         this.age = age;
  215.         return true;
  216.     }
  217.  
  218.     @Override
  219.     public double getDefaultCommission() {
  220.         return Utils.getTeenageCommission();
  221.     }
  222.  
  223.     @Override
  224.     public boolean setMinBalance(int minBalance) {
  225.         if (minBalance != 0)
  226.             return false;
  227.  
  228.         this.minBalance = 0;
  229.         return true;
  230.     }
  231.  
  232.     @Override
  233.     public String toString() {
  234.         // TODO Auto-generated method stub
  235.         return super.toString().replace("]", ", age=" + this.age + "]");
  236.     }
  237.  
  238. }
  239.  
  240.  
  241. Stusent class
  242. ==================
  243. package myPkg;
  244.  
  245. public class Student extends Teenage {
  246.  
  247.     public Student(String name, int personId, int age) {
  248.         super(name, personId, age);
  249.     }
  250.  
  251.     // get amount of money do do loan exit
  252.     public double getExitAmount() {
  253.         return this.loan * (1 + getCommission());
  254.     }
  255.  
  256.     // pay all amount for loan
  257.     public double exit(double amount) {
  258.         // if its the right amount (including commission) then
  259.         if (amount == getExitAmount()) {
  260.             // no loan any more
  261.             this.loan = 0;
  262.             return amount;
  263.         }
  264.         // return the current loan, 0 for success
  265.         return 0;
  266.     }
  267.  
  268.     @Override
  269.     public double getDefaultCommission() {
  270.         // TODO Auto-generated method stub
  271.         return Utils.getStudentCommission();
  272.     }
  273. }
  274.  
  275.  
  276. Utils class
  277. ================
  278. package myPkg;
  279.  
  280. public class Utils {
  281.     public static final double MIN_COMMISSION = 0.0;
  282.     public static final double MAX_COMMISSION = 0.35;
  283.     public static final int MAX_TEENAGE_AGE = 18;
  284.  
  285.     private static double accountCommission = MAX_COMMISSION;
  286.     private static double teenageCommission = 0.0;
  287.     private static double studentCommission = 0.04;
  288.     private static double businessCommission = 0.08;
  289.     private static double regularCommission = 0.14;
  290.  
  291.     private static boolean isValidCommission(double commission) {
  292.         return commission >= Utils.MIN_COMMISSION
  293.                 && commission <= Utils.MAX_COMMISSION;
  294.     }
  295.  
  296.     public static double getAccountCommission() {
  297.         return Utils.accountCommission;
  298.     }
  299.  
  300.     public static boolean setAccountCommission(double accountCommission) {
  301.         if (Utils.isValidCommission(accountCommission)) {
  302.             Utils.accountCommission = accountCommission;
  303.             return true;
  304.         }
  305.         return false;
  306.     }
  307.  
  308.     public static double getRegularCommission() {
  309.         return Utils.regularCommission;
  310.     }
  311.  
  312.     public static boolean setRegularCommission(double regularCommission) {
  313.         if (Utils.isValidCommission(regularCommission)) {
  314.             Utils.regularCommission = regularCommission;
  315.             return true;
  316.         }
  317.         return false;
  318.     }
  319.  
  320.     public static double getStudentCommission() {
  321.         return Utils.studentCommission;
  322.     }
  323.  
  324.     public static boolean setStudentCommission(double studentCommission) {
  325.         if (Utils.isValidCommission(studentCommission)) {
  326.             Utils.studentCommission = studentCommission;
  327.             return true;
  328.         }
  329.         return false;
  330.     }
  331.  
  332.     public static double getTeenageCommission() {
  333.         return Utils.teenageCommission;
  334.     }
  335.  
  336.     public static boolean setTeenageCommission(double teenageCommission) {
  337.         if (Utils.isValidCommission(teenageCommission)) {
  338.             Utils.teenageCommission = teenageCommission;
  339.             return true;
  340.         }
  341.         return false;
  342.     }
  343.  
  344.     public static double getBusinessCommission() {
  345.         return Utils.businessCommission;
  346.     }
  347.  
  348.     public static boolean setbusinessCommission(double businessCommission) {
  349.         if (Utils.isValidCommission(businessCommission)) {
  350.             Utils.businessCommission = businessCommission;
  351.             return true;
  352.         }
  353.         return false;
  354.     }
  355. }
  356.  
  357.  
  358. Bank class
  359. =====================
  360. package myPkg;
  361.  
  362. import java.util.Arrays;
  363.  
  364. public class Bank {
  365.     private Account[] accounts; // array of bank account
  366.  
  367.     public Bank() {
  368.         accounts = new Account[0];
  369.     }
  370.  
  371.     // add new accounts to the bank,
  372.     // return true if successfully created, and false otherwise
  373.     public boolean createRegularAccount(String name, int personId) {
  374.         return addAccount(new Regular(name, personId));
  375.     }
  376.  
  377.     public boolean createBusinessAccount(String name, int personId) {
  378.         return addAccount(new Business(name, personId));
  379.     }
  380.  
  381.     public boolean createTeenageAccount(String name, int personId, int age) {
  382.         return addAccount(new Teenage(name, personId, age));
  383.     }
  384.  
  385.     public boolean createStudentAccount(String name, int personId, int age) {
  386.         return addAccount(new Student(name, personId, age));
  387.     }
  388.  
  389.     // add an account to the accounts array
  390.     private boolean addAccount(Account newAccount) {
  391.         if (getAccountIndex(newAccount.getAccountId()) != -1) {
  392.             return false;
  393.         }
  394.         accounts = Arrays.copyOf(accounts, accounts.length + 1);
  395.         accounts[accounts.length - 1] = newAccount;
  396.         return true;
  397.     }
  398.  
  399.     // close an account
  400.     public boolean closeAccounut(int accountIndex) {
  401.         if (accountIndex >= 0 && accountIndex < accounts.length) {
  402.             if (accounts[accountIndex].getBalance() == 0
  403.                     && accounts[accountIndex].getLoan() == 0) {
  404.                 for (int index = accountIndex; index < accounts.length - 1; index += 1) {
  405.                     accounts[index] = accounts[index + 1];
  406.                 }
  407.                 accounts = Arrays.copyOf(accounts, accounts.length - 1);
  408.                 return true;
  409.             }
  410.         }
  411.         return false;
  412.     }
  413.  
  414.     // return an account by its index
  415.     public Account getAccount(int accountIndex) {
  416.         if (accountIndex >= 0 && accountIndex < accounts.length) {
  417.             return accounts[accountIndex];
  418.         }
  419.  
  420.         return null;
  421.     }
  422.  
  423.     // search for an account, and return its index,
  424.     // return -1 if not found
  425.     public int getAccountIndex(int accountId) {
  426.         int index;
  427.         for (index = 0; index < accounts.length; index += 1) {
  428.             if ((accounts[index].getAccountId() == accountId))
  429.                 return index;
  430.         }
  431.  
  432.         return -1;
  433.     }
  434.  
  435.     // search by personId
  436.     // return accounts of person array
  437.     public Account[] getPersonAccounts(int personId) {
  438.         Account[] resultArray = new Account[0];
  439.         int index = this.getAccountIndex(personId, 0);
  440.  
  441.         while (index != -1) {
  442.             resultArray = Arrays.copyOf(resultArray, resultArray.length + 1);
  443.             resultArray[resultArray.length - 1] = accounts[index];
  444.  
  445.             index = this.getAccountIndex(personId, index + 1);
  446.         }
  447.         return resultArray;
  448.     }
  449.  
  450.     public int getAccountIndex(int personId, int startIndex) {
  451.         int index;
  452.         for (index = startIndex; index < accounts.length; index += 1) {
  453.             if ((accounts[index].getPersonId() == personId))
  454.                 return index;
  455.         }
  456.  
  457.         return -1;
  458.     }
  459.  
  460.     // search by person name
  461.     // return accounts of person array
  462.     public Account[] getPersonAccounts(String personName) {
  463.         Account[] resultArray = new Account[0];
  464.         int index = this.getAccountIndex(personName, 0);
  465.  
  466.         while (index != -1) {
  467.             resultArray = Arrays.copyOf(resultArray, resultArray.length + 1);
  468.             resultArray[resultArray.length - 1] = accounts[index];
  469.  
  470.             index = this.getAccountIndex(personName, index + 1);
  471.         }
  472.         return resultArray;
  473.     }
  474.  
  475.     public int getAccountIndex(String personName, int startIndex) {
  476.         int index;
  477.         for (index = startIndex; index < accounts.length; index += 1) {
  478.             if (accounts[index].getName().compareToIgnoreCase(personName) == 0)
  479.                 return index;
  480.         }
  481.  
  482.         return -1;
  483.     }
  484.  
  485.     public String toString(String title, Account[] accountsArray) {
  486.         String result = title + " (" + accountsArray.length + "):\n";
  487.         for (Account anAccount : accountsArray) {
  488.             result += anAccount + "\n";
  489.         }
  490.  
  491.         return result;
  492.     }
  493.  
  494.     @Override
  495.     public String toString() {
  496.         return toString("All Bank Accounts", this.accounts);
  497.     }
  498.  
  499. }
  500.  
  501.  
  502. main program - Testrer class
  503. =================================
  504. package testerPkg;
  505.  
  506. import myPkg.Account;
  507. import myPkg.Bank;
  508. import myPkg.Utils;
  509.  
  510. public class Tester {
  511.  
  512.     public static void main(String[] args) {
  513.         Bank myBank = new Bank();
  514.  
  515.         myBank.createRegularAccount("Ali", 111111111);
  516.         myBank.createStudentAccount("Usif", 222222222, 12);
  517.         myBank.createBusinessAccount("Hadil", 333333333);
  518.         myBank.createBusinessAccount("Ali", 111111111);
  519.         myBank.createTeenageAccount("Ali", 111111111, 15);
  520.         myBank.createStudentAccount("Usif", 222222222, 12);
  521.         myBank.createBusinessAccount("Gal", 555555555);
  522.         myBank.createStudentAccount("Ali", 111111111, 15);
  523.  
  524.         System.out.println(myBank);
  525.  
  526.         // for all accounts of ali
  527.         // set commission to 0.06
  528.         // deposit 1000
  529.         // and set minBalance to -500
  530.         Account[] aliallAccounts = myBank.getPersonAccounts("ali");
  531.         System.out.println(myBank.toString("Ali Accounts", aliallAccounts));
  532.         for (Account aliAccount : aliallAccounts) {
  533.             aliAccount.setCommission(0.06);
  534.             aliAccount.deposit(1000);
  535.             aliAccount.setMinBalance(-500);
  536.         }
  537.         // show updated accounts
  538.         System.out.println(myBank.toString("Ali Accounts after update",
  539.                 aliallAccounts));
  540.  
  541.         // increase default commission by 1%
  542.         Utils.setRegularCommission(Utils.getRegularCommission() + 0.01);
  543.         Utils.setbusinessCommission(Utils.getBusinessCommission() + 0.01);
  544.         Utils.setTeenageCommission(Utils.getTeenageCommission() + 0.01);
  545.         Utils.setStudentCommission(Utils.getStudentCommission() + 0.01);
  546.  
  547.         // show updated accounts
  548.         System.out.println(myBank.toString(
  549.                 "Ali Accounts after commision changed", aliallAccounts));
  550.  
  551.         // withdraw 1000 from each account
  552.         for (Account aliAccount : aliallAccounts) {
  553.             aliAccount.withdraw(1000);
  554.         }
  555.         // show updated accounts
  556.         System.out.println(myBank.toString("Ali Accounts after withdraw 1000",
  557.                 aliallAccounts));
  558.  
  559.         // show all accounts
  560.         System.out.println(myBank);
  561.     }
  562. }
Add Comment
Please, Sign In to add comment