import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeData {
static List<Employee> employee = new ArrayList<Employee>();
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(true)
{
String choice;
showMenu();
choice = input.nextLine();
System.out.println();
if(choice.equalsIgnoreCase(“A”))
{
System.out.println(“Input employee details on a single line seperated by space”);
System.out.println(” e.g. EmpFirstName EmpLastName EmpID EmpHourlyWage”);
while(true)
{
String empString;
empString = input.nextLine();
if(empString.equals(“0”))
{
System.out.println();
break;
}
parseAndAddEmployee(empString);
System.out.println(“Input another employee details on a single line seperated by space (or press 0 to stop adding)”);
System.out.println(” e.g. EmpFirstName EmpLastName EmpID EmpHourlyWage”);
}
}
else if(choice.equalsIgnoreCase(“P”))
{
printAllEmployees();
}
else if(choice.equalsIgnoreCase(“S”))
{
System.out.print(“Input First name or Last name of employee you want to search: “);
String name = input.nextLine();
System.out.println();
serachandPrintEmployee(name);
}
else if(choice.equalsIgnoreCase(“Q”))
{
System.out.println(“GoodBye.”);
System.out.println(“Have a nice day!”);
break;
}
else
System.out.println(“Invalid input. Please try again”);
}
}
public static void showMenu()
{
System.out.println(“(A)dd Employee.”);
System.out.println(“(P)rint All Employee”);
System.out.println(“(S)earch Employee”);
System.out.println(“(Q)uit”);
System.out.print(“Enter your choice: “);
}
public static void parseAndAddEmployee(String empString)
{
String[] empData = empString.split(” “);
employee.add(new Employee(empData[0], empData[1],
Integer.parseInt(empData[2]), Double.parseDouble(empData[3])));
System.out.println(“Employee added successfullyn”);
}
public static void printAllEmployees()
{
for(int i = 0; i < employee.size(); i++)
{
System.out.println(“Employee #”+(i+1));
System.out.println(“——————————n”);
System.out.println(employee.get(i));
System.out.println();
}
}
public static void serachandPrintEmployee(String name)
{
boolean flag = false;
for(int i = 0; i < employee.size(); i++)
{
if(employee.get(i).getFirstName().equalsIgnoreCase(name) ||
employee.get(i).getLastName().equalsIgnoreCase(name))
{
flag = true;
System.out.println(employee.get(i));
System.out.println();
}
}
if(flag == false)
System.out.println(“No employee found with that name.n”);
}
} |