以下是我的作业说明
“实施一个名为Employee的班级。员工有一个名字并获得薪水。每个员工的每个工资期都会获得额外的奖金。
在您的解决方案中,包含一个接受员工姓名和薪水的构造函数。提供一个接受奖金金额的mutator方法并将其添加到工资中。提供必要的访问者方法以返回姓名,奖金申请前的工资,奖金以及奖金申请后的工资。“
我完成了所有这些工作,但是当我尝试添加另一名员工时,它只会覆盖变量并仅打印最后添加的员工。
继承我的代码:
package employeetester;
import java.util.Scanner;
public class EmployeeTester {
public static void main(String[] args) {
String Employee = "";
String Continue;
String printChart;
double salary = 0;
double bonus = 0;
double totalSalary = 0;
Scanner user_input = new Scanner( System.in );
EmployeePayRoll epr = new EmployeePayRoll(Employee, salary);
System.out.print("Would you like to add an employee to the pay roll? ");
Continue = user_input.next();
while (Continue.equalsIgnoreCase("yes") || Continue.equalsIgnoreCase("y")){
System.out.println("What is the name of the employee?");
Employee = user_input.next();
epr.Employee(Employee);
System.out.println("\nWhat is the salary of " + Employee + "? ");
salary = user_input.nextDouble();
epr.salary(salary);
System.out.println("\nWhat is the bonus of " + Employee + "? ");
bonus = user_input.nextDouble();
epr.bonus(bonus);
System.out.print("Would you like to add an employee to the pay roll? ");
Continue = user_input.next();
}
System.out.println("\nWould you like to print out the pay roll chart? ");
printChart = user_input.next();
if (printChart.equalsIgnoreCase("Yes") || printChart.equalsIgnoreCase("y")){
//EmployeePayRoll epr = new EmployeePayRoll(Employee, salary);
epr.totalSalary(bonus);
epr.printChart();
}
}
}
这是第二部分
package employeetester;
public class EmployeePayRoll {
private String Employee;
private double Salary;
private double bonus;
private double TotalSalary;
public EmployeePayRoll (String Employee, double salary){
this.Employee = Employee;
this.Salary = salary;
}
public void Employee(String emp){
Employee = emp;
}
public void salary(double slry){
Salary = slry;
}
public void bonus(double bnus){
bonus = bnus;
}
public double totalSalary(double bonus){
this.bonus = bonus;
double totalSalary = Salary + bonus;
this.TotalSalary = totalSalary;
return totalSalary;
}
public void printChart(){
System.out.println("\nPayRoll");
System.out.println("-------- EMPLOYEE --------");
System.out.println("Name: \t" + Employee);
System.out.println("Salary: \t" + Salary);
System.out.println("Bonus: \t" + bonus);
System.out.println("Total: \t" + TotalSalary);
System.out.println();
}
}
如何打印出添加的每位员工?我唯一能想到的是使用arraylist,但我们不允许使用它。