employee

 avatar
unknown
plain_text
2 years ago
4.8 kB
3
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 Employee {

    private static final String VALID_ID = "^NV(?!0000)\\d{4}$";
    String id;
    String name;
    double salary;
    double COE;

    Scanner sc = new Scanner(System.in);
    ArrayList<Employee> employee = new ArrayList<>();

    /**
     * Create new employee
     *
     * @param id ID of employee
     * @param name Name of employee
     * @param salary Salary of employee
     * @param COE Coefficients of salary
     */
    public Employee(String id, String name, double salary, double COE) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.COE = COE;
    }

    /**
     * Create a constructor
     */
    public Employee() {
    }

    /**
     * Get Id
     *
     * @return
     */
    public String getId() {
        return id;
    }

    /**
     * Set Id
     *
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Get name
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * Set name
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Get salary
     *
     * @return
     */
    public double getSalary() {
        return salary;
    }

    /**
     * Set salary
     *
     * @param salary
     */
    public void setSalary(double salary) {
        this.salary = salary;
    }

    /**
     * Get COE
     *
     * @return
     */
    public double getCOE() {
        return COE;
    }

    /**
     * Set COE
     *
     * @param COE
     */
    public void setCOE(double COE) {
        this.COE = COE;
    }

    /**
     * Calculate salary
     *
     * @return
     */
    public double calSalary() {
        return getSalary() * getCOE();
    }

    /**
     * Input value ID
     */
    public void inputID() {
        System.out.print("Please enter ID number: ");
        do {
            try {
                id = sc.nextLine();
                //ID must include 6 characters, the 2 first character is "NV", characters remaining are number.
                if (!id.matches(VALID_ID)) {
                    System.out.print("--------Erorr! Please ID number again (NVxxxx): ");
                }
            } catch (Exception e) {
                System.out.print("--------Erorr! Please ID number again (NVxxxx): ");
            }
        } while (!id.matches(VALID_ID));
    }

    /**
     * Input value name, salary, COE
     */
    public void input() {
        //Validate the input. The name can't be empty.
        System.out.print("Please enter name: ");
        do {
            try {
                name = sc.nextLine();
                if (name.trim().equals("")) {
                    System.out.print("--------Erorr! Please name again (Not Null): ");
                }
            } catch (Exception e) {
                System.out.print("--------Erorr! Please name again (Not Null): ");
            }
        } while (name.trim().equals(""));

        System.out.print("Please enter salary: ");
        //Validate the input. The salary must be great than 100.
        do {
            try {
                salary = Double.parseDouble(sc.nextLine());
                if (salary <= 100) {
                    System.out.print("--------Erorr! Please salary again (Salary > 100 USD): ");
                }
            } catch (NumberFormatException e) {
                System.out.print("--------Erorr! Please salary again (Salary > 100 USD): ");
            }
        } while (salary <= 100);

        System.out.print("Please enter coefficients salary: ");
        do {
            try {
                COE = Double.parseDouble(sc.nextLine());
                if (COE < 1 || COE > 5) {
                    System.out.print("--------Erorr! Please coefficients salary again (From 1 to 5): ");
                }
            } catch (NumberFormatException e) {
                System.out.print("--------Erorr! Please coefficients salary again (From 1 to 5): ");
            }
        } while (COE < 1 || COE > 5);
        System.out.println("Add Successfull");
    }

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

}
Editor is loading...