Java 如何创建从存储中提取并交付到存储的方法

Java 如何创建从存储中提取并交付到存储的方法,java,Java,我正在自学java,我想在我自己做的练习上得到帮助。 该类称为Product,用于表示小公司销售的产品 应该可以存储关于每个产品的以下信息。 该类应具有以下方法: 构造器 返回存储中项目单位的方法 向商店发货的方法(增加此产品的单位) 从商店取货的方法(减少该产品的单位) 请注意,如果其中一种方法更改订单点下方的存储项,则应打印一条消息。项目数量也不可能为负数 我在方法上有问题。请看一下我的代码并给我一些提示。我将感谢所有回复。 多谢各位 这是我的节目: public class Pro

我正在自学java,我想在我自己做的练习上得到帮助。 该类称为Product,用于表示小公司销售的产品

应该可以存储关于每个产品的以下信息。 该类应具有以下方法:

  • 构造器
  • 返回存储中项目单位的方法
  • 向商店发货的方法(增加此产品的单位)
  • 从商店取货的方法(减少该产品的单位)
请注意,如果其中一种方法更改订单点下方的存储项,则应打印一条消息。项目数量也不可能为负数

我在方法上有问题。请看一下我的代码并给我一些提示。我将感谢所有回复。 多谢各位

这是我的节目:

  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 --;
}
    }

deliverancetore
方法不正确。为什么要递归调用该方法

该方法可以是:

public int deliveranceToStore(int store) {
    unitsInStore = unitsInStore + store;
    return unitsInStore;
}
如果不需要随此调用返回存储的单位数,则返回类型应为
void
(即,如果更新计数足够):

对于取款,您需要更新
unitsInStore
的类似策略:

public void withdrawal(int units) {
    if(unitsInStore - units >= 0) {
        unitsInStore = unitsInStore - units;
    } else {
        System.out.println("Unable to withdraw. Insufficient units in store.");
    }
}
您还可以使
撤回
方法返回一个
布尔值
,该值指示撤回操作是否成功。在这种情况下,该方法可能如下所示:

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;
    }
}

你的方法有什么问题?为什么不简单地为你创建这样做的方法呢?您不熟悉Java中的方法吗?是的,我不擅长创建方法,这就是我的问题:(谢谢。如果我想创建从存储中提取的方法,对吗?我觉得我做错了什么。你能帮我检查一下吗。'public int-drawing(int-store){return-store-unitsInStore;}“@BraveResponsion更新了我的答案。非常感谢@Nishant Shresh我如何才能更充分地学习java?可以给我一些建议吗?”?
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;
    }
}