This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggling to make this work  RSS feed

 
Kyle Ruey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the assignment is to Modify the Inventory Program by creating a subclass of the product class that uses an 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. I finally got the first part to work write but I am struggling with even getting started on the subclass.

package InventoryProgram;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

/**
*
* @author kyle
*/
public class ProductInventory2 {



/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {


NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);


ArrayList<Product> myItemInventory = new ArrayList<Product>();


System.out.println();
System.out.println();

System.out.println();
System.out.println("Welceome to Warehouse Inventory");
System.out.println();
System.out.println("Here is a list of Iventoried Items");



myItemInventory.add(new Product("Poles", 3,75.00f, 4));
myItemInventory.add(new Product("Transformers", 1,96.75f, 10));
myItemInventory.add(new Product("CutOuts", 2, 36.00f, 100));
myItemInventory.add(new Product("LED LIGHT", 4, 15.00f, 25));



Collections.sort(myItemInventory);

System.out.println();
System.out.println();

System.out.println("Product Information");

double totalInventoryValue = 0;

for (Product item : myItemInventory) {
System.out.println(item.toString());
totalInventoryValue = totalInventoryValue + item.getTotalValue();
}
System.out.println(currency.format(totalInventoryValue));
System.out.println();
System.out.println();
System.out.println("Exiting the Warehouse Inventory");
System.out.println();
System.out.println();

}

public double getTotalValue() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


}

class Product implements Comparable<Product> {

protected String ItemName;
protected int ItemNumber;
protected int ItemsinStock;

protected float ItemPrice;
double restockfee;

public Product(String ItemName, int ItemNumber, int ItemsinStock, float ItemPrice){




}
Product() {

setName("unknown");
setNumber(-1);
setPrice(0);
setItemsinStock(0);
}





Product(String pItemName, int pItemNumber, float pItemPrice,
int pItemsinStock) {

setName(pItemName);
setNumber(pItemNumber);
setPrice(pItemPrice);
setItemsinStock(pItemsinStock);

}

@Override
public int compareTo(Product pAnotherProduct) throws ClassCastException {


if (this.getNumber() > pAnotherProduct.getNumber())
return 1;
else
return -1;

}

public void setName(String pProductName) {

ItemName = pProductName;
}

public void setNumber(int pProductNumber) {
ItemNumber = pProductNumber;
}

public void setPrice(float pItemPrice) {


ItemPrice = pItemPrice;

}

public String getName() {

return ItemName;

}

public int getNumber() {

return ItemNumber;

}

public int getStock() {


return ItemsinStock;

}

public float getItemPrice() {

return ItemPrice;

}

public float getTotalValue() {


return ItemsinStock * ItemPrice;
}

public void setItemsinStock(int pItemsinStock) {
ItemsinStock = pItemsinStock;
}

[code=java]
public String toString() {
return "Item Number[" + ItemNumber + "] Name[" + ItemName
+ "] Quantity[" + ItemsinStock + "] Price Per Unit ["
+ ItemPrice + "] Line Item Value[$" + getTotalValue() + "]";
}
}

I am sorry I do not know how to add the code thingy to make it show up right.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a tutorial on inheritance. https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Since you gave your existing class the generic name "Product" you may want a subclass "ProductWithRestockingFee" (or something like that).
Then you need to look at your requirements and ask what needs to be added to the new class that isn't already in your existing class.

FYI I also fixed the indentation of your code when I re-posted it.

Something you'll need to fix in your existing code is the constructor. It doesn't do anything with the parameters.
Also variable names should start with a lower case character.
 
Kyle Ruey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Here is what the assignment is asking for. Override the toString to display the additional attribute, and your main method will automatically display it if you add an instance of the subclass to your array or array list.

In the subclass, override getTotalValue() to include a 5% restock fee. Similiar to above, if you add an instance of the subclass to your array of Product, you will see it chooses the overridden methods and uses the toString and getTotalValue() from the subclass while instances of the parent class will not.
 
Campbell Ritchie
Marshal
Posts: 55694
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a misunderstanding in that instruction. It says
your main method will automatically display it
What you do is to override the toString method, and then you let polymorphism work its magic. Note the word polymorphism turns into a link. Click on that link. You find out the jiggery‑pokery is done by the Product object, not the main method.
 
Campbell Ritchie
Marshal
Posts: 55694
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you aware of the idiom
super.foo(...);
… where foo is an overridable instance method? That can be very useful and make overriding those three methods very simple.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!