Starting out with JAVA from control structures through objects 6th edition Chapt
ID: 3604058 • Letter: S
Question
Starting out with JAVA from control structures through objects 6th edition Chapter 10 Inheritance PC 9 EXTRA REQuirements under question in Note/ May you please include comments in the program so i can understand it a bit better
Question: Design an abstract class named BankAccount to hold the following data for a bank account
Balance
Number of deposits this month
Number of withdrawls
Annual interest rate
Monthly service charges
The class should also have the following methods:
Constructor: The constructor should accept arguments for the balance and annual interest rate
deposit: A method that accepts an argument for the amount of the deposit. The method should add the argument to the account balance. It should also increment the variable holiding the number of deposits.
withdraw: A method that accepts an argument for the amount of the withdrawl. The method should subtract the argument from the balance. It should also increment the variable holding the number of withdrawls.
calcInterest: A method that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance, This is performed by the following formulas:
Monthly Interest Rate = (Annual Interest Rate / 12)
Monthly Interest = Balance * Monthly Interest Rate
Balance = Balance + Monthly Interest
monthlyProcess: A method that subtracts the monthly service charges from the balance, calls the calcInterest method, and then sets the variables that hold the number of withdrawls, number of deposits, and monthly service charges to zero.
Next design a SavingsAccount class that extends the BankAccount class. The SavingsAccount class should have a status field to represent an active or inactive account. If the balance of a savings account falls below $25, it becomes inactive, (The status field could be a boolean variable) No more withdrawls may be made until the balance is raised above $25, at which time the account becomes active again. The savings account class should have the Following methods:
withdraw: A method that determines whether the account is inactive before a withdrawl is made. (No withdrawl will be allowed if the account is not active.) A withdrawl is then made by calling the superclass version of the method.
deposit: A method that determines whether the account is inactive before a deposit is made. If the account is inactive and the deposit brings the balance above $25, the account becomes active again. A deposit is then made by calling the superclass version of the method.
monthlyProcess: Before the superclass method is called, this method checks the number of withdrawls. If the number of withdrawls for the month is more than 4, a service charge of $1 for each withdrawl above 4 is added to the superclass field that holds the monthly service charges. (Dont forget to check the account balance after the service charge is taken. If the balance falls below $25, the account becomes inactive.)
Note: Answer using everything below vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
BankAccount and SavingsAccount,chapter 10 Programming challenge 9
Write a static main method in each class that demonstrate how the classes work, incl. trying to make a withdrawal when it is not allowed.
Ignore the request for making BankAccount abstract (we are skipping that section)
Hint:follow the instructions one small step at a time (it may help to think of baby steps); if something does not pan out, go back to the last point where things worked.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
thank you for your time so much this would be a big help, ive been having alot of problems latley trying to catch back up in my classes
Explanation / Answer
package banking.app;
public class BankAccount {
private double balance;
private int numDeposits;
private int numWithdrawals;
private double interestRate;
private double serviceCharge;
/**
* Default constructor to create an Account with parameters
* @param balance
* @param interestRate
*/
public BankAccount(double balance, double interestRate)
{
this.balance = balance;
this.interestRate = interestRate;
numDeposits = 1;
numWithdrawals = 0 ;
}
/**
* Getter method to return balance
* @return
*/
public double getbalance()
{
return balance;
}
/**
* Method to withdraw an amount from account safely
* @param amount
*/
public void Withdraw(double amount)
{
// checking whether available balance in the account was sufficient or not to
// process the transaction
if(getbalance() >= amount)
{
balance -= amount;
System.out.println("Amount withdrawn successfully.");
System.out.println("Final Account balance after withdraw is: $"+getbalance());
numWithdrawals++;
}
else
{
System.out.println(">>>Insufficient funds in the account to process this withdraw<<<");
}
}
/**
* Method to deposit an amount to the account
* @param amount
*/
public void deposit(double amount)
{
balance += amount;
System.out.println("Amount deposited successfully.");
numDeposits++;
System.out.println("Final Account balance after deposit is: $"+getbalance());
}
/**
* Method to calculate interest and add it to main balance
*/
public void calcInterest()
{
double monthlyInterestRate = interestRate / 12;
double monthlyInterestAmount = balance * monthlyInterestRate;
balance += monthlyInterestAmount;
}
/**
* Method to set the monthly service charge
* @param charge
*/
public void setMonthlyServiceCharge(double charge)
{
this.serviceCharge = charge;
}
/**
* Method to process the monthly service charge
*/
public void monthlyProcess()
{
if(balance >= serviceCharge)
balance -= serviceCharge;
serviceCharge = 0;
numDeposits = 0;
numWithdrawals = 0;
}
/**
* Method to return number of withdrawals
* @return
*/
public int getNumWithdrawals()
{
return this.numWithdrawals;
}
/**
* Method to return number of deposits
* @return
*/
public int getNumDeposits()
{
return this.numDeposits;
}
}
package banking.app;
public class SavingsAccount extends BankAccount{
// instance variables to hold account info
private int AccountNumber;
private String customerName;
private boolean status;
/**
* Default constructor to create a savings account
* @param balance
* @param interest Rate
* @param account number
* @param customer name
*/
public SavingsAccount(double balance, double interestRate, int acno, String name)
{
super(balance, interestRate);
status = true;
this.AccountNumber = acno;
this.customerName = name;
}
/**
* Overridden Method to withdraw balance
*/
public void Withdraw(double amount)
{
if(status == false)
{
System.out.println("Sorry not able to perform the withdraw. You account is inactive.");
}
else
super.Withdraw(amount);
if(super.getbalance() < 25)
status = false;
}
/**
* Overridden method to deposit
*/
public void deposit(double amount)
{
super.deposit(amount);
if(super.getbalance() > 25)
status = true;
}
/**
* Monthly process method to deduct the service charge if number of withdrawals is more than 4
*/
public void monthlyProcess()
{
if(super.getNumWithdrawals() > 4)
{
double charge = (super.getNumWithdrawals() - 4) * 1;
super.setMonthlyServiceCharge(charge);
super.monthlyProcess();
if(super.getbalance() < 25)
this.status = false;
}
}
/**
* Method to return the account status whether active or inactive
* @return
*/
public boolean getStatus()
{
return status;
}
/**
* Method o retuen the string representation of an account
*/
public String toString()
{
return "Account number: " + this.AccountNumber+
" Customer Name: " + this.customerName+
" Number of Deposits: " + super.getNumDeposits()+
" Number of Withdrawals: " + super.getNumWithdrawals()+
" Available Balance: $" + super.getbalance()+
" Account status: " + this.status;
}
}
package banking.app;
import java.util.Scanner;
public class SavingsAccountTest {
// main method
public static void main(String[] args)
{
int acno;
String name;
Scanner input = new Scanner(System.in);
System.out.print("Enter account number: ");
acno = (Integer.parseInt(input.nextLine()));
System.out.print("Enter account name: ");
name = (input.nextLine());
System.out.print("Enter starting balance to deposit: $");
double balance = Double.parseDouble(input.nextLine());
System.out.print("Enter interest rate: ");
double rate = Double.parseDouble(input.nextLine());
SavingsAccount account= new SavingsAccount(balance, rate, acno, name);
menu(account);
}
// method to display menu and get the choice from user
public static void menu(SavingsAccount account)
{
Scanner input = new Scanner(System.in);
String choice;
while(true)
{
int amount;
System.out.println(" ****** MENU ******");
System.out.println("(D)eposit");
System.out.println("(W)ithdraw ");
System.out.println("(A)ccount details");
System.out.println("(S)tatus");
System.out.println("(Q)uit");
System.out.print("Enter your choice: ");
choice = input.nextLine();
if(choice.equalsIgnoreCase("D"))
{
System.out.print(" Enter amount to deposit: $");
amount = Integer.parseInt(input.nextLine());
if(amount < 0)
{
System.out.println("Invalid amount to deposit! ");
continue;
}
account.deposit(amount);
continue;
}
if(choice.equalsIgnoreCase("W"))
{
System.out.print(" Enter amount to withdraw: $");
amount = Integer.parseInt(input.nextLine());
if(amount < 0)
{
System.out.println("Invalid amount to withdraw! ");
continue;
}
account.Withdraw(amount);
continue;
}
if(choice.equalsIgnoreCase("A"))
{
System.out.println(" ACCOUNT INFORMATION ");
System.out.println(account);
continue;
}
if(choice.equalsIgnoreCase("S"))
{
System.out.println(" Account status: " + account.getStatus());
continue;
}
if(choice.equalsIgnoreCase("Q"))
{
System.out.println(" Thank you for using the application. Have a bright day!");
break;
}
System.out.println(" Invalid Input! Please try again. ");
}
}
}
Enter account number: 123456789
Enter account name: student123
Enter starting balance to deposit: $500
Enter interest rate: 3.50
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: W
Enter amount to withdraw: $600
>>>Insufficient funds in the account to process this withdraw<<<
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: D
Enter amount to deposit: $200
Amount deposited successfully.
Final Account balance after deposit is: $700.0
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: s
Account status: true
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: w
Enter amount to withdraw: $680
Amount withdrawn successfully.
Final Account balance after withdraw is: $20.0
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: s
Account status: false
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: d
Enter amount to deposit: $200
Amount deposited successfully.
Final Account balance after deposit is: $220.0
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: s
Account status: true
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: A
ACCOUNT INFORMATION
Account number: 123456789
Customer Name: student123
Number of Deposits: 3
Number of Withdrawals: 1
Available Balance: $220.0
Account status: true
****** MENU ******
(D)eposit
(W)ithdraw
(A)ccount details
(S)tatus
(Q)uit
Enter your choice: q
Thank you for using the application. Have a bright day!
Instructions: Create a package with name banking.app and within that package create all the three classes with the names given above and paste the codes into their class files.
BankAccount.javapackage banking.app;
public class BankAccount {
private double balance;
private int numDeposits;
private int numWithdrawals;
private double interestRate;
private double serviceCharge;
/**
* Default constructor to create an Account with parameters
* @param balance
* @param interestRate
*/
public BankAccount(double balance, double interestRate)
{
this.balance = balance;
this.interestRate = interestRate;
numDeposits = 1;
numWithdrawals = 0 ;
}
/**
* Getter method to return balance
* @return
*/
public double getbalance()
{
return balance;
}
/**
* Method to withdraw an amount from account safely
* @param amount
*/
public void Withdraw(double amount)
{
// checking whether available balance in the account was sufficient or not to
// process the transaction
if(getbalance() >= amount)
{
balance -= amount;
System.out.println("Amount withdrawn successfully.");
System.out.println("Final Account balance after withdraw is: $"+getbalance());
numWithdrawals++;
}
else
{
System.out.println(">>>Insufficient funds in the account to process this withdraw<<<");
}
}
/**
* Method to deposit an amount to the account
* @param amount
*/
public void deposit(double amount)
{
balance += amount;
System.out.println("Amount deposited successfully.");
numDeposits++;
System.out.println("Final Account balance after deposit is: $"+getbalance());
}
/**
* Method to calculate interest and add it to main balance
*/
public void calcInterest()
{
double monthlyInterestRate = interestRate / 12;
double monthlyInterestAmount = balance * monthlyInterestRate;
balance += monthlyInterestAmount;
}
/**
* Method to set the monthly service charge
* @param charge
*/
public void setMonthlyServiceCharge(double charge)
{
this.serviceCharge = charge;
}
/**
* Method to process the monthly service charge
*/
public void monthlyProcess()
{
if(balance >= serviceCharge)
balance -= serviceCharge;
serviceCharge = 0;
numDeposits = 0;
numWithdrawals = 0;
}
/**
* Method to return number of withdrawals
* @return
*/
public int getNumWithdrawals()
{
return this.numWithdrawals;
}
/**
* Method to return number of deposits
* @return
*/
public int getNumDeposits()
{
return this.numDeposits;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.