Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. Driver.java
  2.  
  3. /***********************************************
  4. *
  5. * Name: Omri Gideoni // OmriG.N7@gmail.com
  6. * Class: CIS 35a
  7. * Assignment Number: Lab4a
  8. * Due Date: 02/10/2017
  9. * Due Submitted: 02/16/2017
  10. *
  11. ***********************************************/
  12. package driver;
  13. import account.*;
  14.  
  15. public class Driver
  16. {
  17. public static void main(String[] args)
  18. {
  19. System.out.printf("Part 1:\n");
  20. partOne();
  21.  
  22. System.out.printf("Part 2:\n");
  23. partTwo();
  24. }
  25.  
  26. public static void partTwo()
  27. {
  28. //Create objects
  29. SavingsAccount saver1 = new SpecialSavings();
  30. SavingsAccount saver2 = new SpecialSavings();
  31.  
  32. //Set savings balance
  33. saver1.setSavingsBalance(2000f);
  34. saver2.setSavingsBalance(3000f);
  35.  
  36. saver1.deposit(9000f);
  37. saver2.withdraw(1000f);
  38.  
  39. SavingsAccount.modifyInterestRate(0.04f);
  40.  
  41. //Calculate balance
  42. saver1.setSavingsBalance(saver1.calculateMonthlyInterest()+saver1.getSavingsBalance());
  43. saver2.setSavingsBalance(saver2.calculateMonthlyInterest()+saver2.getSavingsBalance());
  44.  
  45. //Print output to user
  46. System.out.printf("annaualInterestRate: %.2f\n", saver1.getAnnualInterestRate());
  47. System.out.printf("saver1 balance: %.2f\n", saver1.getSavingsBalance());
  48. System.out.printf("saver2 balance: %.2f\n", saver2.getSavingsBalance());
  49. System.out.printf("\n");
  50.  
  51. }
  52.  
  53.  
  54. public static void partOne()
  55. {
  56.  
  57. //Create objects
  58. SavingsAccount saver1 = new SavingsAccount();
  59. SavingsAccount saver2 = new SavingsAccount();
  60.  
  61. //Set savings balance
  62. saver1.setSavingsBalance(2000f);
  63. saver2.setSavingsBalance(3000f);
  64.  
  65.  
  66. float interestRates[] = new float[2];
  67. interestRates[0] = 0.04f;
  68. interestRates[1] = 0.05f;
  69.  
  70. for(int i = 0; i < 2; i++)
  71. {
  72. //Set interest rate
  73. SavingsAccount.modifyInterestRate(interestRates[i]);
  74.  
  75. //Calculate balance
  76. saver1.setSavingsBalance(saver1.calculateMonthlyInterest()+saver1.getSavingsBalance());
  77. saver2.setSavingsBalance(saver2.calculateMonthlyInterest()+saver2.getSavingsBalance());
  78.  
  79. //Print output to user
  80. System.out.printf("annaualInterestRate: %.2f\n", saver1.getAnnualInterestRate());
  81. System.out.printf("saver1 balance: %.2f\n", saver1.getSavingsBalance());
  82. System.out.printf("saver2 balance: %.2f\n", saver2.getSavingsBalance());
  83. System.out.printf("\n");
  84. }
  85. }
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. SavingsAccount.java
  101.  
  102. /***********************************************
  103. *
  104. * Name: Omri Gideoni // OmriG.N7@gmail.com
  105. * Class: CIS 35a
  106. * Assignment Number: Lab4a
  107. * Due Date: 02/10/2017
  108. * Due Submitted: 02/16/2017
  109. *
  110. ***********************************************/
  111. package account;
  112.  
  113. public class SavingsAccount
  114. {
  115. //Instance Variables
  116. static float annualInterestRate = 0;
  117. private float savingsBalance = 0;
  118.  
  119.  
  120. //Default constructor
  121. public SavingsAccount(){ };
  122.  
  123.  
  124. //Getters and setters
  125. public static void modifyInterestRate(float newInterestRate)
  126. {
  127. annualInterestRate = newInterestRate;
  128. }
  129.  
  130. public static float getAnnualInterestRate()
  131. {
  132. return annualInterestRate;
  133. }
  134.  
  135. public float getSavingsBalance()
  136. {
  137. return savingsBalance;
  138. }
  139.  
  140. public void setSavingsBalance(float savingsBalance)
  141. {
  142. this.savingsBalance = savingsBalance;
  143. }
  144.  
  145.  
  146. //Other methods
  147. public float calculateMonthlyInterest()
  148. {
  149. return (savingsBalance*annualInterestRate/12);
  150. }
  151.  
  152. public void deposit(float depositCnt)
  153. {
  154. savingsBalance = savingsBalance + depositCnt;
  155. }
  156.  
  157. public void withdraw(float withdrawCnt)
  158. {
  159. savingsBalance = savingsBalance - withdrawCnt;
  160. }
  161.  
  162.  
  163. //For debugging
  164. public void print()
  165. {
  166. System.out.printf("annualInterestRate: %f\n",annualInterestRate);
  167. System.out.printf("savingsBalance: %f\n",savingsBalance);
  168. }
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. SpecialSavings.java
  183.  
  184. /***********************************************
  185. *
  186. * Name: Omri Gideoni // OmriG.N7@gmail.com
  187. * Class: CIS 35a
  188. * Assignment Number: Lab4a
  189. * Due Date: 02/10/2017
  190. * Due Submitted: 02/16/2017
  191. *
  192. ***********************************************/
  193. package account;
  194.  
  195. public class SpecialSavings extends SavingsAccount
  196. {
  197. //Default constructor
  198. public SpecialSavings(){ };
  199.  
  200. public float calculateMonthlyInterest()
  201. {
  202. if(getSavingsBalance() > 10000)
  203. {
  204. return (getSavingsBalance()*0.10f/12);
  205. }
  206. else
  207. {
  208. return super.calculateMonthlyInterest();
  209. }
  210. }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement