一意識別子(ID)を持つclass product
のインスタンスを作成しようとしています。インスタンスに一意のIDを設定する方法
IDは店舗の商品のバーコードのように動作し、ユーザーは商品を追加した後にIDを変更することがあります。
これは私がこれまで持っているものです。
public class Product {
private int id;
//and some other attributes...
public Product (int id){
this.id = id;
}
public void setId(){
this.Id = id;
}
//more not relevant methods...
}
私はすべて作成の製品にこのようなものが含まれますクラスを作成することを考えた:
public class Inventory{
ArrayList<Product> products;
//not sure if I should use product array, or ID's array
public Producto createProduct(int id){
if (products.contains(/* product with id*/)){
// not sure what to use here
}
else{
return new Producto(id);
}
}
}
ので、私はわからないんだけどどのように動作させるか、またはclass Inventory
が良い考えであるかどうかを確認してください。
ところで:悪い英語のため申し訳ありませんが、いないネイティブスピーカー
「Inventory」のインスタンスに 'Product'のインスタンスを追加するメソッドを追加できます。このメソッドは 'Product'のコンストラクタで呼び出されます。まだ完了していなければ 'Product'クラスに' getter() 'を書くことをお勧めします。 – Badda
ありがとうございます。 @Badda はい、私はすでにゲッターを作っています。 –