In: Computer Science
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
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:
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.
/***************************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 :)