/* File: EmployeeTest.java
* Author: Maxime Duvelcint
* Date: 2017-03-25
* Purpose: CMIS 242 - Project 1
* EmployeeTest, will read employee from a file and
* generate a report of the results
*/
package employee;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class EmployeeTest {
private ArrayList<Employee> employeeArrayList2014 = new ArrayList<>();
private ArrayList<Employee> employeeArrayList2015 = new ArrayList<>();
public void builder() {
try {
Scanner input
= new Scanner
(new File("data.txt")); // Completes loop when "input.nextLine();" below detects no data
while (input.hasNext()) {
int year = input.nextInt();
// Tests for 2014 in the first element
if (year == 2014) {
double monthlySalary = input.nextDouble();
// Tests for Salesman in the second element
if (type.equals("Salesman")) {
double salesOrStock = input.nextDouble();
employeeArrayList2014.add(new Salesman(year, name, monthlySalary, salesOrStock));
// Tests for Executive in the second element
} else if (type.equals("Executive")) {
double salesOrStock = input.nextDouble();
employeeArrayList2014.add(new Executive(year, name, monthlySalary, salesOrStock));
// If neither Salesman or Executive test happened, it is an Employee
} else {
employeeArrayList2014.add(new Employee(year, name, monthlySalary));
}
// Tests for 2015 employees
} else if (year == 2015) {
double monthlySalary = input.nextDouble();
// Tests for Salesman in the second element
if (type.equals("Salesman")) {
double salesOrStock = input.nextDouble();
employeeArrayList2015.add(new Salesman(year, name, monthlySalary, salesOrStock));
// Tests for Executive in the second element
} else if (type.equals("Executive")) {
double salesOrStock = input.nextDouble();
employeeArrayList2015.add(new Executive(year, name, monthlySalary, salesOrStock));
// If neither Salesman or Executive type found, it is an Employee
} else {
employeeArrayList2015.add(new Employee(year, name, monthlySalary));
}
}
input.nextLine();
}
System.
out.
println("File not found."); System.
out.
println("A null pointer was found, please check the input file."); System.
out.
println("The last line of the document is not blank."); }
}
public void displayAllEmployees() {
// variables to sum and average the salary
double averageSalary2014 = 0;
double averageSalary2015 = 0;
System.
out.
println("\n2014 Data:"); for (Employee em : employeeArrayList2014) {
System.
out.
println(em.
toString()); averageSalary2014 += em.annualSalary();
}
// averageSalary2014's final value should be itself divided by the number of employees
averageSalary2014 = averageSalary2014 / employeeArrayList2014.size();
System.
out.
println("================================================="); System.
out.
println("The average salary in 2014 was: " + df.
format(averageSalary2014
));
System.
out.
println("\n2015 Data:"); for (Employee em : employeeArrayList2015) {
System.
out.
println(em.
toString()); averageSalary2015 += em.annualSalary();
}
// averageSalary2015's final value should be itself divided by the number of employees
averageSalary2015 = averageSalary2015 / employeeArrayList2015.size();
System.
out.
println("================================================="); System.
out.
println("The average salary in 2015 was: " + df.
format(averageSalary2015
)); }
public static void main
(String[] args
) { EmployeeTest list = new EmployeeTest();
list.builder();
list.displayAllEmployees();
}
}