declaring and using a linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    declaring and using a linked list

    This is what I have so far:

    My program!

    Code:
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import ch06.lists.*;
    
    public class UIandDB {
    
        public static void main (String [] args) throws IOException {
    
          Scanner stdin = new Scanner(System.in);
    
          System.out.println("Car Part Database");
    
          //use a RefSortedList
    
          String skip;            //skip end of line after reading an integer
          boolean keepGoing;      //flag for "choose operation" loop
          int operation;            //indicates user's choice of operations
    
          keepGoing = true;
          while (keepGoing) {
              System.out.println("Choose and Operation:");
              //insert command
              System.out.println("1: Add new car part to the database");
              //delete command
              System.out.println("2: Remove a car part from the database");
              //print all parts command
              System.out.println("3: Print all car parts currently in the database");
              //print a part command
              System.out.println("4: Print a particular car part currently in the database");
              //increase part stock command
              System.out.println("5: Increase current stock level for a car part in the database");
              //deliver part command
              System.out.println("6: Deliver a certain amount of a car part in the database to a customer");
              //exit command
              System.out.println("7: Exit the car part database");
              if (stdin.hasNextInt()) {
                operation = stdin.nextInt();
              }
              else {
                System.out.println("Error: you must enter a number between 1 and 7!");
                System.out.println("Terminating Database!");
                return;
              }
              skip = stdin.nextLine();
          
              switch (operation) {
                    case 1: //insert command
                //adds new part type to database(partID, partName, partStock, and partPrice)
                //inputted by user
                break;
          
                case 2: //delete command
                //removes a part from the database(part is no longer produced by the company)
                //user inputs partID of the part and it is removed from the database
                  break;
    
                case 3: //print all parts command
                //displays on screen all parts in the database
                break;
    
                case 4: //print a part command
                //displays on screen data about a specified car part
                //user inputs partID of the part and it is displayed
                break;
    
                case 5: //increase part stock command
                //increase the available stock of a certain part
                //user inputs partID and quantity to be added to existing part stock
                break;
    
                case 6: //deliver part command
                //deliver a specified quantity of a certain part to customer
                //user inputs partID and the quantity to be delivered
                break;
    
                case 7: //exit command
                //exit the application
                keepGoing = false;
                break;
             }
          }
        System.out.println("Closing the car part database.");
        System.out.println("Thanks for using my program!");
        }
    }
    This is the CarPart.java file for the information i must collect for each car part!
    Code:
    public class CarPart {
    
          public int partID;
          public String partName;
          public int partStock;
          public double partPrice;
          
          //constructor with parameters
          public CarPart(int ID, String n, int s, double p) {
                partID = ID;
                partName = n;
                partStock = s;
                partPrice = p;
          }//end constructor
    
          //(part ID accessor)
          public int getPartID() {
                return partID;
          }//end method
    
          //(part name accessor)
          public String getPartName() {
                return partName;
          }//end method
    
          //(part stock accessor)
          public int getStock() {
                return partStock;
          }//end method
    
          //(part price accessor)
          public double getPartPrice() {
                return partPrice;
          }//end method
    
          //(transformer method)
          public void addToStock(int add) {
                partStock += add;
          }//end method
    
          //(transformer method)
          public boolean removeFromStock(int remove) {
                if ( remove <= partStock){
                      partStock -= remove;
                      return true;
                } 
                else {
                      return false;
                }
          }//end method
    
          //print method
          public void print() {
                System.out.println(getPartID() + ", " + getPartName() + ", " +  getStock() + ", $"+  getPartPrice());
          }//end method 
          
    }
    I must reference the following ch.06 files!
    Code:
    //-------------------------------------------------------------------------
    // RefSortedList.java         by Dale/Joyce/Weems                 Chapter 6
    //
    // Implements the SortedListInterface using a linked list.
    //-------------------------------------------------------------------------
    
    package ch06.lists;
    
    import support.LLObjectNode;
    
    public class RefSortedList extends RefList implements SortedListInterface  
    {
    
      public RefSortedList() 
      {
        super();
      }
    
      public void add(Comparable element)
      // Adds element to this list.
      {
        LLObjectNode prevLoc;     // trailing reference
        LLObjectNode location;    // traveling reference
        Comparable listElement;   // current list element being compared      
        boolean moreToSearch;             
    
        // Set up search for insertion point.
        location = list;
        prevLoc = null;
        moreToSearch = (location != null);
    
        // Find insertion point.
        while (moreToSearch)
        {
          listElement = (Comparable)location.getInfo();
          if (listElement.compareTo(element) < 0)  // list element < add element
          {
             prevLoc = location;
             location = location.getLink();
             moreToSearch = (location != null);   // stop looking at end of list
          }
          else
            moreToSearch = false;     // list element >= add element
        }
    
        // Prepare node for insertion.
        LLObjectNode newNode = new LLObjectNode(element);
    
        // Insert node into list.
        if (prevLoc == null)         
        {
          // Insert as first node.
          newNode.setLink(list);
          list = newNode;
        }
        else
        {
          // Insert elsewhere.
          newNode.setLink(location);
          prevLoc.setLink(newNode);
        }
        numElements++;
      }
    }
    
    //-------------------------------------------------------------------------
    // RefList.java            by Dale/Joyce/Weems                    Chapter 6
    //
    // Defines constructs for an unbounded reference-based list of objects that
    // do not depend on whether the list is unsorted or sorted.
    //
    // Our intention is for this class to be extended by classes that furnish
    // the remaining methods needed to support a list - for example, a method
    // that allows objects to be added to the list.
    //
    // Null elements are not permitted on a list.
    //
    // One constructor is provided, one that creates an empty list.
    //------------------------------------------------------------------------
    
    package ch06.lists;
    
    import support.LLObjectNode;
    
    public class RefList
    {
      protected int numElements;          // number of elements in this list
      protected LLObjectNode currentPos;  // current position for iteration
    
      // set by find method
      protected boolean found;         // true if element found, else false
      protected LLObjectNode location; // node containing element, if found
      protected LLObjectNode previous; // node preceeding location
    
      protected LLObjectNode list;     // first node on the list
    
      public RefList()
      {
        numElements = 0;
        list = null;
        currentPos = null;
      }
    
      protected void find(Object target)
      // Searches list for an occurence of an element e such that
      // e.equals(target). If successful, sets instance variables
      // found to true, location to node containing e, and previous
      // to the node that links to location. If not successful, sets 
      // found to false.
      {
        boolean moreToSearch;
        location = list;
        found = false;
    
        moreToSearch = (location != null);
        while (moreToSearch && !found) 
        {
          if (location.getInfo().equals(target))  // if they match
           found = true;
          else
          {
            previous = location;
            location = location.getLink();
            moreToSearch = (location != null);
          }
        }
      }
    
      public int size()
      // Returns the number of elements on this list. 
      {
        return numElements;
      }
    
      public boolean contains (Object element)
      // Returns true if this list contains an element e such that 
      // e.equals(element); otherwise, returns false.
      {
        find(element);
        return found;
      }
    
      public boolean remove (Object element)
      // Removes an element e from this list such that e.equals(element)
      // and returns true; if no such element exists, returns false.
      {
        find(element);
        if (found)
        {
          if (list == location)     
            list = list.getLink();    // remove first node
          else
            previous.setLink(location.getLink());  // remove node at location
    
          numElements--;
        }
        return found;
      }
    
      public Object get(Object element)
      // Returns an element e from this list such that e.equals(element);
      // if no such element exists, returns null.
      {
        find(element);    
        if (found)
          return location.getInfo();
        else
          return null;
      }
      
      public String toString()
      // Returns a nicely formatted string that represents this list.
      {
        LLObjectNode currNode = list;
        String listString = "List:\n";
        while (currNode != null)
        {
          listString = listString + "  " + currNode.getInfo() + "\n";
          currNode = currNode.getLink();
        }
        return listString;
      }  
    
      public void reset()
      // Initializes current position for an iteration through this list,
      // to the first element on this list.
      {
        currentPos  = list;
      }
    
      public Object getNext()
      // Preconditions: The list is not empty
      //                The list has been reset
      //                The list has not been modified since most recent reset
      //
      // Returns the element at the current position on this list.
      // If the current position is the last element, then it advances the value 
      // of the current position to the first element; otherwise, it advances
      // the value of the current position to the next element.
      {
        Object next = currentPos.getInfo();
        if (currentPos.getLink() == null)
          currentPos = list;
        else
          currentPos = currentPos.getLink();
        return next;
      }
    }
    
    //----------------------------------------------------------------------------
    // SortedListInterface.java         by Dale/Joyce/Weems              Chapter 6
    //
    // Extends the ListInterface with methods specific to sorted lists.
    //----------------------------------------------------------------------------
    
    package ch06.lists;
    
    public interface SortedListInterface extends ListInterface
    {
      void add(Comparable element);
      // Adds element to this list. The list remains sorted.
    }
    
    //----------------------------------------------------------------------------
    // ListInterface.java            by Dale/Joyce/Weems                 Chapter 6
    //
    // Interface that defines methods common to various kinds of list.
    // Our intention is that this interface will be extended by other interfaces
    // directly related to the specific kind of list. Those interfaces, in turn,
    // will be implemented by classes.
    //
    // The lists are unbounded and allow duplicate elements, but do not allow 
    // null elements. As a general precondition, null elements are not passed as 
    // arguments to any of the methods.
    //
    // The list has a special property called the current position - the position 
    // of the next element to be accessed by getNext during an iteration through 
    // the list. Only reset and getNext affect the current position.
    //----------------------------------------------------------------------------
    
    package ch06.lists;
    
    public interface ListInterface
    {
      int size();
      // Returns the number of elements on this list.
    
      boolean contains (Object element);
      // Returns true if this list contains an element e such that 
      // e.equals(element); otherwise, returns false.
        
      boolean remove (Object element);
      // Removes an element e from this list such that e.equals(element)
      // and returns true; if no such element exists, returns false. 
      
      Object get(Object element);
      // Returns an element e from this list such that e.equals(element);
      // if no such element exists, returns null.
      
      String toString();
      // Returns a nicely formatted string that represents this list.
      
      void reset();
      // Initializes current position for an iteration through this list,
      // to the first element on this list.
    
      Object getNext();
      // Preconditions: The list is not empty
      //                The list has been reset
      //                The list has not been modified since the most recent reset
      //
      // Returns the element at the current position on this list.
      // If the current position is the last element, then it advances the value 
      // of the current position to the first element; otherwise, it advances
      // the value of the current position to the next element.
    }
    Now what I need help in figuring out is how to implement a RefSortedList, and adding the car part info to it! for each car part, there will be a ID#, Name, Stock and price for example(ID# 001, Name Alternator, Stock 14 [units], Price $45.99)! And each car part with 4 different items should only take up one node in the linked list. Can anyone help me?
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    #2
    I figured in simple terms my best bet was to start with a simple adding of a carPart and then trying to print it, here is that section. I have not implemented anything else yet, figured it would be easier to see if the linked list was populating first! However, what is happening when I try to print it out is this it says

    List:
    CarPart@69b332

    and I cant figure out how to fix this! i am assuming it is adding the carpart because if I dont add one then it comes back as:

    List:

    Here is what I got so far! Changes at Lines (49-63 and 70-73)!!!!!

    Code:
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import ch06.lists.*;
    
    public class UIandDB {
    
        public static void main (String [] args) throws IOException {
    
          Scanner stdin = new Scanner(System.in);
    
          System.out.println("Car Part Database");
    
          //use a RefSortedList (line29)
          SortedListInterface carParts = new ArraySortedList(20);
    
          String skip;            //skip end of line after reading an integer
          boolean keepGoing;      //flag for "choose operation" loop
          int operation;            //indicates user's choice of operations
    
          keepGoing = true;
          while (keepGoing) {
              System.out.println("Choose and Operation:");
              //insert command
              System.out.println("1: Add new car part to the database");
              //delete command
              System.out.println("2: Remove a car part from the database");
              //print all parts command
              System.out.println("3: Print all car parts currently in the database");
              //print a part command
              System.out.println("4: Print a particular car part currently in the database");
              //increase part stock command
              System.out.println("5: Increase current stock level for a car part in the database");
              //deliver part command
              System.out.println("6: Deliver a certain amount of a car part in the database to a customer");
              //exit command (line50)
              System.out.println("7: Exit the car part database");
              if (stdin.hasNextInt()) {
                operation = stdin.nextInt();
              }
              else {
                System.out.println("Error: you must enter a number between 1 and 7!");
                System.out.println("Terminating Database!");
                return;
              }
              skip = stdin.nextLine();
          
              switch (operation) {
                    [B]case 1: //insert command (line62)
                //adds new part type to database(partID, partName, partStock, and partPrice)
                //inputted by user
                System.out.println("Please enter the following:");
                System.out.print("Part ID#: ");
                int ID = stdin.nextInt();
                System.out.print("Part Name: ");
                String n = stdin.next();
                System.out.print("Total " + n + "'s to be added to database:");
                int s = stdin.nextInt();
                System.out.print("Price of each " + n + ": $");
                double p = stdin.nextDouble();
                CarPart carPart = new CarPart(ID, n, s, p);
                carParts.add(carPart);
                break;[/B]
          
                case 2: //delete command
                //removes a part from the database(part is no longer produced by the company)
                //user inputs partID of the part and it is removed from the database
                  break;
    
                [B]case 3: //print all parts command
                //displays on screen all parts in the database
                System.out.println(carParts);
                break;[/B]
    
                case 4: //print a part command
                //displays on screen data about a specified car part
                //user inputs partID of the part and it is displayed
                break;
    
                case 5: //increase part stock command
                //increase the available stock of a certain part
                //user inputs partID and quantity to be added to existing part stock
                break;
    
                case 6: //deliver part command
                //deliver a specified quantity of a certain part to customer
                //user inputs partID and the quantity to be delivered
                break;
    
                case 7: //exit command
                //exit the application
                keepGoing = false;
                break;
             }
          }
        System.out.println("Closing the car part database.");
        System.out.println("Thanks for using my program!");
        }
    }

    Comment

    • AZRebelCowgirl73
      New Member
      • Nov 2006
      • 47

      #3
      Ok I have made quite a few changes. Here are my new problems!!!

      Cases 1, 2, 3, and 7 all work properly! I can not get case 4 to work right! I have not attempted 5 or 6 yet, but I have made alot of changes so here are both files now partially working.

      Code:
      import java.util.*;
      import java.net.*;
      import java.lang.*;
      import support.*;
      
      public class CarPart implements Comparable {
      
      	public int partID;
      	public String partName;
      	public int partStock;
      	public double partPrice;
      	
      	//constructor with parameters
      	public CarPart(int ID, String n, int s, double p) {
      		partID = ID;
      		partName = n;
      		partStock = s;
      		partPrice = p;
      	}//end constructor
      
      	//(part ID accessor)
      	public int getPartID() {
      		return partID;
      	}//end method
      
      	//(part name accessor)
      	public String getPartName() {
      		return partName;
      	}//end method
      
      	//(part stock accessor)
      	public int getStock() {
      		return partStock;
      	}//end method
      
      	//(part price accessor)
      	public double getPartPrice() {
      		return partPrice;
      	}//end method
      
      	//(transformer method)
      	public void addToStock(int add) {
      		partStock += add;
      	}//end method
      
      	//(transformer method)
      	public boolean removeFromStock(int remove) {
      		if ( remove <= partStock){
      			partStock -= remove;
      			return true;
      		} 
      		else {
      			return false;
      		}
      	}//end method
      
      	//print method
      	public void print() {
      		System.out.println(getPartID() + ", " + getPartName() + ", " +  getStock() + ", $"+  getPartPrice());
      	}//end method 
      
      	//toString method
      	public String toString() {
      		return (partID + ", " + partName + ", " + partStock + ", " + partPrice);
      	}//end method
      
      	//compareTo method
      	public int compareTo(Object o) { 
      		// ... method implementation
      		if (partID < ((CarPart)o).partID)
      			return -1;
      		else if (partID == ((CarPart)o).partID)
      			return 0;
      		else
      			return +1;
      	}//end method
      
      	//equals method
      	public boolean equals(Object o) {
      		if(partID == ((CarPart)o).getPartID()) return true;
      		else return false;
      	}//end method
      }
      Code:
      public class UIandDB {
      
          public static void main (String [] args) throws IOException {
      
      	Scanner stdin = new Scanner(System.in);
      
      	System.out.println("Car Part Database");
      	System.out.println("");
      
      	//use a RefSortedList (line29)
      	SortedListInterface carParts = new ArraySortedList();
      
      	String skip;		//skip end of line after reading an integer
      	boolean keepGoing;	//flag for "choose operation" loop
      	int operation;		//indicates user's choice of operations
      
      	keepGoing = true;
      	while (keepGoing) {
      	    System.out.println("Choose an Operation:");
      	    //insert command
      	    System.out.println("1: Add new car part to the database");
      	    //delete command
      	    System.out.println("2: Remove a car part from the database");
      	    //print all parts command
      	    System.out.println("3: Print all car parts in the database");
      	    //print a part command
      	    System.out.println("4: Print a car part in the database");
      	    //increase part stock command
      	    System.out.println("5: Increase current stock level for a car part");
      	    //deliver part command
      	    System.out.println("6: Deliver a certain amount of a car part to a customer");
      	    //exit command (line50)
      	    System.out.println("7: Exit the car part database");
      	    System.out.print("Choice: (1-7): ");
      	    if (stdin.hasNextInt()) {
      		operation = stdin.nextInt();
      	    }
      	    else {
      		System.out.println("Error: you must enter a number between 1 and 7!");
      		System.out.println("Terminating Database!");
      		return;
      	    }
      	    skip = stdin.nextLine();
      	
      	    switch (operation) {
      	    	case 1: //insert command (line62)
      		//adds new part type to database(partID, partName, partStock, and partPrice)
      		//inputted by user
      		System.out.println("");
      		System.out.println("Add a new Car Part:");
      		System.out.println("-------------------");
      		System.out.print("Car Part ID#: ");
      		int ID = stdin.nextInt();
      		System.out.print("Car Part Name: ");
      		String n = stdin.next();
      		System.out.print("Total # of " + n + "'s to be added:");
      		int s = stdin.nextInt();
      		System.out.print("Total Price of each " + n + ": $");
      		double p = stdin.nextDouble();
      		CarPart carPart = new CarPart(ID, n, s, p);
      		carParts.add(carPart);
      		System.out.println("");		
      		break;
      	
      		case 2: //delete command
      		//removes a part from the database(part is no longer produced by the company)
      		//user inputs partID of the part and it is removed from the database
      		System.out.println("");
      		System.out.println("Remove a Car Part:");
      		System.out.println("------------------");
      		System.out.print("partID# of the car part you would like to remove? ");
      		int removeID = stdin.nextInt();
      		CarPart dummy = new CarPart(removeID, "", 0, 0.0);
      		carParts.remove(dummy);
      		System.out.println("");
      	        break;
      
      		case 3: //print all parts command
      		//displays on screen all parts in the database
      		System.out.println("");
      		System.out.println("Print all Car Parts:");
      		System.out.println("--------------------");
      		System.out.print("Sorted ");
      		System.out.println(carParts);
      		System.out.println("");
      		break;
      
      		case 4: //print a car part command
      		//displays on screen data about a specified car part
      		//user inputs partID of the part and it is displayed
      		System.out.println("");
      		System.out.print("What partID would you like displayed?");
      		int partPrint = stdin.nextInt();
      		carParts.get(partPrint);
      		break;
      
      		case 5: //increase part stock command
      		//increase the available stock of a certain part
      		//user inputs partID and quantity to be added to existing part stock
      		break;
      
      		case 6: //deliver part command
      		//deliver a specified quantity of a certain part to customer
      		//user inputs partID and the quantity to be delivered
      		break;
      
      		case 7: //exit command
      		//exit the application
      		keepGoing = false;
      		break;
      	   }
      	}
          System.out.println("");
          System.out.println("Closing the car part database.");
          System.out.println("Thanks for using my program!");
          }
      }

      Comment

      • JoeMac3313
        New Member
        • Jul 2007
        • 16

        #4
        I am having the same problem

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by AZRebelCowgirl7 3
          Now what I need help in figuring out is how to implement a RefSortedList, and adding the car part info to it! for each car part, there will be a ID#, Name, Stock and price for example(ID# 001, Name Alternator, Stock 14 [units], Price $45.99)! And each car part with 4 different items should only take up one node in the linked list. Can anyone help me?
          That RefSortedList already is implemented, see the code you posted in your
          own thread. If you'd read the add() method implementation you'd noticed that
          it takes a Comparable as a parameter.

          You want to add CarParts to that list so your CarPart class should implement
          the Comparable interface; all for obvious reasons: the RefSortedList tries to
          keep the list sorted so it should be able to determine if a < b, a == b or a > b.
          The Comparable interface takes care of that.

          Read the API documentation for that interface.

          kind regards,

          Jos

          Comment

          • AZRebelCowgirl73
            New Member
            • Nov 2006
            • 47

            #6
            I altered case 4 and it is now working!

            I now have a delima with case 5!!!!! Here is what I have done so far!

            Code:
            case 5: //increase part stock command
            		//increase the available stock of a certain part
            		//user inputs partID and quantity to be added to existing part stock
            		System.out.print("What partID would you like to add stock too? ");
            		int incPart = stdin.nextInt();
            		System.out.print("How much stock would you like to add to part # " + incPart + "? ");
            		int increaseStock = stdin.nextInt();
            		System.out.println("");
            		CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
            		carParts.get(dummy3);
            		CarPart.addToStock(increaseStock);
            		System.out.println(carParts.get(dummy3));
            		break;
            However whenever I try to compile UIandDB.java i get the following error! (pulling hair out)!!!!!

            UIandDB.java:13 8: non-static method addToStock(int) cannot be referenced from a static context
            CarPart.addToSt ock(increaseSto ck);
            1 error

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by AZRebelCowgirl7 3
              I altered case 4 and it is now working!

              I now have a delima with case 5!!!!! Here is what I have done so far!

              Code:
              case 5: //increase part stock command
              		//increase the available stock of a certain part
              		//user inputs partID and quantity to be added to existing part stock
              		System.out.print("What partID would you like to add stock too? ");
              		int incPart = stdin.nextInt();
              		System.out.print("How much stock would you like to add to part # " + incPart + "? ");
              		int increaseStock = stdin.nextInt();
              		System.out.println("");
              		CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
              		carParts.get(dummy3);
              		CarPart.addToStock(increaseStock);
              		System.out.println(carParts.get(dummy3));
              		break;
              However whenever I try to compile UIandDB.java i get the following error! (pulling hair out)!!!!!

              UIandDB.java:13 8: non-static method addToStock(int) cannot be referenced from a static context
              CarPart.addToSt ock(increaseSto ck);
              1 error
              Don't just say CarPart.addToSt ock. Call the addToStock method on a particular CarPart object. otherwise you'd need to make the addToStock method static as well.

              Comment

              • AZRebelCowgirl73
                New Member
                • Nov 2006
                • 47

                #8
                I tried dummy3.addToSto ck(increaseStoc k) but it doesnt work!

                Here is what I am now trying!

                Code:
                case 5: //increase part stock command
                		//increase the available stock of a certain part
                		//user inputs partID and quantity to be added to existing part stock
                		System.out.print("What partID would you like to add stock too? ");
                		int incPart = stdin.nextInt();
                		System.out.print("How much stock would you like to add to part # " + incPart + "? ");
                		int increaseStock = stdin.nextInt();
                		System.out.println("");
                		CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
                		carParts.get(dummy3);
                		System.out.println(carParts.get(dummy3));
                		dummy3.getStock();
                		dummy3.addToStock(increaseStock);
                		carParts.remove(dummy3);
                		carParts.add(dummy3);
                		System.out.println(carParts.get(dummy3));
                		break;
                When I attempt this I get the following!

                If I tell the program i want to add stock to partID 123 and I want to add 10 units, this is what I get!
                the first print comes out right but the second i believe is only referencing the change because it prints like this
                ID#:123, Name: qwe, Quantity: 23, Price: $23.0
                ID#: 123, Name: , Quantity: 10, Price: $0.0
                I am losing the origanal values!

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by AZRebelCowgirl7 3
                  I tried dummy3.addToSto ck(increaseStoc k) but it doesnt work!
                  What does it do or not do when you use dummy3?

                  Comment

                  • AZRebelCowgirl73
                    New Member
                    • Nov 2006
                    • 47

                    #10
                    I tried dummy3.addToSto ck(increaseStoc k) but it doesnt work!

                    Here is what I am now trying!

                    Code:
                    case 5: //increase part stock command
                            //increase the available stock of a certain part
                            //user inputs partID and quantity to be added to existing part stock
                            System.out.print("What partID would you like to add stock too? ");
                            int incPart = stdin.nextInt();
                            System.out.print("How much stock would you like to add to part # " + incPart + "? ");
                            int increaseStock = stdin.nextInt();
                            System.out.println("");
                            CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
                            carParts.get(dummy3);
                            System.out.println(carParts.get(dummy3));
                            dummy3.getStock();
                            dummy3.addToStock(increaseStock);
                            carParts.remove(dummy3);
                            carParts.add(dummy3);
                            System.out.println(carParts.get(dummy3));
                            break;
                    When I attempt this I get the following!

                    If I tell the program i want to add stock to partID 123 and I want to add 10 units, this is what I get!
                    the first print comes out right but the second i believe is only referencing the change because it prints like this
                    ID#:123, Name: qwe, Quantity: 23, Price: $23.0
                    ID#: 123, Name: , Quantity: 10, Price: $0.0
                    I am losing the origanal values!

                    Comment

                    • mingster
                      New Member
                      • Jul 2007
                      • 1

                      #11
                      Originally posted by AZRebelCowgirl7 3
                      I tried dummy3.addToSto ck(increaseStoc k) but it doesnt work!

                      Here is what I am now trying!

                      Code:
                      case 5: //increase part stock command
                              //increase the available stock of a certain part
                              //user inputs partID and quantity to be added to existing part stock
                              System.out.print("What partID would you like to add stock too? ");
                              int incPart = stdin.nextInt();
                              System.out.print("How much stock would you like to add to part # " + incPart + "? ");
                              int increaseStock = stdin.nextInt();
                              System.out.println("");
                              CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
                              carParts.get(dummy3);
                              System.out.println(carParts.get(dummy3));
                              dummy3.getStock();
                              dummy3.addToStock(increaseStock);
                              carParts.remove(dummy3);
                              carParts.add(dummy3);
                              System.out.println(carParts.get(dummy3));
                              break;
                      When I attempt this I get the following!

                      If I tell the program i want to add stock to partID 123 and I want to add 10 units, this is what I get!
                      the first print comes out right but the second i believe is only referencing the change because it prints like this
                      ID#:123, Name: qwe, Quantity: 23, Price: $23.0
                      ID#: 123, Name: , Quantity: 10, Price: $0.0
                      I am losing the origanal values!

                      How did you alter 4, Ive been stuck on 4 for a bit now.

                      Comment

                      Working...