Inventory program

I am super new to java and so I am learning everyday. I have been putting together an inventory program and have been coming up with some errors. Any help would be great.

Here is what happened. I am using TextPad and JavaBuilder. Here is the code that I have so far

/*

* Main.java

*

* Created on July 19, 2007, 5:54 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*

*List item number, product name, quantity, price per unit, value of product, and total value of inventory.

*/

import java.util.Scanner;//program uses class Scanner

//ShowInventory program

publicclass ShowInventory

{//main method begins execution of java application

publicstaticvoid main (String args[])

{

Scanner myScanner =new Scanner (System.in);

//item name variable

String itemName1 = cd;//item name for inventory of music cd

String itemName2 = dvd;//item name for inventory of video media

String itemName3 = book;//item name for inventory of printed media

//item code variable

int code1 = 1000;//item code assigned to music cds

int code2 = 2000;//item code assigned to video media

int code3 = 3000;//item code assigned to printed media

//item quantity variable

int quantity1 = 15;//number of music cds on-hand

int quantity2 = 11;//number of video media on-hand

int quantity3 = 27;//number of printed media on-hand

//price of media variable

double price1 = 14.99;//assigned price of music cd

double price2 = 19.99;//assigned price of video media

double price3 = 12.99;//assigned price of printed media

//cd

class CD

{

private String name;

privateint itemCode;

privateint itemQuantity;

privatedouble itemPrice;

//constructor to initialize the item with the information provided

public CD(String name,int itemCode,int itemQuantity,double itemPrice)

{

setName(name);

setItemCode(itemCode);

setItemQuantity(itemQuantity);

setItemPrice(itemPrice);

}

//get item name

public String getName()

{

return this.name;

}

//set item name

privatevoid setName(String name)

{

this.name = name;

}

//set item code

privatevoid setItemCode(int itemCode)

{

this.itemCode = itemCode;

}

//set item quantity

privatevoid setItemQuantity(int itemQuantity)

{

this.itemQuantity = itemQuantity;

}

//set price of item

privatevoid setItemPrice (double itemPrice)

{

this.itemPrice = itemPrice;

}

//calculate value of item on-hand

publicdouble getItemValue()

{

return this.itemQuantity * this.itemPrice;

}

//DVD

class DVD

{

private String name;

privateint itemCode;

privateint itemQuantity;

privatedouble itemPrice;

//constructor to initialize the item with the information provided

public DVD(String name,int itemCode,int itemQuantity,double itemPrice)

{

setName(name);

setItemCode(itemCode);

setItemQuantity(itemQuantity);

setItemPrice(itemPrice);

}

//get item name

public String getName()

{

return this.name;

}

//set item name

privatevoid setName(String name)

{

this.name = name;

}

//set item code

privatevoid setItemCode(int itemCode)

{

this.itemCode = itemCode;

}

//set item quantity

privatevoid setItemQuantity(int itemQuantity)

{

this.itemQuantity = itemQuantity;

}

//set price of item

privatevoid setItemPrice (double itemPrice)

{

this.itemPrice = itemPrice;

}

//calculate value of item on-hand

publicdouble getItemValue()

{

return this.itemQuantity * this.itemPrice;

}

//book

class Book

{

private String name;

privateint itemCode;

privateint itemQuantity;

privatedouble itemPrice;

//constructor to initialize the item with the information provided

public Book(String name,int itemCode,int itemQuantity,double itemPrice)

{

setName(name);

setItemCode(itemCode);

setItemQuantity(itemQuantity);

setItemPrice(itemPrice);

}

//get item name

public String getName()

{

return this.name;

}

//set item name

privatevoid setName(String name)

{

this.name = name;

}

//set item code

privatevoid setItemCode(int itemCode)

{

this.itemCode = itemCode;

}

//set item quantity

privatevoid setItemQuantity(int itemQuantity)

{

this.itemQuantity = itemQuantity;

}

//set price of item

privatevoid setItemPrice (double itemPrice)

{

this.itemPrice = itemPrice;

}

//calculate value of item on-hand

publicdouble getItemValue()

{

return this.itemQuantity * this.itemPrice;

}

//calculate combined value of all inventory

double inventoryValue = cdItemPrice + dvdItemPrice + bookItemPrice;

//build array to house data

//InitInventory.java

publicclass InitInventory

//initializer list specifies the value for each element

{

String inventory[] ={CD, DVD, Book};

int itemcode [] ={1000, 2000, 3000};

int itemquantity [] ={15,11,27};

double itemprice [] ={14.99, 19.99, 12.99};

double itemvalue [] ={15*14.99, 11*19.99, 27*12.99};

System.out.printf("%s&s\n","Index","Value");//column headings

//output each array elements value

for (String counter = 0; counter < array.length; counter ++)

System.out.printf("%5d%8d\n", counter, array[counter]);

for (int counter = 0; counter < array.length; counter ++)

System.out.printf("%5d%8d\n", counter, array[counter]);

for (double counter = 0; counter < array.length; counter ++)

System.out.printf("%5d%8d\n", counter, array[counter]);

}//end class InitInventory

}//end main

}//end ShowInventory

Now I seem to be getting 18 errors. Which sucks. Here are the errors.

Here are the errors in more detail.

C:\Users\Brian\Documents\ShowInventory.java:222: <identifier> expected

System.out.printf("%s&s\n","Index","Value");//column headings

^

C:\Users\Brian\Documents\ShowInventory.java:222: illegal start of type

System.out.printf("%s&s\n","Index","Value");//column headings

^

C:\Users\Brian\Documents\ShowInventory.java:225: illegal start of type

for (String counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:225: > expected

for (String counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:225: <identifier> expected

for (String counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:226: <identifier> expected

System.out.printf("%5d%8d\n", counter, array[counter]);

^

C:\Users\Brian\Documents\ShowInventory.java:226: illegal start of type

System.out.printf("%5d%8d\n", counter, array[counter]);

^

C:\Users\Brian\Documents\ShowInventory.java:228: illegal start of type

for (int counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:228: > expected

for (int counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:228: <identifier> expected

for (int counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:229: <identifier> expected

System.out.printf("%5d%8d\n", counter, array[counter]);

^

C:\Users\Brian\Documents\ShowInventory.java:229: illegal start of type

System.out.printf("%5d%8d\n", counter, array[counter]);

^

C:\Users\Brian\Documents\ShowInventory.java:231: illegal start of type

for (double counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:231: > expected

for (double counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:231: <identifier> expected

for (double counter = 0; counter < array.length; counter ++)

^

C:\Users\Brian\Documents\ShowInventory.java:232: <identifier> expected

System.out.printf("%5d%8d\n", counter, array[counter]);

^

C:\Users\Brian\Documents\ShowInventory.java:232: illegal start of type

System.out.printf("%5d%8d\n", counter, array[counter]);

^

C:\Users\Brian\Documents\ShowInventory.java:238: reached end of filewhile parsing

}//end ShowInventory

^

18 errors

Tool completed with exit code 1

Any help would be welcome

[17456 byte] By [Nexcompaca] at [2007-11-27 11:53:21]
# 1

For starters get all those other classes out of your main method.

floundera at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...
# 2

many errors that I see..

//output each array elements value

for (String counter = 0; counter < array.length; counter ++)

System.out.printf("%5d%8d\n", counter, array[counter]);

for (int counter = 0; counter < array.length; counter ++)

System.out.printf("%5d%8d\n", counter, array[counter]);

for (double counter = 0; counter < array.length; counter ++)

System.out.printf("%5d%8d\n", counter, array[counter]);

}//end class InitInventory

Your for loops should be like..

for( int counter = 0; counter < itemCode.length /** the name of the array */ ; counter++ )

System.out.printf(/* ... */);

here..

String itemName1 = cd;//item name for inventory of music cd

String itemName2 = dvd;//item name for inventory of video media

String itemName3 = book;//item name for inventory of printed media

/** If "cd", "dvd" and "book" are strings, then they should be enclosed in quotes as such */

String itemName1 = "cd";

String itemName2 = "dvd"

// and so on

Fix those and then repost your errors.. And also as last guy said, get all of those new classes out of your main method.

smithdale87a at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...
# 3

F

Nexcompac1a at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...
# 4

> For starters get all those other classes out of your

> main method.

Ok, so how do I keep the main project and the other classes seperate but still running?

Nexcompaca at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...
# 5

> F

G

corlettka at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...
# 6

You can keep them in the same file if you really want to, though it's considered poor form.... just move those blocks of code above (or below) the main method.

corlettka at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...
# 7

This is maybe too much advanced for you, but for OO programming's sake you should really consider, instead of having a CD, DVD, and Book class that are identical, having just one class InventoriedItem (or as you want to call it). You can then subclass it three times, or put a field on it to indicate which item (a CD, DVD or book) it's referring to.

java_knighta at 2007-7-29 18:49:47 > top of Java-index,Java Essentials,New To Java...