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);