[英]How to create method for withdrawal from store and deliverance to store
我自己在學習Java,我想自己做運動方面的幫助。 該類稱為產品,用於表示小公司銷售的產品。
應該可以存儲有關每種產品的以下信息。 該類應具有以下方法:
請注意,如果其中一種方法更改了訂購點以下的存儲項目,則應打印一條消息。 負數的物品也應該是不可能的。
我的方法有問題。 請仔細閱讀我的代碼並給我一些提示。 我將評估所有回應。 謝謝。
這是我的程序:
public class Product {
private int productNumber;
private String productName;
private float price;
private int orderPoint;
private int unitsInStore;
private String proDescription;
public Product(int num, String name, float price, int order, int units, String description){
this.productNumber = num;
this.productName = name;
this.price = price;
this.orderPoint = order;
this.unitsInStore = units;
this.proDescription = description;
}
public int getProductNumber() {
return productNumber;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getOrderPoint() {
return orderPoint;
}
public void setOrderPoint(int orderPoint) {
this.orderPoint = orderPoint;
}
// a method returns the units in store
public int getUnitsInStore() {
return unitsInStore;
}
public void setUnitsInStore(int unitsInStore) {
this.unitsInStore = unitsInStore;
}
public String getProDescription() {
return proDescription;
}
public void setProDescription(String proDescription) {
this.proDescription = proDescription;
}
public int deliveranceToStore(int store){
unitsInStore = unitsInStore + store;
return unitsInStore ++ ;
}
public int withdrawal(int store){
unitsInStore = store - unitsInStore;
return unitsInStore --;
}
}
deliveranceToStore
方法不正確。 為什么要遞歸調用該方法?
該方法可以簡單地是:
public int deliveranceToStore(int store) {
unitsInStore = unitsInStore + store;
return unitsInStore;
}
如果不需要通過此調用返回存儲的單元數,則應將返回類型設置為void
(即,如果更新計數已足夠):
public void deliveranceToStore(int store) {
unitsInStore = unitsInStore + store;
}
要提款,您需要使用類似的策略來更新unitsInStore
:
public void withdrawal(int units) {
if(unitsInStore - units >= 0) {
unitsInStore = unitsInStore - units;
} else {
System.out.println("Unable to withdraw. Insufficient units in store.");
}
}
您還可以使withdrawal
方法返回一個boolean
,該boolean
指示提款操作是否成功。 在這種情況下,該方法可能類似於:
public boolean withdrawal(int units) {
if(unitsInStore - units >= 0) {
unitsInStore = unitsInStore - units;
return true;
} else {
System.out.println("Unable to withdraw. Insufficient units in store.");
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.