我正在实施一个薪资系统,我需要在其中实施一个PayrollSystem
类,将员工添加到ArrayList
个员工,并为每个所述员工创建支票。我已经写了Employee
和Paycheck
个类,但是我遇到了PayrollSystem
类的问题。
如何创建addEmployee
方法?我是否将Employee
对象与该员工的信息一起传递给它,还是有另一种方式?
员工:
package payrollSystem;
public class Employee {
private String firstName;
private String lastName;
private int ID;
private double hourlyWage;
private double hoursWorked;
public Employee(String first, String last, int id, double wage, double hours) {
firstName = first;
lastName = last;
ID = id;
hourlyWage = wage;
hoursWorked = hours;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String last) {
this.lastName = last;
}
public String getLastName() {
return lastName;
}
public void setID(int ID) {
this.ID = ID;
}
public int getID() {
return ID;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHoursWorked() {
return hoursWorked;
}
public double calcPay(double wage, double hours) {
wage = getHourlyWage();
hours = getHoursWorked();
return wage * hours;
}
}
PAYCHECK:
package payrollSystem;
public class PayCheck {
private String firstName;
private String lastName;
private int ID;
private double netAmount;
public PayCheck(String first, String last, int id, double wage, double hours) {
firstName = first;
lastName = last;
ID = id;
netAmount = wage * hours;
}
public String toString() {
return "Paycheck issued for " + netAmount + "to " + firstName + ", "+ lastName + ", employee ID " + ID;
}
}
PayrollSystem:
package payrollSystem;
import java.util.List;
import java.util.ArrayList;
public class PayrollSystem {
public List<Employee> employees = new ArrayList<Employee>();
public String companyName;
PayrollSystem(String company) {
companyName = company;
}
void addEmployee(Employee a) {
employees.add(a);
}
void getHoursWorked(double hrs) {
this.a.getHoursWorked();
}
void issueCheck() {
double checkAmount = this.a.calcPay(a.getHoursWorked(), a.getHourlyWage());
PayCheck check = new PayCheck(a.getFirstName(), a.getLastName(), a.getID(), a.getHoursWorked(), a.getHourlyWage());
check.toString();
}
}
答案 0 :(得分:1)
您可以制作addEmployee()
方法。只要在某处创建员工,然后将其传递到payRoll类。
看起来像这样,假设你有一个List
员工:
public void addEmployee(Employee employee){
employees.add(employee);
}
在你的main
中你可以去(假设myEmployee是一个雇员对象):
PayrollSystem payrollSystem = new PayRollSystem();
payrollSystem.addEmployee(new Employee("John", "Smith", 1, wage, hours)); // Way 1
payrollSystem.addEmployee(myEmployee); // Way 2
Employee对象将包含Employee的所有信息。因此,当传入该Object时,所有信息都将随附。这允许您从payrollSystem中获取所有Employees,或使用内部方法对它们执行操作。