By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 992 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Inventory Program Help

P: 9
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%

Any help or suggestions will be greatly appreciated!

Expand|Select|Wrap|Line Numbers
  1. class Product implements Comparable
  2. {
  3.         private long itemNumber;    // class variable that stores the item number
  4.         private String itemName;    // class variable that stores the item name
  5.         private long invQuantity;   // class variable that stores the quantity in stock
  6.         private double itemPrice;   // class variable that stores the item price
  7.  
  8.             public Product(long number, String name, long quantity, double price) // Constructor for the Supplies class
  9.                 {
  10.                     itemNumber = number;
  11.                     itemName = name;
  12.                     invQuantity = quantity;
  13.                     itemPrice = price;
  14.                 }
  15.  
  16.             public void setItemNumber(long number)  // Method to set the item number
  17.                 {
  18.                     this.itemNumber = number;
  19.                 }
  20.  
  21.             public long getItemNumber()  // Method to get the item number
  22.                 {
  23.                     return itemNumber;
  24.                 }
  25.  
  26.             public void setItemName(String name)  // Method to set the item name
  27.                 {
  28.                     this.itemName = name;
  29.                 }
  30.  
  31.             public String getItemName()  // Method to get the item name
  32.                 {
  33.                     return itemName;
  34.                 }
  35.  
  36.             public void setinvQuantity(long quantity)  // Method to set the quantity in stock
  37.                 {
  38.                     invQuantity = quantity;
  39.                 }
  40.  
  41.             public long getInvQuantity()  // Method to get the quantity in stock
  42.                 {
  43.                     return invQuantity;
  44.                 }
  45.  
  46.             public void setItemPrice(double price)  // Method to set the item price
  47.                 {
  48.                     this.itemPrice = price;
  49.                 }
  50.  
  51.             public double getItemPrice()  // Method to get the item price
  52.                 {
  53.                     return itemPrice;
  54.                 }
  55.  
  56.             public double calculateInventoryValue()  // Method to calculate the value of the inventory
  57.                 {
  58.                     return itemPrice * invQuantity;
  59.                 }
  60.  
  61.             public int compareTo(Object o)
  62.                 {
  63.                     Product p = null;
  64.                         try
  65.                             {
  66.                                 p = (Product) o;
  67.                             }
  68.  
  69.                         catch (ClassCastException cE)
  70.                             {
  71.                                 cE.printStackTrace();
  72.                             }
  73.  
  74.                         return itemName.compareTo(p.getItemName());
  75.                 }
  76.  
  77.             public String toString()
  78.                 {
  79.                     return "Item #: " + itemNumber + "\nName: " + itemName + "\nQuantity: " + invQuantity + "\nPrice: $" + itemPrice + "\nValue: $" + calculateInventoryValue();
  80.                 }
  81. }  //end class Product
  82.  
  83. class DVD extends Product
  84. {
  85.     private double reStockingFee;
  86.  
  87.         public DVD(int itemNumber, String itemName, long invQuantity, double itemPrice, double reStockingFee) 
  88.             {
  89.                    super(itemNumber, itemName, invQuantity, itemPrice);
  90.                    this.reStockingFee = reStockingFee;
  91.               }    
  92.         public double getItemPrice() //returns the value of the inventory, plus the restocking fee
  93.             {
  94.                 return super.getItemPrice() + reStockingFee; // TODO Auto-generated method stub
  95.             }
  96.          public String toString() 
  97.             {
  98.                 return new StringBuffer().append("Price: " + super.getItemPrice()).append(" With RestockingFee: " + getItemPrice()).toString();
  99.             }
  100.  
  101. } // end class DVD
  102.  
  103.  
  104. public class Inventory3
  105.  
  106.     {
  107.         Product[] supplies;
  108.  
  109.         public static void main(String[] args)
  110.             {
  111.                 Inventory3 inventory = new Inventory3();
  112.                 inventory.addProduct(new Product(1001, "Megaforce", 10, 18.95));
  113.                 inventory.addProduct(new Product(502, "Abyss", 25, 12.95));
  114.                 inventory.addProduct(new Product(1003, "Deep Impact", 65, 21.95));
  115.                 inventory.addProduct(new Product(750, "Forest Gump", 28, 7.95));
  116.  
  117.                 System.out.println(); // blank line
  118.                 System.out.println("Welcome to DVD Inventory 1.0"); //display header
  119.                 inventory.sortByName(); //sort list by name
  120.                 System.out.println(); // blank line
  121.                 inventory.showInventory(); //display inventory
  122.  
  123.                 double total = inventory.calculateTotalInventory();
  124.                 System.out.println("Total Value is: $" + total);
  125.  
  126.             }
  127.  
  128.         public void sortByName()
  129.             {
  130.                 for (int i = 1; i < supplies.length; i++)
  131.                     {
  132.                         int j;
  133.                         Product val = supplies[i];
  134.                         for (j = i - 1; j > -1; j--)
  135.                             {
  136.                                 Product temp = supplies[j];
  137.                                 if (temp.compareTo(val) <= 0)
  138.                                     {
  139.                                     break;
  140.                                     }
  141.                                 supplies[j + 1] = temp;
  142.                             }
  143.                         supplies[j + 1] = val;
  144.                     }
  145.             }
  146.  
  147.         public String toString() //creates a String representation of the array of products
  148.             {
  149.                 String s = "";
  150.                 for (Product p : supplies)
  151.                     {
  152.                         s = s + p.toString();
  153.                         s = s + "\n\n";
  154.                     }
  155.                 return s;
  156.             }
  157.  
  158.         public void addProduct(Product p1) //Increases the size of the array
  159.             {
  160.                 if (supplies == null)
  161.                     {
  162.                         supplies = new Product[0];
  163.                     }
  164.                 Product[] p = supplies; //Copy all products into p first
  165.                 Product[] temp = new Product[p.length + 1]; //create bigger array
  166.                 for (int i = 0; i < p.length; i++)
  167.                     {
  168.                         temp[i] = p[i];
  169.                     }
  170.                 temp[(temp.length - 1)] = p1; //add the new product at the last position
  171.                 supplies = temp;
  172.             }
  173.  
  174.         public double calculateTotalInventory() //sorting the array using Bubble Sort
  175.             {
  176.                 double total = 0.0;
  177.                 for (int i = 0; i < supplies.length; i++)
  178.                     {
  179.                         total = total + supplies[i].calculateInventoryValue();
  180.                     }
  181.                 return total;
  182.             }
  183.  
  184.         public void showInventory()
  185.         {
  186.             System.out.println(toString()); //call our toString method
  187.         }
  188.  
  189.     } //end Class Inventory3
  190.  
Mar 21 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 785
The stocking fee is not showing up because you are not creating an object of class DVD (the derived class). You are only creating objects of the parent class Product.
Just ask yourself: How can something magically come out that you have not put in? You must define your stocking fee somewhere, and you have to do it in your code in the constructor.

Here you are only adding "Product", but no "DVD"
Expand|Select|Wrap|Line Numbers
  1.                 Inventory3 inventory = new Inventory3();
  2.                 inventory.addProduct(new Product(1001, "Megaforce", 10, 18.95));
  3.                 inventory.addProduct(new Product(502, "Abyss", 25, 12.95));
  4.                 inventory.addProduct(new Product(1003, "Deep Impact", 65, 21.95));
  5.                 inventory.addProduct(new Product(750, "Forest Gump", 28, 7.95));
  6.  
so simply add

Expand|Select|Wrap|Line Numbers
  1.                 inventory.addProduct(new DVD(1005, "Megaforce on DVD", 10, 18.95, 0.5));
Mar 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.