EMP-Manager

 avatar
unknown
plain_text
2 years ago
4.0 kB
6
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;

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

    ArrayList<Employee> em = new ArrayList<>();

    //Scanner sc = new Scanner(System.in);
    Employee emp = new Employee();
    int func;
    int n;

    /**
     * Show list of function.
     */
    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() {
        //A function selected by user
        do {
            menu();
            func = check();
            switch (func) {
                case 1:
                    add();
                    break;
                case 2:
                    show();
                    break;
                case 0:
                    System.out.println("SEE YOU AGAIN!");
                    return;
            }
        } while (func != 0);
    }

    /**
     * Check data when enter function
     *
     * @return
     */
    public int check() {

        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 input number in rage [0-2]");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     * Check data when enter number
     *
     * @return
     */
    public int checkNumber() {
        do {
            try {
                n = Integer.parseInt(sc.nextLine());
                if (n <= 0 || n > 100) {
                    throw new NumberFormatException();

                }
            } catch (NumberFormatException e) {
                System.out.println("--------Erorr! Please enter a number greater than 0!");
                System.out.print("Enter number of Employee: ");
            }
        } while (n < 0 || n > 100);
        return n;
    }

    /**
     * Enter employee information.
     */
    public void add() {
        System.out.print("Enter number of Employee: ");
        int empNum = checkNumber();
        for (int i = 0; i < empNum; i++) {
            emp = new Employee();
            System.out.println("Employee " + (i + 1) + ":");
            while (true) {
                emp.inputID();
                boolean IDExist = false;
                for (Employee empl : em) {
                    if (empl.getId().equals(emp.getId())) {
                        IDExist = true;
                        break;
                    }
                }
                //Check if the ID exists
                if (IDExist) {
                    System.out.println("--------Erorr: ID already exists!");
                    continue;
                }
                emp.input();
                em.add(emp);
                break;
            }
        }
    }

    /**
     * Show table information
     */
    public void show() {
        if (em.isEmpty()) {
            System.out.println("Empty list of employee!");
        } else {
            System.out.println("======================= List Employee =======================");
            System.out.printf("|ID          |Name                    |Salary          |COE |\n");
            for (int i = 0; i < em.size(); i++) {
                Employee e = em.get(i);
                e.output();
            }
        }
    }
}
Editor is loading...