2017-09-29 44 views
-3

這裏是主類(我想建立一個程序,以檢查在庫存產品):得到我的子方法越來越錯誤,我解決不了他們

/** 
* Shows various details of an item. 
* 
* @author Brian Clarke 
* @version 28 sept 2017 
*/ 
public class Item { 
    // instance variables 
    private String id; 
    private String descr; 
    private int quantity; 
    private double price; 
    private double discount; 

    /** 
    * Constructor for objects of class Item 
    */ 
    public Item(String id, String descr, int quantity, double price, double discount) { 
    // initialise instance variables 
    id = id; 
    descr = descr; 
    quantity = quantity; 
    price = price; 
    discount = discount; 
    } 

    /** 
    * Get and set methods for variables. 
    * 
    * 
    * 
    */ 
    public String getid(String id) { 
    return id; 
    } 
    public String getdescr(String descr) { 
    return descr; 
    } 
    public int getquantity(int quantity) { 
    return quantity; 
    } 
    public double getprice(double price) { 
    return price; 
    } 
    public double getdiscount(double discount) { 
    return discount; 
    } 
    public void setid(String id) { 
    this.id = id; 
    } 
    public void setdescr(String descr) { 
    this.descr = descr; 
    } 
    public void setquantity(int quantity) { 
    if (quantity < 0) 
     quantity = 0; 
    this.quantity = quantity; 
    } 
    public void setprice(double price) { 
    if (price < 0) 
     price = 0.0; 
    this.price = price; 
    } 
    public void setdiscount(double discount) { 
    if (discount < 0 || discount > 0.8) 
     discount = 0; 
    this.discount = discount; 
    } 
    public String toString() { 
    return "Item1{" + "ID = " + id + ", description = " + descr + ", quantity = " + quantity + ", price = " + price + ", discount = " + discount + '}'; 
    } 
    public double computeCost() { 
    return (quantity * price - quantity * price * discount); // Normal price minus price by the percentage discount should give the correct discounted price 
    } 
} 

這裏是測試類在那裏我得到的最後5行的錯誤,例如,(方法getidItem類不能被應用到給定的類型,需要:java.lang.String發現:沒有參數的原因:實際的和正式的參數列表的長度不同)。

我不知道如何解決這些問題,並嘗試編輯:

/** 
* Test the class Item 
* The code tests all methods in the class Item 
* @author Brian Clarke 
* @version 28 Sep 2017 
*/ 

public class ItemTest { 
    public static void main(String[] args) { 
    // Create two objects to represent two stock items 
    Item item1 = new Item("ZA423", "Refurbished smartphone", 14, 149.99, 0.3); 
    Item item2 = new Item("BD015", "New 40' TV", 8, 299.99, 0.8); 

    item1.computeCost(); 
    item2.computeCost(); 

    item1.setid("ZA423"); 
    item1.setdescr("Refurbished smarthphone"); 
    item1.setquantity(14); 
    item1.setprice(149.99); 
    item1.setdiscount(0.3); 

    System.out.println(item1.toString()); 

    item2.setid("BD015"); 
    item2.setdescr("New 40' TV"); 
    item2.setquantity(8); 
    item2.setprice(299.99); 
    item2.setdiscount(0.8); 

    System.out.printf("\nItem2 ID is: %s\n", item2.getid()); 
    System.out.printf("\nItem2 Description is: %s\n", item2.getdescr()); 
    System.out.printf("\nQuantity is: %s\n", item2.getquantity()); 
    System.out.printf("\nPrice is: %f\n", item2.getprice()); 
    System.out.printf("\nDiscount is: %f\n", item2.getdiscount()); 
    } 
} 

的代碼是不完整的,但是這是大部分。我搜索了很多,但找不到任何答案。我對Java很新,希望能得到任何幫助。

+6

爲什麼您的獲得者要求調用者通過他們想要獲得的內容?給我打電話,我會給你我的電話號碼。 –

+0

提示:你可以做'System.out.printf( 「項目2 ID爲:」 + item2.getid())',而不是'System.out.printf( 「\ nItem2 ID爲:%S \ n」,ITEM2。 getid());' – ulima69

回答

3

您錯誤地定義getter方法。在你的實現中,你期望爲每個getter方法傳遞一個參數,這沒有多大意義,因爲getter方法是爲了返回一個值。相反

public String getid(String id) { 
    return id; 
} 

你應該做的:

public String getid() { 
    return id; 
} 

刪除所有來自getter方法的參數,它會工作。

UPDATE:

值得一提的

兩個 三件更多的東西:

構造 Item
  • 類是不正確。像id = id;作業將無法正常工作,你應該做的,而不是this.id = id;(你必須明確地說,在左側的id是類和右側的字段id是作爲構造函數參數傳遞的值)
  • 當您使用期望所有領域傳遞,你可以標記所有領域final和刪除setter方法來防止這個類的一個實例,從改變其狀態的構造 - 這是繼
  • 考慮使用駝峯一個很好的規則值得符號。如果您的字段名稱爲id,則getter方法應稱爲getId和setter - setId。這是一個在Java程序中廣泛使用的約定。
+0

感謝您的建議,它修復了錯誤,並且現在不好使用該表示法!我對java相當陌生,但剛剛被教導使用'this.whatever = whatever''。希望更多的時間和練習會有所幫助,並再次感謝。 – Brian

1

你不想輸入一個參數給getters方法,因爲你需要讓id沒有將它設置爲一個新值,否則你將得到相同的值,你輸入的值, t輸入,這就是爲什麼你會得到錯誤(缺少參數)。請寫下此代碼,以便您可以獲得之前已設置的數據:

/** 
* Shows various details of an item. 
* 
* @author Brian Clarke 
* @version 28 sept 2017 
*/ 
public class Item { 
    // instance variables 
    private String id; 
    private String descr; 
    private int quantity; 
    private double price; 
    private double discount; 

    /** 
    * Constructor for objects of class Item 
    */ 
    public Item(String id, String descr, int quantity, double price, double discount) { 
     // initialise instance variables 
     id = id; 
     descr = descr; 
     quantity = quantity; 
     price = price; 
     discount = discount; 
    } 

    /** 
    * Get and set methods for variables. 
    * 
    * 
    * 
    */ 
    public String getid() { 
     return id; 
    } 
    public String getdescr() { 
     return descr; 
    } 
    public int getquantity() { 
     return quantity; 
    } 
    public double getprice() { 
     return price; 
    } 
    public double getdiscount() { 
     return discount; 
    } 
    public void setid(String id) { 
     this.id = id; 
    } 
    public void setdescr(String descr) { 
     this.descr = descr; 
    } 
    public void setquantity(int quantity) { 
     if (quantity < 0) 
      quantity = 0; 
     this.quantity = quantity; 
    } 
    public void setprice(double price) { 
     if (price < 0) 
      price = 0.0; 
     this.price = price; 
    } 
    public void setdiscount(double discount) { 
     if (discount < 0 || discount > 0.8) 
      discount = 0; 
     this.discount = discount; 
    } 
    public String toString() { 
     return "Item1{" + "ID = " + id + ", description = " + descr + ", quantity = " + quantity + ", price = " + price + ", discount = " + discount + '}'; 
    } 
    public double computeCost() { 
     return (quantity * price - quantity * price * discount); // Normal price minus price by the percentage discount should give the correct discounted price 
    } 
} 
相關問題