fork download
  1. import java.text.NumberFormat ; // format numbers involved
  2. class FutureValue {
  3. public static void main (String[] args) {
  4. // Students substitute your name for mine in the line below.
  5. System.out.println("This code is written by Danny Chen");
  6.  
  7. // We need a container to hold the employees we are about to create.
  8. int size = 10 ;
  9. AllEmployees allEmployees = new AllEmployees (size) ;
  10.  
  11. /* Create some employees whose future value you wish to calculate.
  12. * Call the Employee class’s parameterized constructor, creating an
  13. * instance of/object of the Employee class.
  14. * Each employee will need an index, which will be the next one after the last
  15. * one constructed. We use the index in two ways, one as an identifier of the
  16. * employee and one as an index into an array.
  17. */
  18. int index = 0 ; // initialize data to 0, as per cs.umd.edu
  19. // The following holds the current id (index) of the next employee.
  20. EmployeeIndex employeeIndex = new EmployeeIndex() ;
  21. Employee employee = null; // just creating the variable; no storage
  22. // Now use the current Index; initially set to 0 by the constructor above.
  23.  
  24. // Creating employee at the current index.
  25. index = employeeIndex.getIndex(); // get the current index (id)
  26. employee = new Employee (index, "Prof. Taylor", 250, 200.00, 350.00,
  27. 3.25, 20 ) ;
  28. employeeIndex.incrementIndex () ; // set the index for the next employee
  29. // Place the employee in the array of all employees.
  30. allEmployees.addEmployee (employee) ;
  31.  
  32. // Creating employee at the current index.
  33. index = employeeIndex.getIndex(); // get the current index (id)
  34. employee = new Employee (index, "Prof. Saito", 150, 500.00, 500.00,
  35. 3.25, 40 ) ;
  36. employeeIndex.incrementIndex () ; // set the index for the next employee
  37. // Place the employee in the array of all employees.
  38. allEmployees.addEmployee (employee) ;
  39.  
  40. // Students you create yourself as the next employee and place yourself in
  41. // the array copying the code above. Of course, you need to set your own
  42. // individual parameters for your constructor.
  43. employeeIndex.incrementIndex () ;
  44.  
  45. index = employeeIndex.getIndex();
  46. employee = new Employee (index, "Danny Chen", 200, 600.00, 800.00, 3.25, 60);
  47. allEmployees.addEmployee(employee);
  48.  
  49. // Calculate and place the futureValue for the employees in the container.
  50. allEmployees.calculateFutureValueAllEmployees() ;
  51. // Display the information for all the employees in the container.
  52. allEmployees.printAllEmployees();
  53. // Now print the information for an employee just given their name
  54. allEmployees.printEmployee ("Prof. Saito") ;
  55. } // end main()
  56. } // end class FutureValue
  57. /*
  58.  * In this program, indices replace ids, as ids don’t normally start at 0, while indices
  59.  * always do, and we mean to use the variable index as an id to tie employees to an
  60.  *array.
  61.  */
  62.  
  63.  
  64. class EmployeeIndex {
  65. private int currentIndex = 0; // always initialize data members
  66. EmployeeIndex () { } // Our no argument constructor
  67. public void setIndex (int currentIndex) {
  68. this.currentIndex = currentIndex;
  69. }
  70. public int getIndex () {
  71. return currentIndex;
  72. }
  73. public void incrementIndex() {
  74. ++ currentIndex ;
  75. }
  76. } // end class EmployeeIndex
  77.  
  78. class Employee {
  79. private int index = 0; // will be used to place an employee into an array
  80. private String name = null ; // always initialize data members
  81. private int graduationPresent = 0; // always initialize data members
  82. private double holidayBonus = 0.0; // always initialize data members
  83. private double monthlyIRA_Deduction = 0.0; // always initialize data members
  84. private double yearlyInterestRate = 0.0; // always initialize data members
  85. private int numberOfYears = 0; // always initialize data member
  86. private double futureValue = 0.0; // always initialize data members
  87. /* Below are our constructors (private is not a restriction to
  88. * access inside the class).
  89. * N.B. this below refers to the current instance or object of the class
  90. * Employee. The keyword this is a nice frill, since it doesn’t
  91. * require renaming a parameter/argument of the constructor.
  92. */
  93. // An Employee parameterized constructor
  94. Employee (int index, String name, int graduationPresent,
  95. double holidayBonus, double monthlyIRA_Deduction,
  96. double yearlyInterestRate, int numberOfYears ) {
  97. this.index = index ;
  98. this.name = name ;
  99. this.graduationPresent = graduationPresent ;
  100. this.holidayBonus = holidayBonus;
  101. this.monthlyIRA_Deduction = monthlyIRA_Deduction;
  102. this.yearlyInterestRate = yearlyInterestRate;
  103. this.numberOfYears = numberOfYears;
  104. futureValue = 0.0 ; // can only be changed later by a set() method
  105. } // end Employee()
  106.  
  107.  
  108.  
  109. public void printEmployee () {
  110. System.out.println ("Index = " + index ) ;
  111. System.out.println ("Name = " + name ) ;
  112. System.out.println ("Graduation Present = " +
  113. NumberFormat.getCurrencyInstance().format(graduationPresent));
  114. System.out.println ("Holiday Bonus = " +
  115. NumberFormat.getCurrencyInstance().format(holidayBonus));
  116. System.out.println ("Monthly IRA deduction = " +
  117. NumberFormat.getCurrencyInstance().format(monthlyIRA_Deduction));
  118. System.out.println ("Yearly Interest Rate = " + yearlyInterestRate + "%");
  119. System.out.println ("Number Of Years = " + numberOfYears ) ;
  120. System.out.println ("Future Value = " +
  121. NumberFormat.getCurrencyInstance().format(futureValue));
  122. } // end printEmployee()
  123.  
  124. /* The set() methods place the necessary information into the
  125. * Employee class. These methods are required when placing this
  126. * information outside the class definition as the data members of the
  127. * Employee classes are private.
  128. */
  129. public void setIndex (int index) {this.index = index;}
  130. public void setName (String name) {this.name = name;}
  131. public void setGraduationPresent (int graduationPresent) {
  132. this.graduationPresent = graduationPresent;}
  133. public void setHolidayBonus (double holidayBonus) {
  134. this.holidayBonus = holidayBonus; }
  135. public void setYearlyInterestRate (double yearlyInterestRate) {
  136. /* Note the interest rate will be entered as a percent e.g., 3.25, but
  137. * without a % sign, while the monthly interest rate (created later)
  138. * will be the decimal value, e.g., 3.25%/12 = 0.0325/12= 0.0002257,
  139.   * which is the interest rate used for monthly calculations.
  140. */
  141. this.yearlyInterestRate = yearlyInterestRate ;}
  142. public void setMonthlyIRA_Deduction (double monthlyIRA_Deduction) {
  143. this.monthlyIRA_Deduction = monthlyIRA_Deduction;}
  144. public void setNumberOfYears (int numberOfYears ) {
  145. this.numberOfYears = numberOfYears; }
  146. public void setFutureValue (double futureValue ) {this.futureValue =
  147. futureValue;}
  148.  
  149. /* The get() methods retrieve the necessary information from the EmployeeData
  150. * class. These methods are required when retrieving this information outside the
  151. * class definition. since the data members of the EmployeeData class are private.
  152. */
  153.  
  154. public int getIndex () { return index ;}
  155. public String getName () {return name ;}
  156. public int getGraduationPresent () {return graduationPresent ;}
  157. public double getHolidayBonus () {return holidayBonus ;}
  158. public double getYearlyInterestRate () {return yearlyInterestRate ;}
  159. public double getMonthlyIRA_Deduction () {return monthlyIRA_Deduction ;}
  160. public int getNumberOfYears () {return numberOfYears ;}
  161. public double getFutureValue () {return futureValue ;}
  162. } // end class Employee
  163.  
  164. class AllEmployees {
  165. int size = 10 ; // maximum number of Employees
  166. Employee[] allEmployeesArray = null ;
  167. AllEmployees (int size) { // one-argument constructor.
  168. this.size = size ;
  169. allEmployeesArray = new Employee[size];
  170. } // end one-argument constructor
  171.  
  172. public void addEmployee ( Employee employee) {
  173. int index = employee.getIndex() ;
  174. allEmployeesArray [index] = employee;
  175. } // end addEmployee()
  176.  
  177. public void printEmployee ( String name) {
  178. int invalidIndex = -1; // we use -1 to indicate an invalid index
  179. int index = findEmployeeIndex (name);
  180. if ( index == invalidIndex) {
  181. // Next line is debug code; ultimately to be removed.
  182. System.out.println (name + "is not in the array of Employees.") ;
  183. return ;
  184. }
  185. Employee employee = getEmployee (index) ;
  186. employee.printEmployee () ;
  187. return ;
  188. } // end printEmployee()
  189.  
  190. public int findEmployeeIndex (String searchName) {
  191. Employee employee = null;
  192. String name = null ;
  193. int index = -1 ; // initialize to an invalid index
  194. for ( int i = 0; i < allEmployeesArray.length; i++ ) {
  195. employee = allEmployeesArray[ i ] ;
  196. if (employee == null) {
  197. break ; // from for loop
  198. } // end if
  199. name = employee.getName() ;
  200. if (name.equals (searchName)) {
  201. index = i ;
  202. break ; // from for loop
  203. } // end if
  204. }// end for
  205. return index ;
  206. } // end findEmployeeIndex()
  207.  
  208. public Employee getEmployee (int index) {
  209. Employee employee = null ;
  210. // Safety code to avoid exceptions being thrown
  211. if ( index > -1 && index < allEmployeesArray.length ) {
  212. employee = allEmployeesArray[index] ;
  213. }
  214. return employee ;
  215. } // end getEmployee()
  216.  
  217. public void calculateFutureValueAllEmployees () {
  218. FutureValueCalculations futureValueCalculations =
  219. new FutureValueCalculations(); // Java supplied constructor
  220. Employee employee = null;
  221. double futureValue = 0.0;
  222. for ( int i = 0; i < allEmployeesArray.length; i++ ) {
  223. employee = allEmployeesArray[i] ;
  224. if (employee == null) {
  225. break ; // from for loop
  226. } // end if
  227. // Don’t calculate future value if already calculated.
  228. futureValue = employee.getFutureValue() ;
  229. if ( futureValue == 0 ) {
  230. futureValue =
  231. futureValueCalculations.calculateFutureValue (employee);
  232. employee.setFutureValue (futureValue);
  233. } // end if
  234. } // end for
  235. } // end calculateFutureValueAllEmployees()
  236.  
  237. public void printAllEmployees() {
  238. Employee employee = null;
  239. for ( int i = 0; i < allEmployeesArray.length; i++ ) {
  240. employee = allEmployeesArray[i] ;
  241. if (employee == null) {
  242. break ; // from for loop
  243. }
  244. employee.printEmployee () ;
  245. } // for
  246. }
  247. } // end class AllEmployees
  248.  
  249. class FutureValueCalculations {
  250. public double calculateFutureValue (Employee employee ) {
  251. int graduationPresent = employee.getGraduationPresent() ;
  252. double holidayBonus = employee.getHolidayBonus() ;
  253. double monthlyIRA_Deduction =employee.getMonthlyIRA_Deduction() ;
  254. double yearlyInterestRate = employee.getYearlyInterestRate () ;
  255. int numberOfYears = employee.getNumberOfYears() ;
  256. double futureValue = calculateFutureValue (graduationPresent,
  257. holidayBonus, monthlyIRA_Deduction, yearlyInterestRate,
  258. numberOfYears );
  259. return futureValue ;
  260. } // end calculateFutureValue()
  261.  
  262. // Polymorphism - 2 methods called calculateFutureValue; this one we sell.
  263. public double calculateFutureValue (int graduationPresent,
  264. double holidayBonus, double monthlyIRA_Deduction,
  265. double yearlyInterestRate, int numberOfYears ) {
  266. double futureValue = 0.0 ; // initialization
  267. /* Transform the arguments/parameters into the variables we need to use.
  268. * First transform percent to its double, and then yearly to monthly.
  269. */
  270. double monthlyInterestRate = yearlyInterestRate / 100 / 12 ;
  271. int numberOfMonths = 12 * numberOfYears ;
  272. double monthlyInterestAmount = 0; // Initialization
  273. futureValue += (double) graduationPresent; // typecasting
  274. // Now calculate the future value.
  275. int i = 1; // loop iterator
  276. while ( i <= numberOfMonths) { // loop conditional
  277. // Add a holiday bonus every 12th month.
  278. if ( (i % 12) == 0 ) { // Example 12 % 7 = 5
  279. futureValue += holidayBonus ;
  280. }
  281. futureValue += monthlyIRA_Deduction;
  282. monthlyInterestAmount = futureValue * monthlyInterestRate;
  283. futureValue += monthlyInterestAmount;
  284. i++; // increment the loop iterator
  285. } // end while loop
  286. return futureValue;
  287. } // end calculateFutureValue()
  288. } // end class FutureValueCalculations
  289.  
Success #stdin #stdout 0.21s 57808KB
stdin
Standard input is empty
stdout
This code is written by Danny Chen
Index = 0
Name = Prof. Taylor
Graduation Present = $250.00
Holiday Bonus = $200.00
Monthly IRA deduction = $350.00
Yearly Interest Rate = 3.25%
Number Of Years = 20
Future Value = $124,452.43
Index = 1
Name = Prof. Saito
Graduation Present = $150.00
Holiday Bonus = $500.00
Monthly IRA deduction = $500.00
Yearly Interest Rate = 3.25%
Number Of Years = 40
Future Value = $533,954.40
Index = 1
Name = Prof. Saito
Graduation Present = $150.00
Holiday Bonus = $500.00
Monthly IRA deduction = $500.00
Yearly Interest Rate = 3.25%
Number Of Years = 40
Future Value = $533,954.40