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

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




can't get program to run when all put together

 
Reply to this topicStart new topic

can't get program to run when all put together, program not running

jlewis25
2 Apr, 2008 - 06:38 PM
Post #1

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 21

I can't seem to get my program to work when I put the four files together. When compiled seperat they have no errors, but when put together get errors like class,interface,or enum expected on line 21. Any input would be greatly apprecaited for I have to move forward and I can't untill this works.
Thanks
CODE

public class testInventory {  
     public static void main(String args[]){  
          // Create an inventory object  
          Inventory inv = new Inventory();  
  
          // Create some products to put into inventory  
          DVD movie = new DVD(1,"Cars", 10, 23.99,2007);  
          DVD movie = new DVD(2,"American Gangster", 4, 11.99,2008);  
          DVD movie = new DVD(3, "Gladiator DVD", 3, 15.99, 2000);  
  
          // Add the products to the inventory object using addProduct  
          inv.addProduct(product1);  
          inv.addProduct(product2);  
          inv.addProduct(movie);  
  
          // Now you could display the inventory total  
          System.out.println("Total inventory value is: " + inv.getTotalInvValue());  
     }  
}  

import java.text.DecimalFormat;

public class Inventory {

int inventorySize = 30;
private Product items[] = new Product [inventorySize];

DecimalFormat formatter = new DecimalFormat ("$##,###.00");


// Adds a product to the array of products.

public void addProduct(Product item) {
    for (int i =0; i < inventorySize; i++) {
        if (items[i] == null) {
            items[i] = item;
            return;
        }
    }
}

public double getTotalInvValue() {
    double sumOfInventory = 0.0;
    
    for (Product item : items) {
        if (item != null) {
            sumOfInventory += item.getItemValue();
        }
    }
    return sumOfInventory;
}

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()));
            
        }
    }
    // If no items were found, print a message saying the inventory is empty
    if (!hasItems) {System.out.println("Inventory is empty at the moment. \n"); }
    
}
}

public class Product{
    //Private variables
    private String name;
    private int quantity;
    private double price;
    private int productid = 0;
    
    public Product() {
        this(0,"American Gangster",4,19.99);
        
    }
    public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
        productid = productID;
        setName(itemname);
        setQuantityOnHand(quantityOnHand);
        setPrice(itemprice);
    }
    
    public void setName(String itemname) {
        name = itemname;
    }
    
    public void setPrice(double itemPrice) {
        if (itemPrice > 0.00) {
            price =itemPrice;
        }
        else { price = 0.00;}
    }
    public void setQuantityOnHand(int theQuantity) {
        quantity = theQuantity;
    }
    
    public String getName() {
        return name;
    }
    
    public int getQuantityOnHand() {
        return quantity;
    }
    
    public double getPrice() {
        return price;
    }
    
    public double getItemValue() {
        return (price * (double)quantity);
    }
    
    public String toString() {
        return name + "-" + price;
    }
}

        
    
// Inherited class DVD from the base class Product

public class DVD extends Product  {
    // Holds the year of movie
    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;
    }
    
    //Get the year of this DVD
    public int getYear() {
        return movieyear;
    }
    
    // adding a 5% restocking fee
    public double getItemValue() {
        return super.getItemValue() *1.05;
    }
    
    public double getRestockinkingFee() {
        return super.getItemValue() * .05;
    }
}

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Can't Get Program To Run When All Put Together
2 Apr, 2008 - 07:13 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
You don't put multiple classes in the same file. Each class needs to be in its own file that shares the same exact name as the class, but has the '.java' extension.

You're going to need 4 files:
  • testInventory.java
  • Inventory.java
  • Product.java
  • DVD.java

User is offlineProfile CardPM
+Quote Post

jlewis25
RE: Can't Get Program To Run When All Put Together
2 Apr, 2008 - 07:32 PM
Post #3

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 21

QUOTE(GWatt @ 2 Apr, 2008 - 08:13 PM) *

You don't put multiple classes in the same file. Each class needs to be in its own file that shares the same exact name as the class, but has the '.java' extension.

You're going to need 4 files:
  • testInventory.java
  • Inventory.java
  • Product.java
  • DVD.java

thank I did have them in four diff file but I don't understand what or how you make this program run and display and add inventory and all that good stuff.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Can't Get Program To Run When All Put Together
2 Apr, 2008 - 07:38 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
javac *.java
java testInventory
User is offlineProfile CardPM
+Quote Post

Purity86
RE: Can't Get Program To Run When All Put Together
2 Apr, 2008 - 07:39 PM
Post #5

New D.I.C Head
*

Joined: 28 Jan, 2008
Posts: 35


My Contributions
not sure if this is what you mean but,

testInventory is the only class with a main method *Which is required to execute* which means testInventory is the only class that you can execute with the "Java" command.

use this command
java testInventory

edit *pbl beat me to it smile.gif *

This post has been edited by Purity86: 2 Apr, 2008 - 07:42 PM
User is offlineProfile CardPM
+Quote Post

jlewis25
RE: Can't Get Program To Run When All Put Together
2 Apr, 2008 - 07:58 PM
Post #6

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 21

QUOTE(Purity86 @ 2 Apr, 2008 - 08:39 PM) *

not sure if this is what you mean but,

testInventory is the only class with a main method *Which is required to execute* which means testInventory is the only class that you can execute with the "Java" command.

use this command
java testInventory

edit *pbl beat me to it smile.gif *

thanks guy I got that part but what I don't understand is how to make it show a list of what you have and how to add more to it the inventory that is
User is offlineProfile CardPM
+Quote Post

pbl
RE: Can't Get Program To Run When All Put Together
2 Apr, 2008 - 08:32 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
You will have to play with your main method
Actually when I have a look at it

CODE

public class testInventory {  
     public static void main(String args[]){  
          // Create an inventory object  
          Inventory inv = new Inventory();  
  
          // Create some products to put into inventory  
          DVD movie = new DVD(1,"Cars", 10, 23.99,2007);  
          DVD movie = new DVD(2,"American Gangster", 4, 11.99,2008);  
          DVD movie = new DVD(3, "Gladiator DVD", 3, 15.99, 2000);  
  
          // Add the products to the inventory object using addProduct  
          inv.addProduct(product1);  
          inv.addProduct(product2);  
          inv.addProduct(movie);  
  
          // Now you could display the inventory total  
          System.out.println("Total inventory value is: " + inv.getTotalInvValue());  
     }  
}  


You create movie with "Cars"
Then movie with "American Gangster" erasing "Cars"
The movie with "Gladiator DVD" erasing "American Gangsters"

what are product1 and product2 ?

the only product you add to your inventory is movie holding "Gladiator DVD"

may be you want to

CODE

          DVD movie = new DVD(1,"Cars", 10, 23.99,2007);  
          inv.addProduct(movie);  
          System.out.println("Total inventory value 1 is: " + inv.getTotalInvValue());  
          movie = new DVD(2,"American Gangster", 4, 11.99,2008);  
          inv.addProduct(movie);  
          System.out.println("Total inventory value 2 is: " + inv.getTotalInvValue());  
          movie = new DVD(3, "Gladiator DVD", 3, 15.99, 2000);  
          inv.addProduct(movie);  
          System.out.println("Total inventory value 3 is: " + inv.getTotalInvValue());





User is offlineProfile CardPM
+Quote Post

jlewis25
RE: Can't Get Program To Run When All Put Together
3 Apr, 2008 - 08:26 PM
Post #8

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 21

QUOTE(pbl @ 2 Apr, 2008 - 09:32 PM) *

You will have to play with your main method
Actually when I have a look at it

CODE

public class testInventory {  
     public static void main(String args[]){  
          // Create an inventory object  
          Inventory inv = new Inventory();  
  
          // Create some products to put into inventory  
          DVD movie = new DVD(1,"Cars", 10, 23.99,2007);  
          DVD movie = new DVD(2,"American Gangster", 4, 11.99,2008);  
          DVD movie = new DVD(3, "Gladiator DVD", 3, 15.99, 2000);  
  
          // Add the products to the inventory object using addProduct  
          inv.addProduct(product1);  
          inv.addProduct(product2);  
          inv.addProduct(movie);  
  
          // Now you could display the inventory total  
          System.out.println("Total inventory value is: " + inv.getTotalInvValue());  
     }  
}  


You create movie with "Cars"
Then movie with "American Gangster" erasing "Cars"
The movie with "Gladiator DVD" erasing "American Gangsters"

what are product1 and product2 ?

the only product you add to your inventory is movie holding "Gladiator DVD"

may be you want to

CODE

          DVD movie = new DVD(1,"Cars", 10, 23.99,2007);  
          inv.addProduct(movie);  
          System.out.println("Total inventory value 1 is: " + inv.getTotalInvValue());  
          movie = new DVD(2,"American Gangster", 4, 11.99,2008);  
          inv.addProduct(movie);  
          System.out.println("Total inventory value 2 is: " + inv.getTotalInvValue());  
          movie = new DVD(3, "Gladiator DVD", 3, 15.99, 2000);  
          inv.addProduct(movie);  
          System.out.println("Total inventory value 3 is: " + inv.getTotalInvValue());



ok that makes sense.But how do i recall what is already in the inventory. maybe I have not gotten this far in class but curios minds want to know.
User is offlineProfile CardPM
+Quote Post

thef0rce
RE: Can't Get Program To Run When All Put Together
3 Apr, 2008 - 10:16 PM
Post #9

New D.I.C Head
*

Joined: 2 Nov, 2006
Posts: 39


My Contributions
to see what's in the inventory, you can use your printInventory method on the inventory object.

CODE
inv.printInventory();


if you want to actually get the stuff out again, you'll have to write another method.
User is offlineProfile CardPM
+Quote Post

jlewis25
RE: Can't Get Program To Run When All Put Together
4 Apr, 2008 - 04:17 PM
Post #10

New D.I.C Head
*

Joined: 22 Mar, 2008
Posts: 21

QUOTE(thef0rce @ 3 Apr, 2008 - 11:16 PM) *

to see what's in the inventory, you can use your printInventory method on the inventory object.

CODE
inv.printInventory();


if you want to actually get the stuff out again, you'll have to write another method.

would that be in the main method
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:19PM

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