Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1.  
  2. public class Investment {
  3.  
  4. private double balance;
  5.  
  6. private double interestRate;
  7.  
  8. public Investment() {
  9. balance = 1000.0;
  10. interestRate = 3.5;
  11. }
  12.  
  13. public Investment(double balance, double rate) {
  14. this.setBalance(balance);
  15. this.setInterestRate(rate);
  16. }
  17.  
  18. public double getBalance() {
  19. return balance;
  20. }
  21.  
  22. public double getInterestRate() {
  23. return interestRate;
  24. }
  25.  
  26. public void setBalance(double balance) {
  27. if (balance < 0) {
  28. throw new IllegalArgumentException("Balance should be greater than 0");
  29. } else {
  30. this.balance = balance;
  31. }
  32. }
  33.  
  34. public void setInterestRate(double rate) {
  35. if (interestRate <= 0 || interestRate > 12) {
  36. throw new IllegalArgumentException("Interest must be greater than 0, and no more than 12.");
  37. } else {
  38. this.interestRate = rate;
  39. }
  40. }
  41.  
  42. public double totalInterest() {
  43. return (interestRate/100.0)*(balance);
  44. }
  45.  
  46. public double totalInvestment(int periods) {
  47. return Math.pow(balance * (1 + interestRate/100), periods);
  48. }
  49.  
  50. public String toString() {
  51. return String.format("Investment Information%nCurrent Balance: %4.2f%nInterest Rate: %4.1f %", balance, interestRate);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement