You are on page 1of 2

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.println("Enter Interest rate: ");

savings.setInterestRate(scan.nextDouble());

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


savings.deposit(scan.nextDouble());

System.out.println("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());
break;
default:
System.out.println("Invalid Input");
}
System.out.print("your new balance is: ");
savings.showBalance(savings);

package runsavingsaccount;
public class SavingsAccount {

private double Balance;


public static double interestRate = 0;

SavingsAccount(){
Balance = 0;
}

static void setInterestRate(double newRate){


interestRate = newRate;
}

static double getInterestRate(){


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

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

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

double interest = Balance * interestRate;


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

}
}

You might also like