Question: We use eclipse ide. For this assignment you will change the implementation to use lists instead of arrays. Specific Instructions: 1)Create a class GrowableList that

We use eclipse ide. For this assignment you will change the implementation to use lists instead of arrays.

Specific Instructions:

1)Create a class GrowableList that implements java.util.List. Use an array of objects for the internal data implementation. Provide a complete implementation for the add(), get(), clear(), isEmpty(), and size() methods. The add method should reallocate the internal array whenever there is no more space available in the array. Initially create the array with a size of four and double it each time it runs out of space. For all other methods throw a new UnsupportedOperationException passing a string with the method name and signature of the method being called..

Define GrowableList in package edu.iup.cosc210.util.

2)Write a program to test your GrowableList class. Make sure the add(), get(), clear(), isEmpty(), and size() methods are working correctly. For add, test the array is doubles in size in the event of insufficient space. In addition, test UnsupportedOperationException are thrown when calling any of the other methods. Do this for at least 3 of the methods where this exception is thrown.

3)Change the Company class to use GrowableList for your list of departments (instead of using an array). Define the type of the departments variable to be a List. You will need to modify the for loop accordingly. Still use a for (int i =0; i< getNoDepartments(); i++) loop. MAKE SURE to delete the noDepartments variable.

4)Change Department to use ArrayList for the list of employees (instead of an array). Define the employees variable as a List of Employees. MAKE SURE to delete the noEmployees variable.

5)Change the loops to get the manager and total vacation days in Department to use a for each loop (e.g. List emps = new ArrayList(); for (Employee emp : emps) { }).

Implementation requirements:

1)ABIDE by good programming practices. Define javadoc, good formatting, etc.

2)Add your name to the list of authors to all the files that you. For new files make sure you indicate that you are the author.

3)FOLLOW THE DIRECTIONS ABOVE.

Company.java:

package edu.xxx.cosc.company.bo;

import java.text.SimpleDateFormat;

/**

* A Company. Maintains a list of departments and provides methods to access

* the company's departments.

*

* @author

*/

