Java 如何创建无需使用数组即可添加员工的工资表?
这是我的作业说明 实现一个名为Employee的类。一个雇员有一个名字,并得到一份薪水。每个发薪期,每位员工都会获得额外的奖金 在您的解决方案中,包括一个接受员工姓名和工资的构造函数。提供一个接受奖金金额并将其添加到工资中的mutator方法。提供必要的访问器方法以返回姓名、奖金发放前的工资、奖金以及奖金发放后的工资 我已经完成了所有这些,除了当我尝试添加另一个员工时,它只会覆盖变量并只打印最后添加的员工 这是我的密码:Java 如何创建无需使用数组即可添加员工的工资表?,java,Java,这是我的作业说明 实现一个名为Employee的类。一个雇员有一个名字,并得到一份薪水。每个发薪期,每位员工都会获得额外的奖金 在您的解决方案中,包括一个接受员工姓名和工资的构造函数。提供一个接受奖金金额并将其添加到工资中的mutator方法。提供必要的访问器方法以返回姓名、奖金发放前的工资、奖金以及奖金发放后的工资 我已经完成了所有这些,除了当我尝试添加另一个员工时,它只会覆盖变量并只打印最后添加的员工 这是我的密码: package employeetester; import ja
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,但我们不允许使用它。您需要某种数组或集合来存储同一类型的多个实例。在java中使用。作为旁注,我会避免使用Continue作为字符串的名称。continue实际上是Java中用于循环的一个关键字。这本书的底部解释了这一点。