我正在努力获得正确的总销售额,我继续为第一名和第二名员工获得0.0而不是相应的400和950。
我认为我的问题是在委员会班级中使用覆盖的付费方式
佣金中的重写付款方式必须从每小时的父类调用付费方式来计算工作小时数的工资,然后将佣金增加到销售佣金(总销售额乘以佣金率)总销售额应该是计算付款后重置为0。 -
public class Commission extends Hourly
{
double total_sales;
double commission_rate;
public Commission(String name, String address, String phone,
String soc_sec_number, double rate,double commission_rate)
{
super( name, address, phone,
soc_sec_number, rate);
// set commission rate
commission_rate = 0.02;
}
public void addSales (double sales)
{
total_sales += sales;
}
public double pay()
{
double payment = super.pay(); // call the method of the parent
// add other code
payment = payment + (total_sales * commission_rate);
total_sales = 0.0;
return payment;
}
public String toString()
{
return super.toString() + "\nTotalSales: " + total_sales;
}
}
每小时,使用付费方式的父类
// ********************************************************************
// Hourly.java Java Foundations
//
// Represents an employee that gets paid by the hour
// ********************************************************************
public class Hourly extends Employee
{
private int hours_worked;
// -------------------------------------------------------------------------
// Constructor: Sets up this hourly employee using the specified information
// -------------------------------------------------------------------------
public Hourly(String name, String address, String phone,
String soc_sec_number, double rate)
{
super(name, address, phone, soc_sec_number, rate);
hours_worked = 0;
}
// -----------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours
// -----------------------------------------------------
public void addHours(int more_hours)
{
hours_worked += more_hours;
}
// -----------------------------------------------------
// Computes and returns the pay for this hourly employee
// -----------------------------------------------------
public double pay()
{
double payment = pay_rate * hours_worked;
hours_worked = 0;
return payment;
}
// ----------------------------------------------------------
// Returns information about this hourly employee as a string
// ----------------------------------------------------------
public String toString()
{
return super.toString() + "\nCurrent hours: " + hours_worked;
}
}
和存储所有信息的工作人员
// ********************************************************************
// Staff.java Java Foundations
//
// Represents the personnel staff of a particular business
// ********************************************************************
import java.text.DecimalFormat;
public class Staff
{
private static DecimalFormat fmt = new DecimalFormat("0.00");
private StaffMember[] staff_list =
{
new Executive ("Tony", "123 Main Line", "555-0469", "123-45-6789", 2423.07),
new Employee ("Paulie", "456 Off Line", "555-0101", "987-65-4321", 1246.15),
new Employee ("Vito", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23),
new Hourly ("Michael", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55),
new Volunteer ("Adrianna", "987 Babe Blvd.", "555-8374"),
new Volunteer ("Benny", "321 Dud Lane", "555-7282"),
new Commission("Christopher", "345 Movie Lane", "555-3831", "302-48-3871", 6.25, 0.2),
new Commission("Bobby", "61 Train St.", "555-2869", "492-58-2956", 9.75, 0.15)
};
// ----------------------------------
// Constructor: Updates staff members
// ----------------------------------
public Staff()
{
((Executive)staff_list[0]).awardBonus(500.00);
((Hourly)staff_list[3]).addHours(40);
((Commission)staff_list[6]).addHours(35); // add commissioned employees
((Commission)staff_list[7]).addHours(40);
}
// ----------------------
// Pays all staff members
// ----------------------
public void payday()
{
double amount;
for (int count=0; count < staff_list.length; count++)
{
System.out.println(staff_list[count]);
amount = staff_list[count].pay();
if (amount == 0.0)
System.out.println("Thanks!");
else
System.out.println("Paid: " + fmt.format(amount));
System.out.println("-----------------------------------");
}
}
}
答案 0 :(得分:1)
首先,我没有在您的委员会构造函数中看到您的字段total_sales
被初始化。
另外,我不确定第一和第二名员工的意思,如果您指的是staff_list[1]
和staff_list[2]
他们是Employee类,那么请验证Employee构造函数是否正确初始化,我认为他们赚钱的唯一方法是,如果他们在Employee.pay()
方法中返回了一些硬编码的数字。