In Java create two subclasses for checking and savings accounts. A checking acco
ID: 3639362 • Letter: I
Question
In Java create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Write a test program that creates objects of Account, Savings account, and checking and invokes their toString() methods. Draw a UML diagram that demonstrates this.Explanation / Answer
01 import javax.swing.JOptionPane; 02 03 public class testAccount { 04 public static void main(String[] args) { 05 boolean done = true; 06 Account account = null; 07 Account account2 = null; 08 String accountTypeString = JOptionPane.showInputDialog(null, " Choose an account type: " + " " + "1- Saving Account " + " " + 09 "2- Checking Accoun " + " "); 10 int accountType = Integer.parseInt(accountTypeString); 11 12 if(accountType == 1 || accountType == 2){ 13 Account Account(1, 100, .045); 14 account = oneAccount; 15 Account twoAccount = new Account(2, 100, .045); 16 account2 = twoAccount; 17 } 18 else if(accountType < 1 || accountType > 2){ 19 JOptionPane.showMessageDialog(null, "Invalid input"); 20 } 21 22 if(accountType == 1) 23 while(done){ 24 String savingsAccountActionsString = JOptionPane.showInputDialog( null, " Choose an option: " + " " + "1- Withdraw " + " " + 25 "2- Deposit " + " "); 26 int savingsAccountActions = Integer.parseInt(savingsAccountActionsString); 27 if(savingsAccountActions == 1){ 28 String savingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: "); 29 double savingsWithdrawAmount = Double.parseDouble(savingsWithdrawAmountString); 30 if(savingsWithdrawAmount > account.getBalance()){ 31 JOptionPane.showMessageDialog(null, "Insufficient funds"); 32 } 33 else { 34 account.withdraw(savingsWithdrawAmount); 35 JOptionPane.showMessageDialog(null, "The balance is " + account.getBalance()); 36 done = false; 37 } 38 } 39 40 } 41 42 if(accountType == 2) 43 while(done){ 44 String checkingsAccountActionsString = JOptionPane.showInputDialog( null, " Choose an option: " + " " + "1- Withdraw " + " " + 45 "2- Deposit " + " "); 46 int checkingsAccountActions = Integer.parseInt(checkingsAccountActionsString); 47 if(checkingsAccountActions == 1){ 48 String checkingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: "); 49 double checkingsWithdrawAmount = Double.parseDouble(checkingsWithdrawAmountString); 50 if(checkingsWithdrawAmount > account2.getBalance()){ 51 JOptionPane.showMessageDialog(null, "Insufficient funds"); 52 } 53 else { 54 account2.withdraw(checkingsWithdrawAmount); 55 JOptionPane.showMessageDialog(null, "The balance is " + account2.getBalance()); 56 done = false; 57 } 58 } 59 60 } 61 } 62 } 001 import javax.swing.JOptionPane; 002 import java.util.Date; 003 public class TestAccount { 004 public static void main(String[] args) { 005 006 Account account = new Account(1122, 20000, 4.5); 007 008 account.setWithdraw(2500); 009 account.setDeposite(3000); 010 account.setTotal(0); 011 account.setMonthly(0); 012 013 JOptionPane.showMessageDialog(null, account.toString()); 014 015 JOptionPane.showMessageDialog(null, "Good Buy"); 016 } 017 } 018 019 class Account { 020 021 private int id = 0; 022 private double balance = 0; 023 private double annualInterestRate = 0; 024 private double withdraw = 0; 025 private double deposite = 0; 026 private double total = 0; 027 private double monthly = 0; 028 private Date date = new Date(); 029 030 public Account() { 031 } 032 033 public Account(int id, double balance, double annualInterestRate){ 034 this.id = id; 035 this.balance = balance; 036 this.annualInterestRate = annualInterestRate; 037 } 038 039 public int getId(){ 040 041 return this.id; 042 043 } 044 045 public void setId(int newId) { 046 047 id = newId; 048 } 049 050 public double getBalance() { 051 052 return this.balance; 053 } 054 055 public void setBalance(double newBalance) { 056 057 balance = newBalance; 058 } 059 060 public double getAnnualInterestRate() { 061 062 return this.annualInterestRate; 063 } 064 065 public void setAnnualInterestRate(double newAnnualInterestRate) { 066 067 annualInterestRate = newAnnualInterestRate; 068 069 } 070 071 public java.util.Date getDate() { 072 073 return this.date; 074 075 } 076 077 public double getWithdraw() { 078 079 return this.withdraw; 080 } 081 082 public void setWithdraw(double newWithdraw) { 083 084 withdraw = newWithdraw; 085 086 } 087 088 public double getDeposite() { 089 090 return this.deposite; 091 } 092 093 public void setDeposite(double newDeposite) { 094 095 deposite = newDeposite; 096 097 } 098 099 public double getTotal(double balance, double deposite, double withdraw) { 100 101 return this.total; 102 } 103 104 public void setTotal(double newTotal) { 105 106 total = balance - withdraw + deposite; 107 108 } 109 110 public double getMonthly(double total) { 111 112 return this.monthly; 113 } 114 115 public void setMonthly(double newMonthly) { 116 117 monthly = total * .045 / 12; 118 119 } 120 121 public String toString() { 122 return "Banking Account Information " 123 + " Your Customer ID # " + id 124 + " Starting Balnce $" + balance 125 + " Annual Interest Rate " + annualInterestRate + "%" 126 + " Withdraw Amount $" + withdraw 127 + " Deposite Amount $" + deposite 128 + " Subtotal $" + total 129 + " Monthly Interest $" 130 + Math.round(monthly * 100.0)/100.0 131 + " " + " " + "As of " + date; 132 } 133 } To the banking package, add the SavingsAccount and CheckingAccount subclasses, as modeled by Figure 1 above. Change the access mode of the balance attribute to protected. Use the TestBankingA.java file (http://www.networks-lab.net/zhouj/SYCS203/ TestBankingA.java ) for this exercise. 1. Implement the SavingsAccount Class (1). The SavingsAccount class must extend the Account class. (2) It must include an interestRate attribute with type double. (3) It must include a public constructor that has two parameters: balance and interest_rate. This constructor must pass the balance parameter to the parent constructor by calling super(balance). 2. Implement the CheckingAccount Class (1) The CheckingAccount class must extend the Account class. (2) It must include an overdraftProtection attribute with type double. (3) It must include one public constructor that takes one parameter: balance. This constructor must pass the balance parameter to the parent constructor by calling super(balance). (4) It must include another public constructor that takes two parameters: balance and protect. This constructor must pass the balance parameter to the parent constructor by calling super(balance) and set the overdraftProtection attribute. (5) The CheckingAccount class must override the withdraw method. It must perform the following check: If the current balance is adequate to cover the amount to withdraw, then proceed as usual. If not and if there is overdraft protection, then attempt to cover the difference (balance - amount) by value of the overdraftProtection. If the amount needed to cover the overdraft is greater than the current level of protection, then the whole transaction must fail with the checking balance unaffected. 3. Compile and execute the TestingBankingA program. The output should be: Creating the customer Jane Smith. Creating her Savings Account with a 500.00 balance and 3% interest. Creating the customer Owen Bryant. Creating his Checking Account with a 500.00 balance and no overdraft protection. Creating the customer Tim Soley. Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection. Creating the customer Maria Soley. Maria shares her Checking Account with her husband Tim. Retrieving the customer Jane Smith with her savings account. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: false Customer [Simms, Jane] has a balance of 324.88 Retrieving the customer Owen Bryant with his checking account with no overdraft protection. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: false Customer [Bryant, Owen] has a balance of 324.88 Retrieving the customer Tim Soley with his checking account that has an overdraft protection. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: true Customer [Soley, Tim] has a balance of 0.00 Retrieving the customer Maria Soley with her joint checking account with husband Tim. Deposit 150.00: true Withdraw 750.00: false Customer [Soley, Maria] has a balance of 150.00 Jane's savings account and Owen's checking account fundamentally behave as a plain- old bank account. But Tim and Maria's joint checking account has 500.00 worth of overdraft protection. Tim's transactions dip into that protection and therefore his ending balance is 0.00. His account's overdraft protection level is 424.88. Finally, Maria deposits 150.00 into this joint account; raising the balance from 0.00 to 150.00. Then she tries to withdraw 750.00, which fails because neither balance nor the overdraft protection can cover that requested amount. Exercise 2 - Creating Customer Accounts Exercise objective - In this exercise you will create a heterogeneous array to represent the aggregation of customers to accounts. That is, a given customer can have several accounts of different types. Use the TestBankingB.java file (http://www.networks-lab.net/zhouj/SYCS203/ TestBankingB.java )for this exercise. Tasks Figure 2 A Bank With One Or More Customers Modify the Customer Class, add account management methods (1) Modify the Customer class to handle the accounts association with generic multiplicity; It must include the public methods: addAccount(account), getAccount(int), and getNumOfAccounts(). This program creates a set of customers and accounts and generates a report of customers and their account balances. (2) In the TestBankingB.java file you will find comment blocks that start and end with / *** … ***/. These comments indicate the locations of the code that you must supply. (a) Use the instanceof operator to test what type of account you have and set account_type to an appropriate value, such as "Savings Account" or "Checking Account." (b). Print out the type of account and balance. Use the currency_format formatter to generate a "currency string" for the balance. (3) Compile and run TestBankingB . You should see the following output. Customer: Simms, Jane Savings Account: current balance is $500.00 Checking Account: current balance is $200.00 Customer: Bryant, Owen Checking Account: current balance is $200.00 Customer: Soley, Tim Savings Account: current balance is $1,500.00 Checking Account: current balance is $200.00 Customer: Soley, Maria Checking Account: current balance is $200.00 Savings Account: current balance is $150.00 Exercise 3 - Bonus question Exercise objectives - In this exercise, you will create two subclasses of the Account class in the Banking project: SavingsAccount and CheckingAccount. Note - This is an alternative version of Exercise 1. It incorporates a more complex model of the overdraft protection mechanism. It uses the customer's savings account to perform the overdraft protection. This exercise must not be attempted until after both previous exercises have been completed. Tasks Figure 3 Two Coupled Subclasses of the Account Class To the banking package, add the SavingsAccount and CheckingAccount subclasses, Implement the SavingsAccount class same as in exercise 1. 1. Implement CheckingAccount as following (a) The CheckingAccount class must extend the Account class. It has no additional data attributes, but an association attribute, called protectedBy, must be included with the type SavingsAccount; the default value must be null. (b) The CheckingAccount class must override the withdraw method. It must perform the following check: If the current balance is adequate to cover the amount to withdraw, then proceed as usual. If not and if there is overdraft protection, then attempt to cover the difference (balance - amount) by the savings account. If the latter transaction fails, then the whole transaction must fail with the checking balance unaffected. 01 public class CheckingAccount extends Account { 02 03 double overDraft; 04 double overDraftFee = 25.00; 05 double checkFee = .10; 06 07 public CheckingAccount(String accNum,String accType, double balance) { 08 super(accNum, accType, balance); 09 10 11 12 } 13 Transaction tran[] = new Transaction[numTrans]; 14 public double getDeposit(){ 15 if 16 } 17 18 /** 19 * @return the overDraft 20 */ 21 public double getOverDraft() { 22 return overDraft; 23 } 24 25 @Override 26 public double getDeposit() { 27 28 return deposit; 29 } 30 31 @Override 32 public double getWithdraw() { 33 34 return withdraw; 35 } 36 37 38 39 }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.