• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Struggling to make this work

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9565
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go
 
Carey Brown
Saloon Keeper
Posts: 9565
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic