所以,解释我的代码->
我有一个Employee类型的抽象类,它有自己的字段,比如名字。
然后,我拥有从 Employee 扩展而来的 Admin、Sales 和 Manager 类。这些都有自己的字段,例如管理员的固定奖金、销售的百分比奖金和经理的员工数组列表。
然后我有一个包含员工 ArrayList 的驱动程序类,因此它包含管理员、销售和经理。
我的问题出现在 Arraylist 元素上。假设我从 ArrayList 中获得了一个 Admin 类的员工,并尝试使用 getter getFixedBonus() 访问他们的 fixedBonus 字段,但我根本做不到。我怎样才能做到这一点,以便我可以使用特定于类的 getter 和 setter,而不仅仅是继承的 getter 和 setter?这是我的代码的副本,其中出现了问题。具体来说,问题出在 switch 语句的第 5 和第 6 种情况下。
private Employee editAnEmployee(Employee emp) {
boolean exit = false;
boolean validChoice = false;
int choice = 0;
Boolean sure = false; // used in the switch statement
String newName;
double newValue;
Employee admin = new AdminWorker("0", "0", 0, 0, 0); // these are used
Employee sales = new SalesWorker("0", "0", 0, 0, 0, 0); // to check the
Employee manager = new Manager("0", "0", 0, 0); // class of the
// employee
// being sent
// in, to edit
// the fields
// the employee
// may hold
do {
do {
validChoice = true;
emp.toString();
StdOut.println("Which field would you like to edit?");
StdOut.println("1) First Name.");
StdOut.println("2) last Name.");
StdOut.println("3) Hourly rate.");
StdOut.println("4) Hours worked in the last week.");
if (emp.getClass().equals(admin)) {
StdOut.println("5) Fixed Bonus.");
} else if (emp.getClass().equals(sales)) {
StdOut.println("5) Percentage bonus on sales.");
StdOut.println("6) Value of sales made in the last week.");
} else if (emp.getClass().equals(manager)) {
StdOut.println("5) Department.");
}
StdOut.println("0) Exit.");
choice = StdIn.readInt();
if (choice < 0 || choice > 4) {
if (emp.getClass().equals(admin) && choice <= 5) {
validChoice = true;
} else if (emp.getClass().equals(sales) && choice <= 6) {
validChoice = true;
} else if (emp.getClass().equals(manager)) {
validChoice = true;
} else {
StdOut.println("You entered an invalid number! Try Again...");
validChoice = false;
}
}
} while (!validChoice);
switch (choice) {
case 0:
StdOut.println("You are now exiting...");
exit = true;
break;
case 1:
StdOut.println("The employees first name is: " + emp.getFirstName() + ". What would you like the first name to be now?");
newName = StdIn.readString();
StdOut.println("Are you sure you want to change " + emp.getFirstName() + " to " + newName +"?(y/n)");
sure = ynChoice();
if(sure) emp.setFirstName(newName);
break;
case 2:
StdOut.println("The employees last name is: " + emp.getLastName() + ". What would you like the last name to be now?");
newName = StdIn.readString();
StdOut.println("Are you sure you want to change " + emp.getLastName() + " to " + newName +"?(y/n)");
sure = ynChoice();
if(sure) emp.setLastName(newName);
break;
case 3:
StdOut.println("The employees hourly rate is: " + emp.getHourlyRate() + ". What would you like the hourly rate to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getHourlyRate() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHourlyRate(newValue);
break;
case 4:
StdOut.println("The employee has worked: " + emp.getHoursWorked() + " hours in the last week. What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getHoursWorked() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
break;
case 5:
if (emp.getClass().equals(admin)) {
StdOut.println("The employees fixed bonus is: " + emp.getFixedBonus() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getFixedBonus() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
} else if (emp.getClass().equals(sales)) {
StdOut.println("The employees percentage bonus is: " + emp.getPercentageBonus() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getPercentageBonus() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
} else if (emp.getClass().equals(manager)) {
StdOut.println("The employees fixed bonus is: " + emp.getFixedBonus() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getFixedBonus() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
}
break;
case 6:
StdOut.println("The employees sales in the last week are: " + emp.getLastWeeksSales() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getLastWeeksSales() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
break;
}
} while (!exit);
return emp;
}