2016-05-01 38 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()方法返回。