V01 Emp-Manager

 avatar
unknown
plain_text
2 years ago
3.5 kB
10
Indexable
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package v01;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author MSI GTX
 */
public class Emp_Manager extends Employee {

    Scanner sc = new Scanner(System.in);
    public static ArrayList<Employee> employee = new ArrayList<>();
    int n;

    /**
     * Adds a new employee into the list of employee
     *
     */
    public void add() {
        System.out.print("Enter number of Employee: ");
        while (true) {
            try {
                n = Integer.parseInt(sc.nextLine().trim());
                if (n < 0 || n > 1000) {
                    throw new NumberFormatException();
                }
                break;
            } catch (NumberFormatException e) {
                System.out.println("The number of Employee must be a positive integer!");
                System.out.print("Enter again: ");
            }
        }
        for (int i = 0; i < n; i++) {
            Employee emp = new Employee();
            System.out.println("Employee " + (i + 1) + ":");
            emp.input();
            employee.add(emp);
        }

    }

    public int checkFunc() {
        //loop until user input correct
        while (true) {
            try {
                int result = Integer.parseInt(sc.nextLine().trim());
                if (result < 0 || result > 2) {
                    throw new NumberFormatException();
                }
                return result;
            } catch (NumberFormatException e) {
                System.out.println("Please enter number limt [0-2]");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     * Show the list of employee
     */
    public void show() {
        if (employee.isEmpty()) {
            System.out.println("The Employee list is empty!");
        } else {
            System.out.println("============================ List Employee ===========================");
            System.out.println("|ID          |Name                    |Salary          |COE          |");
            for (int i = 0; i < employee.size(); i++) {
                Employee emp = employee.get(i);
                emp.output();
            }
        }
    }

    /**
     * Check if the ID already exists
     *
     * @param ID
     * @return
     */
    public boolean isIDExist(String ID) {
        return employee.stream().map(Employee::getId).anyMatch(id -> id.equalsIgnoreCase(ID));
    }

    /**
     * Display menu to user
     *
     */
    public void menu() {
        System.out.println("----------MENU----------");
        System.out.println("1. Input employee.");
        System.out.println("2. Show list employee.");
        System.out.println("0. Exit.");
        System.out.print("Please choose: ");
    }

    public void openMenu() {
        int func;
        do {
            menu();
            func = checkFunc();
            switch (func) {
                case 1:
                    add();
                    break;
                case 2:
                    show();
                    break;
                case 0:
                    System.out.println("THANKS FOR USING!\nSEE YOU AGAIN!");
                    break;
            }

        } while (func != 0);

    }

}
Editor is loading...