2010-12-01 24 views
0

在填寫下面的Java類的任何援助將不勝感激 -編寫Java getBonusPercentage()方法返回一個僱員的獎金加工資

在一個固定的每小時收費按小時收費永久員工。 他們還可能會或可能不會有資格獲得獎金 -

,如果他們沒有資格那麼他們的獎金比例應設置爲0 如果他們不符合他們的獎金必須大於零,但恐怕比5 如果輸入的百分比值不正確,應將百分比設置爲零,並打印錯誤消息。 獎金是根據他們的薪水計算並添加到其中。

這是我迄今(延伸從僱員超) -

public class PermanentEmployee extends Employee 
{ 

private double PermanentEmployeeBonus; 

public PermanentEmployee(String firstName, String lastName, double hourlyRate, double PermanentEmployeeBonus) 
{ 

    super(firstName, lastName, hourlyRate); 

    setPermanentEmployeeBonus(PermanentEmployeeBonus); 


} 

public double getPermanentEmployeeBonus() 
{ 

    return PermanentEmployeeBonus; 

} 

public void setPermanentEmployeeBonus(double PermanentEmployeeBonus) 
{ 

    //If the user input is valid, update the managerial bonus with the newly inputted value. 
    if(PermanentEmployeeBonus > 0) 
    { 

    this.PermanentEmployeeBonus = PermanentEmployeeBonus; 

    } 
    //Otherwise prevent a managerial bonus greater than zero being overwritten 
    else if(PermanentEmployeeBonus <= 0) 
    { 

    if(PermanentEmployeeBonus <= 0) 
    { 

    this.PermanentEmployeeBonus = 0; 

    } 

    super.decorateConsole(); 

    //Alert the user to their mistake. 
    System.out.println("Error ! ! ! - An attempt to set the employee " + super.getFirstName() + " " + super.getLastName() + "'s permanent employee bonus to zero was detected.\n"); 

    super.decorateConsole(); 

    } 

} 

public void printState() 
{ 

    super.printState(); 

    System.out.println("[PERMANENT EMPLOYEE BONUS] for " +super.getFirstName() + " " + super.getLastName() + " = " + PermanentEmployeeBonus + "\n"); 

    super.decorateConsole(); 

} 


} 
+0

好的,有什麼問題?該代碼似乎幾乎是正確的。 – 2010-12-01 12:27:26

回答

0

你需要做的(我相信)設置基於您調用超級獎金時薪。

super(firstname, lastname, (hourlyrate * (1 + ((PermanentEmployeeBonus<0) ? 0 : PermanentEmployeeBonus)/100.0))); 

此調用根據獎金調整小時率。該調用有點難看,因爲檢查了對PermanentEmployeeBonus的錯誤輸入。

如果(建設後)您撥打setPermanentEmployeeBonus,則會出現問題。你如何將這些信息提供給基類​​?在基類中創建一個setHourlyRate,並在調用setPermanentEmployeeBonus時對其進行更改。

0

最明顯的問題是,你沒有檢查5的上限。除此之外,目前還不清楚你想要它做什麼,它沒有做。具體一點 - 你對這段代碼做了什麼樣的調用,你得到了什麼結果,以及你期望得到什麼結果?

目前還不清楚你的「規格」,一旦設定了獎金百分比,你想要做什麼。正如Starkey所描述的,一種選擇是增加小時費率以包括獎勵。在真實的系統中,這對我來說似乎並不明智,但也許這就是你所期待的(我猜這是作業)。

我期望一個更真實的實現將有一個計算一段時間內的總工資的步驟,這將使小時乘以小時費率,然後增加總額的獎金百分比。但是,從規格來看,你還不清楚你想要做什麼。