473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

declaring and using a linked list

This is what I have so far:

My program!

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import ch06.lists.*;
  5.  
  6. public class UIandDB {
  7.  
  8.     public static void main (String [] args) throws IOException {
  9.  
  10.       Scanner stdin = new Scanner(System.in);
  11.  
  12.       System.out.println("Car Part Database");
  13.  
  14.       //use a RefSortedList
  15.  
  16.       String skip;            //skip end of line after reading an integer
  17.       boolean keepGoing;      //flag for "choose operation" loop
  18.       int operation;            //indicates user's choice of operations
  19.  
  20.       keepGoing = true;
  21.       while (keepGoing) {
  22.           System.out.println("Choose and Operation:");
  23.           //insert command
  24.           System.out.println("1: Add new car part to the database");
  25.           //delete command
  26.           System.out.println("2: Remove a car part from the database");
  27.           //print all parts command
  28.           System.out.println("3: Print all car parts currently in the database");
  29.           //print a part command
  30.           System.out.println("4: Print a particular car part currently in the database");
  31.           //increase part stock command
  32.           System.out.println("5: Increase current stock level for a car part in the database");
  33.           //deliver part command
  34.           System.out.println("6: Deliver a certain amount of a car part in the database to a customer");
  35.           //exit command
  36.           System.out.println("7: Exit the car part database");
  37.           if (stdin.hasNextInt()) {
  38.             operation = stdin.nextInt();
  39.           }
  40.           else {
  41.             System.out.println("Error: you must enter a number between 1 and 7!");
  42.             System.out.println("Terminating Database!");
  43.             return;
  44.           }
  45.           skip = stdin.nextLine();
  46.  
  47.           switch (operation) {
  48.                 case 1: //insert command
  49.             //adds new part type to database(partID, partName, partStock, and partPrice)
  50.             //inputted by user
  51.             break;
  52.  
  53.             case 2: //delete command
  54.             //removes a part from the database(part is no longer produced by the company)
  55.             //user inputs partID of the part and it is removed from the database
  56.               break;
  57.  
  58.             case 3: //print all parts command
  59.             //displays on screen all parts in the database
  60.             break;
  61.  
  62.             case 4: //print a part command
  63.             //displays on screen data about a specified car part
  64.             //user inputs partID of the part and it is displayed
  65.             break;
  66.  
  67.             case 5: //increase part stock command
  68.             //increase the available stock of a certain part
  69.             //user inputs partID and quantity to be added to existing part stock
  70.             break;
  71.  
  72.             case 6: //deliver part command
  73.             //deliver a specified quantity of a certain part to customer
  74.             //user inputs partID and the quantity to be delivered
  75.             break;
  76.  
  77.             case 7: //exit command
  78.             //exit the application
  79.             keepGoing = false;
  80.             break;
  81.          }
  82.       }
  83.     System.out.println("Closing the car part database.");
  84.     System.out.println("Thanks for using my program!");
  85.     }
  86. }
This is the CarPart.java file for the information i must collect for each car part!
Expand|Select|Wrap|Line Numbers
  1. public class CarPart {
  2.  
  3.       public int partID;
  4.       public String partName;
  5.       public int partStock;
  6.       public double partPrice;
  7.  
  8.       //constructor with parameters
  9.       public CarPart(int ID, String n, int s, double p) {
  10.             partID = ID;
  11.             partName = n;
  12.             partStock = s;
  13.             partPrice = p;
  14.       }//end constructor
  15.  
  16.       //(part ID accessor)
  17.       public int getPartID() {
  18.             return partID;
  19.       }//end method
  20.  
  21.       //(part name accessor)
  22.       public String getPartName() {
  23.             return partName;
  24.       }//end method
  25.  
  26.       //(part stock accessor)
  27.       public int getStock() {
  28.             return partStock;
  29.       }//end method
  30.  
  31.       //(part price accessor)
  32.       public double getPartPrice() {
  33.             return partPrice;
  34.       }//end method
  35.  
  36.       //(transformer method)
  37.       public void addToStock(int add) {
  38.             partStock += add;
  39.       }//end method
  40.  
  41.       //(transformer method)
  42.       public boolean removeFromStock(int remove) {
  43.             if ( remove <= partStock){
  44.                   partStock -= remove;
  45.                   return true;
  46.             } 
  47.             else {
  48.                   return false;
  49.             }
  50.       }//end method
  51.  
  52.       //print method
  53.       public void print() {
  54.             System.out.println(getPartID() + ", " + getPartName() + ", " +  getStock() + ", $"+  getPartPrice());
  55.       }//end method 
  56.  
  57. }
