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! -
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
-
1 2651
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" -
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));
-
so simply add - inventory.addProduct(new DVD(1005, "Megaforce on DVD", 10, 18.95, 0.5));
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: zaidalin79 |
last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the assignments...
4. CheckPoint: Inventory Program Part 1
• Resource: Java: How to Program
• Due Date: Day 5 forum
• Choose a product that lends...
|
by: south622 |
last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment.
Here is the...
|
by: nexcompac |
last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder.
Still getting used to the whole java world and I am trying to compile a program using TextPad. Here is what I have so far.
/*
* Main.java
*
* Created on July 19, 2007, 5:54 PM
*
|
by: hamiltongreg |
last post by:
I am new to Java and am having problems getting my program to compile correctly.
My assignment is as follows;
Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).
• Create a product class that holds the item number, the name of the product, the...
|
by: cblank |
last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory program that I have built so far with no problem. But I cannot figure out how to turn it into a GUI application.
This is what I'm trying to get it to do....
| |
by: cblank |
last post by:
I'm having some trouble with my inventory program. Its due tom and my teacher is not wanting to help. He keeps giving me a soluction that is not related to my code. I have everything working except the Delete and Modify Button. The Search button works but it only searchs the new .dat file or what is set in the array. Not sure what is going on...
|
by: pinkf24 |
last post by:
I cannot figure out how to add the following:
Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an...
|
by: 100grand |
last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one
product at a time, including the item number, the name of the product, the number of
units in stock, the price of each unit, and the value of the inventory of that product. In
addition, the GUI should display the value of the entire inventory, the additional...
|
by: blitz1989 |
last post by:
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the reading has very little in it that helps can someone take a look at this code and point me in the right direction please. Thanks for any help that is...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |