9 Replies - 807 Views - Last Post: 04 April 2008 - 05:17 PM Rate Topic: -----

#1 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

can't get program to run when all put together

Post icon  Posted 02 April 2008 - 07:38 PM

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
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;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: can't get program to run when all put together

#2 GWatt  Icon User is offline

  • member icon

Reputation: 274
  • View blog
  • Posts: 3,074
  • Joined: 01-December 05

Re: can't get program to run when all put together

Posted 02 April 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

Was This Post Helpful? 0
  • +
  • -

#3 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

Re: can't get program to run when all put together

Posted 02 April 2008 - 08:32 PM

View PostGWatt, on 2 Apr, 2008 - 08:13 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: can't get program to run when all put together

Posted 02 April 2008 - 08:38 PM

javac *.java
java testInventory
Was This Post Helpful? 0
  • +
  • -

#5 Purity86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 28-January 08

Re: can't get program to run when all put together

Posted 02 April 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 :) *

This post has been edited by Purity86: 02 April 2008 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#6 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

Re: can't get program to run when all put together

Posted 02 April 2008 - 08:58 PM

View PostPurity86, on 2 Apr, 2008 - 08:39 PM, said:

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

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
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: can't get program to run when all put together

Posted 02 April 2008 - 09:32 PM

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

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

		  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()); 



Was This Post Helpful? 0
  • +
  • -

#8 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

Re: can't get program to run when all put together

Posted 03 April 2008 - 09:26 PM

View Postpbl, on 2 Apr, 2008 - 09:32 PM, said:

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

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

		  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.
Was This Post Helpful? 0
  • +
  • -

#9 thef0rce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 02-November 06

Re: can't get program to run when all put together

Posted 03 April 2008 - 11:16 PM

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

 inv.printInventory(); 


if you want to actually get the stuff out again, you'll have to write another method.
Was This Post Helpful? 0
  • +
  • -

#10 jlewis25  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 22-March 08

Re: can't get program to run when all put together

Posted 04 April 2008 - 05:17 PM

View Postthef0rce, on 3 Apr, 2008 - 11:16 PM, said:

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

 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1