Question

In: Computer Science

General Requirements:           Create a Java application that inputs, processes and stores Employee data. Due date:...

General Requirements:

          Create a Java application that inputs, processes and stores Employee data.

Due date:

Before 11:59:59PM on August 15th 2019.

Specifications:

1. Your application should be able to accept Employee data from the user and add the information in a file type of your choice.

2. Employee Data should include the following

  • EmployeeID
  • FirstName
  • LastName
  • Department
  • Phone
  • Salary

Example:

111222333

Percy

Hayden

Billing

416-222-2222

4000

3. Your application should provide the functionality to display the Employee data as needed.

4. Your application should also provide sorting capabilities, at least by First Name, Last Name and Department.

5. Your application should also provide a functionality to search for a specific Employee by:

  • EmployeeID
  • Last-Name
  • Department

6. Your application should allow modification of any Employee data by providing proper UI. All the modifications should be persisted on a file type of your choice (same file as in item 1).

7. Your choice of the type of the application (Console or GUI based).

8. If using Console based application, proper menu’s need to be provided after every command is executed.

Solutions

Expert Solution

/***************************Employee.java************************************/

package assignment2;

import java.util.Comparator;

public class Employee {

   // data field for Employee
   private int employeeId;
   private String firstName;
   private String lastName;
   private String department;
   private String phone;
   private double salary;

   // no argument constructor
   public Employee() {
       this.employeeId = 111111111;
       this.firstName = "";
       this.lastName = "";
       this.department = "";
       this.phone = "";
       this.salary = 0.0;
   }

   /**
   *
   * @param employeeId
   * @param firstName
   * @param lastName
   * @param department
   * @param phone
   * @param salary
   */
   public Employee(int employeeId, String firstName, String lastName, String department, String phone, double salary) {
       super();
       this.employeeId = employeeId;
       this.firstName = firstName;
       this.lastName = lastName;
       this.department = department;
       this.phone = phone;
       this.salary = salary;
   }

   // getter and setter
   public int getEmployeeId() {
       return employeeId;
   }

   // set employee id
   public void setEmployeeId(int employeeId) {
       this.employeeId = employeeId;
   }

   // return firstName
   public String getFirstName() {
       return firstName;
   }

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

   // get lastName
   public String getLastName() {
       return lastName;
   }

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

   // get department
   public String getDepartment() {
       return department;
   }

   // set department
   public void setDeparment(String department) {
       this.department = department;
   }

   // return phone
   public String getPhone() {
       return phone;
   }

   // set phone
   public void setPhone(String phone) {
       this.phone = phone;
   }

   // return salary
   public double getSalary() {
       return salary;
   }

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

   // compare by first name
   public static Comparator<Employee> empFirstNameComparator = new Comparator<Employee>() {

       public int compare(Employee e1, Employee e2) {
           String empFirstName1 = e1.getFirstName().toUpperCase();
           String empFirstName2 = e2.getFirstName().toUpperCase();

           // ascending order
           return empFirstName1.compareTo(empFirstName2);

       }
   };
   // compare by last name
   public static Comparator<Employee> empLastNameComparator = new Comparator<Employee>() {

       public int compare(Employee e1, Employee e2) {
           String empLastName1 = e1.getLastName().toUpperCase();
           String empLastName2 = e2.getLastName().toUpperCase();

           // ascending order
           return empLastName1.compareTo(empLastName2);

       }
   };
   // compare by department
   public static Comparator<Employee> empDepartmentComparator = new Comparator<Employee>() {

       public int compare(Employee e1, Employee e2) {
           String empDepartment1 = e1.getDepartment().toUpperCase();
           String empDepartment2 = e2.getDepartment().toUpperCase();

           // ascending order
           return empDepartment1.compareTo(empDepartment2);

       }
   };

   // toString method for information
   @Override
   public String toString() {
       return "Employee Id: " + employeeId + "\nFirst Name: " + firstName + " " + lastName + "\nDeparment: "
               + department + "\nPhone: " + phone + "\nSalary=" + salary;
   }

}
/*****************************************TestEmployee.java*********************************/

package assignment2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class TestEmployee {

   static ArrayList<Employee> employees;

   public static void main(String[] args) {

       //scanner object to read input
       Scanner scan = new Scanner(System.in);
       employees = new ArrayList<>();
       readFromFile("emp.txt");
       System.out.println("*****************Employee Management System****************");
       int option = 0;
       //do while loop till user don't want to exit
       do {
           menu();
           System.out.print("Please select option: ");
           option = scan.nextInt();
           scan.nextLine();
           switch (option) {
           case 1:
               //calling method for display information in unsorted order
               displayInfo();
               break;
           case 2:
               //display by first name order
               displayInfoByFirstName();
               break;
           case 3:
               //display by last name order
               displayInfoByLastName();
               break;
           case 4:
               //display by department wise
               displayInfoByDepartment();
               break;
           case 5:
               //search by employee id
               searchByEmployeeID();
               break;
           case 6:
               //search by last name
               searchByLastName();
               break;
           case 7:
               //search by department
               searchByDepartment();
               break;
           case 8:
               //To modify the employee data
               modifyEmployeeData();
               break;
           case 9:
               System.out.println("Thanks for using System, Bye!");
               break;

           default:
               System.out.println("Invalid Choice! Try again!");
               break;
           }

       } while (option != 9);

       //calling method for write into the file
       writeToFile("emp.txt", employees);

   }

   //read from file
   private static void readFromFile(String fileName) {

       File file = new File(fileName);

       try {

           Scanner sc = new Scanner(file);
           // discard one line as header
           sc.nextLine();
           // while loop till file has line
           while (sc.hasNextLine()) {
               // read the line
               String line = sc.nextLine();
               int empID = Integer.parseInt(line.split(",")[0]);
               String firstName = line.split(",")[1];
               String lastName = line.split(",")[2];
               String department = line.split(",")[3];
               String phone = line.split(",")[4];
               double salary = Double.parseDouble(line.split(",")[5]);
               employees.add(new Employee(empID, firstName, lastName, department, phone, salary));

           }
           sc.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }

   //write to file
   private static void writeToFile(String fileName, ArrayList<Employee> employees) {

       try {
          
           FileWriter fw = new FileWriter(fileName);
           // write to the file
           fw.write("EmployeeID,FirstName,LastName,Department,Phone,Salary\n");
           for (Employee employee : employees) {

               fw.write(employee.getEmployeeId() + "," + employee.getFirstName() + "," + employee.getLastName() + ","
                       + employee.getDepartment() + "," + employee.getPhone() + "," + employee.getSalary());
               fw.write("\n");
           }
           fw.flush();
           fw.close();
       } catch (IOException e) {

           e.printStackTrace();
       }

   }

   //display information
   private static void displayInfo() {

       for (Employee employee : employees) {

           System.out.println(employee.toString());
       }
   }

   private static void displayInfoByFirstName() {

       Collections.sort(employees, Employee.empFirstNameComparator);
       for (Employee employee : employees) {

           System.out.println(employee.toString());
       }
   }

   private static void displayInfoByLastName() {
       Collections.sort(employees, Employee.empLastNameComparator);
       for (Employee employee : employees) {

           System.out.println(employee.toString());
       }
   }

   private static void displayInfoByDepartment() {
       Collections.sort(employees, Employee.empDepartmentComparator);
       for (Employee employee : employees) {

           System.out.println(employee.toString());
       }

   }

   private static void searchByEmployeeID() {

       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter employee id: ");
       int employeeId = scan.nextInt();
       int flag = 0;
       for (Employee employee : employees) {

           if (employee.getEmployeeId() == employeeId) {

               System.out.println(employee.toString());
               flag = 1;
           }
       }

       if (flag == 0) {

           System.out.println("No Employee found by this id!");
       }
   }

   private static void searchByLastName() {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter employee last name: ");
       String lastName = scan.nextLine();
       int flag = 0;
       for (Employee employee : employees) {

           if (employee.getLastName().equalsIgnoreCase(lastName)) {

               System.out.println(employee.toString());
               flag = 1;
           }
       }

       if (flag == 0) {

           System.out.println("No Employee found by this id!");
       }
   }

   private static void searchByDepartment() {

       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter employee department name: ");
       String department = scan.nextLine();
       int flag = 0;
       for (Employee employee : employees) {

           if (employee.getDepartment().equalsIgnoreCase(department)) {

               System.out.println(employee.toString());
               flag = 1;
           }
       }

       if (flag == 0) {

           System.out.println("No Employee found by this id!");
       }
   }

   private static void modifyEmployeeData() {

       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter employee id: ");
       int employeeId = scan.nextInt();
       scan.nextLine();
       int flag = 0;
       for (Employee employee : employees) {

           if (employee.getEmployeeId() == employeeId) {

               System.out.print("Enter Employee FirstName: ");
               String firstName = scan.nextLine();
               System.out.print("Enter Employee LastName: ");
               String lastName = scan.nextLine();
               System.out.print("Enter Employee Department: ");
               String department = scan.nextLine();
               System.out.print("Enter Employee Phone: ");
               String phone = scan.nextLine();
               System.out.print("Enter Employee Salary: ");
               double salary = scan.nextDouble();
               scan.nextLine();
               employee.setFirstName(firstName);
               employee.setLastName(lastName);
               employee.setDeparment(department);
               employee.setPhone(phone);
               employee.setSalary(salary);
               flag = 1;
           }
       }

       if (flag == 0) {

           System.out.println("No Employee found by this id!");
       }

   }

   private static void menu() {

       System.out.println("1. Display Employee Details in Unsorted order: "
               + "\n2. Display Employee Details by First Name: " + "\n3. Display Employee Details by Last Name: "
               + "\n4. Display Employee Details by Department: " + "\n5. Search Employee by employee id: "
               + "\n6. Search Employee by Last Name: " + "\n7. Search Employee by Department: "
               + "\n8. Modify Employee data: " + "\n9. Exit");
   }
}
/******************************************output*******************************************************/

*****************Employee Management System****************
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 1
Employee Id: 111222444
First Name: Mahendra Singh Dhoni
Deparment: Banking
Phone: 121-232-343
Salary=10000.0
Employee Id: 111222333
First Name: Percy Hayden
Deparment: Billing
Phone: 416-222-2222
Salary=4000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 2
Employee Id: 111222444
First Name: Mahendra Singh Dhoni
Deparment: Banking
Phone: 121-232-343
Salary=10000.0
Employee Id: 111222333
First Name: Percy Hayden
Deparment: Billing
Phone: 416-222-2222
Salary=4000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 3
Employee Id: 111222444
First Name: Mahendra Singh Dhoni
Deparment: Banking
Phone: 121-232-343
Salary=10000.0
Employee Id: 111222333
First Name: Percy Hayden
Deparment: Billing
Phone: 416-222-2222
Salary=4000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 4
Employee Id: 111222444
First Name: Mahendra Singh Dhoni
Deparment: Banking
Phone: 121-232-343
Salary=10000.0
Employee Id: 111222333
First Name: Percy Hayden
Deparment: Billing
Phone: 416-222-2222
Salary=4000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 5
Please enter employee id: 111222333
Employee Id: 111222333
First Name: Percy Hayden
Deparment: Billing
Phone: 416-222-2222
Salary=4000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 6
Please enter employee last name: Dhoni
Employee Id: 111222444
First Name: Mahendra Singh Dhoni
Deparment: Banking
Phone: 121-232-343
Salary=10000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 7
Please enter employee department name: banking
Employee Id: 111222444
First Name: Mahendra Singh Dhoni
Deparment: Banking
Phone: 121-232-343
Salary=10000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 8
Please enter employee id: 111222444
Enter Employee FirstName: Virat
Enter Employee LastName: Kohli
Enter Employee Department: hod
Enter Employee Phone: 895-632-364
Enter Employee Salary: 20000
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 7
Please enter employee department name: hod
Employee Id: 111222444
First Name: Virat Kohli
Deparment: hod
Phone: 895-632-364
Salary=20000.0
1. Display Employee Details in Unsorted order:
2. Display Employee Details by First Name:
3. Display Employee Details by Last Name:
4. Display Employee Details by Department:
5. Search Employee by employee id:
6. Search Employee by Last Name:
7. Search Employee by Department:
8. Modify Employee data:
9. Exit
Please select option: 9
Thanks for using System, Bye!
/***********************************************emp.txt*************************************/

EmployeeID,FirstName,LastName,Department,Phone,Salary
111222444,Virat,Kohli,hod,895-632-364,20000.0
111222333,Percy,Hayden,Billing,416-222-2222,4000.0

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should...
Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should be able to accept Student data from the user and add the information in a file type of your choice. 2. your application is a menu driven and allow the user to choose from the following menu Menu: 1 – Add students to file 2 – print all the student information 3 – print specific student information using studentID 4 – Exit the program...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create an abstract Insurance class (Insurance.java) that has the following instance variables: - Insurance number, - customer name, - start date of policy ( This should be represented by an object of predefined date class in java), - customer address [( Create a new Address class ( Address.java ) having following instance data members: House number , Street name, City, Province, Zip code. Implement getter...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to...
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to read all data from the file echoing the data to standard output. After all data has been read, display how many data were read. For example, if 10 integers were read, the application should display all 10 integers and at the end of the output, print "10 data values were read" My issue is displaying how many integers were read and how many strings...
JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT