0

私は独学で 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 --;
}
    }
4

1 に答える 1

1

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。これは、撤退アクションが成功したかどうかを示します。その場合、メソッドは次のようになります。

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;
    }
}
于 2013-08-18T11:10:02.527 に答える