4 Replies - 1650 Views - Last Post: 13 November 2009 - 01:18 PM Rate Topic: -----

#1 KrGmdeary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Homework Help (linked list/ Iinput Output)

Post icon  Posted 12 November 2009 - 07:49 PM

Hello Everyone,

My project that I am working on is based on using linked lists to take inventory of books based on author, price, title, best seller, and whether or not they are in stock. I am asking if someone could check over what I have done thus far in my project to make sure I am on the right path.

First off I needed to create a class called Book.

A book has the following attributes:
1. an author – you’ll only need to record the last name
2. a title
3. a price – it will be in dollars and cents
4. number of copies sold – this, of course, will be a non-negative integer
5. number of copies in stock – also a non-negative integer

It's been a while since I've done this so I'm not entirely too sure. I think I may need a toString()? I believe I instantiated all the variables that need to be... here is my code:

public class Book 
{
	public String author;
	public String title;
	public double price;
	public int sold;
	public int inStock;
	


public Book(String newAuthor, String newTitle, double newPrice, int newSold, int newInStock)
{
	author = newAuthor;
	title = newTitle;
	price = newPrice;
	sold = newSold;
	inStock = newInStock;
}

public String getAuthor()
{
	return author;
}

public String getTitle()
{
	return title;
}

public double getPrice()
{
	return price;
}

public int getSold()
{
	return sold;
}

public int getInStock()
{
	return inStock;
}
}


I feel like I'm missing more but I am not sure.

My next task is to create the inventory class which I am in the process of doing right now.

Any guidance or suggestions would be greatly appreciated :^:

Is This A Good Question/Topic? 0
  • +

Replies To: Homework Help (linked list/ Iinput Output)

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Homework Help (linked list/ Iinput Output)

Posted 12 November 2009 - 08:13 PM

fiirst, as to the book class, it seems good, but i dont think that number of copies sold should be passed as a parameter in the constructor.
i think this variable should be instantiated to 0 each time you create a new book, and when selling a book, increase that number by 1. but this is only a matter of design, and depands on how you see it.
overriding the toString method is a good idea, also for you tobe able to locate errors if there are such (by printing tothe console).

you dont provide enough information on how the inventory should work.
but basically, have a checkAvailable(String title) to check if a certain book is available.
then you could have a getBook method, which will look for a book by title, and will return it only if it is available.
Was This Post Helpful? 0
  • +
  • -

#3 KrGmdeary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Homework Help (linked list/ Iinput Output)

Posted 12 November 2009 - 08:57 PM

View Postjapanir, on 12 Nov, 2009 - 07:13 PM, said:

fiirst, as to the book class, it seems good, but i dont think that number of copies sold should be passed as a parameter in the constructor.
i think this variable should be instantiated to 0 each time you create a new book, and when selling a book, increase that number by 1. but this is only a matter of design, and depands on how you see it.
overriding the toString method is a good idea, also for you tobe able to locate errors if there are such (by printing tothe console).

you dont provide enough information on how the inventory should work.
but basically, have a checkAvailable(String title) to check if a certain book is available.
then you could have a getBook method, which will look for a book by title, and will return it only if it is available.



Excellent, I am on track then. Thank you! As far as the Inventory class, here are my guidelines. I wont post my code just yet because I am not finished it:

When you start your program, the inventory should have no books in it.

All interaction with your program will be menu-driven. A user should be able to perform each of the following operations:
1. Add a new book – If a user chooses this option, he or she should then be prompted for the author, title, and price of that book. You may assume that all info is typed correctly and that it is really a new book (i.e. it’s not already listed in the inventory). The program should then add this book to the list with three copies in stock.
2. List all in-stock books – Print (in a neatly formatted way) all the books in the inventory that are in stock with all of their associated information. The program should print the message “No books in the inventory” if there are (duh!) no books in the inventory, or “No books in stock” if there are (double duh!) no books in stock.
3. List all the books by a particular author – The user should be prompted for the author’s name. Print all the information associated with each book by that author. If there are no such books, print an appropriate message instead. (Print info for books in or out of stock.)
4. Record a purchase - Prompt the user for the title of the book bought, and how many copies were purchased. If there are enough copies in stock, the total price paid for the books should be added to the total revenue, the number of copies sold should be incremented appropriately, and the number of copies in stock should be decremented appropriately. If there aren’t enough copies, print an error message. You can assume the title actually exists in the inventory, and that the number of copies purchased is a non-negative integer. Print the total price paid to the screen.
5. Print all “best sellers” – Print the information for the book(s) that sold the most copies. If there is more than one such book, print all of them. (So if no books are sold, you’ll print all the books. If there are no books in the inventory, print an appropriate message. A book can be a best seller even if it’s out of stock.)
6. Refresh inventory – Set the number of copies in stock of all books in the inventory back to three.
7. Close the program – This should delete all the books from the inventory, and print the total revenue earned during the program’s execution.

You should adapt the functions from the book and from class to do these tasks. You must solve this assignment using a linked list class. There will be substantial deductions if you use arrays or some other structure.



-So far I have think I may have the first operation completed but I wont post the code until I am either stuck or finished.

Thank you so much for the guidance!
Was This Post Helpful? 0
  • +
  • -

#4 KrGmdeary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Homework Help (linked list/ Iinput Output)

Posted 12 November 2009 - 10:59 PM

Ok second question. Using the seven operations listed above, am I composing this Inventory class correctly or will I need to alter it so the terms of cursor match with my linkedlist class?

LinkedList class:

/**  
 * An implementation of the List interface using references.
 *
 * This code assumes that the preconditions stated in the comments are
 * true when a method is invoked and therefore does not check the
 * precondtions.
 */ 

public class LinkedList<Book> implements List<Book> {

	protected int count;		  // The number of nodes in the list
	protected LinkedNode<Book> head;	// The first node in the list
	protected LinkedNode<Book> last;	// The last node in the list
	protected LinkedNode<Book> cursor;  // The current node in the list

	/**
	 * Create a new list.
	 */
	public LinkedList() { 
		count = 0;
		head = null;
		last = null;
		cursor = null;
	}
	
	/** 
	 * Moves the cursor to the first element in the list.
	 *
	 * Preconditions:  
	 *   None
	 *
	 * Postconditions: 
	 *   The cursor is set to the first element in the list.
	 *   The list's structure and content are unchanged.
	 */ 
	public void first() {
		cursor = head;
	}
	
	/** 
	 * Moves the cursor to the last element in the list.
	 *
	 * Preconditions:  
	 *   None
	 *
	 * Postconditions: 
	 *   The cursor is set to the last element in the list.
	 *   The list's structure and content are unchanged.
	 */ 
	public void last() { 
		cursor = last; 
	}
	
	/** 
	 * Moves the cursor to the next element in the list.
	 *
	 * Preconditions:  
	 *   The cursor is on the list.
	 * 
	 * Postconditions: 
	 *   If the cursor is at the end of the list the cursor is moved off 
	 *	 the list.  Otherwise the cursor is set to the next element.
	 *   The list's structure and content are unchanged.
	 */ 
	public void next() { 
		cursor = cursor.getNext();
	}

	/** 
	 * Moves the cursor to the previous node in the list.
	 *
	 * Since this list only has links in the forward direction, the only
	 * way to find the previous node is to work from the front of the
	 * the list until we locate the previous node.  This is an O(n)
	 * operation.
	 *
	 * Preconditions:  
	 *   The cursor is on the list.
	 * 
	 * Postconditions: 
	 *   If the cursor is at the front of the list the cursor is moved off 
	 *	 the list.  Otherwise the cursor is moved to the previous element.
	 *   The list's structure and content are unchanged.
	 */ 
	public void previous() {
		cursor = findPrevious();
	}

	/** 
	 * Deletes the element the cursor is referring to.
	 *
	 * Preconditions:  
	 *   The cursor is on the list.
	 * 
	 * Postconditions: 
	 *   The element the cursor is referring to is removed.
	 *   The size of the list has decreased by 1.
	 *   If the element that was removed was the last element in the list, 
	 *	 the cursor is moved off the list.  Otherwise the cursor is 
	 *	 moved to the removed element's successor.
	 */ 
	public void remove() {
		removeNode( cursor, findPrevious() );
	}

	/**
	 * Remove the element at the specified position in the list.
	 *
	 * Preconditions:
	 *   position >= 0 and position < size().
	 *
	 * Postconditions:
	 *   The element at the specified position was removed from the list.
	 *   The size of the list is decreased by 1.
	 *   If the element that was removed was the last element in the list, 
	 *	 the cursor is moved off the list.  Otherwise the cursor is 
	 *	 moved to the removed element's successor.
	 *
	 * @param position the position in the list where the element will
	 *				 be placed.
	 */
	public void remove( int position ) {
		LinkedNode<Book> target = head;
		LinkedNode<Book> prev = null;
	
		// Find the node that contains the element we wish to
		// delete and the node immediately before it.
		while ( position > 0 ) {
			prev = target;
			target = target.getNext();
			position = position - 1;
		}

		// Target now refers to the node we wish to remove
		removeNode( target, prev );
	}

	/** 
	 * Add a new element at the position indicated by the cursor.
	 *
	 * Preconditions:
	 *   The list is empty or the cursor is on the list.
	 *
	 * Postconditions:
	 *   If the list is empty, the element becomes the only element
	 *	 in the list.  Otherwise the element is added at the
	 *	 position specified by the cursor.
	 *   The size of the list has increased by 1.
	 *   The cursor refers to the element added to the list.
	 *
	 * @param element the element to be added to the list.
	 */
	public void add( Book element ) {

		LinkedNode<Book> after = null;
		if( cursor != null ){
			 after = cursor.getNext();
		}

		// Add the node to the list
		addNode( element, cursor , after );
	}

	/**
	 * Add a new element at the specified position in the list.
	 *
	 * Preconditions:
	 *   position >= 0 and position <= size().
	 *
	 * Postconditions:
	 *   The element is added at the specified position in the list.
	 *   Elements at positions greater than or equal to the specified position 
	 *	 (if any) are shifted to the right by one position.
	 *   The size of the list is increased by 1.
	 *   The cursor refers to the element added to the list.
	 *
	 * @param position the position in the list where the element will
	 *				 be placed.
	 * @param element the element to be added to the list.
	 */
	public void add( Book element, int position ) {
		LinkedNode<Book> after = null;
		LinkedNode<Book> before = null;

		// Set before and after to the appropriate values
		if ( position == 0 ) {
			before = null;
			after = head;
		}
		else {
			before = positionToReference( position - 1 );
			after = before.getNext();
		}

		// Add the node to the list
		addNode( element, before, after );
	}

	/** 
	 * Add a new element at the beginning of the list.
	 * 
	 * Preconditions:
	 *   None
	 *
	 * Postconditions:
	 *   The element is added at the specified position in the list.
	 *   The size of the list has increased by 1.
	 *   The cursor refers to the element added to the list.
	 *
	 * @param element the element to be added to the list.
	 */
	public void addFirst(Book element) {
		addNode( element, null, head );
	}
	
	/**
	 * Returns the element that the cursor is referring to.
	 *
	 * @return the element the cursor refers to
	 *
	 * Precondtions:
	 *   The cursor is on the list.
	 *
	 * Postconditions:
	 *   The list's structure, content, and cursor are unchanged.
	 *
	 * @return the element the cursor refers to.
	 */
	public Book get() {
		return cursor.getData();
	}

	/** 
	 * Returns the element stored in the specified position in the list.
	 *
	 * @return the element at the given position
	 *
	 * Preconditions:
	 *   position >= 0 and position < size().
	 *
	 * Postconditions: 
	 *   The list's structure, content, and cursor are unchanged.
	 *
	 * @param position the location of the element to be retrieved.
	 *
	 * @return the element at the specified position.
	 */ 
	public Book get( int position ) {
		return positionToReference( position ).getData();
	}

	/**
	 * Sets the element stored at the location identified by the cursor.
	 *
	 * Preconditions:
	 *   The cursor is on the list.
	 *
	 * Postconditions:
	 *   The element at the current cursor position is changed to the
	 *	 specified value.
	 *   The list's structure and cursor are unchanged.
	 *
	 * @param element the element to place in the list.
	 */
	public void set( Book element ) {
		cursor.setData( element );
	}

	/** 
	 * Sets the element stored in the specified position in the list.
	 *
	 * Preconditions:
	 *   position >= 0 and position < size().
	 *
	 * Postconditions:
	 *   The element at the specified position in the list is changed
	 *	 to the specified value.
	 *   The list's structure and cursor are unchanged.
	 *
	 * @param position the location of the element to be changed.
	 * @param element the element to place in the specified location
	 *				of the list.
	 */ 
	public void set( Book element, int position ) {
		LinkedNode<Book> target = positionToReference( position );
		target.setData( element );
	}

	/**
	 * Return the number of elements in the list.
	 *
	 * Preconditions:
	 *   None.
	 *
	 * Postconditions:
	 *   The list's structure, content, and cursor are unchanged.
	 *
	 * @return the number of elements in the list.
	 */
	public int size() {
		return count;
	}

	/**
	 * Determine if the cursor is on the list.
	 *
	 * Preconditions:
	 *   None.
	 *
	 * Postcondtions:
	 *   The list's structure, content and cursor are unchanged.
	 *
	 * @return true if the cursor is on the list, false otherwise.
	 */
	public boolean isOnList() {
		return cursor != null;
	}

	/**
	 * Return a string representation of this list.
	 *
	 * @return a string representaion of this list.
	 */
	public String toString() {
		// Use a StringBuffer so we don't create a new
		// string each time we append
		StringBuffer retVal = new StringBuffer();

		retVal = retVal.append( "[ " );

		// Step through the list and use toString on the elements to
		// determine their string representation
		for ( LinkedNode<Book> cur = head; cur != null; cur = cur.getNext() ) {
			retVal = retVal.append( cur.getData() + " " );
		}

		retVal = retVal.append( "]" );

		// Convert the StringBuffer to a string
		return retVal.toString();
	}

	/**
	 * Return a reference to the previous node in the list.
	 *
	 * @return a reference to the node before the node referred to by
	 *		 the cursor.
	 */
	private LinkedNode<Book> findPrevious() {
		LinkedNode<Book> prev = null;  // Where we are in the list

		// If the cursor is off the list or the cursor is at the
		// head, there is no previous node.
		if ( cursor != null && cursor != head ) {
			prev = head;

			// Keep looking until we find the node whose next reference
			// is equal to the current node
		
			while ( prev.getNext() != cursor ) {
				prev = prev.getNext();
			}
		}

		// The variable prev is now refering to the previous node
		return prev;
	}

	/**
	 * Remove the specified node from the list.
	 *
	 * @param target the node to remove.
	 * @param prev the node before the node to be deleted.
	 */
	private void removeNode( LinkedNode<Book> target, LinkedNode<Book> prev ) {
		if(target != null){
			// Cursor is always set the the target's successor
			cursor = target.getNext();  

			if ( prev == null ) {
				// We are deleting the head 
				head = target.getNext();
			}
			else {
				// We are somewhere in the middle of the list
				prev.setNext( target.getNext() );
			}

			// Did we just delete the tail?	
			if ( target == last ) {
				last = prev;
			}
	 
			// One less item in the list
			count = count - 1;
		}
	}

	/**
	 * Add the specified element to the list between the nodes
	 * identified by before and after.
	 *
	 * @param element the element to add.
	 * @param before the node before the node to be added.
	 * @param after the node after the node to be added.
	 */
	private void addNode( Book element, LinkedNode<Book> before, LinkedNode<Book> after ) {

		// Create the node
		cursor = new LinkedNode<Book>( element, after );

		// Is this a new head?
		if ( before == null ) {
			head = cursor;
		} 
		else {
			before.setNext( cursor );
		}

		// Is it a new tail?
		if ( after == null ) {
			last = cursor;
		}   

		// One more element in the list
		count = count + 1;
	}

	/**
	 * Return a reference to the node with the specified position in the
	 * list.
	 *
	 * @param position the node to obtain the reference for.
	 *
	 * @return a reference to the node at the specified position.
	 */

	private LinkedNode<Book> positionToReference( int position ) {
		LinkedNode<Book> retVal = head;

		while ( position > 0 ) {
			retVal = retVal.getNext();
			position = position - 1;
		}

		return retVal;
	}



} // LinkedList





**** here is the beginning of my Inventory Class. I am creating a driver that will have given data that will run these methods.

import java.util.*;

public class Inventory 
{
	public static int inven;
	public static String addBook;
	public String inStockBook;
	public String byAuthor;
	public double recPurchase;
	public String bestSeller;
	public String refresh;
	public String close;
	Scanner in = new Scanner(System.in);


public Inventory(String newAddBook, String newInStockBook, String newByAuthor, double newRecPurchase, String newBestSeller,String newRefresh, String nowClose, int newInven)
{
	addBook = newAddBook;
	inStockBook = newInStockBook;
	byAuthor = newByAuthor;
	recPurchase = newRecPurchase;
	bestSeller = newBestSeller;
	refresh = newRefresh;
	close = nowClose;
	inven = newInven;
}

public static void main(String[] args) 
{
	String author;
	String title;
	double price;
	Scanner in = new Scanner(System.in);
	System.out.println("Please enter the author's name, The title of the book, and the price in seperate lines");
	author = in.nextLine();
	title = in.nextLine();
	price = in.nextDouble();
	addBook = author + title + price;
	
	System.out.println( "The author is: " + author + title + price );
	inven = addBook.length() +3;
	 
}

public String getInStock()
{
	for(i=0; i<= inven.length;i++)
	{
		if(inven >= 0)
		{
			System.out.println(addBook);
		}
		else
		{
			System.out.println("There are no books in the inventory!");
		}
	
	}
	

Was This Post Helpful? 0
  • +
  • -

#5 KrGmdeary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Homework Help (linked list/ Iinput Output)

Posted 13 November 2009 - 01:18 PM

Ok here is what I have of my driver and inventory class as well as two of the seven operations that i need to do. It will only read what I enter if I press one, but if I press 0 or 8, it will not return my system.out.print. If I press two, it will not complete the operation in Inventory.class. I Dont know what I'm doing wrong...

Inventory Class
import java.util.*;

public class Inventory 
{
	public static int inven;
	public static String addBook;
	public String inStockBook;
	public String byAuthor;
	public double recPurchase;
	public String bestSeller;
	public String refresh;
	public String close;
	Scanner in = new Scanner(System.in);


public Inventory(String newAddBook, String newInStockBook, String newByAuthor, double newRecPurchase, String newBestSeller,String newRefresh, String nowClose, int newInven)
{
	addBook = newAddBook;
	inStockBook = newInStockBook;
	byAuthor = newByAuthor;
	recPurchase = newRecPurchase;
	bestSeller = newBestSeller;
	refresh = newRefresh;
	close = nowClose;
	inven = newInven;
}

public String newBook() 
{
	String author = null;
	String title = null;
	double price = 0;
	return addBook= author + title + price;
	 
}

public String inStock()
{
	for(int i=0; i<= inStockBook.length(); i++)
	{
		if(inStockBook == null)
		{
			System.out.println("There are no books in the inventory!");
		}
		else
		{
			System.out.println(addBook);	
		}
	
	}
	return inStockBook;
	
}
public String byAuthor()
{
	for(int i=0; i<=Book.length();i++)
	{
		
}




}
}


My driver Class:
import java.util.Scanner;

 public class Driver
 {
	
	public static void main(String[] args)
   {
	Scanner myScanner = new Scanner(System.in);
	int numChoice;
	
	System.out.println("1. Add a new book to the inventory.");
	System.out.println("2. List all the books in stock.");
	System.out.println("3. List books by the author.");
	System.out.println("4. Record a purchase that has been made.");
	System.out.println("5. Print the Best Sellers.");
	System.out.println("6. Refresh the inventory.");
	System.out.println("7. Close the program.");
	System.out.println();
	System.out.println("Please choose a number between 1 and 7:");
	
	numChoice = Integer.parseInt(myScanner.nextLine());
	
	if (numChoice <0 && numChoice > 7)
	{
		
		System.out.println("You have selected a number that is not on the menu.  Please select again!");
	
   
	if (numChoice == 1)
	{
		Book newBook = null;
		String author;
		String title;
		double price;
		
		System.out.println("Please enter the author's name: ");
		author = myScanner.nextLine();
		System.out.println("Please enter the title's name: ");
		title = myScanner.nextLine();
		System.out.println("Please enter the price of the book: ");
		price = myScanner.nextDouble();
		
		System.out.println( author+ " "  +title+ " " +price);
		
   
	if(numChoice == 2)
	{
		newBook.getInStock();
		System.out.println(newBook.getInStock());
	}
	
	}
	
	
	
	
	
	
	}
   
  
   
   
   
   
   
   
   }}






I cannot firgure out how to get my driver to call the methods from my inventory class do compile the operations? Any guidance will be greatly apprectiated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1