I must reference the following ch.06 files!
Expand|Select|Wrap|Line Numbers
  1. //-------------------------------------------------------------------------
  2. // RefSortedList.java         by Dale/Joyce/Weems                 Chapter 6
  3. //
  4. // Implements the SortedListInterface using a linked list.
  5. //-------------------------------------------------------------------------
  6.  
  7. package ch06.lists;
  8.  
  9. import support.LLObjectNode;
  10.  
  11. public class RefSortedList extends RefList implements SortedListInterface  
  12. {
  13.  
  14.   public RefSortedList() 
  15.   {
  16.     super();
  17.   }
  18.  
  19.   public void add(Comparable element)
  20.   // Adds element to this list.
  21.   {
  22.     LLObjectNode prevLoc;     // trailing reference
  23.     LLObjectNode location;    // traveling reference
  24.     Comparable listElement;   // current list element being compared      
  25.     boolean moreToSearch;             
  26.  
  27.     // Set up search for insertion point.
  28.     location = list;
  29.     prevLoc = null;
  30.     moreToSearch = (location != null);
  31.  
  32.     // Find insertion point.
  33.     while (moreToSearch)
  34.     {
  35.       listElement = (Comparable)location.getInfo();
  36.       if (listElement.compareTo(element) < 0)  // list element < add element
  37.       {
  38.          prevLoc = location;
  39.          location = location.getLink();
  40.          moreToSearch = (location != null);   // stop looking at end of list
  41.       }
  42.       else
  43.         moreToSearch = false;     // list element >= add element
  44.     }
  45.  
  46.     // Prepare node for insertion.
  47.     LLObjectNode newNode = new LLObjectNode(element);
  48.  
  49.     // Insert node into list.
  50.     if (prevLoc == null)         
  51.     {
  52.       // Insert as first node.
  53.       newNode.setLink(list);
  54.       list = newNode;
  55.     }
  56.     else
  57.     {
  58.       // Insert elsewhere.
  59.       newNode.setLink(location);
  60.       prevLoc.setLink(newNode);
  61.     }
  62.     numElements++;
  63.   }
  64. }
  65.  
  66. //-------------------------------------------------------------------------
  67. // RefList.java            by Dale/Joyce/Weems                    Chapter 6
  68. //
  69. // Defines constructs for an unbounded reference-based list of objects that
  70. // do not depend on whether the list is unsorted or sorted.
  71. //
  72. // Our intention is for this class to be extended by classes that furnish
  73. // the remaining methods needed to support a list - for example, a method
  74. // that allows objects to be added to the list.
  75. //
  76. // Null elements are not permitted on a list.
  77. //
  78. // One constructor is provided, one that creates an empty list.
  79. //------------------------------------------------------------------------
  80.  
  81. package ch06.lists;
  82.  
  83. import support.LLObjectNode;
  84.  
  85. public class RefList
  86. {
  87.   protected int numElements;          // number of elements in this list
  88.   protected LLObjectNode currentPos;  // current position for iteration
  89.  
  90.   // set by find method
  91.   protected boolean found;         // true if element found, else false
  92.   protected LLObjectNode location; // node containing element, if found
  93.   protected LLObjectNode previous; // node preceeding location
  94.  
  95.   protected LLObjectNode list;     // first node on the list
  96.  
  97.   public RefList()
  98.   {
  99.     numElements = 0;
  100.     list = null;
  101.     currentPos = null;
  102.   }
  103.  
  104.   protected void find(Object target)
  105.   // Searches list for an occurence of an element e such that
  106.   // e.equals(target). If successful, sets instance variables
  107.   // found to true, location to node containing e, and previous
  108.   // to the node that links to location. If not successful, sets 
  109.   // found to false.
  110.   {
  111.     boolean moreToSearch;
  112.     location = list;
  113.     found = false;
  114.  
  115.     moreToSearch = (location != null);
  116.     while (moreToSearch && !found) 
  117.     {
  118.       if (location.getInfo().equals(target))  // if they match
  119.        found = true;
  120.       else
  121.       {
  122.         previous = location;
  123.         location = location.getLink();
  124.         moreToSearch = (location != null);
  125.       }
  126.     }
  127.   }
  128.  
  129.   public int size()
  130.   // Returns the number of elements on this list. 
  131.   {
  132.     return numElements;
  133.   }
  134.  
  135.   public boolean contains (Object element)
  136.   // Returns true if this list contains an element e such that 
  137.   // e.equals(element); otherwise, returns false.
  138.   {
  139.     find(element);
  140.     return found;
  141.   }
  142.  
  143.   public boolean remove (Object element)
  144.   // Removes an element e from this list such that e.equals(element)
  145.   // and returns true; if no such element exists, returns false.
  146.   {
  147.     find(element);
  148.     if (found)
  149.     {
  150.       if (list == location)     
  151.         list = list.getLink();    // remove first node
  152.       else
  153.         previous.setLink(location.getLink());  // remove node at location
  154.  
  155.       numElements--;
  156.     }
  157.     return found;
  158.   }
  159.  
  160.   public Object get(Object element)
  161.   // Returns an element e from this list such that e.equals(element);
  162.   // if no such element exists, returns null.
  163.   {
  164.     find(element);    
  165.     if (found)
  166.       return location.getInfo();
  167.     else
  168.       return null;
  169.   }
  170.  
  171.   public String toString()
  172.   // Returns a nicely formatted string that represents this list.
  173.   {
  174.     LLObjectNode currNode = list;
  175.     String listString = "List:\n";
  176.     while (currNode != null)
  177.     {
  178.       listString = listString + "  " + currNode.getInfo() + "\n";
  179.       currNode = currNode.getLink();
  180.     }
  181.     return listString;
  182.   }  
  183.  
  184.   public void reset()
  185.   // Initializes current position for an iteration through this list,
  186.   // to the first element on this list.
  187.   {
  188.     currentPos  = list;
  189.   }
  190.  
  191.   public Object getNext()
  192.   // Preconditions: The list is not empty
  193.   //                The list has been reset
  194.   //                The list has not been modified since most recent reset
  195.   //
  196.   // Returns the element at the current position on this list.
  197.   // If the current position is the last element, then it advances the value 
  198.   // of the current position to the first element; otherwise, it advances
  199.   // the value of the current position to the next element.
  200.   {
  201.     Object next = currentPos.getInfo();
  202.     if (currentPos.getLink() == null)
  203.       currentPos = list;
  204.     else
  205.       currentPos = currentPos.getLink();
  206.     return next;
  207.   }
  208. }
  209.  
  210. //----------------------------------------------------------------------------
  211. // SortedListInterface.java         by Dale/Joyce/Weems              Chapter 6
  212. //
  213. // Extends the ListInterface with methods specific to sorted lists.
  214. //----------------------------------------------------------------------------
  215.  
  216. package ch06.lists;
  217.  
  218. public interface SortedListInterface extends ListInterface
  219. {
  220.   void add(Comparable element);
  221.   // Adds element to this list. The list remains sorted.
  222. }
  223.  
  224. //----------------------------------------------------------------------------
  225. // ListInterface.java            by Dale/Joyce/Weems                 Chapter 6
  226. //
  227. // Interface that defines methods common to various kinds of list.
  228. // Our intention is that this interface will be extended by other interfaces
  229. // directly related to the specific kind of list. Those interfaces, in turn,
  230. // will be implemented by classes.
  231. //
  232. // The lists are unbounded and allow duplicate elements, but do not allow 
  233. // null elements. As a general precondition, null elements are not passed as 
  234. // arguments to any of the methods.
  235. //
  236. // The list has a special property called the current position - the position 
  237. // of the next element to be accessed by getNext during an iteration through 
  238. // the list. Only reset and getNext affect the current position.
  239. //----------------------------------------------------------------------------
  240.  
  241. package ch06.lists;
  242.  
  243. public interface ListInterface
  244. {
  245.   int size();
  246.   // Returns the number of elements on this list.
  247.  
  248.   boolean contains (Object element);
  249.   // Returns true if this list contains an element e such that 
  250.   // e.equals(element); otherwise, returns false.
  251.  
  252.   boolean remove (Object element);
  253.   // Removes an element e from this list such that e.equals(element)
  254.   // and returns true; if no such element exists, returns false. 
  255.  
  256.   Object get(Object element);
  257.   // Returns an element e from this list such that e.equals(element);
  258.   // if no such element exists, returns null.
  259.  
  260.   String toString();
  261.   // Returns a nicely formatted string that represents this list.
  262.  
  263.   void reset();
  264.   // Initializes current position for an iteration through this list,
  265.   // to the first element on this list.
  266.  
  267.   Object getNext();
  268.   // Preconditions: The list is not empty
  269.   //                The list has been reset
  270.   //                The list has not been modified since the most recent reset
  271.   //
  272.   // Returns the element at the current position on this list.
  273.   // If the current position is the last element, then it advances the value 
  274.   // of the current position to the first element; otherwise, it advances
  275.   // the value of the current position to the next element.
  276. }
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?
Jul 23 '07 #1
10 6546
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)!!!!!

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import ch06.lists.*;
  5.  
  6. public class UIandDB {
  7.  
  8.     public static void main (String [] args) throws IOException {
  9.  
  10.       Scanner stdin = new Scanner(System.in);
  11.  
  12.       System.out.println("Car Part Database");
  13.  
  14.       //use a RefSortedList (line29)
  15.       SortedListInterface carParts = new ArraySortedList(20);
  16.  
  17.       String skip;            //skip end of line after reading an integer
  18.       boolean keepGoing;      //flag for "choose operation" loop
  19.       int operation;            //indicates user's choice of operations
  20.  
  21.       keepGoing = true;
  22.       while (keepGoing) {
  23.           System.out.println("Choose and Operation:");
  24.           //insert command
  25.           System.out.println("1: Add new car part to the database");
  26.           //delete command
  27.           System.out.println("2: Remove a car part from the database");
  28.           //print all parts command
  29.           System.out.println("3: Print all car parts currently in the database");
  30.           //print a part command
  31.           System.out.println("4: Print a particular car part currently in the database");
  32.           //increase part stock command
  33.           System.out.println("5: Increase current stock level for a car part in the database");
  34.           //deliver part command
  35.           System.out.println("6: Deliver a certain amount of a car part in the database to a customer");
  36.           //exit command (line50)
  37.           System.out.println("7: Exit the car part database");
  38.           if (stdin.hasNextInt()) {
  39.             operation = stdin.nextInt();
  40.           }
  41.           else {
  42.             System.out.println("Error: you must enter a number between 1 and 7!");
  43.             System.out.println("Terminating Database!");
  44.             return;
  45.           }
  46.           skip = stdin.nextLine();
  47.  
  48.           switch (operation) {
  49.                 case 1: //insert command (line62)
  50.             //adds new part type to database(partID, partName, partStock, and partPrice)
  51.             //inputted by user
  52.             System.out.println("Please enter the following:");
  53.             System.out.print("Part ID#: ");
  54.             int ID = stdin.nextInt();
  55.             System.out.print("Part Name: ");
  56.             String n = stdin.next();
  57.             System.out.print("Total " + n + "'s to be added to database:");
  58.             int s = stdin.nextInt();
  59.             System.out.print("Price of each " + n + ": $");
  60.             double p = stdin.nextDouble();
  61.             CarPart carPart = new CarPart(ID, n, s, p);
  62.             carParts.add(carPart);
  63.             break;
  64.  
  65.             case 2: //delete command
  66.             //removes a part from the database(part is no longer produced by the company)
  67.             //user inputs partID of the part and it is removed from the database
  68.               break;
  69.  
  70.             case 3: //print all parts command
  71.             //displays on screen all parts in the database
  72.             System.out.println(carParts);
  73.             break;
  74.  
  75.             case 4: //print a part command
  76.             //displays on screen data about a specified car part
  77.             //user inputs partID of the part and it is displayed
  78.             break;
  79.  
  80.             case 5: //increase part stock command
  81.             //increase the available stock of a certain part
  82.             //user inputs partID and quantity to be added to existing part stock
  83.             break;
  84.  
  85.             case 6: //deliver part command
  86.             //deliver a specified quantity of a certain part to customer
  87.             //user inputs partID and the quantity to be delivered
  88.             break;
  89.  
  90.             case 7: //exit command
  91.             //exit the application
  92.             keepGoing = false;
  93.             break;
  94.          }
  95.       }
  96.     System.out.println("Closing the car part database.");
  97.     System.out.println("Thanks for using my program!");
  98.     }
  99. }
Jul 23 '07 #2
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.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.net.*;
  3. import java.lang.*;
  4. import support.*;
  5.  
  6. public class CarPart implements Comparable {
  7.  
  8.     public int partID;
  9.     public String partName;
  10.     public int partStock;
  11.     public double partPrice;
  12.  
  13.     //constructor with parameters
  14.     public CarPart(int ID, String n, int s, double p) {
  15.         partID = ID;
  16.         partName = n;
  17.         partStock = s;
  18.         partPrice = p;
  19.     }//end constructor
  20.  
  21.     //(part ID accessor)
  22.     public int getPartID() {
  23.         return partID;
  24.     }//end method
  25.  
  26.     //(part name accessor)
  27.     public String getPartName() {
  28.         return partName;
  29.     }//end method
  30.  
  31.     //(part stock accessor)
  32.     public int getStock() {
  33.         return partStock;
  34.     }//end method
  35.  
  36.     //(part price accessor)
  37.     public double getPartPrice() {
  38.         return partPrice;
  39.     }//end method
  40.  
  41.     //(transformer method)
  42.     public void addToStock(int add) {
  43.         partStock += add;
  44.     }//end method
  45.  
  46.     //(transformer method)
  47.     public boolean removeFromStock(int remove) {
  48.         if ( remove <= partStock){
  49.             partStock -= remove;
  50.             return true;
  51.         } 
  52.         else {
  53.             return false;
  54.         }
  55.     }//end method
  56.  
  57.     //print method
  58.     public void print() {
  59.         System.out.println(getPartID() + ", " + getPartName() + ", " +  getStock() + ", $"+  getPartPrice());
  60.     }//end method 
  61.  
  62.     //toString method
  63.     public String toString() {
  64.         return (partID + ", " + partName + ", " + partStock + ", " + partPrice);
  65.     }//end method
  66.  
  67.     //compareTo method
  68.     public int compareTo(Object o) { 
  69.         // ... method implementation
  70.         if (partID < ((CarPart)o).partID)
  71.             return -1;
  72.         else if (partID == ((CarPart)o).partID)
  73.             return 0;
  74.         else
  75.             return +1;
  76.     }//end method
  77.  
  78.     //equals method
  79.     public boolean equals(Object o) {
  80.         if(partID == ((CarPart)o).getPartID()) return true;
  81.         else return false;
  82.     }//end method
  83. }
Expand|Select|Wrap|Line Numbers
  1. public class UIandDB {
  2.  
  3.     public static void main (String [] args) throws IOException {
  4.  
  5.     Scanner stdin = new Scanner(System.in);
  6.  
  7.     System.out.println("Car Part Database");
  8.     System.out.println("");
  9.  
  10.     //use a RefSortedList (line29)
  11.     SortedListInterface carParts = new ArraySortedList();
  12.  
  13.     String skip;        //skip end of line after reading an integer
  14.     boolean keepGoing;    //flag for "choose operation" loop
  15.     int operation;        //indicates user's choice of operations
  16.  
  17.     keepGoing = true;
  18.     while (keepGoing) {
  19.         System.out.println("Choose an Operation:");
  20.         //insert command
  21.         System.out.println("1: Add new car part to the database");
  22.         //delete command
  23.         System.out.println("2: Remove a car part from the database");
  24.         //print all parts command
  25.         System.out.println("3: Print all car parts in the database");
  26.         //print a part command
  27.         System.out.println("4: Print a car part in the database");
  28.         //increase part stock command
  29.         System.out.println("5: Increase current stock level for a car part");
  30.         //deliver part command
  31.         System.out.println("6: Deliver a certain amount of a car part to a customer");
  32.         //exit command (line50)
  33.         System.out.println("7: Exit the car part database");
  34.         System.out.print("Choice: (1-7): ");
  35.         if (stdin.hasNextInt()) {
  36.         operation = stdin.nextInt();
  37.         }
  38.         else {
  39.         System.out.println("Error: you must enter a number between 1 and 7!");
  40.         System.out.println("Terminating Database!");
  41.         return;
  42.         }
  43.         skip = stdin.nextLine();
  44.  
  45.         switch (operation) {
  46.             case 1: //insert command (line62)
  47.         //adds new part type to database(partID, partName, partStock, and partPrice)
  48.         //inputted by user
  49.         System.out.println("");
  50.         System.out.println("Add a new Car Part:");
  51.         System.out.println("-------------------");
  52.         System.out.print("Car Part ID#: ");
  53.         int ID = stdin.nextInt();
  54.         System.out.print("Car Part Name: ");
  55.         String n = stdin.next();
  56.         System.out.print("Total # of " + n + "'s to be added:");
  57.         int s = stdin.nextInt();
  58.         System.out.print("Total Price of each " + n + ": $");
  59.         double p = stdin.nextDouble();
  60.         CarPart carPart = new CarPart(ID, n, s, p);
  61.         carParts.add(carPart);
  62.         System.out.println("");        
  63.         break;
  64.  
  65.         case 2: //delete command
  66.         //removes a part from the database(part is no longer produced by the company)
  67.         //user inputs partID of the part and it is removed from the database
  68.         System.out.println("");
  69.         System.out.println("Remove a Car Part:");
  70.         System.out.println("------------------");
  71.         System.out.print("partID# of the car part you would like to remove? ");
  72.         int removeID = stdin.nextInt();
  73.         CarPart dummy = new CarPart(removeID, "", 0, 0.0);
  74.         carParts.remove(dummy);
  75.         System.out.println("");
  76.             break;
  77.  
  78.         case 3: //print all parts command
  79.         //displays on screen all parts in the database
  80.         System.out.println("");
  81.         System.out.println("Print all Car Parts:");
  82.         System.out.println("--------------------");
  83.         System.out.print("Sorted ");
  84.         System.out.println(carParts);
  85.         System.out.println("");
  86.         break;
  87.  
  88.         case 4: //print a car part command
  89.         //displays on screen data about a specified car part
  90.         //user inputs partID of the part and it is displayed
  91.         System.out.println("");
  92.         System.out.print("What partID would you like displayed?");
  93.         int partPrint = stdin.nextInt();
  94.         carParts.get(partPrint);
  95.         break;
  96.  
  97.         case 5: //increase part stock command
  98.         //increase the available stock of a certain part
  99.         //user inputs partID and quantity to be added to existing part stock
  100.         break;
  101.  
  102.         case 6: //deliver part command
  103.         //deliver a specified quantity of a certain part to customer
  104.         //user inputs partID and the quantity to be delivered
  105.         break;
  106.  
  107.         case 7: //exit command
  108.         //exit the application
  109.         keepGoing = false;
  110.         break;
  111.        }
  112.     }
  113.     System.out.println("");
  114.     System.out.println("Closing the car part database.");
  115.     System.out.println("Thanks for using my program!");
  116.     }
  117. }
