Manage Employees

PHOTO EMBED

Thu Dec 09 2021 23:54:00 GMT+0000 (UTC)

Saved by @Mahmoudhaney #java

package com.mycompany.testoop;
import java.util.ArrayList;
import java.util.Scanner;


public class Main {
    //*********************Main Needs***********************
    static public Scanner in = new Scanner(System.in);
    static ArrayList<Employee> ourEmployees = new ArrayList<>();
    //*********************Our Functions***********************
    static void displayMenu(){
        System.out.println("Please Choose From This Menu:\n   1- Add New Employee\n   2- Edit Current Employee\n   3- Remove Current Employee\n   4- Display All Employee\n");
    }
    static void addEmployee(){
        Employee emp = new Employee();
        System.out.println("ID: ");
        emp.setId(in.nextInt());
        System.out.println("First Name: ");
        emp.setFirstName(in.next());
        System.out.println("Last Name: ");
        emp.setLastName(in.next());
        System.out.println("Salary: ");
        emp.setSalary(in.nextFloat());
        System.out.println("Phone Number: ");
        emp.setPhoneNumber(in.next());
        ourEmployees.add(emp);
        //ourEmployees.add(new Employee(in.nextInt(), in.next(), in.next(), in.nextFloat(), in.next()));
    }
    static void editEmployee(int id){
        int lenght = ourEmployees.size();
        for(int i=0; i<lenght; i++){
            if(ourEmployees.get(i).getId() == id){
                System.out.println("ID: ");
                ourEmployees.get(i).setId(in.nextInt());
                System.out.println("First Name: ");
                ourEmployees.get(i).setFirstName(in.next());
                System.out.println("Last Name: ");
                ourEmployees.get(i).setLastName(in.next());
                System.out.println("Salary: ");
                ourEmployees.get(i).setSalary(in.nextFloat());
                System.out.println("Phone Number: ");
                ourEmployees.get(i).setPhoneNumber(in.next());
                break;
            }                
        }
    }
    static void removeEmployee(int id){
        int lenght = ourEmployees.size();
        for(int i=0; i<lenght; i++){
            if(ourEmployees.get(i).getId() == id){
                ourEmployees.remove(i);
                break;
            }                
        }
    }
    static void printAllEmployees(){
        for (int i=0; i<ourEmployees.size(); i++){
            System.out.println("Employee #" + (i+1) + " ==> " + ourEmployees.get(i) + " ");
        }
    }
    static void manageEmployees(){
        while(true){
            displayMenu();
            short choice;
            choice = in.nextShort();
            switch (choice){
                case 1:
                    addEmployee();
                    System.out.println("Done: New Employee Added");
                    System.out.println("================================");
                    break;
                case 2:
                    System.out.println("Enter Employee ID To Edit:");
                    int emppId = in.nextInt();
                    editEmployee(emppId);
                    System.out.println("Done: Current Employee Updated");
                    System.out.println("================================");
                    break;
                case 3:
                    System.out.println("Enter Employee ID To Remove:");
                    int empId = in.nextInt();
                    removeEmployee(empId);
                    System.out.println("Done: Current Employee Removed");
                    System.out.println("================================");
                    break;
                case 4:                   
                    printAllEmployees();
                    System.out.println("================================");
                    break;
                default:
                    System.out.println("!!!! Error: Invalid Number, please choose from the menu");
                    System.out.println("================================");
                    manageEmployees();
                    break;
            }
        }
    }
    
    //*********************Main Functions***********************
    public static void main(String args[]){
          manageEmployees();
    }
    
    
}





package com.mycompany.testoop;

public class Employee {
    //*********************Data Members*********************
    int id;
    String firstName;
    String lastName;
    float salary;
    String phoneNumber;
    
    //*********************Constructors*********************
    public Employee() {
    }
    public Employee(int id, String firstName, String lastName, float salary, String phoneNumber) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.phoneNumber = phoneNumber;
    }
    
    //*********************Setters & Getters*********************
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 float getSalary() {
        return salary;
    }

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

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    //*********************Our Methods*********************
    @Override
    public String toString() {
        return "ID = " + id + ", Full Name = \"" + firstName + " " + lastName + "\" , Salary = " + salary + ", Phone Number = " + phoneNumber;
    }
    
    
}
content_copyCOPY