This is what I have so far:
My program!
This is the CarPart.java file for the information i must collect for each car part!
I must reference the following ch.06 files!
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?
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!");
}
}
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
}
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.
}
Comment