2016-12-05 4 views
0

그래서 여기에 눈부신 오류가 누락되어 있지만 알아낼 수는 없습니다. 사용자가 상점의 전체 인벤토리를 표시하는 데 사용할 수있는 메뉴가 있습니다. 그러나 인벤토리 목록을 반복하는 메서드를 호출하면 전체 목록이 무한대로 표시됩니다. 원래의 표시 방법을 가지고 내 InventoryItem의 클래스는 여기에있다무한 루프마다 java - 전체 목록을 무한대로 표시

import java.util.Scanner; 
import java.util.ArrayList; 

public class MyStore 
{ 
private Scanner kbd; 
private int inventorySize; 
private ArrayList<InventoryItem> list; 

public MyStore() 
{ 

    kbd = new Scanner(System.in); 
    System.out.print("What is the size of the inventory you are creating?"); 
    inventorySize = kbd.nextInt(); 
    list = new ArrayList<InventoryItem>(inventorySize); 
    initInventory(); 
} 

public void initInventory() 
{ 
    for (int i = 0; i < this.inventorySize; i++) 
    { 
     System.out.println("Please select one of the following options: G)uitar D)rums"); 
     String input = kbd.next(); 
     String n; 
     int nis; 
     double p; 

     if (input.equals("G")) 
     { 
      System.out.print("What is the name of this item?"); 
      n = kbd.next(); 
      System.out.print("How many items are you adding?"); 
      nis = kbd.nextInt(); 
      System.out.print("What is the price of this item?"); 
      p = kbd.nextDouble(); 

      Guitar guitar = new Guitar(n, nis, p); 
      list.add(guitar); 

     } 
     else if(input.equals("D")) 
     { 
      System.out.print("What is the name of this item?"); 
      n = kbd.next(); 
      System.out.print("How many items are you adding?"); 
      nis = kbd.nextInt(); 
      System.out.print("What is the price of this item?"); 
      p = kbd.nextDouble(); 

      Drums drums = new Drums(n, nis, p); 
      list.add(drums); 
     } 
    } 
} 

public void start() 
{ 
    String nm; 
    boolean done = false; 
    while (!done) 
    { 
     System.out.print("Please select from the following menu:\n 1: Search for an item by name. \n 2: Display Inventory. \n 3: Quit \n ->"); 
     int menuNum = kbd.nextInt(); 
     int count = 0; 
     while(count != 3) 
     { 
      if (menuNum == 1) 
      { 
       System.out.print("Please enter the name of the item: "); 
       nm = kbd.next(); 
       searchAndDisplay(nm); 
       count = 0; 
      } 
      else if (menuNum == 2) 
      { 
       displayWholeInventory(); 
      } 

     } 

    } 

} 

public void searchAndDisplay(String name) 
{ 
    search(name); 
    boolean found = false; 
    if (name.equals(search(name))) 
    { 

     found = true; 
    } 
    else 
    { 
     System.out.print("Sorry, we currently don't have that in stock."); 
    } 
} 

public InventoryItem search(String name) 
{ 
    for (InventoryItem item : list.) 
    { 
     if (item.name().contains(name)) 
     { 
      return item; 
     } 
    } 
    return null; 
} 

public void displayWholeInventory() 
{ 
    for (InventoryItem item : list) //loops through array and calls each item's print method 
    { 
     System.out.println(); 
     item.display(); 
    } 
} 

public void buyItem(String name) //extra credit 
{ 
    //find item, reduce numberInStock, print message for user 
    //or if numberInStock is already 0, print suitable message 
} 

public static void main(String[] args) 
{ 
    MyStore store = new MyStore(); 
    store.start(); 
} 
} 

: 그리고 여기 내 코드입니다

public class InventoryItem 
{ 
private String name; 
private int numberInStock; 

//Constructor 
public InventoryItem(String n, int nis) 
{ 
    name = n; 
    numberInStock = nis; 
} 

//display the item 
public void display() 
{ 
    System.out.print("Item Name: " + name + " Stock Number: " + numberInStock); 
} 

//return name 
public String name() 
{ 
    return name; 
} 
} 

displayWholeInventory() 반복이 일어나는 곳 방법은의 나머지 부분에 대한 유감입니다 그 코드는 여전히 진행중입니다.

올바른 방향으로 밀기 만해도 도움이 될 것입니다.

감사합니다.

+1

'count'가 수정 된 곳은 두 곳입니다. 두 위치 모두 0으로 설정됩니다. – Sam

+0

'while (count! = 3)'-'count'가 언제 3이 될 것입니까? – user2357112

답변

0

감사합니다. 여기에서 코드를 조정했습니다 :

public void start() 
{ 
    String nm; 
    int count = 0; 
    while (count == 0) 
    { 
     System.out.println("Please select from the following menu:\n 1: Search for an item by name. \n 2: Display Inventory. \n 3: Quit \n ->"); 
     int menuNum = kbd.nextInt(); 

     if (menuNum == 1) 
     { 
      System.out.print("Please enter the name of the item: "); 
      nm = kbd.next(); 
      searchAndDisplay(nm); 
     } 
     else if (menuNum == 2) 
     { 
      displayWholeInventory(); 
      System.out.println(); 
     } 
     else if (menuNum == 3) 
     { 
      System.out.println("Goodbye."); 
      count = 1; 
     } 
    } 
} 

내부에 너무 많은 루프가 있습니다.