\**
* 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.