The java programing question goes like this: Write two classes to describe BankA
ID: 3725689 • Letter: T
Question
The java programing question goes like this:
Write two classes to describe BankAccount objects.
A bank account is described by the account owner's name, an account ID (stored as text), and the balance in the account.
A savings account is a type of bank account that is described by this same information and also by an interest rate.
In each class, include (or use inherited versions of):
instance data variables
two constructors
one constructor takes a starting balance; the second constructor creates an account with no money
you decide what other parameters should be included in each
getters and setters
include appropriate value checks when applicable
a toString method
you decide the output
a deposit method
include appropriate value checks
a withdrawal method
include appropriate value check
I already did some part:
public class SavingsAccount extends BankAccount {
private double interestRate;
private double minBalance;
// private static final double DEFAULD_BALANCE = 0.00;
public SavingsAccount(String name, String id, double balance, double interestRate, double minBalance) {
super(name, id, balance);
this.interestRate = interestRate;
this.minBalance = minBalance;
}
/*
* public SavingsAccount(String name, String id,double interestRate){
*
* this(name, id, DEFAULD_BALANCE, interestRate); }
*/
public double getInterestRate() {
return interestRate;
}
@Override
public void setBalance(double newBalance) {
if (newBalance >= minBalance) {
balance = newBalance;
}else {
System.out.println("Invalid Value.");
}
}
public void setInterstRate(double newInterestRate) {
interestRate = newInterestRate;
}
@Override
public String toString() {
String s = super.toString() + " Interest Rate: " + interestRate;
return s;
}
@Override
public boolean withdrawal(double withdrawleValue) {
if ((balance - withdrawleValue) >= minBalance) {
return super.withdrawal(withdrawleValue);
} else {
System.out.println("Banlance cannot be less than minimun balance. ");
return false;
}
}
}
My question is :
Include a minimum balance as part of what describes a savings account. Update the class as necessary, making sure that the account is not allowed to go below this minimum balance. (use the saving account program above)
Explanation / Answer
//TestSavingsAccount.java
public class TestSavingsAccount {
public static void main(String[] args) {
//Create an instance of SavingsAccount with name,id,
//balance,interest rate and min balance
SavingsAccount sacct=
new SavingsAccount("Johnson", "SB101", 10000, 0.10, 1000);
System.out.println("Withdrawl: 9000");
//calling withdrawal for 9000
sacct.withdrawal(9000);
//Print current balance
System.out.println("Current Balance: "+sacct.getBalance());
System.out.println("Withdrawl: 500");
//calling withdrawal for 500
sacct.withdrawal(500);
//Print current balance
System.out.println("Current Balance: "+sacct.getBalance());
}
}
------------------------------------------------------------------------------------------------------
//SavingsAccount.java
public class SavingsAccount extends BankAccount {
//declare variables
private double interestRate;
private double minBalance;
//Constructor
public SavingsAccount(String name, String id,
double balance, double interestRate, double minBalance) {
super(name, id, balance);
this.interestRate = interestRate;
this.minBalance = minBalance;
}
public double getInterestRate() {
return interestRate;
}
//Checking account balance is minimum balance
public void setBalance(double newBalance) {
if (newBalance >= minBalance) {
balance = newBalance;
}else {
System.out.println("Invalid Value.");
}
}
//Set interest rate
public void setInterstRate(double newInterestRate) {
interestRate = newInterestRate;
}
//Override toString method
public String toString() {
String s = super.toString() + " Interest Rate: " + interestRate;
return s;
}
//withdrawal method
public boolean withdrawal(double withdrawleValue) {
if ((balance - withdrawleValue) >= minBalance) {
return super.withdrawal(withdrawleValue);
} else {
System.out.println("***Error :Min Balance = 1000*** ");
return false;
}
}
}//end of SavingsAccount
------------------------------------------------------------------------------------------------------
//BankAccount.java
public class BankAccount
{
protected double balance;
private String customerName;
private String id;
/**
* Constructs a bank account with a given balance
*/
public BankAccount( String name,String id,double initialBalance)
{
balance = initialBalance;
customerName = name;
this.id=id;
}
public void setBalance(double newBalance)
{
balance=newBalance;
}
/**
* Gets the customer name for this account
*/
public String getCustomerName()
{
return customerName;
}
/*
Deposits money into the bank account.
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/*
Withdraws money from the bank account.
*/
public boolean withdrawal(double amount)
{
if(balance-amount<0)
return false;
else
{
balance = balance - amount;
return true;
}
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}//end of BankAccount class
------------------------------------------------------------------------------------------------------
Sample Output:
Withdrawl: 9000
Current Balance: 1000.0
Withdrawl: 500
***Error :Min Balance = 1000***
Current Balance: 1000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.