簡體   English   中英

如何創建從商店提款並交付到商店的方法

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM