In: Computer Science
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”. The function keeps on asking the user to search again if he wants untill user don’t want to search anymore.
import java.util.*;
class Employee {
int id;
String firstname, lastname;
int hourlypay;
public Employee(int id, String firstname, String lastname, int hourlypay) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.hourlypay = hourlypay;
}
public int getId() {
return this.id;
}
}
public class Main {
static void searchbyid(Employee emp[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Employee id to search");
int id = sc.nextInt();
for(int i = 0;i<2;i++)
{
if (emp[i].getId() == id) {
System.out.println("Search Succesful");
System.out.println("Displaying Employee Records");
System.out.println("Firstname:" + emp[i].firstname + " lastname:" + emp[i].lastname + " hourly pay :"
+ emp[i].hourlypay);
}
}
sc.close();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Employee[] emp = new Employee[5];
for (int i = 0; i < 2; i++) {
System.out.println("Enter Employee id");
int id = sc.nextInt();
System.out.println("Enter Employee Firstname");
String firstname = sc.next();
System.out.println("Enter Employee Lastname");
String lastname = sc.next();
System.out.println("Enter Employee Hourly Pay");
int hpay = sc.nextInt();
emp[i] = new Employee(id, firstname, lastname, hpay);
}
searchbyid(emp);
sc.close();
}
}