EmployeeTest/EmployeeTest.java

xkfx發表於2024-09-22

Employee.java

import java.time.LocalDate;

public class Employee {
    // 3個欄位,用來存放將要操作的資料
    private String name;
    private double salary;
    private LocalDate hireDay;

    // 透過Generate...生成後修改
    public Employee(String name, double salary, int year, int month, int day) {
        this.name = name;
        this.salary = salary;
        hireDay = LocalDate.of(year, month, day);
    }

    // 透過Generate...生成即可
    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDay() {
        return hireDay;
    }

    // 自己寫
    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent / 100;
        salary += raise;
    }
}

EmployeeTest.java

public class EmployeeTest {
    public static void main(String[] args) {
        Employee[] staff = new Employee[3];

        staff[0] = new Employee("張三", 1000, 2001, 3, 15);
        staff[1] = new Employee("李四", 2500, 2000, 9, 18);
        staff[2] = new Employee("王五", 6000, 2003, 1, 1);

        for (Employee e : staff) {
            e.raiseSalary(5);

            String tmp = "name=" + e.getName() +
                    ", salary=" + e.getSalary() +
                    ", hireDay=" + e.getHireDay();
            System.out.println(tmp);
        }
    }
}

輸出:

name=張三, salary=1050.0, hireDay=2001-03-15
name=李四, salary=2625.0, hireDay=2000-09-18
name=王五, salary=6300.0, hireDay=2003-01-01