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!
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");
} } // 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; } }
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 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.
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: 2 Apr, 2008 - 07:42 PM
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
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());
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.