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