Hello,

I have to create a product class that holds the item number, product name, units in stock, and the unit price.

I also have to create a java application that displays the same info.

I have been working on this for three days now... I am getting frustrated because it is due tonight and it doesn't seem to work.

A little help will be very much appreciated..

Here is what I have so far:

import java.util.Arrays;

	public class InventoryProgramPart1
	{

	public static void main(String[] args)
	{
		InventoryProgramPart1 invProgram = new InventoryProgramPart1();
		invProgram.musicCD();
  		}

		String musicCd[] = { "Fergie", "Celine Dion","Kelly Clarkson", "Chris Daughtry"};
		double itemNum;
		double count[] = {5, 7, 3, 8};
		double totalValue [] = new double [4];
		double price[] = { 19.95, 21.99, 16.50, 21.75};
		double totalInvVal;

		// Method for printing CD Titles
		public void musicCd()
		{
			// For loop to calculate total value
			Arrays.sort(musicCd);
			for( int itemNum = 0; itemNum < musicCd.length; itemNum ++ )
			totalValue[itemNum] = count[itemNum] * price[itemNum];


			System.out.printf( "%s %15s %12s %12s %12s\n", "Item Number", "CD Title", "Price", "Stock", "Total");

			for ( int itemNum = 0; itemNum <musicCd.length; itemNum ++ )

			System.out.printf("%-8d %20s %10.02f %12.02f %12.02f\n",
				itemNum, musicCd[itemNum], price [itemNum],count[itemNum], totalValue[itemNum]);
			} // end of method to print dvdName

			// Method for total value of the inventory
			public void totalInvValue()

			{ //start of method to calculate the total inventory value
			totalInvVal = count [0] * price [0] + count [1] * price [1] + count [2] * price [2] + count [3] * price [3];
			System.out.printf("%s", "The Total Value of the Inventory is:","%10.02f");
			System.out.printf("$%.02f", totalInvVal);

		} // end of method

	} // end public class InventoryProgramPart1

and here is the error:

Cannot find the symbol
Symbol: method musicCd()
Location: class InventoryProgramPart1
invProgram.musicCD();

I think that because you do something like this:

String musicCd[] = { "Fergie", "Celine Dion","Kelly Clarkson", "Chris Daughtry"};

public void musicCd() {

}

Maybe because you are using the same name. I am not sure but you might want to test it

I have a few extra suggestions, but I didn't have time to post them because I was hungry.

First:

String musicCd[] = { "Fergie", "Celine Dion","Kelly Clarkson", "Chris Daughtry"};
double count[] = {5, 7, 3, 8};
double price[] = { 19.95, 21.99, 16.50, 21.75};

Arrays.sort(musicCd);

As you can see you sort the musicCD array but the other arrays are left unchanged. If we assume that initially you meant that:
Fergie, 5, 19.95
Celine Dion, 7, 21.99

Then after the sort the elements at the array will change order but the elements at the other arrays will not and will not correspond to the right title.

One way to fix this is write your own sorting method (bubblesort) to sort the musicCD array (use the compareTo method of the String class) and whenever you swap 2 elements do the same with the other arrays.

Second:
Did you understand the above suggestion? . . . Yes? . . Ok, now forget the above suggestion because:
There are 3 ways to do things:
The right way,
The wrong way,
And the Object Oriented Way

And the above doesn't use the OO Way.
You need an Object to represent a music CD. Try this:

public class MusicCD {
    private String title = null;
    private int count = 0;
    private double price = 0.0;

    public MusicCD() {
        
    }

    public MusicCD(String title, int count, double price) {
        setCount(count);
        setTitle(title);
        setPrice(price);
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    /**
     * @param count the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @return the price
     */
    public double getPrice() {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }
}

And in a main method you can do this:

public static void main(String [] args) {
        MusicCD [] cdS = {
            new MusicCD("Fergie", 5, 19.95),
            new MusicCD("Celine Dion", 7, 21.99)
        };

        for (int i=0;i<cdS.length;i++) {
            System.out.println(cdS[i].getTitle()+", "+cdS[i].getCount()+", "+cdS[i].getPrice());
        }

        for (int i=0;i<cdS.length;i++) {
            System.out.println(cdS[i]);
        }
    }

In order for the last: System.out.println(cdS[i]); to work your class MusicCD needs to override the toString method. Whenever you do this: System.out.println(anObject); you call the toString method of that object. So if you add this at your MusicCD class:

public String toString() {
        return title+", "+count+", "+price;
    }

The last System.out.println, will work. You can return whatever you want at the toString method

Third:
About sorting: Arrays.sort(musicCd); You can also do this:

MusicCD [] cdS = {
            new MusicCD("Fergie", 5, 19.95),
            new MusicCD("Celine Dion", 7, 21.99)
        };

Arrays.sort(cdS);

But your class needs to implement the Comparable interface. Once you do that override the compareTo method. When you call the sort() method, "java" does the sorting based on what the compareTo method will return in order to determine whether one element of the array is greater than the other. when you did this: Arrays.sort(musicCd); "java" used the compareTo method of the String class.
Now it must use the compareTo method of the MusicCD class. So implement the Comparable interface and override that method. Follow the link that will help you understand how the compareTo method should be implemented. I will leave that to you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.