I need help with a class project I'm working on, Below is my assignment and the code I have currently created.

Assignment:
Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
• Modify the output to display this additional feature you have chosen and the restocking fee.

Problem:
My code complies but my restocking fee doesn't show up, and I'm not sure how to make it. Do I need to hardcode the fee at the beginning like a statement: reStockFee = .05 or reStockFee = 5%.

And finally I just need reassurance that I've meet the requirements of the assignment, I believe I have once I get the 5% restocking fee to show up but could use some encouragement as well :-/

Any help or suggestions will be greatly appreciated!

class Product implements Comparable
{
		private long itemNumber;    // class variable that stores the item number
		private String itemName;    // class variable that stores the item name
		private long invQuantity;   // class variable that stores the quantity in stock
		private double itemPrice;   // class variable that stores the item price
			
			public Product(long number, String name, long quantity, double price) // Constructor for the Supplies class
				{
					itemNumber = number;
					itemName = name;
					invQuantity = quantity;
					itemPrice = price;
				}
				
			public void setItemNumber(long number)  // Method to set the item number
				{
					this.itemNumber = number;
				}
				
			public long getItemNumber()  // Method to get the item number
				{
					return itemNumber;
				}
				
			public void setItemName(String name)  // Method to set the item name
				{
					this.itemName = name;
				}
				
			public String getItemName()  // Method to get the item name
				{
					return itemName;
				}
				
			public void setinvQuantity(long quantity)  // Method to set the quantity in stock
				{
					invQuantity = quantity;
				}
				
			public long getInvQuantity()  // Method to get the quantity in stock
				{
					return invQuantity;
				}
				
			public void setItemPrice(double price)  // Method to set the item price
				{
					this.itemPrice = price;
				}
				
			public double getItemPrice()  // Method to get the item price
				{
					return itemPrice;
				}
				
			public double calculateInventoryValue()  // Method to calculate the value of the inventory
				{
					return itemPrice * invQuantity;
				}
			
			public int compareTo(Object o)
				{
					Product p = null;
						try
							{
								p = (Product) o;
							}
						
						catch (ClassCastException cE)
							{
								cE.printStackTrace();
							}
						
						return itemName.compareTo(p.getItemName());
				}
 
			public String toString()
				{
					return "Item #: " + itemNumber + "\nName: " + itemName + "\nQuantity: " + invQuantity + "\nPrice: $" + itemPrice + "\nValue: $" + calculateInventoryValue();
				}
}  //end class Product

class DVD extends Product
{
	private double reStockingFee;
  
		public DVD(int itemNumber, String itemName, long invQuantity, double itemPrice, double reStockingFee) 
			{
           		super(itemNumber, itemName, invQuantity, itemPrice);
           		this.reStockingFee = reStockingFee;
      		}	
		public double getItemPrice() //returns the value of the inventory, plus the restocking fee
			{
				return super.getItemPrice() + reStockingFee; // TODO Auto-generated method stub
			}
 		public String toString() 
			{
				return new StringBuffer().append("Price: " + super.getItemPrice()).append(" With RestockingFee: " + getItemPrice()).toString();
			}
 
} // end class DVD

		
public class Inventory3

	{
		Product[] supplies;
		
		public static void main(String[] args)
			{
				Inventory3 inventory = new Inventory3();
				inventory.addProduct(new Product(1001, "Megaforce", 10, 18.95));
				inventory.addProduct(new Product(502, "Abyss", 25, 12.95));
				inventory.addProduct(new Product(1003, "Deep Impact", 65, 21.95));
				inventory.addProduct(new Product(750, "Forest Gump", 28, 7.95));
 
				System.out.println(); // blank line
				System.out.println("Welcome to DVD Inventory 1.0"); //display header
				inventory.sortByName(); //sort list by name
				System.out.println(); // blank line
				inventory.showInventory(); //display inventory
 
				double total = inventory.calculateTotalInventory();
				System.out.println("Total Value is: $" + total);
 
			}
 
		public void sortByName()
			{
				for (int i = 1; i < supplies.length; i++)
					{
						int j;
						Product val = supplies[i];
						for (j = i - 1; j > -1; j--)
							{
								Product temp = supplies[j];
								if (temp.compareTo(val) <= 0)
									{
									break;
									}
								supplies[j + 1] = temp;
							}
						supplies[j + 1] = val;
					}
			}
 
		public String toString() //creates a String representation of the array of products
			{
				String s = "";
				for (Product p : supplies)
					{
						s = s + p.toString();
						s = s + "\n\n";
					}
				return s;
			}
 
		public void addProduct(Product p1) //Increases the size of the array
			{
				if (supplies == null)
					{
						supplies = new Product[0];
					}
				Product[] p = supplies; //Copy all products into p first
				Product[] temp = new Product[p.length + 1]; //create bigger array
				for (int i = 0; i < p.length; i++)
					{
						temp[i] = p[i];
					}
				temp[(temp.length - 1)] = p1; //add the new product at the last position
				supplies = temp;
			}
 
		public double calculateTotalInventory() //sorting the array using Bubble Sort
			{
				double total = 0.0;
				for (int i = 0; i < supplies.length; i++)
					{
						total = total + supplies[i].calculateInventoryValue();
					}
				return total;
			}
 
		public void showInventory()
		{
			System.out.println(toString()); //call our toString method
		}
		
	} //end Class Inventory3

I got this output when I ran your program:

Welcome to DVD Inventory 1.0

Item #: 502
Name: Abyss
Quantity: 25
Price: $12.95
Value: $323.75

Item #: 1003
Name: Deep Impact
Quantity: 65
Price: $21.95
Value: $1426.75

Item #: 750
Name: Forest Gump
Quantity: 28
Price: $7.95
Value: $222.6

Item #: 1001
Name: Megaforce
Quantity: 10
Price: $18.95
Value: $189.5


Total Value is: $2162.6

I haven't done the calculations to check the math, but it looks right.

In order to help you with the 5% restocking fee, I guess I would need to know what the output should look like if that part was implemented. I see you have written a DVD class, but I don't see you attempt to call anything from that class in your program.

So the output is correct from my first part of the program. I created the DVD sub class to append the display to add an additional line to calculate a %5 restocking fee based on the DVD value. This is where I need help, I have the code but it's not displaying the restocking fee or even calculating it and I'm lost as to how to do that. Some directional help would be great or if someone can give me an example that would help too.

An example of the output I think I need would be something like this

Item #: 1001
Name: Megaforce
Quantity: 10
Price: $18.95
Value: $189.5
Restocking Fee: $.94

So the output is correct from my first part of the program. I created the DVD sub class to append the display to add an additional line to calculate a %5 restocking fee based on the DVD value. This is where I need help, I have the code but it's not displaying the restocking fee or even calculating it and I'm lost as to how to do that. Some directional help would be great or if someone can give me an example that would help too.

An example of the output I think I need would be something like this

Item #: 1001
Name: Megaforce
Quantity: 10
Price: $18.95
Value: $189.5
Restocking Fee: $.94

Well, you'll have to change a few things around later in the program to get a correct display, but to get the DVD into the inventory array, I'd do something like this in main:

Inventory3 inventory = new Inventory3();
                               
DVD dvd = new DVD (1001, "Megaforce", 10, 18.95, 0.05);
inventory.addProduct(dvd);
inventory.addProduct(new Product(502, "Abyss", 25, 12.95));
inventory.addProduct(new Product(1003, "Deep Impact", 65, 21.95));
inventory.addProduct(new Product(750, "Forest Gump", 28, 7.95));

In your DVD constructor, I'd change it to this:

