2016-05-01 40 views
1

我想获得正确的总销售产出,我继续获得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("-----------------------------------"); 
    } 
    } 
} 
+2

你做过任何调试吗? – Blip

回答

1

首先父类的,我没有看到你的领域total_sales正在您的委员会构造函数中初始化。

而且,我不知道你所说的第一和第二个雇员的意思,如果你指的是staff_list[1]staff_list[2]他们是Employee类,以便验证Employee构造被正确初始化,只有这样,我看到他们赢得任何钱是如果他们有一些硬编码号码在Employee.pay()方法返回。