\**
* InventoryProgramPart3.java
* @ author Amy Summers
* Inventory Program which uses an array to store items and creates
* a subclass of the CD class
*
*/
import java.util.Arrays;//program uses Arrays

class InventoryPart3
{
	//main method begins execution of Java program
	public static void main (String[] args)
	{	
		
		double total = 0.0;
		
		CD inventory [ ] = new CD [8];//allocates 8 integers
		
		//create object
		inventory[0] = new CD("Fantasia","Fantasia",00012, 10, 13.00);
		inventory[1] = new CD("Tamia","Tamia", 00015, 10, 15.00);
		inventory[2] = new CD("Love","Alicia Keys", 00016, 10, 16.00);
		inventory[3] = new CD("Always","Keisha Cole", 00025, 10, 15.00);
		inventory[4] = new CD("Al","Al Green", 00022, 10, 12.00);
		inventory[5] = new CD("Smile","Marvin Gaye", 00026, 10, 13.00);
		inventory[6] = new CD("Jump","DJ Quick", 00036, 10, 13.00);
		inventory[7] = new CD("Graduation","Kanye West", 00017, 10, 16.00);
		


	for (int counter = 0; counter < 8; counter++)
      {
 
         System.out.printf(inventory[counter].toString());
 
      } // end for
      
       System.out.printf( "The total value of the inventory is $%.2f\n\n", total );

				
	}//end main
}// end class InventoryPart3


/**
* CD class that uses one additional unique feature of the CD
* Artist is the additional feature
*/

class CD
{
	 String NameIn;
	 String ArtistIn;
	 double  NumberIn;
	 double UnitsIn;//number of units in stock
	 double PriceIn;//price of each unit
	 double ValueIn;//value of inventory
	
	//five-argument constructor initializes CD
	public CD( String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn )
             {
		//implicit call to Object constructor occurs here
			String product = NameIn;// initialize name
			String Artist = ArtistIn;//initialize name
			double number = NumberIn;// initialize number
			double units = UnitsIn;// initialize units
			double price = PriceIn;// initialize price
	 }//end five-argument constructor

	// set product name
	public void setNameIn (String product)
	{
	  product = NameIn;//store product name
	}//end method setNameIn

	//return product name
	public String getNameIn()
	{
	  return NameIn;
	}//end method getNameIn

	//set artist name
	public void setArtistIn (String artist)
	{
	artist = ArtistIn;//store artist name
	}//end method setArtistIn
	
	//return artist name
	public String getArtistIn()
	{
	return ArtistIn;
	}//end method getArtistIn

	
	// set number
	public void setNumberIn (double number)
	{

	  number = (NumberIn > 0.0) ? 0.0: number;//store number
	}//end method setNumberIn

	//return number
	public double getNumberIn()
	{
	  return NumberIn;
	}//end method getNumberIn

	//set units
	public void setUnitsIn ( double units)
	{
	  units = (UnitsIn > 0.0) ? 0.0: units;//store units
	}//end method setUnitsIn

	//return units
	public double getUnitsIn ()
	{
	return UnitsIn;
	}//end method getUnitsIn

	//set price
	public void setPriceIn ( double price) 
	{
	 price = (PriceIn > 0.0) ? 0.0: price;//store price
	}//end method setPriceIn

	//return price
	public double getPriceIn()
	{
	  return PriceIn;
	}//end method getPriceIn
	
	//getValue
	public double getValue()
	{
	 	return (UnitsIn * PriceIn);
	}//end getValue
	
	
	  // method getInventoryValue
   	public static double getInventoryValue ( CD inventory3[] )
   {
      double total = 0.0;
      total = (inventory3[ 0 ].getValue() +
               inventory3[ 1 ].getValue() +
               inventory3[ 2 ].getValue() +
               inventory3[ 3 ].getValue() +
               inventory3[ 4 ].getValue());
 
      System.out.printf( "The total value of this entire inventory is: $%.2f\n\n", total );
      return ( total );
 
   } // End method getInventoryValue
    
     
   //toString 
   public String toString ()       
   {
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	   		+= "Price:$%.2f\n";
      formatString          += "Value: $%.2f\n\n";
      
      return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) ); 
         
   } // End toString method
    
}//End class CD


/**
 * The Artist is a subclass of CD. A method is created
 * to calculate the value of the inventory and a
 * 5% restocking fee is added, also. 
*/

class Artist extends CD {
    
