2016-05-01 7 views
1

私は適切な総売上を得るために努力しています.1番目と2番目の従業員に適切な400と950の代わりに0.0を取得し続けます。スーパーと上書きを使用して情報を出力する

私は私の問題は、上書きされた有料法と欧州委員会のクラスであると思い

そのに追加労働時間のために賃金を計算するために毎時から親クラスから有料メソッドを呼び出す必要があります委員会でオーバーライドされた給与方法販売手数料からの払い(合計販売回数と手数料率)支払いが計算された後、合計売上は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; 
    } 

} 

時報、すべての情報は、すべての

// ******************************************************************** 
// 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

最初に保存されている有料法

// ******************************************************************** 
// 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; 
    } 
} 

やスタッフと親クラス、私はあなたのフィールドが表示されませんtotal_salesが委員会のコンストラクタで初期化されています。

staff_list[1]staff_list[2]を参照している場合は、Employeeクラスであるため、Employeeコンストラクタが正しく初期化されていることを確認してください。お金は、ハードコードされた番号がEmployee.pay()メソッドで返されている場合です。

関連する問題