2016-12-05 4 views
0

Так что я, вероятно, не вижу вопиющей ошибки здесь, но я не могу понять это. У меня есть меню, которое пользователь может использовать для отображения всего инвентаря магазина. Однако, когда я вызываю метод, который выполняет итерацию через список инвентаря, он отображает весь список бесконечно. Вот мой код: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(); 
} 
} 

И тогда вот мой InventoryItem класс, который имеет оригинальный способ отображения:

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() метод, где итерация происходит, извините за остальные код, это все еще работа.

Любая помощь будет оценена, даже если это просто толчок в правильном направлении.

Thanks

+1

Я вижу только два места, где изменяется 'count'. В обоих местах он равен нулю. – 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; 
     } 
    } 
} 

У меня было слишком много петель внутри.

Смежные вопросы