2017-03-05 13 views
0

私は、私のShoeLibraryクラスの下にあるarraylistを持っています。また、この配列リストの変数にsetterとgetterを持つShoeという別のクラスがあります。私MainActivityのGUIクラスでShoeLibraryクラス配列リストの値をユーザー入力から変更するにはどうすればよいですか?

public class ShoeLibrary { 

private ArrayList<Shoe> shoes; 

public ShoeLibrary() { 
    shoes = new ArrayList<Shoe>(); 
    shoes.add(new Shoe("Shoe 1", 100)); // the integer represents stock 
    shoes.add(new Shoe("Shoe 2", 200)); 
    shoes.add(new Shoe("Shoe 3", 300)); 
} 

iは整数値のユーザー入力を受け取り、入力ダイアログを持ち、その後、バスケットに追加します。

ユーザーがこの値を入力したときに配列リストの番号(在庫)を更新する方法が必要です。どうしたらいい?

+0

「靴1」はタイプ、100は数量ですか?そうであれば、リストから靴を入手し、靴のクラスでセッターを使用して数量を変更したいとします。これがあなたが話していることであれば、私はコードを助けることができます。 –

+0

@ChrisSharp yes thats correct – user982467

+0

以下の回答が役に立ちましたか、間違った質問に回答しましたか? –

答えて

0

私は自分のコメントが正しいと仮定し、これがどのように行われたかを示す基本的なコードを書いています。状況に合わせてこれを変更し、GUIから顧客データを取得する必要があります。エラーチェックも必要です。

public class Sandbox { //opens class 

    public static void main(String[] args) { 
     ArrayList<Shoe>shoes = new ArrayList<Shoe>(); 
     shoes.add(new Shoe("Shoe 1", 100)); // the integer represents stock 
     shoes.add(new Shoe("Shoe 2", 200)); 
     shoes.add(new Shoe("Shoe 3", 300)); 
     Shoe temp; 
     String shoeSelected = "Shoe 3"; // you need to use the customer's input here 
     int numSeleted = 20; // again, you need this data from the customer input 
     for (int i = 0; i < shoes.size(); i++) { 
      temp = shoes.get(i); 
      if(temp.name == shoeSelected) { 
       shoes.get(i).setQuantity(temp.quantity - numSeleted); 
       System.out.println(shoes.get(i).name); 
       System.out.println(shoes.get(i).quantity); 
      } 
     } 
     System.out.println("wait"); 
    } 

} 

class Shoe { 
    String name; 
    int quantity; 

    public Shoe(String name, int quantity) { 
     this.name = name; 
     this.quantity = quantity; 
    } 

    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 
} 
+0

arraylistとコードをmainメソッドに入れたのはなぜですか? – user982467

+0

あなたのクラスがどのようにセットアップされているかわからないので、私はあなたのGUIインターフェースを持っています。私はそれを置くので、リストの項目を修正する方法をあなたに示すことができます。通話とリストはどこから来てもかまいません。 –

関連する問題