My program compiles and runs okay, but is not displaying my restocking fee and I am not sure why. Attached is my printout of how it displays.
//Product.java works with Inventory4.java file
import java.util.Scanner; // program uses class Scanner
public class Product4
{
//Private Variables
private String name; //Product name
private int identificationNumber; //The product identification number
private int unitsInStock; //Number of units in stock
private double unitPriceInDollars; //The price of each unit in dollars
private double restockFee; //price for restocking units
//Constructors
//Default constructor
public Product4()
{
//Set some default values for each private variable
name = "";
identificationNumber = 0;
unitsInStock = 0;
unitPriceInDollars = 0.00;
restockFee = 0.00;
}//End Inventory constructor
Product4 inventory [] = new Product4 [5];
//Initialization constructor
public Product4(
String nameIn, int identificationNumberIn,
int unitsInStockIn, double unitPriceInDollarsIn, double restockFeeIn
)
{
//Set the item name--use the set methods to enforce bounds checking
setName( nameIn );
//Set the item identifcation number
setIdentificationNumber( identificationNumberIn );
//Set the number of units in stock
setUnitsInStock( unitsInStockIn );
//Set the unit price in dollars
setUnitPriceInDollars( unitPriceInDollarsIn );
}//End Inventory constructor
//Stores the item name
public void setName( String nameIn )
{
name = nameIn; //Store the item name
}//End setName
//Stores the product identification number
public void setIdentificationNumber( int identificationNumberIn )
{
//Store the item identification number
// If the value is negative, then store 0
identificationNumber = ( (identificationNumberIn > 0) ? identificationNumberIn : 0 );
}//End setID
//Stores the number of units in stock
public void setUnitsInStock( int unitsInStockIn )
{
//Store the number of units in stock
// If the value is negative, then store 0
unitsInStock = ( (unitsInStockIn > 0)?unitsInStockIn:0 );
}//End setUnitsInStock
//Stores the price of each unit in dollars
public void setUnitPriceInDollars( double unitPriceInDollarsIn )
{
//Store the unit price
// If the value is negative, then store 0.0
unitPriceInDollars = ( (unitPriceInDollarsIn > 0.0)?unitPriceInDollarsIn:0.0);
}//End setUnitPriceInDollars
//Stores the item restock fee
public void setRestockFee( double restockFeeIn )
{
restockFee = ( (restockFeeIn > 0.0)?restockFeeIn:0.0 ); //Store the item restock fee
}//End setRestockFee
//Returns the item name
public String getName()
{
return ( name ); //Return the identification name
} // End getName
//Returns the item identification number
public int getIdentificationNumber()
{
return ( identificationNumber ); //Return the identification number
}//End getIdentificationNumber
//Returns the number of units in stock
public int getUnitsInStock()
{
return( unitsInStock ); //Return the number of units in stock
}//End getUnitsInStock
//Returns the price of each unit
public double getUnitPriceInDollars()
{
return( unitPriceInDollars ); //Return the unit price
}//End getUnitPriceInDollars
//Returns the restocking fee
public double getRestockFee()
{
return( restockFee );
}
//Other Methods
//Returns the total value of the inventory in dollars
public double stockValueInDollars()
{
return ( unitsInStock * unitPriceInDollars );
}//End stockValueInDollars
//Returns a formatted String for output purposes
public String toString()
{
String formatString = "Identification Number : %d\n";
formatString += "Product : %s\n";
formatString += "Units In Stock : %d\n";
formatString += "Unit Price : $%.2f\n";
formatString += "Stock Value : $%.2f\n\n";
formatString += "Restocking Fee : $%.2f\n";
return ( String.format( formatString, identificationNumber, name, unitsInStock,
unitPriceInDollars, stockValueInDollars(), restockFee )
);
}//End toString()
//Display an array of products and their value
public static void listInventory ( Product4 inventory[] )
{
//Print each product
for ( Product4 product:inventory )
{
//Print product
System.out.println( product.toString () );
}//End for
System.out.printf( "Total Inventory Value: $%.2f\n", totalValue ( inventory ) );
}//End method listInventory()
//Calculate and return total value of inventory
public static double totalValue( Product4 inventory[] )
{
double inventoryValue = 0.0;
for ( Product4 product: inventory )
{
//Add the total value of this product to the total value of inventory
inventoryValue += product.stockValueInDollars();
}//End enhanced for
return ( inventoryValue ); //returns the value of all inventory
}//End totalValue method
//Overloads the String objects compareTo() function so Product can be compared and sorted
public int compareTo ( Product4 aProduct )
{
//Compare the name of this product to the passed Product ( aProduct )
return( name.compareTo ( aProduct.getName() ) );
}
//Sorts the Product array by name
public static void sortAlphabetical ( Product4 inventory[] )
{
Product4 temp; //for swapping
int bottom;
int i;
//This is called a bubble sort
for (bottom = inventory.length-1; bottom > 0; bottom--)
{
for ( i = 0; i < bottom; i ++ )
{
//If inventory[i] is lexographically greater than inventory[i+1], then swap the two around
if ( inventory[i].compareTo ( inventory[i+1] ) > 0 )
{
//swap inventory[i] with inventory[i+1]
temp = inventory[i];
inventory[i] = inventory[i+1];
inventory[i+1] = temp;
}//End if
}//End for
}//End for
}//End alphabetical sort method
}//End class Product
---------
//Works with Product4.java file
import java.util.Arrays;
public class Inventory4
{
public static void main( String args[] )
{
double restockFee = 0.05;
//Declare and initialize the inventory array
Product4 inventory []; //Array variables
//Size of the array
inventory = new Product4[5];
//Initialize each array using the Product Constructor
inventory[0] = new Product4("HP BLACK", 1, 3, 72.91, 3.65);
inventory[1] = new Product4("HP 41 COLOR", 2, 2, 21.29, 1.06);
inventory[2] = new Product4("HP 78 TRI-COLOR", 3, 4, 22.45, 1.12);
inventory[3] = new Product4("HP 102 Gray", 4, 16, 22.99, 1.15);
inventory[4] = new Product4("HP Black", 5, 14, 13.60, 0.68);
//Sorts the Array Variables
Product4.sortAlphabetical ( inventory );
int x = inventory[0].compareTo ( inventory [1] );
System.out.println ( x+"\n" );
for ( int i=0; i<5; i++ )
System.out.println ( inventory[i].toString() );
//Display each product in the array, including the total value of inventory
Product4.listInventory( inventory );
} //End main
} //End Array Inventory4
I appreciate whatever assistance is offered. Thank you.