Welcome to Dream.In.Code
Become a Java Expert!

Join 150,118 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,972 people online right now. Registration is fast and FREE... Join Now!




Started Over with new Program

 
Reply to this topicStart new topic

Started Over with new Program, Inventory Program Parts 1 to 3

javagreenhorn
21 Jun, 2008 - 03:10 PM
Post #1

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 24

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.

CODE

* 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:

CODE


* 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:

CODE

/* 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??
User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Started Over With New Program
21 Jun, 2008 - 06:27 PM
Post #2

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
CODE
(item.getItemValue()));


There is an extra ')' here ...
User is offlineProfile CardPM
+Quote Post

javagreenhorn
RE: Started Over With New Program
21 Jun, 2008 - 06:33 PM
Post #3

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 24

I have tried removing one and I get the same error message.
User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Started Over With New Program
21 Jun, 2008 - 06:41 PM
Post #4

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
CODE
System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + "Value of Stock: " + formatter.format(item.getItemValue()));


You forgot a '+' after the "Vlue of Stock" smile.gif
User is offlineProfile CardPM
+Quote Post

javagreenhorn
RE: Started Over With New Program
21 Jun, 2008 - 07:07 PM
Post #5

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 24

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.
User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Started Over With New Program
21 Jun, 2008 - 07:20 PM
Post #6

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
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 tongue.gif ...

This post has been edited by neotrumatrix: 21 Jun, 2008 - 07:22 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:15AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month