我在这里有一个工资单程序,我希望能够将对象添加到 Employee 类。目前我定义了与 Employee 类相关的对象 emp。如果我希望能够动态创建对象,直到我在脚本中输入 STOP 命令。此外,一旦我完成了如何打印与 Employee 类相关的所有对象。提前致谢。
import java.util.Scanner;
class PayrollProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();
while (!emp.name.equals("STOP"))
{
Employee emp = new Employee();
System.out.print("Employee's Name: ");
emp.name = scan.next();
if(emp.name.equals("STOP"))
{
System.out.printf("The Application is STOPPING......");
break;
}
System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
while (emp.wage < 0)
{
System.out.printf("Please Enter a Positive Number! \n");
System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
}
System.out.print("Hours Worked in Week: ");
emp.hours = scan.nextDouble();
while (emp.hours < 0)
{
System.out.printf("Please Enter a Positive Number! \n");
System.out.print("Hours Worked in Week: ");
emp.hours = scan.nextDouble();
}
employees.add(emp);
emp.printEmployee();
}
for(Employee emp : employees)
{
System.outprintln(emp.name);
}
}
}