this program compiles and runs - but only prints line 47 of the "book" file.
I am unsure what to do, I think something is missing like a curly bracket or a piece of code . . .
if anyone has time to take a peek . . . please help me a bit
Thank you.
-big
bluesky
Error message:
at Book.stockValueInDollars(Book.java:47) this prints about 340 times in the command prompt window - quite shocking! LOL
c:\Documents and Settings\Pam\My Documents\Java\Inventory\Inventory3\Inventory 3
\Inventory 3>
CODE
//Product.java
public class Product implements Comparable
{
//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
//Constructors
//Default constructor
public Product()
{
//Set some default values for each private variable
name = "";
identificationNumber = 0;
unitsInStock = 0;
unitPriceInDollars = 0.0;
}//End Inventory constructor
//Initialization constructor
public Product( String nameIn, int identificationNumberIn,
int unitsInStockIn, double unitPriceInDollarsIn )
{
//Set the item name--use the set methods to enforce bounds checking
setName( nameIn );
//Set the item identification number
setIdentificationNumber( identificationNumberIn );
//Set the number of units in stock
setUnitsInStock( unitsInStockIn );
//Set the unit price in dollars
setUnitPriceInDollars( unitPriceInDollarsIn );
}//End Inventory constructor
//Public Methods
//Set Methods
//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
//Get Methods
//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
//Other Methods
//Returns the total value of the inventory in dollars
public double stockValueInDollars()
{
return ( unitsInStock * unitPriceInDollars );
}//End stockValueInDollars
public static double totalInventoryValue( Product inventory[] )
{
double value = 0.0;
for(Product p: inventory)
{
value += p.stockValueInDollars();
}//End for
return( value );
}//End method totalInventoryValue
//Overloads the String objects compareTo() method so Products can be
//compared and potentially sorted by name.
public int compareTo (Object o)
{
String s2 = ((Product)o).name.toUpperCase();
String s1 = name.toUpperCase();
return ( s1.compareTo(s2) );
} //End compareTo
//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";
return ( String.format( formatString, identificationNumber, name, unitsInStock,
unitPriceInDollars, stockValueInDollars())
);
}//End toString()
}//End class Product
CODE
import java.util.Arrays; //For Arrays.sort
public class Inventory3
{
public static void main( String args[] )
{
double totalInventoryValue = 0.0;
Book [] inventory;
inventory = new Book[3];
inventory[0] = new Book("Inkspell", "Funke, Cornellia", 1, 600, 20.00);
inventory[1] = new Book("Thousand Orcs, The", "Salvatore, RA", 2, 800, 25.95);
inventory[2] = new Book("Harry Potter and the Half Blood Prince", "Rowling, JK", 3, 1000, 29.99);
//Sort the array
Arrays.sort( inventory );
//print out each Book
for (Book book:inventory)
{
//Print this Book
System.out.println( book.toString() );
}//End for
//Print out totalInventoryValue in dollars
totalInventoryValue = Product.totalInventoryValue( inventory );
System.out.printf( "Total Inventory Value : $%.2f\n", totalInventoryValue );
} //End method main
}//End Inventory3
CODE
//In a file Book.java
public class Book extends Product implements Comparable
{
//Variables
private String author; //Book author
//Constructors
//Initailization constructor
public Book(String titleIn, String authorIn, int idIn, int unitsInStockIn, double unitPriceInDollarsIn)
{
//Call super class contructor
super( titleIn, idIn, unitsInStockIn, unitPriceInDollarsIn);
//Call a set method to ensure proper initialization
setAuthor( authorIn );
}//End Book constructor
//Public Methods
//Set Methods
//Store Book author
public void setAuthor( String authorIn )
{
author = authorIn;
} // end method setAuthor
//Get Methods
//Return Book Author
public String getAuthor()
{
return ( author ); //Return Book author
}//End getAuthor
//Other Methods
//Calculate the stock value
// Returns total value of product in stock, adding a %5 restocking fee
public double stockValueInDollars()
{
// Add a 5% restocking fee
return(stockValueInDollars() * 1.05);
} // End stockValueInDollars
//A method for returning the restockingFee()
public double restockingFee()
{
//Return 5% of the super-class stock value
return( super.stockValueInDollars() * 0.05 );
}//End method for returning restockingFee
//Display
//Returns a formatted String for output purposes
public String toString()
{
String formatString = "Book Title : %s\n";
formatString += "Restocking Fee : $%.2f\n";
//Create a formatted string
formatString = String.format(formatString, author, restockingFee() );
//Return the string for use in display
return( formatString + super.toString() );
}//End toString()
}//End class Book