2017-03-05 4 views
0

나는 아래 ShawLibrary 클래스에있는 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은 수량입니까? 그렇다면 목록에서 신발을 가져 와서 Shoe 클래스의 setter를 사용하여 수량을 변경하고 싶습니다. 이것이 바로 당신이 말하는 내용이라면 코드에 대한 도움을 줄 수 있습니다. –

+0

@ChrisSharp 예 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 인터페이스가 없습니다. 나는 목록에 항목을 수정하는 방법에 대해 설명 할 수 있도록 목록에 넣었습니다. 전화와 목록은 어디서나 올 수 있습니다. –

관련 문제