Jul 24 '07 #3
I am having the same problem
Jul 25 '07 #4
JosAH
11,448 Expert 8TB
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
Jul 25 '07 #5
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!

Expand|Select|Wrap|Line Numbers
  1. case 5: //increase part stock command
  2.         //increase the available stock of a certain part
  3.         //user inputs partID and quantity to be added to existing part stock
  4.         System.out.print("What partID would you like to add stock too? ");
  5.         int incPart = stdin.nextInt();
  6.         System.out.print("How much stock would you like to add to part # " + incPart + "? ");
  7.         int increaseStock = stdin.nextInt();
  8.         System.out.println("");
  9.         CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
  10.         carParts.get(dummy3);
  11.         CarPart.addToStock(increaseStock);
  12.         System.out.println(carParts.get(dummy3));
  13.         break;
However whenever I try to compile UIandDB.java i get the following error! (pulling hair out)!!!!!

UIandDB.java:138: non-static method addToStock(int) cannot be referenced from a static context
CarPart.addToStock(increaseStock);
1 error
Jul 25 '07 #6
r035198x
13,262 8TB
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!

Expand|Select|Wrap|Line Numbers
  1. case 5: //increase part stock command
  2.         //increase the available stock of a certain part
  3.         //user inputs partID and quantity to be added to existing part stock
  4.         System.out.print("What partID would you like to add stock too? ");
  5.         int incPart = stdin.nextInt();
  6.         System.out.print("How much stock would you like to add to part # " + incPart + "? ");
  7.         int increaseStock = stdin.nextInt();
  8.         System.out.println("");
  9.         CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
  10.         carParts.get(dummy3);
  11.         CarPart.addToStock(increaseStock);
  12.         System.out.println(carParts.get(dummy3));
  13.         break;
