Question

Part 1 Start by designing the Employee class Employee holds the following private attributes: a count...

Part 1

Start by designing the Employee class

Employee holds the following private attributes:

  • a count of employees (a static variable called NUM_EMP)*
  • an employee’s last name
  • an employee’s first name
  • an employeeNumber (a String that is unique – that will use the NUM_EMP)*
  • a department
  • a jobTitle
  • an annual salary (e.g., 45000).

NOTE: *NUM_EMP should start off as zero and get updated every time a new Employee is created. The employeeNumber always starts with empl_ followed by the current count of employees and is created automatically every time an Employee object is created.

  • The class should have appropriate constructors, get and set methods, toString (control zeros for the salary), and other needed methods.
  • Also, it should have a static method called getCount that returns the number of employees and another static method called setCount that will allow you to change the number of employees.
  • To control for decimals in the toString method, you can use the DecimalFormat class (you need to import the class import java.text.DecimalFormat). Here is an example of how it works:
public String toString() { 
    // assume you want to print a value that has several decimal places and you only
    // want it to print only two decimal places
    DecimalFormat df = new DecimalFormat("#.00");  // give it the format
    // changes double value to have two decimal places
    return "Value is : " + df.format(value);  
}

Details

EmployeeDemo:
Read in data using a Scanner object to create five Employee objects. Print the employees and then using a call to the Employee class, print out the number of Employees.

Input

Data for five employees in the following format:
{last name} {first name} {dept} {job} {salary} (repeat for five total employees)

Sample Input:
Brown Sue HumanResources Manager 65000 Smith Bob Marketing Accounts 38000 Kyle John HumanResources Administrator 29000 Brown Lynn Sales OnlineSalesRep 32000 Byrne Sara Sales Manager 65000

Output

Employee: Sue Brown (empl_1): Manager, HumanResources ($65000.00)
Employee: Bob Smith (empl_2): Accounts, Marketing ($38000.00)
Employee: John Kyle (empl_3): Administrator, HumanResources ($29000.00)
Employee: Lynn Brown (empl_4): OnlineSalesRep, Sales ($32000.00)
Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)
Number of employees: 5

Part 2

Now design the EmployeeRecord class

The EmployeeRecord class uses an ArrayList that holds employees, and has an attribute that holds the company name (both are private attributes):

public class EmployeeRecord {
    private ArrayList<Employee> list;
    private String name;
    // rest of the code
}

The EmployeeRecord class should have the following methods:

  • An appropriate constructor (see the input examples)
  • Get and set methods
  • toString method that prints the company name and how many employees it currently has.
  • A method to add an employee:
    • When an employee is added (hired), it should add it to the ArrayList alphabetically by last name (and by the first name if there are multiple same last names). To sort alphabetically, look at the compareTo String method which compares Strings lexicographically (returns an int >0, <0, or 0 which indicates the order, where 0 means the Strings are the same, less than 0 means the calling string occurs first alphabetically, and greater than 0 means the calling string occurs after alphabetically.)
    • For example, if String a = "Apple", String b = "Orange", the result of a.compareTo(b) is -14. Apple comes before Orange alphabetically.
  • Print all employees who work at the company (see output for formatting)
  • Search for a specific employee by an employeeID and return the Employee object (if the employee does not exist, then return null)
  • Search for a specific employee by the last name, and returns a new ArrayList of Employees that contains all the employees with the same last name or an empty ArrayList (if there are no matches)
  • Search for all employees that have a job title (e.g., Manager) and return an ArrayList of Employees who have the same job title (or empty if there are no employees in that job)
  • Search for all employees who work in a department (e.g., Human Resources) and return an ArrayList of employees who work in that department (or empty if there are no employees in that department)
  • Search for employees who earn the most. Return an ArrayList of all employees who earn the most.
  • The program should be able to delete an Employee, either by passing the Employee object itself or by an EmployeeNumber (override the method delete for both cases). And, when you delete an Employee you should decrease the EMPL_NUM.

Note: You can add other methods that you think are helpful, but remember to comment them!

Part 3

In this part, you will be implementing a hefty demo for your EmployeeClass.

