Search code examples
javalinked-listinventory

How do I search through a Java Linked List


I am working on my last assignment for my intro Java class. We are doing a Linked List (we built ourselves) Inventory program. I am having trouble with my findItem method. How would I search through my Linked List by Item number?? The way I am doing it works, but I need to get rid of the break. My instructor said I "need to set a compound condition, the !found you have and the not null." I have tried several different variations and cannot get it. Please, any help, suggestions, or examples would be greatly appreciated, its finals week and everything is due tomorrow. My main issue is in findItem() in InventoryLL class, but I posted the whole code for context.

InventoryLL

import java.util.Scanner;
import java.io.PrintWriter;
import java.io. FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class InventoryLL
{
int count = 0;
private ItemNode head;
Scanner scannerObject = new Scanner(System.in);

public InventoryLL()
{
    head = null;
}

public void addItem()
{
    try{
        System.out.print("\nPlease enter name of item: ");
        String lname = scannerObject.nextLine();
        System.out.print("\nPlease enter a brief description of the item: ");
        String ldesc = scannerObject.nextLine();
        System.out.print("\nPlease enter the amount on hand: ");
        int lonHand = scannerObject.nextInt();
        System.out.print("\nPlease enter unit price of the item: $");
        Double lunitPrice = scannerObject.nextDouble();
        ItemNode temp = new ItemNode(count + 1, lname, ldesc, lonHand, lunitPrice);
        count++;
        temp.setNext(head);
        head = temp;
        System.out.println("\nThank you. The ID number for " + lname + " is " + count);
        scannerObject.nextLine();
    }catch(Exception e){    
            System.out.println("\nERROR! Please try again:\n");
            scannerObject.nextLine();
            }   
}

public ItemNode findItem(){
    int inputID;
    boolean found = false;
    ItemNode current = null;
        try{
        System.out.print("\nGreetings, please enter the ID number for item:\n");
        inputID = scannerObject.nextInt();
        scannerObject.nextLine();
            for (current = head; !found; current = current.getNext()){
                if (current.getID() == inputID){
                    found = true;
                    break; // I need to get rid of break
                    }
                }       
            }catch(Exception e)
            {
                System.out.println("\nERROR!");
            }
    return current;
}

public void modify()
{   
    ItemNode current = findItem();
        if (current == null){
        System.out.println("\nInvalid input! Please try again:");
            }else{
        try{
            System.out.print("\nPlease enter name of item: ");
            String lname = scannerObject.nextLine();
            current.setName(lname);
            System.out.print("\nPlease enter a brief description of the item: ");
            String ldesc = scannerObject.nextLine();
            current.setDesc(ldesc);
            System.out.print("\nPlease enter the amount on hand: ");
            int lonHand = scannerObject.nextInt();
            current.setOnHand(lonHand);
            System.out.print("\nPlease enter unit price of the item: $");
            double lunitPrice = scannerObject.nextDouble();
            current.setUnitPrice(lunitPrice);
            scannerObject.nextLine();
        }catch (Exception e)
                {
                System.out.println("\nInvalid command! Please try again: ");
                }
        }
}

public void displayAll()
{   System.out.println("_______________________________________________________________________________\n");    
    System.out.println("                                 Inventory                                     ");
    System.out.println("_______________________________________________________________________________\n");
    System.out.printf("\n%-6s%-20s%-24s%-12s%-6s\n", "ID:", "Name:", "Description:","On Hand:", "Unit Price:\n"); //Header
    System.out.println("_______________________________________________________________________________\n");
    ItemNode current = head;
        if(current == null){
            System.out.println("The list is empty.");
        }else{
            while(current != null){
                current.display();
                current = current.getNext();
            }
        }
    }


public void displayOne()
{
    ItemNode current = findItem();
    if (current == null){
        System.out.println("\nInvalid input! Please try again:");
          }else{
        System.out.println("_______________________________________________________________________________\n");    
        System.out.println("                                 Inventory                                     ");
        System.out.println("_______________________________________________________________________________\n");
        System.out.printf("\n%-6s%-20s%-24s%-12s%-6s\n", "ID:", "Name:", "Description:","On Hand:", "Unit Price:\n"); //Header
        System.out.println("_______________________________________________________________________________\n");
            current.display();
        }
    }



}

ItemNode

import java.text.NumberFormat;