However whenever I try to compile UIandDB.java i get the following error! (pulling hair out)!!!!!

UIandDB.java:138: non-static method addToStock(int) cannot be referenced from a static context
CarPart.addToStock(increaseStock);
1 error
Don't just say CarPart.addToStock. Call the addToStock method on a particular CarPart object. otherwise you'd need to make the addToStock method static as well.
Jul 25 '07 #7
I tried dummy3.addToStock(increaseStock) but it doesnt work!

Here is what I am now trying!

Expand|Select|Wrap|Line Numbers
  1. case 5: //increase part stock command
  2.         //increase the available stock of a certain part
  3.         //user inputs partID and quantity to be added to existing part stock
  4.         System.out.print("What partID would you like to add stock too? ");
  5.         int incPart = stdin.nextInt();
  6.         System.out.print("How much stock would you like to add to part # " + incPart + "? ");
  7.         int increaseStock = stdin.nextInt();
  8.         System.out.println("");
  9.         CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
  10.         carParts.get(dummy3);
  11.         System.out.println(carParts.get(dummy3));
  12.         dummy3.getStock();
  13.         dummy3.addToStock(increaseStock);
  14.         carParts.remove(dummy3);
  15.         carParts.add(dummy3);
  16.         System.out.println(carParts.get(dummy3));
  17.         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!
Jul 25 '07 #8
r035198x
13,262 8TB
I tried dummy3.addToStock(increaseStock) but it doesnt work!
What does it do or not do when you use dummy3?
Jul 25 '07 #9
I tried dummy3.addToStock(increaseStock) but it doesnt work!

Here is what I am now trying!

Expand|Select|Wrap|Line Numbers
  1. case 5: //increase part stock command
  2.         //increase the available stock of a certain part
  3.         //user inputs partID and quantity to be added to existing part stock
  4.         System.out.print("What partID would you like to add stock too? ");
  5.         int incPart = stdin.nextInt();
  6.         System.out.print("How much stock would you like to add to part # " + incPart + "? ");
  7.         int increaseStock = stdin.nextInt();
  8.         System.out.println("");
  9.         CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
  10.         carParts.get(dummy3);
  11.         System.out.println(carParts.get(dummy3));
  12.         dummy3.getStock();
  13.         dummy3.addToStock(increaseStock);
  14.         carParts.remove(dummy3);
  15.         carParts.add(dummy3);
  16.         System.out.println(carParts.get(dummy3));
  17.         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!
Jul 25 '07 #10
I tried dummy3.addToStock(increaseStock) but it doesnt work!

Here is what I am now trying!

Expand|Select|Wrap|Line Numbers
  1. case 5: //increase part stock command
  2.         //increase the available stock of a certain part
  3.         //user inputs partID and quantity to be added to existing part stock
  4.         System.out.print("What partID would you like to add stock too? ");
  5.         int incPart = stdin.nextInt();
  6.         System.out.print("How much stock would you like to add to part # " + incPart + "? ");
  7.         int increaseStock = stdin.nextInt();
  8.         System.out.println("");
  9.         CarPart dummy3 = new CarPart(incPart, "", 0, 0.0);
  10.         carParts.get(dummy3);
  11.         System.out.println(carParts.get(dummy3));
  12.         dummy3.getStock();
  13.         dummy3.addToStock(increaseStock);
  14.         carParts.remove(dummy3);
  15.         carParts.add(dummy3);
  16.         System.out.println(carParts.get(dummy3));
  17.         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.
Jul 30 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
8
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.