Note: You may assume that the strings read in for Employee and EmployeeDemoare a single word.

  1. Read the name of the company and create a new EmployeeRecord object
  2. Then, read in data using a Scanner to create five Employee objects and add those to your EmployeeRecord
  3. Print the EmployeeRecord and then call printAll to print all the employees
  4. Read in a name using the Scanner object and then call search for last name method in the EmployeeRecord class. Print the results from the returned ArrayList.
  5. Read in an EmployeeNumber using the Scanner object and then call search for employeeNumber method in the EmployeeRecord class. Print the result.
  6. Read in a Department name using the Scanner object and then call the search for department method in the EmployeeRecord class. Print the returned ArrayList.
  7. Read in a job title using the Scanner object and then call the search for job title method in the EmployeeRecord class. Print the results from the returned ArrayList.
  8. Call the method to find the employee/s who earn the most from the EmployeeRecord class. Print the returned ArrayList.
  9. Remove the first Employee object that you created (i.e., the one with EmployeeNumber empl_1) by using the delete method that takes in an Employee object as a parameter.
  10. Then remove the third employee object you created by calling the delete method that uses the employeeNumber as a parameter (“empl_3”).
  11. Print the EmployeeRecord and then call the printAll method to print all the employees

Details

Input

Sample Input:
AcmeToys Brown Sue HumanResources Manager 65000 Smith Bob Marketing Accounts 38000 Kyle John HumanResources Administrator 29000 Brown Lynn Sales OnlineSalesRep 32000 Byrne Sara Sales Manager 65000 Brown empl_1 Sales Manager

Output

Sample Output:

AcmeToys Total Employees: 5
Employee: Lynn Brown (empl_4): OnlineSalesRep, Sales ($32000.00)
Employee: Sue Brown (empl_1): Manager, HumanResources ($65000.00)
Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)
Employee: John Kyle (empl_3): Administrator, HumanResources ($29000.00)
Employee: Bob Smith (empl_2): Accounts, Marketing ($38000.00)

Employees with the last name of Brown:
[Employee: Lynn Brown (empl_4): OnlineSalesRep, Sales ($32000.00), Employee: Sue Brown (empl_1): Manager, HumanResources ($65000.00)]

Employees with employee number empl_1:
Employee: Sue Brown (empl_1): Manager, HumanResources ($65000.00)

Employees in Sales:
[Employee: Lynn Brown (empl_4): OnlineSalesRep, Sales ($32000.00), Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)]

Employees who are a Manager:
[Employee: Sue Brown (empl_1): Manager, HumanResources ($65000.00), Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)]

Employee/s who earn the most:
[Employee: Sue Brown (empl_1): Manager, HumanResources ($65000.00), Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)]

After deleting the employee:
AcmeToys Total Employees: 4
Employee: Lynn Brown (empl_4): OnlineSalesRep, Sales ($32000.00)
Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)
Employee: John Kyle (empl_3): Administrator, HumanResources ($29000.00)
Employee: Bob Smith (empl_2): Accounts, Marketing ($38000.00)

After deleting the employee:
AcmeToys Total Employees: 3
Employee: Lynn Brown (empl_4): OnlineSalesRep, Sales ($32000.00)
Employee: Sara Byrne (empl_5): Manager, Sales ($65000.00)
Employee: Bob Smith (empl_2):
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

package student;

import java.text.DecimalFormat;

public class Employee {

    private static int NUM_EMP = 0;
    private String firstName;
    private String lastName;
    private int employeeNumber;
    private double salary;
    private String jobTitle;
    private String department;

    public Employee(String firstName, String lastName, double salary, String jobTitle, String department) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.jobTitle = jobTitle;
        this.department = department;
        employeeNumber = ++NUM_EMP;
    }

    public static int getCount() {
        return NUM_EMP;
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(int employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        DecimalFormat df = new DecimalFormat("#.00");
        return "Employee : " + firstName + " " + lastName +
                " (empl_" + employeeNumber + "): " +
                jobTitle +
                ", " + department + "($" + df.format(salary) + ")";
    }

}

=======================================================================================

package student;

import java.util.ArrayList;

public class EmployeeRecord {

    private ArrayList<Employee> list;
    private String name;

    public EmployeeRecord(String name) {
        this.name = name;
        list = new ArrayList<>();
    }

// add employee such that they are sorted lexicographically 
    public void addEmployee(Employee emp) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getFirstName().compareTo(emp.getFirstName()) > 0) {
                list.add(i, emp);
                return;
            }
        }
        list.add(emp);
    }

    public void printEmployees() {
        for (Employee emp : list) {
            System.out.println(emp);
        }
    }
// returns the employee with same id
    public Employee getEmployee(int employeeId) {
        for (Employee emp : list) {
            if (emp.getEmployeeNumber() == employeeId) {
                return emp;
            }
        }
        return null;
    }
// returns the employee list with same last name

    public ArrayList<Employee> getEmployeeByLastName(String lName) {
        ArrayList<Employee> lastNames = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getLastName().equalsIgnoreCase(lName)) {
                lastNames.add(emp);
            }
        }
        return lastNames;
    }

