fork download
  1.  
  2.  
  3.  
  4. import java.util.*;
  5.  
  6. class SavingsAccount
  7. {
  8. private double balance;
  9. private double interestRate;
  10. private double lastInterest;
  11.  
  12. public SavingsAccount(double balance, double interestRate)
  13. {
  14. this.balance = balance;
  15. this.interestRate = interestRate;
  16. }
  17. public void withdraw(double amount)
  18. {
  19. balance = balance - amount;
  20. }
  21. public void deposit(double amount)
  22. {
  23. balance = balance + amount;
  24. }
  25. public void addInterest()
  26. {
  27. balance = balance+ balance*interestRate;
  28. }
  29. public double getBalance()
  30. {
  31. return balance;
  32. }
  33. public double getInterestRate()
  34. {
  35. return interestRate;
  36. }
  37. public double getLastInterest()
  38. {
  39. return (balance*interestRate *1.0/12)/100;
  40. }
  41.  
  42.  
  43. }
  44. class DepositsAndWithdrawals
  45. {
  46. public static void main (String[] args)
  47. {
  48. Scanner input = new Scanner(System.in);
  49.  
  50. System.out.println("Enter the savings account annual interest rate : ");
  51. double ir = input.nextDouble();
  52.  
  53. System.out.println("Starting balance :");
  54. double balance = input.nextDouble();
  55.  
  56.  
  57. SavingsAccount sa = new SavingsAccount(balance,ir);
  58.  
  59. for(int i=1;i<=4;i++)
  60. {
  61. double dep = input.nextDouble();
  62. sa.deposit(dep);
  63. System.out.printf("\nDeposit = $%.2f , balance = %.2f",dep,sa.getBalance());
  64. }
  65.  
  66. for(int i=1;i<=5;i++)
  67. {
  68. double wdr = input.nextDouble();
  69. sa.withdraw(wdr);
  70. System.out.printf("\nWithdraw = $%.2f , balance = %.2f",wdr,sa.getBalance());
  71. }
  72.  
  73. System.out.printf("\nInterest earned :$%.2f",sa.getLastInterest());
  74.  
  75. System.out.printf("\nEnding balance :$%.2f",sa.getBalance()+sa.getLastInterest());
  76. }
  77. }
Success #stdin #stdout 0.12s 37136KB
stdin
2.5
500.00
100.00
124.00
78.92
37.55
29.88
110.00
27.52
50.00
12.90
stdout
Enter the savings account annual interest rate : 
Starting balance :

Deposit = $100.00 , balance = 600.00
Deposit = $124.00 , balance = 724.00
Deposit = $78.92 , balance = 802.92
Deposit = $37.55 , balance = 840.47
Withdraw = $29.88 , balance = 810.59
Withdraw = $110.00 , balance = 700.59
Withdraw = $27.52 , balance = 673.07
Withdraw = $50.00 , balance = 623.07
Withdraw = $12.90 , balance = 610.17
Interest earned :$1.27
Ending balance :$611.44