public DVD(int itemNumber, String itemName, long invQuantity, double itemPrice, double reStockPercent) 
{
     super(itemNumber, itemName, invQuantity, itemPrice);
     this.reStockingFee = itemPrice * reStockPercent;
 }

Again, you'll have to change one or more of the later procedures around to get it to display right, but this gets it into the inventory correctly and makes a call to the DVD constructor which wasn't happening before. If you are using a debugger, stick a breakpoint in main after everything has been added to inventory and you'll see that the restocking fee is in the inventory. You'll have to change some of the functions that showInventory calls to make it display correctly.

Ok so I have better picture of what I need to do.

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
• Modify the output to display this additional feature you have chosen and the restocking fee.

I need to add a new variable like "Director" to my output along with a 5% restocking fee.

Below is my current sub class of DVD

class DVD extends Product
		{
			//private String genre;
			private double reStockingFee;
  
				
				public DVD(int itemNumber, String itemName, long invQuantity, double itemPrice, double reStockingFee) 
					{
            					super(itemNumber, itemName, invQuantity, itemPrice);
            					this.reStockingFee = reStockingFee;
      					}	
				//public Product(int itemNumber, String itemName, long invQuantity, double itemPrice, double reStockingFee)
					//{
						//super(itemNumber, itemName, invQuantity, itemPrice);
						//this.reStockingFee = reStockingFee;
       					//} // TODO Auto-generated constructor stub
    
				public double getItemPrice() //returns the value of the inventory, plus the restocking fee
					{
						return super.getItemPrice() + reStockingFee; // TODO Auto-generated method stub
					}
 
				public String toString() 
				{
					return new StringBuffer().append("Price: " + super.getItemPrice()).append(" With RestockingFee: " + getItemPrice()).toString();
				}
 
		} // end class DVD

This complies in the code but I am unable to get the ItemPrice and display the restocking fee (starting small first with just the fee). Anymore direction on how to calculate the 5% restock fee and display it with my current out put would be greatly appreciated.

class Product implements Comparable
{
		private long itemNumber;    // class variable that stores the item number
		private String itemName;    // class variable that stores the item name
		private long invQuantity;   // class variable that stores the quantity in stock
		private double itemPrice;   // class variable that stores the item price
			
			public Product(long number, String name, long quantity, double price) // Constructor for the Supplies class
				{
					itemNumber = number;
					itemName = name;
					invQuantity = quantity;
					itemPrice = price;
				}
				
			public void setItemNumber(long number)  // Method to set the item number
				{
					this.itemNumber = number;
				}
				
			public long getItemNumber()  // Method to get the item number
				{
					return itemNumber;
				}
				
			public void setItemName(String name)  // Method to set the item name
				{
					this.itemName = name;
				}
				
			public String getItemName()  // Method to get the item name
				{
					return itemName;
				}
				
			public void setinvQuantity(long quantity)  // Method to set the quantity in stock
				{
					invQuantity = quantity;
				}
				
			public long getInvQuantity()  // Method to get the quantity in stock
				{
					return invQuantity;
				}
				
			public void setItemPrice(double price)  // Method to set the item price
				{
					this.itemPrice = price;
				}
				
			public double getItemPrice()  // Method to get the item price
				{
					return itemPrice;
				}
				
			public double calculateInventoryValue()  // Method to calculate the value of the inventory
				{
					return itemPrice * invQuantity;
				}
			
			public int compareTo(Object o)
				{
					Product p = null;
						try
							{
								p = (Product) o;
							}
						
						catch (ClassCastException cE)
							{
								cE.printStackTrace();
							}
						
						return itemName.compareTo(p.getItemName());
				}
 
			public String toString()
				{
					return "Item #: " + itemNumber + "\nName: " + itemName + "\nQuantity: " + invQuantity + "\nPrice: $" + itemPrice + "\nValue: $" + calculateInventoryValue();
				}
}  //end class Product

