import java.text.NumberFormat ; // format numbers involved
class FutureValue {
public static void main
(String[] args
) { // Students substitute your name for mine in the line below.
System.
out.
println("This code is written by Danny Chen");
// We need a container to hold the employees we are about to create.
int size = 10 ;
AllEmployees allEmployees = new AllEmployees (size) ;
/* Create some employees whose future value you wish to calculate.
* Call the Employee class’s parameterized constructor, creating an
* instance of/object of the Employee class.
* Each employee will need an index, which will be the next one after the last
* one constructed. We use the index in two ways, one as an identifier of the
* employee and one as an index into an array.
*/
int index = 0 ; // initialize data to 0, as per cs.umd.edu
// The following holds the current id (index) of the next employee.
EmployeeIndex employeeIndex = new EmployeeIndex() ;
Employee employee = null; // just creating the variable; no storage
// Now use the current Index; initially set to 0 by the constructor above.
// Creating employee at the current index.
index = employeeIndex.getIndex(); // get the current index (id)
employee = new Employee (index, "Prof. Taylor", 250, 200.00, 350.00,
3.25, 20 ) ;
employeeIndex.incrementIndex () ; // set the index for the next employee
// Place the employee in the array of all employees.
allEmployees.addEmployee (employee) ;
// Creating employee at the current index.
index = employeeIndex.getIndex(); // get the current index (id)
employee = new Employee (index, "Prof. Saito", 150, 500.00, 500.00,
3.25, 40 ) ;
employeeIndex.incrementIndex () ; // set the index for the next employee
index = employeeIndex.getIndex();
employee = new Employee (index, "Danny Chen", 200, 600.00, 800.00, 3.25, 60);
// Place the employee in the array of all employees.
allEmployees.addEmployee (employee) ;
// Students you create yourself as the next employee and place yourself in
// the array copying the code above. Of course, you need to set your own
// individual parameters for your constructor.
// Calculate and place the futureValue for the employees in the container.
allEmployees.calculateFutureValueAllEmployees() ;
// Display the information for all the employees in the container.
allEmployees.printAllEmployees();
// Now print the information for an employee just given their name
allEmployees.printEmployee ("Prof. Saito") ;
} // end main()
} // end class FutureValue
/*
* In this program, indices replace ids, as ids don’t normally start at 0, while indices
* always do, and we mean to use the variable index as an id to tie employees to an
*array.
*/
class EmployeeIndex {
private int currentIndex = 0; // always initialize data members
EmployeeIndex () { } // Our no argument constructor
public void setIndex (int currentIndex) {
this.currentIndex = currentIndex;
}
public int getIndex () {
return currentIndex;
}
public void incrementIndex() {
++ currentIndex ;
}
} // end class EmployeeIndex
class Employee {
private int index = 0; // will be used to place an employee into an array
private String name
= null ; // always initialize data members private int graduationPresent = 0; // always initialize data members
private double holidayBonus = 0.0; // always initialize data members
private double monthlyIRA_Deduction = 0.0; // always initialize data members
private double yearlyInterestRate = 0.0; // always initialize data members
private int numberOfYears = 0; // always initialize data member
private double futureValue = 0.0; // always initialize data members
/* Below are our constructors (private is not a restriction to
* access inside the class).
* N.B. this below refers to the current instance or object of the class
* Employee. The keyword this is a nice frill, since it doesn’t
* require renaming a parameter/argument of the constructor.
*/
// An Employee parameterized constructor
Employee
(int index,
String name,
int graduationPresent,
double holidayBonus, double monthlyIRA_Deduction,
double yearlyInterestRate, int numberOfYears ) {
this.index = index ;
this.name = name ;
this.graduationPresent = graduationPresent ;
this.holidayBonus = holidayBonus;
this.monthlyIRA_Deduction = monthlyIRA_Deduction;
this.yearlyInterestRate = yearlyInterestRate;
this.numberOfYears = numberOfYears;
futureValue = 0.0 ; // can only be changed later by a set() method
} // end Employee()
public void printEmployee () {
System.
out.
println ("Index = " + index
) ; System.
out.
println ("Name = " + name
) ; System.
out.
println ("Graduation Present = " + NumberFormat.
getCurrencyInstance().
format(graduationPresent
)); System.
out.
println ("Holiday Bonus = " + System.
out.
println ("Monthly IRA deduction = " + NumberFormat.
getCurrencyInstance().
format(monthlyIRA_Deduction
)); System.
out.
println ("Yearly Interest Rate = " + yearlyInterestRate
+ "%"); System.
out.
println ("Number Of Years = " + numberOfYears
) ; System.
out.
println ("Future Value = " + } // end printEmployee()
/* The set() methods place the necessary information into the
* Employee class. These methods are required when placing this
* information outside the class definition as the data members of the
* Employee classes are private.
*/
public void setIndex (int index) {this.index = index;}
public void setName
(String name
) {this.
name = name
;} public void setGraduationPresent (int graduationPresent) {
this.graduationPresent = graduationPresent;}
public void setHolidayBonus (double holidayBonus) {
this.holidayBonus = holidayBonus; }
public void setYearlyInterestRate (double yearlyInterestRate) {
/* Note the interest rate will be entered as a percent e.g., 3.25, but
* without a % sign, while the monthly interest rate (created later)
* will be the decimal value, e.g., 3.25%/12 = 0.0325/12= 0.0002257,
* which is the interest rate used for monthly calculations.
*/
this.yearlyInterestRate = yearlyInterestRate ;}
public void setMonthlyIRA_Deduction (double monthlyIRA_Deduction) {
this.monthlyIRA_Deduction = monthlyIRA_Deduction;}
public void setNumberOfYears (int numberOfYears ) {
this.numberOfYears = numberOfYears; }
public void setFutureValue (double futureValue ) {this.futureValue =
futureValue;}
/* The get() methods retrieve the necessary information from the EmployeeData
* class. These methods are required when retrieving this information outside the
* class definition. since the data members of the EmployeeData class are private.
*/
public int getIndex () { return index ;}
public String getName
() {return name
;} public int getGraduationPresent () {return graduationPresent ;}
public double getHolidayBonus () {return holidayBonus ;}
public double getYearlyInterestRate () {return yearlyInterestRate ;}
public double getMonthlyIRA_Deduction () {return monthlyIRA_Deduction ;}
public int getNumberOfYears () {return numberOfYears ;}
public double getFutureValue () {return futureValue ;}
} // end class Employee
class AllEmployees {
int size = 10 ; // maximum number of Employees
Employee[] allEmployeesArray = null ;
AllEmployees (int size) { // one-argument constructor.
this.size = size ;
allEmployeesArray = new Employee[size];
} // end one-argument constructor
public void addEmployee ( Employee employee) {
int index = employee.getIndex() ;
allEmployeesArray [index] = employee;
} // end addEmployee()
public void printEmployee
( String name
) { int invalidIndex = -1; // we use -1 to indicate an invalid index
int index = findEmployeeIndex (name);
if ( index == invalidIndex) {
// Next line is debug code; ultimately to be removed.
System.
out.
println (name
+ "is not in the array of Employees.") ; return ;
}
Employee employee = getEmployee (index) ;
employee.printEmployee () ;
return ;
} // end printEmployee()
public int findEmployeeIndex
(String searchName
) { Employee employee = null;
int index = -1 ; // initialize to an invalid index
for ( int i = 0; i < allEmployeesArray.length; i++ ) {
employee = allEmployeesArray[ i ] ;
if (employee == null) {
break ; // from for loop
} // end if
name = employee.getName() ;
if (name.equals (searchName)) {
index = i ;
break ; // from for loop
} // end if
}// end for
return index ;
} // end findEmployeeIndex()
public Employee getEmployee (int index) {
Employee employee = null ;
// Safety code to avoid exceptions being thrown
if ( index > -1 && index < allEmployeesArray.length ) {
employee = allEmployeesArray[index] ;
}
return employee ;
} // end getEmployee()
public void calculateFutureValueAllEmployees () {
FutureValueCalculations futureValueCalculations =
new FutureValueCalculations(); // Java supplied constructor
Employee employee = null;
double futureValue = 0.0;
for ( int i = 0; i < allEmployeesArray.length; i++ ) {
employee = allEmployeesArray[i] ;
if (employee == null) {
break ; // from for loop
} // end if
// Don’t calculate future value if already calculated.
futureValue = employee.getFutureValue() ;
if ( futureValue == 0 ) {
futureValue =
futureValueCalculations.calculateFutureValue (employee);
employee.setFutureValue (futureValue);
} // end if
} // end for
} // end calculateFutureValueAllEmployees()
public void printAllEmployees() {
Employee employee = null;
for ( int i = 0; i < allEmployeesArray.length; i++ ) {
employee = allEmployeesArray[i] ;
if (employee == null) {
break ; // from for loop
}
employee.printEmployee () ;
} // for
}
} // end class AllEmployees
class FutureValueCalculations {
public double calculateFutureValue (Employee employee ) {
int graduationPresent = employee.getGraduationPresent() ;
double holidayBonus = employee.getHolidayBonus() ;
double monthlyIRA_Deduction =employee.getMonthlyIRA_Deduction() ;
double yearlyInterestRate = employee.getYearlyInterestRate () ;
int numberOfYears = employee.getNumberOfYears() ;
double futureValue = calculateFutureValue (graduationPresent,
holidayBonus, monthlyIRA_Deduction, yearlyInterestRate,
numberOfYears );
return futureValue ;
} // end calculateFutureValue()
// Polymorphism - 2 methods called calculateFutureValue; this one we sell.
public double calculateFutureValue (int graduationPresent,
double holidayBonus, double monthlyIRA_Deduction,
double yearlyInterestRate, int numberOfYears ) {
double futureValue = 0.0 ; // initialization
/* Transform the arguments/parameters into the variables we need to use.
* First transform percent to its double, and then yearly to monthly.
*/
double monthlyInterestRate = yearlyInterestRate / 100 / 12 ;
int numberOfMonths = 12 * numberOfYears ;
double monthlyInterestAmount = 0; // Initialization
futureValue += (double) graduationPresent; // typecasting
// Now calculate the future value.
int i = 1; // loop iterator
while ( i <= numberOfMonths) { // loop conditional
// Add a holiday bonus every 12th month.
if ( (i % 12) == 0 ) { // Example 12 % 7 = 5
futureValue += holidayBonus ;
}
futureValue += monthlyIRA_Deduction;
monthlyInterestAmount = futureValue * monthlyInterestRate;
futureValue += monthlyInterestAmount;
i++; // increment the loop iterator
} // end while loop
return futureValue;
} // end calculateFutureValue()
} // end class FutureValueCalculations