public class Company {

private Department[] departments = new Department[10];

private int noDepts = 0;

// define a simple date format to be used throughout the Company application

public static final SimpleDateFormat standardDateFormat = new SimpleDateFormat("MM/dd/yyyy");

/**

* Add a Department to the list of departments for this company.

*

* @param department the department to be added

*/

public void addDepartment(Department department) {

departments[noDepts++] = department;

}

/**

* Find a department with a given department code

*

* @param deptCode the department code used to find a department

*

* @return the department with the given code. Returns null if

* a department by the given department code is not found.

*/

Department.java:

package edu.xxx.cosc.company.bo;

/**

* A Department of a Company. A Department holds a list of employees that are in

* the department.

*

* @author

*

*/

public class Department {

private String deptCode;

private String deptName;

private int mgrEmpId;

private Employee[] employees = new Employee[100];

private int noEmployees;

private Employee manager;

/**

* Constructor for a Department.

*

* @param deptCode The department code

* @param deptName The department name

* @param mgrEmpId The manager's employee id for the department

*/

public Department(String deptCode, String deptName, int mgrEmpId) {

this.deptCode = deptCode;

this.deptName = deptName;

this.mgrEmpId = mgrEmpId;

}

/**

* Add an employee to the department's list of employees

*

* @param employee

* Employee to be added to the department

*/

public void addEmployee(Employee employee) {

employees[noEmployees++] = employee;

}

/**

* Get the code for the department

*

* @return The code for the department

*/

public String getDeptCode() {

return deptCode;

}

/**

* Get the name of the department

*

* @return The name of the department

*/

public String getDeptName() {

return deptName;

}

/**

* Get the manager of the department. The manager is indicated by the mgrEmpId

* passed on the constructor. The manager must be an employee of the department,

* otherwise null is returned.

*

* @return The department manager

*/

public Employee getManager() {

if (manager == null) {

for (int i = 0; i < noEmployees; i++) {

Employee emp = employees[i];

if (emp.getEmployeeId() == mgrEmpId) {

manager = emp;

break;

}

}

}

return manager;

}

/**

* Get the number of employees in the department

*

* @return The number of employees in the department

*/

public int getNoEmployees() {

return noEmployees;

}

/**

* Get an employee given an index position

*

* @param index Index identifying the employee to be returned

*

* @return The employee at the given position

*

* @throws IndexOutOfBoundsException

* If the index is less that 0 or greater than or equal to the

* number of employees.

*/

public Employee getEmployee(int index) {

return employees[index];

}

/**

* Get total number of vacation days of all employees in the department

*

* @return The total number of vacation days for department

*/

public int getTotalVacationDays() {

int totalVacationDays = 0;

for (int i = 0; i < noEmployees; i++) {

Employee emp = employees[i];

totalVacationDays += emp.getVacationDays();

}

return totalVacationDays;

}

}

Employee.java:

package edu.xxx.cosc.company.bo;

import java.util.Date;

/**

* An Employee of a Company.

*

* @author

*/

public class Employee {

private String firstName;

private String lastName;

private String mi;

private int employeeId;

private String employeeIndicator;

private String deptCode;

private double salary;

private boolean exempt;

private Date hireDate;

private short vacationDays;

private byte training;

/**

* Constructor for Employee

*

* @param employeeId The employee's id number

* @param employeeIndicator Indicates if an employee ("E") or a contractor ("C")

* @param deptCode Department code of the employee's department

* @param firstName First name of the employee

* @param lastName Last name of the employee

* @param mi Middle initial of the employee

* @param salary The employee's salary

* @param exempt Indicates if the employee is exempt (i.e., does not get paid overtime)

* @param hireDate The date the employee was hired

* @param vacationDays The number of vacation days

* @param training A byte using bits to indicated the training the employee has received

*/

public Employee(int employeeId, String employeeIndicator, String deptCode, String firstName, String lastName, String mi, double salary, boolean exempt, Date hireDate, short vacationDays, byte training){

this.employeeId = employeeId;

this.employeeIndicator = employeeIndicator;

this.deptCode = deptCode;

this.firstName = firstName;

this.lastName = lastName;

this.mi = mi;

this.salary = salary;

this.exempt = exempt;

this.hireDate = hireDate;

this.vacationDays = vacationDays;

this.training = training;

}

/**

* Get the employee's id

*

* @return The employee's id

*/

public int getEmployeeId() {

return employeeId;

}

/**

* Get the employee's indicator code E for employee, C for contractor

*

* @return The employee's indicator code

*/

public String getEmployeeIndicator() {

return employeeIndicator;

}

/**

* Get the employee's department code.

*

* @return The employee's department code

*/

public String getDeptCode() {

return deptCode;

}

/**

* Get the employee's first name

*

* @return The employee's first name

*/

public String getFirstName() {

return firstName;

}

/**

* Get the employee's last name

*

* @return The employee's last name

*/

public String getLastName() {

return lastName;

}

/**

* Get the employee's middle initial

*

* @return The employee's middle initial

*/

public String getMi() {

return mi;

}

/**

* Get the employee's full name (i.e., first name, middle initial, then last name)

*

* @return The employee's full name

*/

public String getFullName() {

return getFirstName() + " " + getMi() + " " + getLastName();

}

/**

* Get the employee's salary

*

* @return The employee's salary

*/

public double getSalary() {

return salary;

}

/**

* Determine if the employee is exempt (i.e., no overtime)

*

* @return True if the employee is exempt, false otherwise

*/

public boolean isExempt() {

return exempt;

}

/**

* Get the employee's hire date.

*

* @return The employee's hire date

*/

public Date getHireDate() {

return hireDate;

}

/**

* Get the number of vacation days for an employee

*

* @return The number of vacation days an employee has

*/

public short getVacationDays() {

return vacationDays;

}

/**

* Get the encoded training byte.

* @return - a byte whose bits indicate training the employee has received

*/

public byte getTraining() {

return training;

}

}

DepartmentReader.java:

package edu.xxx.cosc.company.io;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import edu.xxx.cosc.company.bo.Department;

/**

* Helper class to read departments from a comma separated

* department text file. The method readDepartment() returns

* the next department from the department file.

*

* @author

*/

public class DepartmentReader {

BufferedReader input;

/**

* Constructor - opens the department file.

*

* @param fileName - name of the department file.

*

* @throws FileNotFoundException In the event the file could not be opened.

*/

public DepartmentReader(String fileName) throws FileNotFoundException {

input = new BufferedReader(new FileReader(fileName));

}

/**

* Read the next department from the department file.

*

* @return Next department from the file. Returns null in the event the end of

* the department file is reached.

*

* @throws IOException

*/

public Department readDepartment() throws IOException {

String line = input.readLine();

// Test for end of file

if (line == null) {

return null;

}

String[] fields = line.split(",");

String deptCode = fields[0];

String deptName = fields[1];

int mgrEmpId = Integer.parseInt(fields[2]);

return new Department(deptCode, deptName, mgrEmpId);

}

/**

* Close the department file

*

* @throws IOException

*/

public void close() throws IOException {

input.close();

}

}

EmployeeInputStream:

package edu.xxx.cosc.company.io;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import edu.xxx.cosc.company.bo.Company;

import edu.xxx.cosc.company.bo.Employee;

/**

* Helper class to read employees from a binary employee file.

* The method readEmployee() returns the next employee from

* the employee file.

*

* @author

*/

public class EmployeeInputStream {

private DataInputStream input;

/**

* Constructor - opens the employee file.

* @param fileName - name of the employee file

*

* @throws FileNotFoundException In the event the file could not be opened

*/

public EmployeeInputStream(String fileName) throws FileNotFoundException {

input = new DataInputStream(new FileInputStream(fileName));

}

/**

* Read the next employee from the employee file.

* @return The next employee from the file. Returns null in the event the end of

* the employee file is reached.

*

* @throws IOException

*/

public Employee readEmployee() throws IOException {

// Test for end of file

if (input.available() == 0) {

return null;

}

// declare byte buffers for the ascii fields.

byte[] d = new byte[10];

int employeeId = input.readInt();

String lastName = input.readUTF();

String firstName = input.readUTF();

String mi = "" + (char) input.readByte();

String empIndicator = "" + (char) input.readByte();

String deptCode = "" + (char) input.readByte();

double salary = input.readDouble();

boolean exempt = input.readBoolean();

input.read(d);

Date hireDate;

try {

hireDate = Company.standardDateFormat.parse(new String(d));

} catch (ParseException e) {

hireDate = null;

e.printStackTrace();

}

short vacationDays = input.readShort();

byte training = input.readByte();

Employee employee = new Employee(employeeId, empIndicator, deptCode, firstName,

lastName, mi, salary, exempt, hireDate, vacationDays, training);

return employee;

}

/**

* Close the employee file

*

* @throws IOException

*/

public void close() throws IOException {

input.close();

}

}

CompanyReport.java:

package edu.xxx.cosc.company.reports;

import java.io.FileNotFoundException;

import java.io.IOException;

import edu.xxx.cosc.company.bo.Company;

import edu.xxx.cosc.company.bo.Department;

import edu.xxx.cosc.company.bo.Employee;

import edu.xxx.cosc.company.io.DepartmentReader;

import edu.xxx.cosc.company.io.EmployeeInputStream;

/**

* The company report. Prints information on departments and employees loaded from a department file

* and employee file.

*

* @author

*/

public class CompanyReport {

private Company company = new Company();

/**

* Main method to print the company report: Creates a company Loads

* Departments from the file name given in the first command line argument

* Loads Employees from the file name given in the last command line

* argument.

*

* @param args The command line arguments.

*/

public static void main(String[] args) {

if (args.length < 2) {

System.out

.println("Usage: java edu.iup.cosc210.reports.CompanyReport ");

System.exit(-100);

}

CompanyReport companyReport = new CompanyReport();

try {

companyReport.loadDepts(args[0]);

companyReport.loadEmployees(args[1]);

companyReport.printCompanyReport();

} catch (FileNotFoundException e) {

System.out.println("Unable to open file: " + e.getMessage());

System.exit(-1);

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Load departments from a text file.

*

* @param fileName The filename of the file that contains the departments.

*

* @throws IOException In the event the file cannot be opened or read.

*/

public void loadDepts(String fileName) throws NumberFormatException,

IOException {

DepartmentReader in = new DepartmentReader(fileName);

Department department;

while ((department = in.readDepartment()) != null) {

company.addDepartment(department);

}

in.close();

}

/**

* Load Employees from a binary file. The employees are added to the list of employees

* for their respective Department as indicated by deptCode.

*

* @param fileName The name of the file that contains the employees.

*

* @throws Exception In the event the file cannot be opened or read.

*/

public void loadEmployees(String fileName) throws Exception {

EmployeeInputStream in = new EmployeeInputStream(fileName);

Employee employee;

while ((employee = in.readEmployee()) != null) {

// find the department to which the employee will be added

Department dept = company.findDepartment(employee.getDeptCode());

dept.addEmployee(employee);

}

in.close();

}

/**

* Prints a company report. Report include information on the department

* and a list of all employees.

*/

public void printCompanyReport() {

// loop over all departments

for (int i = 0; i < company.getNoDepts(); i++) {

Department department = company.getDeparment(i);

// print the department header

System.out.println(department.getDeptName() + " Department");

System.out.println();

System.out.printf("%-20s%-20s ", "Manager: ", department.getManager().getFullName());

System.out.printf("%-20s%-20s ", "Staff Size: ", department.getNoEmployees());

System.out.printf("%-20s%d ", "Vacation Days: ",department.getTotalVacationDays());

System.out.println();

// print the column labels for employees

System.out.printf("%-3s %-22s %-12s %-11s %5s %-10s ", "ID",

"Employee Name", "Hire Date", " Salary", "Exmpt", "Vac Days");

// loop over all employees in the department

for (int j = 0; j < department.getNoEmployees(); j++) {

Employee emp = department.getEmployee(j);

System.out.printf("%3d %-22s %-12s $%10.2f %s %6d ",

emp.getEmployeeId(),

emp.getFullName(),

Company.standardDateFormat.format(emp.getHireDate()),

emp.getSalary(),

!emp.isExempt() ? " " : "Y",

emp.getVacationDays());

}

System.out.println();

System.out.println();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!