//// returns the employee with same job titles
    public ArrayList<Employee> getEmployeeByTitle(String title) {
        ArrayList<Employee> titles = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getJobTitle().equalsIgnoreCase(title)) {
                titles.add(emp);
            }
        }
        return titles;
    }
// returns the employee with same dept name
    public ArrayList<Employee> getEmployeeByDepartment(String dept) {
        ArrayList<Employee> depts = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getDepartment().equalsIgnoreCase(dept)) {
                depts.add(emp);
            }
        }
        return depts;
    }
    
// returns the employee with max salary
    public Employee getEmployeeEarnsMost() {
        double salary = 0;
        Employee employee = null;
        for (Employee emp : list) {
            if (salary < emp.getSalary()) {
                employee = emp;
                salary = emp.getEmployeeNumber();
            }
        }
        return employee;

    }

    public ArrayList<Employee> getList() {
        return list;
    }

    public void setList(ArrayList<Employee> list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Company Name: " + getName() + ", Total Employees: " + list.size();
    }
 

// driver method
    public static void main(String[] args) {

        Employee e1 = new Employee("Sue", "Brown", 65000, "Manager", "Human Resources");
        Employee e2 = new Employee("Bob", "Smith", 38000, "Accounts", "Marketing");
        Employee e3 = new Employee("John", "Kyle", 29000, "Administrator", "Human Resources");
        Employee e4 = new Employee("Lynn", "Brown", 32000, "Online Sales Rep", "Sales");
        Employee e5 = new Employee("Sara", "Nyrne", 65000, "Manager", "Sales");

        EmployeeRecord record = new EmployeeRecord("Microsoft");
        record.addEmployee(e1);
        record.addEmployee(e2);
        record.addEmployee(e3);
        record.addEmployee(e4);
        record.addEmployee(e5);
        record.printEmployees();
        System.out.println("Number of employees: " + Employee.getCount());

    }
}

According to HomeworkLib rules I am allowed to answer only 2 subquestions . Feel free to reach out regarding any queries . Thank you .

Add a comment
Know the answer?
Add Answer to:
Part 1 Start by designing the Employee class Employee holds the following private attributes: a count...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that implements a rudimentary name-finder. It should start out by filling up a...

    Write a program that implements a rudimentary name-finder. It should start out by filling up a name ArrayList with Name objects. Then it should ask the user if he/she wants to find a particular name. Then it should search for the name in the name ArrayList and return the names information if the name is found. Here is a suggested UML class diagram: Implement Name filltheblackbook method by adding anonymous objects representing these four names: First Name                       Last Name               Address...

  • Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string...

    Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string SSN; The Employee class has ▪ a no-arg constructor (a no-arg contructor is a contructor with an empty parameter list): In this no-arg constructor, use “unknown” to initialize all private attributes. ▪ a constructor with parameters for each of the attributes ▪ getter and setter methods for each of the private attributes ▪ a method getFullName() that returns the last name, followed by a...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...

    Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class. HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...

  • DatabaseFilter Design a DatabaseFilter class that will manage the file 1/O (input/output) of an employee ArrayList...

    DatabaseFilter Design a DatabaseFilter class that will manage the file 1/O (input/output) of an employee ArrayList object. For a Database Filter class to actually store an ArrayList object, it must know the file to which the list must be read from or written to. Note: It is expected that you may have more methods within your class design than requested in the specifications described below. However, your class should support at a minimum the following data and operations: encapsulated data...

  • Following is the EmployeeTester class, which creates object of the class Employee and calls the methods...

    Following is the EmployeeTester class, which creates object of the class Employee and calls the methods of that object. The EmployeeTester class is written to test another class: Employee. You are required to implement the class Employee by: declaring its instance variables: name, age, salary. implementing its constructor. implementing its methods: getEmployeeName(), getEmployeeAge(), getEmployeeSalary(), setEmployeeAge(int empAge),      setEmployeeSalary(double empSalary) public class EmployeeTester { public static void main(String args[]) { // Construct an object Employee employeeOne = new Employee("Ahmad Abdullah"); //...

  • Create a Java application, that support the following: Create an Employee class, which holds following information:...

    Create a Java application, that support the following: Create an Employee class, which holds following information: Employee First Name Employee Last Name Employee Phone Number Employee Address Employee id Employee Title Employee salary Create an application that uses the Employee class Constructors –Getters and setters A minimum of 3 constructors including default constructor equals() method Helper methods toString() method Create an array of 5 Employee objects Use a Scanner to read in 5 employee information Change 2 employees information (3-items...

  • In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate...

    In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate the pension to date as the equivalent of a monthly salary for every year in service. 4) Print the calculated pension for all employees. ************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************ import java.io.*; public class ManagerTest { public static void main(String[] args) { InputStreamReader...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT