Started Over with new Program

Inventory Program Parts 1 to 3

Page 1 of 1

5 Replies - 1900 Views - Last Post: 21 June 2008 - 08:20 PM Rate Topic: -----

#1 javagreenhorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-June 08

Started Over with new Program

Post icon  Posted 21 June 2008 - 04:10 PM

Hi again, I am back. I have decided to scrap what I had and start over, sort of. This is what I have now for the Inventory Program Part 3.

 * Inventory Program - Parts 1
 */

package productjava;

/** Inventory Program 1-3
 * IT 215
 * @author Tammy 
 * June 21, 2008
 */ 
public class Product {
	// Private variables
	private String name;
	private int quantity;
	private double price;
	private int productid = 0;
	
	// Set the default state of the object
	public Product() {
		this(0, "Unknown",0,0.00);
	}
   
	// User can specify name, quantity, and price for items
	public Product (int productId, String itemname, int quantityOnHand, double itemprice) {
		productid = productId;
		setName (itemname);
		setQuantityOnHand(quantityOnHand);
		setPrice (itemprice);
	}
	// Change state of object
	public void setName(String itemname) {
		name = itemname;
		
	}
	// Set quantity on hand and default to zero, if negative
	public void setQuantityOnHand(int quantityOnHand) {
		if (quantityOnHand > 0) {
			quantity = quantityOnHand;
		}
		else {quantity = 0;}
	}
	
	// Set product price and default to zero, if negative
	public void setPrice (double itemPrice) {
		if (itemPrice > 0.00) {
			price = itemPrice;
		}
		else {price = 0.00;}
	}
	
	public String getName() {
		return name;
	}
	
	public int getQuantityOnHand() {
		return quantity;
	}
	
	public double getPrice() {
		return price;
	}
	
	// Calculate value of stock on this item
	public double getItemValue() {
		return (price * (double)quantity);
	}
	
	// String that represents product
	@Override
	public String toString() {
		return name + " - " + price;
	}
	
		}
	





Part 2:


 * Inventory Program Part 2
 */

package productjava;

/** Inventory Program Part 1-3
 * IT 21
 * @author Tammy
 * June 21, 2008
 */

import java.text.DecimalFormat;

public class Inventory {
	// Set up an array of Products to hold 30 items
	int inventorySize = 30;
	private Product items[] = new Product[inventorySize];
	
	// Set up to format values into currency
	DecimalFormat formatter = new DecimalFormat ("$##,###.00");
	
	// Adds a product to first empty slot in array
	public void addProduct(Product item) {
		for (int i = 0; i < inventorySize; i++) {
			if (items[i] == null) {
				items[i] = item;
				return;
			}
		}
	}
	
	// Add up total value of products
	// Add quantity on hand, item by item; and multiply it by its price
	// Add the value to a running total variable
	public double getTotalInvValue() {
		double sumOfInventory = 0.0;
		
		for (Product item : items) {
			if (item != null) {
				sumOfInventory += item.getItemValue();
			}
		}
		return sumOfInventory;
	}
	
	// Prints inventory list including name, quantity, price, and total stock value for each item
	public void printInventory() {
		System.out.println("Printing items in inventory...\n");
		
		boolean hasItems = false;
		
		for (Product item : items) {
			if (item != null) {
				hasItems = true;
				System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + "Value of Stock: " formatter.format(item.getItemValue()));
				
			}
		}
		// Print message if no items are found
		if (!hasItems) {
			System.out.println("Inventory is currently empty.\n");
			}
		}
}




and Part 3:

/* Inventory Program Part 3
 * Inherited class from class Product
 * Extra feature of year movie was made
 */

package productjava;

/** Inventory Program Part 1-3
 * IT 215
 * @author Tammy
 * June 21, 2008
 */

public class DVD extends Product {
	// Hold year movie was made
	private int movieyear;
	
	
	public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) {
		super(productId, itemname, quantityOnHand, itemprice);
		movieyear = year;
	}
	
	public void setYear (int year) {
		movieyear = year;
	}
	
	public int getYear() {
		return movieyear;
	}
	
	//Override getItemValue() in Product class; calls base class version and adds a 5% restocking fee
	@Override
	public double getItemValue() {
		return super.getItemValue() * 1.05;
		
	}
	
	public double getRestockingFee () {
		return super.getItemValue() * .05;
		
	}

}





This is my error message:

Compiling 3 source files to C:\Documents and Settings\Tammy\MyDocuments\NetBeansProjects\Product.java\build\classes C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Product.java\src\productjava\Inventory.java:56: ')' expected
System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + "Value of Stock: " formatter.format(item.getItemValue()));
1 error



I see that I am missing a quotation mark somewhere, but I cannot seem to find where??

Is This A Good Question/Topic? 0
  • +

Replies To: Started Over with new Program

#2 neotrumatrix  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-October 05

Re: Started Over with new Program

Posted 21 June 2008 - 07:27 PM

(item.getItemValue()));


There is an extra ')' here ...
Was This Post Helpful? 0
  • +
  • -

#3 javagreenhorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-June 08

Re: Started Over with new Program

Posted 21 June 2008 - 07:33 PM

I have tried removing one and I get the same error message.
Was This Post Helpful? 0
  • +
  • -

#7 neotrumatrix  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-October 05

Re: Started Over with new Program

Posted 21 June 2008 - 07:41 PM

System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + "Value of Stock: " + formatter.format(item.getItemValue()));


You forgot a '+' after the "Vlue of Stock" :)
Was This Post Helpful? 1

#8 javagreenhorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-June 08

Re: Started Over with new Program

Posted 21 June 2008 - 08:07 PM

Thanks sooo much! Now I have a different problem, somehow I do not have a main class and this program will not run. It states " projectjava.Main class wasn't found in Product.java package.
AAArrrggghhhh.
Was This Post Helpful? 0
  • +
  • -

#9 neotrumatrix  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-October 05

Re: Started Over with new Program

Posted 21 June 2008 - 08:20 PM

What are you actually trying to do ?? You have a product inventory and a DVD class (Which i don't see you using anywhere) ... You want to add the sum of all products and print the inventory values right ??

If yes then add a main method inside the Inventory class or create a separate class for executing the entire program.

The main method should look somewhat like this ...

Product p1 = new Product(pass the necessary values)
Product p2 = ...
..
..
..

Create an instance of Inventory class
Inventory i = new Inventory();
i.addProduct (p1);
i.addProduct (p2);
..
..
System.out.println(i.getTotalInvValue());
i.printInventory();

P.S And obviously you havent paid much attention to Object Oriented Programming classes :P ...

This post has been edited by neotrumatrix: 21 June 2008 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1