public class Inventory3

	{
		Product[] supplies;
		
		public static void main(String[] args)
			{
				Inventory3 inventory = new Inventory3();
				inventory.addProduct(new Product(1001, "Megaforce", 10, 18.95));
				inventory.addProduct(new Product(502, "Abyss", 25, 12.95));
				inventory.addProduct(new Product(1003, "Deep Impact", 65, 21.95));
				inventory.addProduct(new Product(750, "Forest Gump", 28, 7.95));
 
				System.out.println(); // blank line
				System.out.println("Welcome to DVD Inventory 1.0"); //display header
				inventory.sortByName(); //sort list by name
				System.out.println(); // blank line
				inventory.showInventory(); //display inventory
 
				double total = inventory.calculateTotalInventory();
				System.out.println("Total Value is: $" + total);
 
			}
 
		public void sortByName()
			{
				for (int i = 1; i < supplies.length; i++)
					{
						int j;
						Product val = supplies[i];
						for (j = i - 1; j > -1; j--)
							{
								Product temp = supplies[j];
								if (temp.compareTo(val) <= 0)
									{
									break;
									}
								supplies[j + 1] = temp;
							}
						supplies[j + 1] = val;
					}
			}
 
		public String toString() //creates a String representation of the array of products
			{
				String s = "";
				for (Product p : supplies)
					{
						s = s + p.toString();
						s = s + "\n\n";
					}
				return s;
			}
 
		public void addProduct(Product p1) //Increases the size of the array
			{
				if (supplies == null)
					{
						supplies = new Product[0];
					}
				Product[] p = supplies; //Copy all products into p first
				Product[] temp = new Product[p.length + 1]; //create bigger array
				for (int i = 0; i < p.length; i++)
					{
						temp[i] = p[i];
					}
				temp[(temp.length - 1)] = p1; //add the new product at the last position
				supplies = temp;
			}
 
		public double calculateTotalInventory() //sorting the array using Bubble Sort
			{
				double total = 0.0;
				for (int i = 0; i < supplies.length; i++)
					{
						total = total + supplies[i].calculateInventoryValue();
					}
				return total;
			}
 
		public void showInventory()
		{
			System.out.println(toString()); //call our toString method
		}
		
	} //end Class Inventory3

You are still not touching your DVD class as far as I can tell. Modify your toString method in your DVD class, look at my last post, create one of your products as a DVD by making a call to the DVD constructor as I did in my last post, and you should get your output with the restocking fee. Make sure that the correct functions are being called. Either use a debugger and put breakpoints in the DVD constructor function and the DVD toString function, or put some lines of code in the functions that display to the console screen, to make sure those two functions from the DVD class are being called. Again you need to modify toString in the DVD class.You may decide to put a call to super.toSting () in the toString() function of the DVD class.

OK did he also leave out the subclass in his latest round of code? It is OK I was just trying to follow along with the conversation and thought that I was loosing it. Thanks

OK did he also leave out the subclass in his latest round of code? It is OK I was just trying to follow along with the conversation and thought that I was loosing it. Thanks

The DVD class is in there, but the OP's code is not calling the DVD constructor:

Inventory3 inventory = new Inventory3();
inventory.addProduct(new Product(1001, "Megaforce", 10, 18.95));
inventory.addProduct(new Product(502, "Abyss", 25, 12.95));
inventory.addProduct(new Product(1003, "Deep Impact", 65, 21.95));
inventory.addProduct(new Product(750, "Forest Gump", 28, 7.95));

All the new products are calling the Product constructor when created, not the DVD constructor.

Ok so I have completely re-written my code using the base code I created for my first project. I have most of the stuff working right now but can't figure out my display error. The program complies correctly but errors out when I try to run it.

class Product 
{
        private long itemNumber;    // class variable that stores the item number
        private String itemName;    // class variable that stores the item name
        private long invQuantity;   // class variable that stores the quantity in stock
        private double itemPrice;   // class variable that stores the item price
            