	private double BasePriceIn;
   	private double BaseInventoryValue;
    private double RestockFee;
    
    {
      // variables
      RestockFee = 0.05;
    }
 	

		//five-argument constructor
        public Artist(String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn)
        {	
        // explicit call to superclass CD constructor
		super( NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn );	
        	
        
      	 } // End five-argument constructor
 
   		// set price
   		public void setBasePriceIn( double getPriceIn)
   		{
      	BasePriceIn = ( getPriceIn < 0.0 ) ? 0.0 : getPriceIn;
   		} // end method setPriceIn
 
  		 // return  price
   		public double getBasePriceIn()
   		{
      	return BasePriceIn;
   		} // end getBasePriceIn
 
   		// calculate, return price, and the restocking fee
   		public double getBasePriceInPlusFee()
   		{
      	return  BasePriceIn + (BasePriceIn * RestockFee);
   		} // End getBasePriceInPlusFee
   
      	// set base inventory value
   		public void setBaseInventoryValue( double getInventoryValue)
  		 {
      	BaseInventoryValue = ( getInventoryValue < 0.0 ) ? 0.0 : getInventoryValue;
   		} // end setInventoryValue
 
   		// return base inventory value
   		public double getBaseInventoryValue()
   		{	
      	return BaseInventoryValue;
   		} // end getBaseInventoryValue
 
   // calculate, return value, and the restocking fee
   public double getValuePlusFee()
   {
      return  BaseInventoryValue + (BaseInventoryValue * RestockFee);
   }
  //Stringto
  public String toString(){
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	    	+= "Price:$%.2f\n";
      formatString          += "Value : $%.2f\n\n";
      formatString          += "Value with Restock Fee: $%.2f\n\n";
   
   
    return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getBasePriceInPlusFee(), getValuePlusFee()) ); 
   } // End toString 
	   
}//end class Artist extends CD

Please forgive me if I did not code my program correctly. If you could let me know if I did it right or not I would greatly appreciate it.

I am able to complie my program, but I get run errors that state the following:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
at java.util.Formatter.checkText(Formatter.java:2502)
at java.util.Formatter.parse(Formatter.java:2466)
at java.util.Formatter.format(Formatter.java:2413)
at java.util.Formatter.format(Formatter.java:2366)
at java.lang.String.format(String.java:2770)
at CD.toString(InventoryPart3.java:166)
at InventoryPart3.main(InventoryPart3.java:35)


I know I am doing something strangely wrong, but can not figure it out.

Recommended Answers

All 20 Replies

This is your first post, but now onwards, use code tags to display your code in the posts.

\**
* InventoryProgramPart3.java
* @ author Amy Summers
* Inventory Program which uses an array to store items and creates
* a subclass of the CD class
*
*/
import java.util.Arrays;//program uses Arrays

class InventoryPart3
{
	//main method begins execution of Java program
	public static void main (String[] args)
	{	
		
		double total = 0.0;
		
		CD inventory [ ] = new CD [8];//allocates 8 integers
		
		//create object
		inventory[0] = new CD("Fantasia","Fantasia",00012, 10, 13.00);
		inventory[1] = new CD("Tamia","Tamia", 00015, 10, 15.00);
		inventory[2] = new CD("Love","Alicia Keys", 00016, 10, 16.00);
		inventory[3] = new CD("Always","Keisha Cole", 00025, 10, 15.00);
		inventory[4] = new CD("Al","Al Green", 00022, 10, 12.00);
		inventory[5] = new CD("Smile","Marvin Gaye", 00026, 10, 13.00);
		inventory[6] = new CD("Jump","DJ Quick", 00036, 10, 13.00);
		inventory[7] = new CD("Graduation","Kanye West", 00017, 10, 16.00);
		


	for (int counter = 0; counter < 8; counter++)
      {
 
         System.out.printf(inventory[counter].toString());
 
      } // end for
      
       System.out.printf( "The total value of the inventory is $%.2f\n\n", total );

				
	}//end main
}// end class InventoryPart3


/**
* CD class that uses one additional unique feature of the CD
* Artist is the additional feature
*/

class CD
{
	 String NameIn;
	 String ArtistIn;
	 double  NumberIn;
	 double UnitsIn;//number of units in stock
	 double PriceIn;//price of each unit
	 double ValueIn;//value of inventory
	
	//five-argument constructor initializes CD
	public CD( String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn )
             {
		//implicit call to Object constructor occurs here
			String product = NameIn;// initialize name
			String Artist = ArtistIn;//initialize name
			double number = NumberIn;// initialize number
			double units = UnitsIn;// initialize units
			double price = PriceIn;// initialize price
	 }//end five-argument constructor

	// set product name
	public void setNameIn (String product)
	{
	  product = NameIn;//store product name
	}//end method setNameIn

	//return product name
	public String getNameIn()
	{
	  return NameIn;
	}//end method getNameIn

	//set artist name
	public void setArtistIn (String artist)
	{
	artist = ArtistIn;//store artist name
	}//end method setArtistIn
	
	//return artist name
	public String getArtistIn()
	{
	return ArtistIn;
	}//end method getArtistIn

	
	// set number
	public void setNumberIn (double number)
	{

	  number = (NumberIn > 0.0) ? 0.0: number;//store number
	}//end method setNumberIn

	//return number
	public double getNumberIn()
	{
	  return NumberIn;
	}//end method getNumberIn

	//set units
	public void setUnitsIn ( double units)
	{
	  units = (UnitsIn > 0.0) ? 0.0: units;//store units
	}//end method setUnitsIn

	//return units
	public double getUnitsIn ()
	{
	return UnitsIn;
	}//end method getUnitsIn

	//set price
	public void setPriceIn ( double price) 
	{
	 price = (PriceIn > 0.0) ? 0.0: price;//store price
	}//end method setPriceIn

	//return price
	public double getPriceIn()
	{
	  return PriceIn;
	}//end method getPriceIn
	
	//getValue
	public double getValue()
	{
	 	return (UnitsIn * PriceIn);
	}//end getValue
	
	
	  // method getInventoryValue
   	public static double getInventoryValue ( CD inventory3[] )
   {
      double total = 0.0;
      total = (inventory3[ 0 ].getValue() +
               inventory3[ 1 ].getValue() +
               inventory3[ 2 ].getValue() +
               inventory3[ 3 ].getValue() +
               inventory3[ 4 ].getValue());
 
      System.out.printf( "The total value of this entire inventory is: $%.2f\n\n", total );
      return ( total );
 
   } // End method getInventoryValue
    
     
   //toString 
   public String toString ()       
   {
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	   		+= "Price:$%.2f\n";
      formatString          += "Value: $%.2f\n\n";
      
      return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) ); 
         
   } // End toString method
    
}//End class CD


/**
 * The Artist is a subclass of CD. A method is created
 * to calculate the value of the inventory and a
 * 5% restocking fee is added, also. 
*/

class Artist extends CD {
    
	private double BasePriceIn;
   	private double BaseInventoryValue;
    private double RestockFee;
    
    {
      // variables
      RestockFee = 0.05;
    }
 	

		//five-argument constructor
        public Artist(String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn)
        {	
        // explicit call to superclass CD constructor
		super( NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn );	
        	
        
      	 } // End five-argument constructor
 
   		// set price
   		public void setBasePriceIn( double getPriceIn)
   		{
      	BasePriceIn = ( getPriceIn < 0.0 ) ? 0.0 : getPriceIn;
   		} // end method setPriceIn
 
  		 // return  price
   		public double getBasePriceIn()
   		{
      	return BasePriceIn;
   		} // end getBasePriceIn
 
   		// calculate, return price, and the restocking fee
   		public double getBasePriceInPlusFee()
   		{
      	return  BasePriceIn + (BasePriceIn * RestockFee);
   		} // End getBasePriceInPlusFee
   
      	// set base inventory value
   		public void setBaseInventoryValue( double getInventoryValue)
  		 {
      	BaseInventoryValue = ( getInventoryValue < 0.0 ) ? 0.0 : getInventoryValue;
   		} // end setInventoryValue
 
   		// return base inventory value
   		public double getBaseInventoryValue()
   		{	
      	return BaseInventoryValue;
   		} // end getBaseInventoryValue
 
   // calculate, return value, and the restocking fee
   public double getValuePlusFee()
   {
      return  BaseInventoryValue + (BaseInventoryValue * RestockFee);
   }
  //Stringto
  public String toString(){
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	    	+= "Price:$%.2f\n";
      formatString          += "Value : $%.2f\n\n";
      formatString          += "Value with Restock Fee: $%.2f\n\n";
   
   
    return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getBasePriceInPlusFee(), getValuePlusFee()) ); 
   } // End toString 
	   
}//end class Artist extends CD

Is this the correct way?

>Is this the correct way?

Absolutely. You can see the difference it makes for yourself.

>System.out.printf(inventory[counter].toString());

I do not think there is any function like printf in Java. It should be either print or println.

>System.out.printf( "The total value of the inventory is $%.2f\n\n", total );

Same as above.

>formatString += "Units: $%.d\n";

should be

formatString          += "Units: $%d\n";

I think this will remove the following errors:

at CD.toString(InventoryPart3.java:166)
at InventoryPart3.main(InventoryPart3.java:35)

Try compiling it after the above stated changes.

As error say there is problem with line 166 which is part of your method toString

public String toString ()       
   {
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	   		+= "Price:$%.2f\n";
      formatString          += "Value: $%.2f\n\n";
      
      return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) ); 
         
   } // End toString method

You calling variables that are return by get methods instead of calling methods to get this variables

>System.out.printf(inventory[counter].toString());

I do not think there is any function like printf in Java. It should be either print or println.

As of 1.5, yes there is, but it is not used in this manner. You need to pass it an optional locale object, a format pattern string, and an array of objects to match the arguments in the pattern string.

Ok. I was not aware of that. Thank You!

You calling variables that are return by get methods instead of calling methods to get this variables

Ah! I didn't notice this error while inspecting the program for the first time. I really need to keep my eyes wide open.

Ah! I didn't notice this error while inspecting the program for the first time. I really need to keep my eyes wide open.

I do not know how to fix this error..I see the line

return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) );


Does it make sense to format a String for all the variable? And how do I call on the variables?

Ok. I was not aware of that. Thank You!

it's not used much. It's been available for like 4 years now, I use Java 1.5 professionally on a daily basis, and I've never encountered it outside web articles listing the new features of 1.5.
Even most books dealing specifically with 1.5 (and were written after it hit the street) don't so much as mention it.

One of the less successful things Sun did to Java before 1.6.

it's not used much. It's been available for like 4 years now, I use Java 1.5 professionally on a daily basis, and I've never encountered it outside web articles listing the new features of 1.5.
Even most books dealing specifically with 1.5 (and were written after it hit the street) don't so much as mention it.

One of the less successful things Sun did to Java before 1.6.

Yup, perhaps that is the reason I had never heard about it before ;)

Does it make sense to format a String for all the variable?

I didn't insist you to do that.

And how do I call on the variables?

Inside the return(String.format()), use the get methods that you have defined for each data field to refer to data like NameIn, ArtistIn, NumberIn... etc.

/* Hi i corrected some codes in your program .. compile and run the program.. i didnt correct your Artist class.. because you didnt use in your program.... You are using most of the C language syntax thats why so much error... */.. Chech and tell ...

/**
* InventoryProgramPart3.java
* @ author Amy Summers
* Inventory Program which uses an array to store items and creates
* a subclass of the CD class
*
*/

class InventoryPart3
{
	// main method begins execution of Java program
	public static void main (String[] args)
	{	
		
		double total = 0.0;
		
		CD inventory [ ] = new CD [8];// allocates 8 integers
		
		// create object
		inventory[0] = new CD("Fantasia","Fantasia",00012, 10, 13.00);
		inventory[1] = new CD("Tamia","Tamia", 00015, 10, 15.00);
		inventory[2] = new CD("Love","Alicia Keys", 00016, 10, 16.00);
		inventory[3] = new CD("Always","Keisha Cole", 00025, 10, 15.00);
		inventory[4] = new CD("Al","Al Green", 00022, 10, 12.00);
		inventory[5] = new CD("Smile","Marvin Gaye", 00026, 10, 13.00);
		inventory[6] = new CD("Jump","DJ Quick", 00036, 10, 13.00);
		inventory[7] = new CD("Graduation","Kanye West", 00017, 10, 16.00);
		


	for (int counter = 0; counter < inventory.length; counter++) {
         System.out.println(inventory[counter].toString());
      } // end for
		total=inventory[0].getInventoryValue(inventory);
       //System.out.println("The total value of the inventory is "+total );

				
	}// end main
}// end class InventoryPart3


/**
 * CD class that uses one additional unique feature of the CD Artist is the
 * additional feature
 */

class CD
{
	 String NameIn;
	 String ArtistIn;
	 double  NumberIn;
	 double UnitsIn;// number of units in stock
	 double PriceIn;// price of each unit
	 double ValueIn;// value of inventory
	// five-argument constructor initializes CD
	public CD( String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn )
             {
		// implicit call to Object constructor occurs here
			this.NameIn = NameIn;// initialize name
			this.ArtistIn = ArtistIn;// initialize name
			this.NumberIn = NumberIn;// initialize number
			this.UnitsIn = UnitsIn;// initialize units
			this.PriceIn = PriceIn;// initialize price
	 }// end five-argument constructor

	// return product name
	public String getNameIn()
	{
	  return NameIn;
	}// end method getNameIn

	// return artist name
	public String getArtistIn()
	{
	return ArtistIn;
	}// end method getArtistIn

	// return number
	public double getNumberIn()
	{
	  return NumberIn;
	}// end method getNumberIn

	// return units
	public double getUnitsIn ()
	{
	return UnitsIn;
	}// end method getUnitsIn

	// return price
	public double getPriceIn()
	{
	  return PriceIn;
	}// end method getPriceIn
	
	// getValue
	public double getValue()
	{
	 	return (UnitsIn * PriceIn);
	}// end getValue
	
	
	  // method getInventoryValue
   	public static double getInventoryValue ( CD inventory3[] )
   {
      double total = 0.0;
      total = (inventory3[ 0 ].getValue() +
               inventory3[ 1 ].getValue() +
               inventory3[ 2 ].getValue() +
               inventory3[ 3 ].getValue() +
               inventory3[ 4 ].getValue());
 
      System.out.println( "The total value of this entire inventory is: "+ total );
      return ( total );
 
   } // End method getInventoryValue
    
     
   // toString
   public String toString ()       
   {
      String formatString    = "Name: "+getNameIn()+"\n";
      formatString          += "Artist: "+getArtistIn()+"\n";
      formatString          += "Number: "+getNumberIn()+"\n";
      formatString          += "Units: "+getUnitsIn()+"\n";
      formatString	   		+= "Price: "+getPriceIn()+"\n";
      formatString          += "Value: "+getValue()+"\n";   
      
      return formatString;
         
   } // End toString method
    
}// End class CD


/**
 * The Artist is a subclass of CD. A method is created to calculate the value of
 * the inventory and a 5% restocking fee is added, also.
 */

class Artist extends CD {
    
	private double BasePriceIn;
   	private double BaseInventoryValue;
    private double RestockFee;
    
    {
      // variables
      RestockFee = 0.05;
    }
 	

		// five-argument constructor
        public Artist(String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn)
        {	
        // explicit call to superclass CD constructor
		super( NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn );	
        	
        
      	 } // End five-argument constructor
 
   		// set price
   		public void setBasePriceIn( double getPriceIn)
   		{
      	BasePriceIn = ( getPriceIn < 0.0 ) ? 0.0 : getPriceIn;
   		} // end method setPriceIn
 
  		 // return price
   		public double getBasePriceIn()
   		{
      	return BasePriceIn;
   		} // end getBasePriceIn
 
   		// calculate, return price, and the restocking fee
   		public double getBasePriceInPlusFee()
   		{
      	return  BasePriceIn + (BasePriceIn * RestockFee);
   		} // End getBasePriceInPlusFee
   
      	// set base inventory value
   		public void setBaseInventoryValue( double getInventoryValue)
  		 {
      	BaseInventoryValue = ( getInventoryValue < 0.0 ) ? 0.0 : getInventoryValue;
   		} // end setInventoryValue
 
   		// return base inventory value
   		public double getBaseInventoryValue()
   		{	
      	return BaseInventoryValue;
   		} // end getBaseInventoryValue
 
   // calculate, return value, and the restocking fee
   public double getValuePlusFee()
   {
      return  BaseInventoryValue + (BaseInventoryValue * RestockFee);
   }
  // Stringto
  public String toString(){
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	    	+= "Price:$%.2f\n";
      formatString          += "Value : $%.2f\n\n";
      formatString          += "Value with Restock Fee: $%.2f\n\n";
   
   
    //return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getBasePriceInPlusFee(), getValuePlusFee()) );
      return formatString;
   } // End toString
	   
}// end class Artist extends CD

If you made any correction to the code it would be helpfull if you highlighted (display in different colour).
PS: Please use code tag for whole supplied code not for section. The tag is to keep original aligment not to break it down how you did.

I cant understand about what you asked... I am new to this Community.. I will learn the Procedure of sending and replying and all...

hey there are so many error

hey there are so many error

What are those errors.......... Send that code here or send the program to my id <snip> I will check and tell...

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.