我的程序的目的是显示员工(经理编码器)的工资,具体取决于该员工在一个月内工作了多少小时。包含 3 名员工的数组作为参数转到会计。会计返回每个员工的工资。编码员和经理的薪水以不同的方式计算。
这是我的Java代码:
public class OOP
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects.
Employee[] staff = new Employee[3];
staff[0] = new Manager("Carl Cracker",75000.00,(short)160);
staff[1] = new Coder("Harry Hacker", 50000.00,(short)165);
staff[2] = new Coder("Tony Tester", 40000.00,(short)200);
Accountant Counter = new Accountant();
Counter.printSalary(staff, (byte)2);
}
}
abstract class Employee
{
public Employee(String n, double w, short h)
{
name = n;
wageRate = w;
workedHours = h;
}
public String getName()
{
return name;
}
public double getSalary(short workingHours)
{
this.salary = this.wageRate * (this.workedHours/workingHours);
return salary;
}
private String name;
protected double wageRate;
protected short workedHours;
protected double salary;
}
class Coder extends Employee
{
public Coder (String n, double w, short h)
{
super( n, w, h);
}
public double getSalary(short workingHours)
{
return super.getSalary(workingHours);
}
}
class Manager extends Employee
{
public Manager (String n, double w, short h)
{
super( n, w, h);
}
public double getSalary(short workingHours)
{
if (workingHours > workedHours)
return super.getSalary(workingHours)*0.7;
else
{
salary = wageRate;
return salary;
}
}
}
class Accountant
{
public Accountant ()
{
}
public void printSalary(Employee[] emp, byte monthInd)
{
Employee[] workers = emp;
byte MID = monthInd;
MID = (byte) (MID - 1);
for (Employee worker : workers)
{
System.out.printf("%s's salary this month is %8.2f",worker.getName(), worker.getSalary(workingHours[MID]));
System.out.println();
}
}
private short[] workingHours = new short[]{168,160,160,172,162,154,184,168,168,184,168,172};
}
预期结果:
Carl Cracker 这个月的工资是 75000
Harry Hacker 这个月的工资是 51562.5
Tony Tester 这个月的工资是 50000
实际结果:
Carl Cracker 这个月的薪水是 75000,00
Harry Hacker 这个月的薪水是 50000,00
Tony Tester 这个月的薪水是 40000,00
有人可以解释一下为什么吗?