import java.util.Scanner;
public class RunSavingsAccount {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
SavingsAccount savings = new SavingsAccount();
System.out.print("Enter interest rate: ");
double rate = input.nextDouble();
SavingsAccount.setInterestRate(rate);
System.out.print("Enter amount to deposit: ");
double amount = input.nextDouble();
savings.deposit(amount);
SavingsAccount.showBalance(savings);
System.out.print("Press D for another deposit or W for withdrawal: ");
char choice = input.next().charAt(0);
if (choice == 'D' || choice == 'd') {
System.out.print("Enter amount to deposit: ");
amount = input.nextDouble();
savings.deposit(amount);
} else if (choice == 'W' || choice == 'w') {
System.out.print("Enter amount to withdraw: ");
amount = input.nextDouble();
savings.withdraw(amount);
}
SavingsAccount.showBalance(savings);
if (savings.getBalance() > 1000) {
savings.addInterest();
System.out.println("Interest added.");
SavingsAccount.showBalance(savings);
}
input.close();
}
}
class SavingsAccount {
private double balance;
public static double interestRate = 0;
public SavingsAccount() {
balance = 0;
}
public static void setInterestRate(double newRate) {
interestRate = newRate;
}
public static double getInterestRate() {
return interestRate;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance = balance - amount;
} else {
amount = 0;
}
}
public void addInterest() {
double interest = balance * interestRate;
balance = balance + interest;
}
public static void showBalance(SavingsAccount account) {
System.out.printf("Current balance: %.2f%n", account.getBalance());
}
}9 views