Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class BankAccount { private String customerName; private double savingAccountBalance; private double checkingAccountBalance; / / Constructor public BankAccount ( String newName, double amt 1 , double

class BankAccount {
private String customerName;
private double savingAccountBalance;
private double checkingAccountBalance;
// Constructor
public BankAccount(String newName, double amt1, double amt2){
this.customerName = newName;
//if amount amt1 is less than 0.
if(amt1<0)
amt1=0;
//if amount amt1 is less than 0.
if(amt2<0)
amt2=0;
this.checkingAccountBalance = amt1;
this.savingAccountBalance = amt2;
}
// methods
public String getCustomerName(){
return customerName;
}
public void setCustomerName(String newName){
this.customerName = newName;
}
public double getSavingAccountBalance(){
return savingAccountBalance;
}
public void setSavingAccountBalance(double amt){
this.savingAccountBalance = amt;
}
public double getCheckingAccountBalance(){
return checkingAccountBalance;
}
public void setCheckingAccountBalance(double amt){
this.checkingAccountBalance = amt;
}
//- add parameter amt to the checking account balance (only if positive)
public void depositChecking(double amt){
if(amt>0)
this.checkingAccountBalance = this.checkingAccountBalance + amt;
}
//- add parameter amt to the savings account balance (only if positive)
public void depositSavings(double amt){
if(amt>0)
this.savingAccountBalance = this.savingAccountBalance + amt;
}
//- subtract parameter amt from the checking account balance (only if positive)
public void withdrawChecking(double amt){
if(amt>0){
if(this.checkingAccountBalance-amt>=0)
this.checkingAccountBalance = this.checkingAccountBalance-amt;
else
System.out.println("Insufficient balance");
}
}
//- subtract parameter amt from the savings account balance (only if positive)
public void withdrawSavings(double amt){
if(amt>0){
if(this.savingAccountBalance-amt>=0)
this.savingAccountBalance = this.savingAccountBalance-amt;
else
System.out.println("Insufficient balance");
}
}
//- subtract parameter amt from the checking account balance and add to the savings account balance (only if positive
public void transferToSavings(double amt){
if(amt>0){
if(this.checkingAccountBalance-amt >=0){
this.checkingAccountBalance = this.checkingAccountBalance - amt;
this.savingAccountBalance = this.savingAccountBalance + amt;
}
else
System.out.println("Insufficient balance");
}
}
@Override
public String toString(){
return "customerName : "+ this.customerName +"
Saving Account Balance: "+ this.savingAccountBalance +"
Checking Account Balance: "+ this.checkingAccountBalance;
}
}
// drive class
public class BankAccountTest{
public static void main(String[] args){
// create a object of BankAccount class
BankAccount obj = new BankAccount("Amit",8000,10000);
System.out.println("***************Account Details ******************");
System.out.println(obj);
System.out.println("**************************************************
");
obj.depositSavings(2000);
System.out.println("
Saving Account Balance after deposit 2000: "+ obj.getSavingAccountBalance());
obj.depositChecking(3000);
System.out.println("
Checking Account Balance after deposit 3000: "+ obj.getCheckingAccountBalance());
obj.withdrawSavings(3000);
System.out.println("
Saving Account Balance after withdraw 3000: "+ obj.getSavingAccountBalance());
obj.withdrawChecking(5000);
System.out.println("
Checking Account Balance after withdraw 5000: "+ obj.getCheckingAccountBalance());
obj.transferToSavings(1000);
System.out.println("
Checking Account Balance after transfer 1000 to Saving Account: "+ obj.getCheckingAccountBalance());
System.out.println("Saving Account Balance after transfer 1000: "+ obj.getSavingAccountBalance());
obj.depositSavings(-2000);
System.out.println("
Saving Account Balance after deposit -2000: "+ obj.getSavingAccountBalance());
obj.depositChecking(-3000);
System.out.println("
Checking Account Balance after deposit -3000: "+ obj.getCheckingAccountBalance());
obj.withdrawSavings(8000);
System.out.println("
Saving Account Balance after withdraw 8000: "+ obj.getSavingAccountBalance());
System.out.print("
Withdraw 6000 from Checking Account Balance: ");
obj.withdrawChecking(6000);
System.out.println("Checking Account Balance after withdraw 6000: "+ obj.getCheckingAccountBalance());
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions