0% found this document useful (0 votes)
59 views2 pages

Run

The document defines a SavingsAccount class with methods to set and get interest rates, deposit, withdraw, add interest, and show balances. It also defines a RunSavingsAccount class to test the SavingsAccount functionality by prompting a user for deposits and withdrawals and displaying updated balances.

Uploaded by

Jamil Bataga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views2 pages

Run

The document defines a SavingsAccount class with methods to set and get interest rates, deposit, withdraw, add interest, and show balances. It also defines a RunSavingsAccount class to test the SavingsAccount functionality by prompting a user for deposits and withdrawals and displaying updated balances.

Uploaded by

Jamil Bataga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package runsavingsaccount;

public class SavingsAccount {

private double Balance;


public static double interestRate = 0;

SavingsAccount(){
Balance = 0;
}

public static void setInterestRate(double newRate){ // signature,


setInterestRate(double) // parameter, setInterestRate(double newRate)
interestRate = newRate;
}

public static double getInterestRate(){


return interestRate;
}
double getBalance(){
return Balance;

}
public void deposit (double amount){
Balance = Balance + amount;

}
public double withdraw(double amount){
if (Balance >= amount){
Balance = Balance - amount;
}else {
System.out.println("Amount is Equal to 0");
}
return amount;
}
public void addInterest(){

double interest = Balance * interestRate;


Balance = Balance + interest;
}
public static void showBalance(SavingsAccount account){
System.out.printf("%.2f\n", account.getBalance());

}
}

package runsavingsaccount;

import java.util.Scanner;

public class RunSavingsAccount {


public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


SavingsAccount savings = new SavingsAccount();

System.out.print("Enter Interest rate: ");

savings.setInterestRate(scan.nextDouble());

System.out.print("Enter Deposit Amount: ");


savings.deposit(scan.nextDouble());

System.out.print("Your Balance is:"+ savings.getBalance());

while(true){

System.out.print("\nPress D for another deposit or W to Withdraw: ");


char Choice = scan.next().toUpperCase().charAt(0);

switch(Choice){

case'D':

System.out.print("Enter deposit amount: ");


savings.deposit(scan.nextDouble());

if (savings.getBalance()> 1000){
savings.addInterest();
}
break;

case 'W':
System.out.print("Enter withdraw amount: ");
savings.withdraw(scan.nextDouble());//Arguments
withdraw(scan.nextDouble())
break;
default:
System.out.println("Invalid Input");
}
System.out.print("your new balance is: ");
savings.showBalance(savings);

You might also like