Created
October 9, 2012 00:48
-
-
Save zachlatta/3855887 to your computer and use it in GitHub Desktop.
SavingsAcccount Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SavingsAccount { | |
private double annualInterestRate, balance, accumulativeInterest, accumulativeDeposits = 0, accumulativeWithdrawals = 0; | |
// Constructor | |
public SavingsAccount(double b) | |
{ | |
balance = b; | |
} | |
// Mutators | |
public void withdraw(double w) | |
{ | |
balance -= w; | |
accumulativeWithdrawals += w; | |
} | |
public void deposit(double d) | |
{ | |
balance += d; | |
accumulativeDeposits += d; | |
} | |
public void addMonthlyInterest() | |
{ | |
accumulativeInterest += balance * (annualInterestRate/12); | |
balance += balance * (annualInterestRate/12); | |
} | |
// Setters | |
public void setBalance(double b) | |
{ | |
balance = b; | |
} | |
public void setAnnualInterestRate(double air) | |
{ | |
annualInterestRate = air; | |
} | |
// Getters | |
public double getBalance() | |
{ | |
return balance; | |
} | |
public double getAnnualInterestRate() | |
{ | |
return annualInterestRate; | |
} | |
public double getMonthlyInterest() | |
{ | |
return balance * (annualInterestRate/12); | |
} | |
public double getAccumulativeInterest() | |
{ | |
return accumulativeInterest; | |
} | |
public double getAccumulativeDeposits() | |
{ | |
return accumulativeDeposits; | |
} | |
public double getAccumulativeWithdrawals() | |
{ | |
return accumulativeWithdrawals; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SavingsAccountDemo { | |
public static void main(String[] args) | |
{ | |
int monthsPassed = 0; | |
Scanner userInput = new Scanner(System.in); | |
// Get starting balance & create demoAccount object | |
System.out.print("Please enter the starting balance: $"); | |
SavingsAccount demoAccount = new SavingsAccount(userInput.nextDouble()); | |
// Gets annual interest rate | |
System.out.print("Enter the annual interest rate (decimal): "); | |
demoAccount.setAnnualInterestRate(userInput.nextDouble()); | |
// Gets numbers of months that have passed since the establishment of the account | |
System.out.print("Enter the number of months that have passed since the account\'s establishment: "); | |
monthsPassed = userInput.nextInt(); | |
// Loops through all of the | |
for (int count = 1; count <= monthsPassed; count++) | |
{ | |
// Display month number | |
System.out.println("\n-----MONTH " + count + "-----"); | |
// Gets amount deposited into account for the month | |
System.out.print("Enter the amount deposited into the account: $"); | |
demoAccount.deposit(userInput.nextDouble()); | |
// Gets amount withdrawn from the account for the month | |
System.out.printf("Enter the amount withdrawn from the account: $"); | |
demoAccount.withdraw(userInput.nextDouble()); | |
// Displays and adds monthly interest | |
System.out.printf("This month\'s interest is: $%.2f\n", demoAccount.getMonthlyInterest()); | |
demoAccount.addMonthlyInterest(); | |
} | |
System.out.printf("\nEnding balance: $%.2f", demoAccount.getBalance()); | |
System.out.printf("\nTotal deposits: $%.2f", demoAccount.getAccumulativeDeposits()); | |
System.out.printf("\nTotal withdrawals: $%.2f", demoAccount.getAccumulativeWithdrawals()); | |
System.out.printf("\nTotal interest: $%.2f", demoAccount.getAccumulativeInterest()); | |
userInput.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment