EMP-manager

 avatar
unknown
plain_text
2 years ago
4.6 kB
0
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 {

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

    Scanner sc = new Scanner(System.in);
    Employee emp = new Employee();

    /**
     * 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() {
        int func;   //A function selected by user
        do {
            menu();
            func = check(true);
            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
     *
     * @param func
     * @return
     */
    public int check(boolean func) {
        int n = 0;
        boolean a = false;
        do {
            try {
                n = Integer.parseInt(sc.nextLine());
                if (func && (n < 0 || n > 2)) {
                    System.out.println("--------Erorr! Please enter numbers from 1, 2 or 0!");
                    System.out.print("Please choose again: ");
                } else {
                    a = true;
                }
            } catch (NumberFormatException e) {
                System.out.println("--------Erorr! Please enter numbers from 1, 2 or 0!");
                System.out.print("Please choose again: ");
            }
        } while (!a);
        return n;
    }

    /**
     * Check data when enter number
     *
     * @param func
     * @return
     */
    public int checkNumber(boolean func) {
        int n = 0;
        boolean a = false;
        do {
            try {

                n = Integer.parseInt(sc.nextLine());
                if (func && (n < 0 || n > 2)) {
                    System.out.println("--------Erorr! Please enter a number greater than 0!");
                    System.out.print("Enter number of Employee: ");
                } else {
                    a = true;
                }
            } catch (NumberFormatException e) {
                System.out.println("--------Erorr! Please enter a number greater than 0!");
                System.out.print("Enter number of Employee: ");
            }
        } while (!a);
        return n;
    }

    /**
     * Enter employee information.
     */
    public void add() {
        System.out.print("Enter number of Employee: ");
        int empNum = checkNumber(true);
        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 : employee) {
                    if (empl.getId().equals(emp.getId())) {
                        IDExist = true;
                        break;
                    }
                }
                //Check if entered ID is the same with old ID
                if (IDExist) {
                    System.out.println("--------Erorr: ID already exists!");
                    continue;
                }
                emp.input();
                employee.add(emp);
                break;
            }
        }
    }

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