Employee

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

/**
 *
 * @author MSI GTX
 */
import java.util.Scanner;

public class Employee {

    String id;
    String name;
    double salary;
    double COE;

    /**
     * The constructor creates a new object Employee
     */
    public Employee() {

    }

    /**
     * Gets the ID of employee
     *
     * @return
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the ID of employee
     *
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Gets the name of employee
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of employee
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the salary of employee
     *
     * @return
     */
    public double getSalary() {
        return salary;
    }

    /**
     * Sets the salary of employee
     *
     * @param salary
     */
    public void setSalary(double salary) {
        this.salary = salary;
    }

    /**
     * Gets the COE of employee
     *
     * @return
     */
    public double getCOE() {
        return COE;
    }

    /**
     * Sets the COE of employee
     *
     * @param COE
     */
    public void setCOE(double COE) {
        this.COE = COE;
    }

    /**
     * The constructor creates a new object Employee
     *
     * @param id
     * @param name
     * @param salary
     * @param COE
     * @throws Exception
     */
    public Employee(String id, String name, double salary, double COE) throws Exception {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.COE = COE;
    }

    /**
     * Calculate the salary of employee
     *
     * @return
     */
    public double calSalary() {
        return this.salary * this.COE;
    }

    /**
     * Take the input
     *
     */
    public void input() {
        Scanner sc = new Scanner(System.in);
        id = "";
        name = "";
        salary = 0;
        COE = 1;

        boolean isDataValid = false;
        Emp_Manager employee = new Emp_Manager();

        //Check the ID of user's input until it's valid
        do {
            try {
                System.out.print("Please enter ID number: ");
                do {
                    isDataValid = true;
                    id = sc.nextLine();
                    if (id.length() != 6 || (id.charAt(0) != 'N') || (id.charAt(1) != 'V')) {
                        isDataValid = false;
                        System.out.print("--------Erorr! Please ID number again (NVxxxx): ");
                    } else if (employee.isIDExist(id)) {
                        System.out.println("Error! ID already exists. Please enter a different ID.");
                        System.out.print("Please enter ID number: ");
                        isDataValid = false;
                    }
                } while (!isDataValid);
                setId(id);
                break;
            } catch (Exception e) {
                System.out.print("Please enter ID number: ");
            }
        } while (!isDataValid);

        //Check name of user's input until it's valid
        do {
            try {
                System.out.print("Please enter name: ");
                do {
                    isDataValid = true;
                    name = sc.nextLine().trim();
                    if (name.equals("")) {
                        isDataValid = false;
                        System.out.print("--------Erorr! Please name again (Not Null): ");
                    }
                } while (!isDataValid);
                setName(name);
                break;
            } catch (Exception e) {
                System.out.print("--------Erorr! Please name again (Not Null): ");
            }
        } while (!isDataValid);

        //Check salary of user's input until it's valid
        do {
            try {
                System.out.print("Please enter salary: ");
                do {
                    isDataValid = true;
                    salary = Double.parseDouble(sc.nextLine().trim());
                    if (salary <= 100) {
                        isDataValid = false;
                        System.out.print("--------Erorr! Please salary again (Salary > 100 USD): ");
                    }
                } while (!isDataValid);
                setSalary(salary);
                break;
            } catch (NumberFormatException e) {
                System.out.print("--------Erorr! Please salary again (Salary > 100 USD): ");
            }
        } while (!isDataValid);

        //Check coefficient salary of user's input until it's valid
        do {
            try {
                System.out.print("Please enter coefficients salary: ");
                do {
                    isDataValid = true;
                    COE = Double.parseDouble(sc.nextLine().trim());
                    if (COE < 1 || COE > 5) {
                        isDataValid = false;
                        System.out.print("-----Erorr! Please coefficients salary again (From 1 to 5): ");
                    }
                } while (!isDataValid);
                setCOE(COE);
                break;
            } catch (NumberFormatException e) {
                System.out.print("-----Erorr! Please coefficients salary again (From 1 to 5): ");
            }
        } while (!isDataValid);
        System.out.println("Save successfully!");
    }

    /**
     * Display the output
     */
    public void output() {
        System.out.printf("|%-12s|%-24s|%-16.0f|%-13.1f|\n", this.getId(), this.getName(), this.getSalary(), this.getCOE());
    }
}