Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Investment {
- private double balance;
- private double interestRate;
- public Investment() {
- balance = 1000.0;
- interestRate = 3.5;
- }
- public Investment(double balance, double rate) {
- this.setBalance(balance);
- this.setInterestRate(rate);
- }
- public double getBalance() {
- return balance;
- }
- public double getInterestRate() {
- return interestRate;
- }
- public void setBalance(double balance) {
- if (balance < 0) {
- throw new IllegalArgumentException("Balance should be greater than 0");
- } else {
- this.balance = balance;
- }
- }
- public void setInterestRate(double rate) {
- if (interestRate <= 0 || interestRate > 12) {
- throw new IllegalArgumentException("Interest must be greater than 0, and no more than 12.");
- } else {
- this.interestRate = rate;
- }
- }
- public double totalInterest() {
- return (interestRate/100.0)*(balance);
- }
- public double totalInvestment(int periods) {
- return Math.pow(balance * (1 + interestRate/100), periods);
- }
- public String toString() {
- return String.format("Investment Information%nCurrent Balance: %4.2f%nInterest Rate: %4.1f %", balance, interestRate);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement