2016-12-05 1 views
0

Also ich vermisse hier wahrscheinlich einen eklatanten Fehler, aber ich kann es nicht herausfinden. Ich habe ein Menü, mit dem ein Benutzer das gesamte Inventar eines Geschäfts anzeigen kann. Wenn ich jedoch die Methode aufruft, die die Inventarliste durchläuft, zeigt sie die gesamte Liste unendlich an. Hier ist mein Code:Java für jede Endlosschleife - Anzeige der gesamten Liste unendlich

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(); 
} 
} 

Und dann hier ist meine InventoryItem Klasse, die die ursprünglichen Anzeigeverfahren hat:

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; 
} 
} 

Die displayWholeInventory() Methode ist, wo die Iteration geschieht, sorry für den Rest der code, das ist noch ein work in progress.

Jede Hilfe wäre willkommen, auch wenn es nur ein Schritt in die richtige Richtung ist.

Danke

+1

Ich sehe nur zwei Stellen, wo "count" geändert wird. In beiden Orten ist es auf Null gesetzt. – Sam

+0

'while (count! = 3)' - wann würde 'count' jemals 3 werden? – user2357112

Antwort

0

Danke Jungs, ich fand es heraus. Ich habe den Code hier eingestellt:

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; 
     } 
    } 
} 

Ich hatte eine zu viele Schleifen innen.