這裏是主類(我想建立一個程序,以檢查在庫存產品):得到我的子方法越來越錯誤,我解決不了他們
/**
* 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行的錯誤,例如,(方法getid
在Item
類不能被應用到給定的類型,需要: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很新,希望能得到任何幫助。
爲什麼您的獲得者要求調用者通過他們想要獲得的內容?給我打電話,我會給你我的電話號碼。 –
提示:你可以做'System.out.printf( 「項目2 ID爲:」 + item2.getid())',而不是'System.out.printf( 「\ nItem2 ID爲:%S \ n」,ITEM2。 getid());' – ulima69