code

no
 avatar
unknown
java
4 years ago
4.8 kB
5
Indexable
package employee_nv;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Employee {
    //các thuộc tính
    public String id;           // mã nhân viên
    public String fullName;     // tenn đầy đủ
    public String address;      // địa chỉ
    public int age;             // tuổi
    public String phone;        // số điện thoại
    public float salary;        // lương
    public float experience;    // năm kinh nghiệm

    //khởi tại mặc định
    public Employee() {
        id = "";
        fullName = "";
        address = "";
        age = 0;
        phone = "";
        salary = 0;
        experience = 0;
    }

    //phương thức tạo 1 tham số
    public Employee(String id) {
        try {
            setId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(id);
    }

    //phương thức tạo 2 tham số
    public Employee(String id, String fullName) {
        this(id);
        this.fullName = fullName;
//        System.out.println(fullName);
    }

    //phương thức tạo 3 tham số
    public Employee(String id, String fullName, String address)  {
        this(id, fullName);
        this.address = address;
//        System.out.println(address);
    }

    //phương thức tạo 4 tham số
    public Employee(String id, String fullName,
                    String address, int age) {
        this(id, fullName, address);
        this.age = age;
//        System.out.println(age);
    }

    //phương thức tạo 5 tham số
    public Employee(String id, String fullName,
                    String address, int age, String phone)  {
        this(id, fullName, address, age);
        this.phone = phone;
//        System.out.println(phone);
    }

    //phương thức tạo 6 tham số
    public Employee(String id, String fullName,
                    String address, int age,
                    String phone, float salary) {
        this(id, fullName, address, age, phone);
        this.salary = salary;
//        System.out.println(salary);
    }

    //phương thức tạo 7 tham số
    public Employee(String id, String fullName,
                    String address, int age, String phone,
                    float salary, float experience)  {
        this(id, fullName, address, age, phone, salary);
        this.experience = experience;
//        System.out.println(experience);
    }

    //phương thức hành động làm việc
    public void work(String job) {
        System.out.println(fullName + " đang làm nhiệm vụ " + job);
    }

    //phương thức thể hiện hành động nghỉ ngơi
    public void relax() {
        System.out.println(fullName + " đang nghỉ ngơi.");
    }

    //phương thức thể hiện hành động nhận lương

    public void receiveSalary(float amount) {
        salary = amount;
        System.out.println(fullName + " đã nhận lương tháng là " + salary);
    }

    //phương thức thể hiện hành động đi du lịch
    public void travel(String someWhere) {
        System.out.println(fullName + " đang ở " + someWhere);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) throws InvalidIdException {
        var regex = "^(NV|EMP)\\d{6}[a-z]{2,5}";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(id);
        if(matcher.matches())
            this.id = id.toUpperCase();
        else {
            var msg = "Ma nv k hop le: " + id;
            this.id = "NV000000az";
            throw new InvalidIdException(msg, id);
        }
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    public float getExperience() {
        return experience;
    }

    public void setExperience(float experience) {
        this.experience = experience;
    }
}

Editor is loading...