public class SavingsAccount{
private double interestRate;
private double balance;
private double lastInterest;
public SavingsAccount(double accountBalance, double annualInterestRate) {
balance = accountBalance;
interestRate = annualInterestRate;
lastInterest = 0.0;
}
public void withdraw(double withdrawAmount) {
balance -= withdrawAmount;
}
public void deposit(double depositAmount) {
balance += depositAmount;
}
public void addInterest() {
double monthlyInterestRate = interestRate / 12;
double lastInterest = monthlyInterestRate * balance;
balance = balance + lastInterest;
}
public double getBalance() {
return balance;
}
public double getInterestRate() {
return interestRate;
}
public double getLastInterest() {
return lastInterest;
}
}
import java.util.Scanner;
import java.text.DecimalFormat;
public class SavingsAccountDemo{
public static void main(String args[]) {
double monthlyDeposit;
double monthlyWithdrawl;
double interestEarned = 0.0;
double totalDeposits = 0;
double totalWithdrawn = 0;
System.out.print("StudentLoansss 12/2/2018");
System.out.print("\n");
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter the savings account's starting balance: ");
double startingBalance = keyboard.nextDouble();
System.out.print("Enter the savings account's annual interest rate: ");
double annualInterestRate = keyboard.nextDouble();
SavingsAccount savingsAccount = new SavingsAccount( startingBalance, annualInterestRate);
System.out.print("How many months have passed since the account was opened? ");
double months = keyboard.nextInt();
for (int i = 1; i <= months; i++) {
System.out.print("Enter the amount deposited during month " + i + ": ");
monthlyDeposit = keyboard.nextDouble();
totalDeposits += monthlyDeposit;
savingsAccount.deposit(monthlyDeposit);
System.out.print("Enter the amount withdrawn during month " + i + ": ");
monthlyWithdrawl = keyboard.nextDouble();
totalWithdrawn += monthlyWithdrawl;
savingsAccount.withdraw(monthlyWithdrawl);
savingsAccount.addInterest();
interestEarned += savingsAccount.getLastInterest();
}
DecimalFormat dollar = new DecimalFormat("#,##0.00");
System.out.println("Total deposited: $" + dollar.format(totalDeposits));
System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
System.out.println("Interest earned: $" + dollar.format(interestEarned));
System.out.println("Ending balance: $" + dollar.format(savingsAccount.getBalance()));
}
}
This post has been edited by modi123_1: 02 December 2018 - 10:40 PM
Reason for edit:: Name removed.

New Topic/Question
Reply


MultiQuote




|