public class ItemNode
{
private int ID;
private String name;
private String Desc;
private int onHand;
private double unitPrice;
private ItemNode next;

public ItemNode(int pID)
{
    ID = pID;
}

public ItemNode(int pID, String pName, String pDesc, int pOnHand, double pUnitPrice) {
    ID = pID;
    name = pName;
    Desc = pDesc;
    onHand = pOnHand;
    unitPrice = pUnitPrice;
}

public void display()
{ 
    NumberFormat dollars = NumberFormat.getCurrencyInstance();
    System.out.printf("%-6s%-20s%-24s%-12s%-6s\n", ID, name, Desc, onHand, dollars.format(unitPrice));
}

// GETTERS AND SETTERS
public void setNext(ItemNode pNext)
{
    next = pNext;
}

public ItemNode getNext()
{
    return next;
}

public int getID()
{
    return ID;
}

public void setName(String pName)
{
    name = pName;
}

public String getName()
{
    return name;
}

public void setDesc(String pDesc)
{
    Desc = pDesc;
}

public String getDesc()
{
    return Desc;
}

    public void setOnHand(int pOnHand)
{
    onHand = pOnHand;
}

public int getOnHand()
{
    return onHand;
}
public void setUnitPrice(double pUnitPrice)
{
    unitPrice = pUnitPrice;
}

public double getUnitPrice()
{
    return unitPrice;
}

}

inventUserLL

import java.util.Scanner;

public class inventUserLL 
{

public static void main(String[] args) 
{

    InventoryLL myInvent = new InventoryLL();
    Scanner scannerObject = new Scanner(System.in);
    int Choice = 0;



        do{

        dispMenu();

        Choice = getChoice(scannerObject);

        proChoice(Choice, myInvent);

        }while (Choice !=0);


}

public static void dispMenu()
{
    System.out.println("\n|=============================================|");
    System.out.println("|                                             |");
    System.out.println("|******************Welcome********************|");
    System.out.println("|_____________________________________________|");
    System.out.println("|                                             |");
    System.out.println("|       Press [1] To Add An Item              |");
    System.out.println("|                                             |");
    System.out.println("|       Press [2] To Display One Item         |");
    System.out.println("|                                             |");
    System.out.println("|       Press [3] To Display All Items        |");
    System.out.println("|                                             |");
    System.out.println("|       Press [4] To Modify An Item           |");
    System.out.println("|                                             |");
    System.out.println("|       Press [0] To Exit                     |");
    System.out.println("|_____________________________________________|");
    System.out.println("|=============================================|");
    System.out.println("|         Please Make Selection Now...        |");
    System.out.println("|=============================================|");
    System.out.println("|_____________________________________________|\n");
    }

    public static int getChoice(Scanner scannerObject)
    {
        boolean x = false;
        int pChoice = 0;
            do{
             try{
                pChoice = scannerObject.nextInt();
                x = true;
            }catch (Exception e){
                scannerObject.next();
                System.out.println("\nInvalid command! Please try again:\n");
                }
            }while (x == false);
        return pChoice;
    }

    public static void proChoice(int Choice, InventoryLL myInvent)
    {

        switch(Choice){
            case 1: myInvent.addItem();
            break;
            case 2: myInvent.displayOne();
            break;
            case 3: myInvent.displayAll();
            break;
            case 4: myInvent.modify();
            break;
            case 0: System.out.println("\nHave a nice day!");
            break;
            }
    }
}

UPDATE

public ItemNode findItem(){
    int inputID;
    boolean found = false;
    ItemNode current = head;
        try{
        System.out.print("\nGreetings, please enter the ID number for item:\n");
        inputID = scannerObject.nextInt();
        scannerObject.nextLine();
                if (head != null){
                    while ((current.getNext() != null) && (!found)) {
                        if (current.getID() == inputID){
                        found = true;
                        } 
                        if (!found){
                            current = current.getNext();
                            }   
                        }
                    if (!found){
                        current = null;
                        }   
                }                   
            }catch(Exception e)
            {
                System.out.println("\nERROR!");
            }
    return current;
}

Solution

  • If i'm not wrong here, a "compound condition" is as follows:

    if (head != null) {
        while ((current != null) && (!found)) {
            // check to see if you have the right value, set `found` if you do
            // otherwise, continue
            if (!found) { // do this otherwise you'll return the next value everytime
                current = current.getNext();
            }
        }
        if (!found) { // if you're at the end of the list and you didn't find the value
            // set current = to something you want to return if the value was not found
        }
    
    }
    return current;