Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
java_composition_over_inheritance/InterestAccount.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
43 lines (35 sloc)
1017 Bytes
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 InterestAccount { | |
private double balance; | |
private double interest; | |
// Constructors | |
public InterestAccount (double initialBalance, double initialInterest) { | |
this.balance = initialBalance; | |
this.interest = initialInterest; | |
} | |
// Getters and Setters | |
public double getBalance () { return this.balance; } | |
public void setBalance (double bal) { this.balance = bal; } | |
public double getInterest () { return this.interest; } | |
public void setInterest (double interest) { this.interest = interest; } | |
// Other member functions | |
/** | |
* Accrue account balance by one year's interest | |
* @return Account balance after the accrual | |
*/ | |
public double accrue () { | |
this.balance = balance + (balance * interest); | |
return this.balance; | |
} | |
/** | |
* Accrue account balance by specified years of interest | |
* @param y Number of years to accrue | |
* @return Account balance after accrual | |
*/ | |
public double accrueYears (int y) { | |
while (y != 0) { | |
accrue(); | |
y--; | |
} | |
return this.balance; | |
} | |
} |