2016-03-04 47 views
-4

我目前正在製作拖拉機租賃計劃,迄今爲止我有一個測試拖拉機和拖拉機班的主要方法。首先,我將張貼拖拉機類,比我會發布的主要方法:從我的程序獲取意想不到的輸出

拖拉機類:

class Tractor 
{ 
    private int RentalRate; 
    private int RentalDays; 
    private int VehicleID; 
    private int RentalProfit; 

    public void setRentalRate(int r) 
    { 
     if (r > 0) { 
      this.RentalDays = r; 
     } else { 
      System.out.println("Error: bad RentalRate!"); 
     } 
     RentalDays = r; 
    } 

    public int getRentalRate() 
    { 
     return this.RentalRate; 
    } 

    public void setVehicleID(int v) 
    { 
     if (v > 0) { 
      this.VehicleID = v; 
     } else { 
      System.out.println("Error: bad VehicleID!"); 
     } 
     RentalDays = v;  
    } 

    public int getVehicleID() 
    { 
     return this.VehicleID; 
    } 

    public void setRentalDays(int d) 
    { 
     if (d > 0) { 
      this.RentalDays = d; 
     } else { 
      System.out.println("Error: bad Rental Days!"); 
     } 
     RentalDays = d;  
    } 

    public int getRentalDays() 
    { 
     return this.RentalDays; 
    } 

    public int RentalProfit(int RentalRate, int RentalDays) 
    { 
     RentalProfit = RentalRate * RentalDays; 
     return this.RentalProfit; 
    } 

    //Tractor(int RD, int RR, int RP, int VID) 
    //{ 
     //RentalDays = RD; 
     //RentalRate = RR; 
     //RentalProfit = RP; 
     //VehicleID = VID; 

    //} 


    @Override 
    public String toString() { 
     return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate + 
     ", Rental profit = " + RentalProfit + ", VehicleID = " + VehicleID + ")"; 
    } 

主要方法:

public static void main(String[] args){ 
      Tractor tractor; 
      tractor = new Tractor(); 
      tractor.setRentalRate(9); 
      tractor.setRentalDays(45); 
      tractor.setVehicleID(9145949); 
      System.out.println(tractor.toString()); 

看到輸出的節目是給我下面,我我多次瀏覽了我的代碼,但無法弄清楚爲什麼會這樣。

Tractor (RentalDays = 9145949, Rental Rate = 0, Rental profit = 0, VehicleID 9145949) 
+0

有蹊蹺的是'RentalProfit'方法的設計:它基於這樣不一定匹配類的字段的字段值計算值(但你可以使用字段值並刪除方法參數)。你也計算和存儲這裏的值,除了toString方法外,從來不會使用它,這是由於各種原因造成的:這種方法類似於getter方法,但其他的toString依賴於被調用的方法(這不一定是這種情況) ;因此該領域的價值可能不是最新的。 – fabian

+0

劃傷,我想出來感謝您的幫助,我不得不將它設置爲一套和獲得方法比添加tractor.setRentalProfit();在我的主要方法 – ried

+0

事情是不應該有一個設置方法,而不是一個領域。使用getter(每次根據其他字段的值重新計算該值)替換該字段的所有用法。這樣的價值永遠是最新的。另一種方法是保留現場並添加一個'recalculateRentalProfit'方法,並在每次出租利潤所依賴的值被設置時(在設置者中)調用它。 – fabian

回答

0

我假設你提到的「奇怪的輸出」是租賃天數等於車輛ID?如果是這樣,請參見下文。

public void setVehicleID(int v) 
{ 
    if (v > 0) { 
     this.VehicleID = v; 
    } else { 
     System.out.println("Error: bad VehicleID!"); 
    } 
    *****RentalDays = v;***** <-- This is why.  
} 
+0

哇,你們人很快! –

1

您的setRentalRate方法不正確。在這種方法中,您設置'RentalDays'而不是'RentalRate'。

public void setRentalRate(int r) 
{ 
    if (r > 0) { 
     this.RentalDays = r; // Should be this.RentalRate = r; 
    } else { 
     System.out.println("Error: bad RentalRate!"); 
    } 
    RentalDays = r; // Should be RentalRate = r; 
} 

另外,我很困惑,爲什麼你在if-else之後設置了一個額外的時間。我想應該是這樣的:

public void setRentalRate(int r) 
{ 
    if (r > 0) { 
     this.RentalRate = r; 
    } else { 
     System.out.println("Error: bad RentalRate!"); 
     RentalRate = 0; 
    } 
}