            public Product(long number, String name, long quantity, double price) // Constructor for the Supplies class
                {
                    itemNumber = number;
                    itemName = name;
                    invQuantity = quantity;
                    itemPrice = price;
                }
            
            public long getItemNumber() { return itemNumber; } // Method to set item number
            public String getItemName() { return itemName;} // Method to set the item name
            public long getInvQuantity() { return invQuantity;} // Method to set the quantity in stock
            public double getItemPrice() { return itemPrice;} // Method to get the item Price
            
            public double getValue() { return (double) invQuantity * itemPrice; } // Method to calculate the valure of the inventory
            
}  //end class Product

class DVD extends Product
    {
        public DVD(int itemNumber, String itemName, long invQuantity, double itemPrice)
        {
            super(itemNumber, itemName, invQuantity, itemPrice);
        }

       public double getValue()
       {
        double inventoryValue = super.getValue();
        return (inventoryValue * 1.05);
        }    
}

class Inventory3
{
    public static final int MAXIMUM_ITEMS = 4;
    private static Product product[] = new Product[MAXIMUM_ITEMS];
    //Product[] supplies;

        public static void main(String args[])
            {
                buildInventory();
                getInventory();        
            }

        // Build the inventory, storing 10 different products.
        public static void buildInventory()
            {

                product[0] = new Product(1001, "Megaforce", 10, 18.95);
                product[1] = new Product(502, "Abyss", 25, 12.95);
                product[2] = new Product(1003, "Deep Impact", 65, 21.95);
                product[3] = new Product(750, "Forest Gump", 28, 7.95);
                
            }

    public static void getInventory()
        {
            System.out.println(); // blank line
            System.out.println("Welcome to DVD Inventory 1.0"); //display header
            System.out.println();
            for(int i = 0; i < product.length; i++)
                {
                    System.out.println("Item Number: " + product[i].getItemNumber());
                    System.out.println("Item Name: " + product[i].getItemName());
                    System.out.println("Inventory On Hand: " + product[i].getInvQuantity());
                    System.out.printf("Item Price: $%,.2f\n " + product[i].getItemPrice());
                    System.out.printf("Value of Inventory: $%,.2f\n " + product[i].getValue());
                                
                    System.out.println("Restock Fee: " + (product[i].getInvQuantity() * product[i].getItemPrice()) * 0.05);
                    System.out.println();
                    

                }  
        }    
            
    
} //end Class Inventory3

Here is the error I get, I can see my problem is with my display of the itemPrice and Value but how do I correct it. I want to display in dollar value and only display the last 2 digits after the period. if I remove this value from my display it works but gives me a long digital readout "%,.2f"

C:\Temp>java Inventory3

Welcome to DVD Inventory 1.0

Item Number: 1001
Item Name: Megaforce
Inventory On Hand: 10
Item Price: $Exception in thread "main" java.util.MissingFormatArgumentException
: Format specifier ',.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Inventory3.getInventory(Inventory3.java:102)
at Inventory3.main(Inventory3.java:74)

Thanks,

Would you recommend a different method to displaying the value with a 2 digit return after the period?

I'm not saying printf won't work. Perhaps DecimalFormat:
http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

or its parent class, NumberFormat could do the job too:
http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html

Google "java DecimalFormat example" and some decent examples of how to use DecimalFormat come up.

I'm not saying printf won't work. Perhaps DecimalFormat:
http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

or its parent class, NumberFormat could do the job too:
http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html

Google "java DecimalFormat example" and some decent examples of how to use DecimalFormat come up.

Vernon,

Thank you for your help, I think I have figured out why my printf isn't work it's because i'm pulling a value from my array (Inventory) and printf needs a stored value. I've looked over the other 2 links and am a little confused as to how to use the decimalformat to format my out put to display just 10.75 instead of 10.756. Would you have a recommendation for some code I could try outside my program and I can see how to correctly implament it in my program?

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.