Hi! I've been trying to get my inventory program to compile, but haven't had any luck. My instructor tells me that my arguments in the Inventory3 class need to match the datatype of the constructor in my Product class. I'm sure this is something rather simple that I should be able to figure out, but I'm at my wits end trying to make this work. Does anyone have any suggestions? My code is below. Thanks so much to anyone with advice in advance!! - class Product
-
{
-
private int itemNumber; //Number assigned to product
-
private String itemName; //Name of product
-
private int stock; //Number of units of the product in stock
-
private double price; //Price of each unit of the product
-
-
public Product () //constructor for the product class
-
{
-
itemNumber = 0;
-
itemName = "";
-
stock = 0;
-
price = 0.00;
-
} //end constructor
-
-
public Product (int itemNumber, String itemName, int stock, double price) //begin constructor for item array
-
{
-
this.itemNumber = itemNumber;
-
this.itemName = itemName;
-
this.stock = stock;
-
this.price = price;
-
}
-
-
public void setItemNumber(int itemNumber) //sets and gets the item number
-
{
-
this.itemNumber = itemNumber;
-
}
-
public int getItemNumber()
-
{
-
return itemNumber;
-
}
-
-
-
public void setItemName(String itemName) //sets and gets the item name
-
{
-
this.itemName = itemName;
-
}
-
public String getItemName()
-
{
-
return itemName;
-
}
-
-
-
public void setStock(int stock) //sets and gets the number of units in stock
-
{
-
this.stock = stock;
-
}
-
public int getStock()
-
{
-
return stock;
-
}
-
-
-
public void setPrice(double price) //sets and gets the price per unit
-
{
-
this.price = price;
-
}
-
public double getPrice()
-
{
-
return price;
-
}
-
-
-
public double value() //calculates the value of the stock by multiplying the number of units on hand by the price of each unit
-
{
-
return stock * price;
-
}
-
-
public String toString()
-
{
-
return "Item Number: "+itemNumber + "\nProduct Name: "+itemName+"\nQuantity in Stock: "+stock+"\nPrice per Unit: $"+price+"\nValue of Entire Stock: $"+value();
-
}
-
-
}//end class Product
-
-
class Plastic extends Product
-
{
-
private String itemColor; //color of product
-
-
public Plastic () //begin constructor
-
{
-
super();
-
itemColor = "";
-
}
-
-
public Plastic ( int itemNumber, String itemName, int stock, double price, String itemColor )
-
{
-
super(itemNumber, itemName, stock, price );
-
this.itemColor = itemColor; //add new attribute
-
}
-
-
public void setItemColor(String itemColor) //sets and gets the item color
-
{
-
this.itemColor = itemColor;
-
}
-
public String getItemColor()
-
{
-
return itemColor;
-
}
-
-
public double restockFee()
-
{
-
return value() * 0.05;
-
}
-
-
} //end class Plastic
-
-
public class Inventory3 //begin class Inventory3
-
{
-
public static void main( String args[])
-
{
-
Product[] item = new Product [4];
-
-
Product item1 = new Product (1003182, "Polytrope TPP", 7, 1.81, "Black" );
-
Product item2 = new Product (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
-
Product item3 = new Product (1013972, "Polyvin PVC-DC", 8, 1.75, "Beige" );
-
Product item4 = new Product (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
-
-
item[0] = item1;
-
item[1] = item2;
-
item[2] = item3;
-
item[3] = item4;
-
-
-
double all = 0.00;
-
-
for (int total= 0; total < 4;total++)
-
{
-
all = all + item[total].value();
-
}
-
-
for (Product myProduct: item)
-
{
-
System.out.println(item);
-
System.out.println();
-
}
-
-
System.out.println("The Value of all items combined is: $"+all);
-
} //end method main
-
} //end class Inventory3
17 2246
Hey, next time also post the errors you're getting when you try to compile, that would help people out.
See in inventory3 where you're calling new Product(bla bla)
Don't you mean new Plastic?
See, in inventory3 you're writing this:
"Product item1 = new Product (1003182, "Polytrope TPP", 7, 1.81, "Black" );"
which is Product (int, string, int, double, string), but looking at your product's constructor, like your teacher said, it doesn't match, it's like this, from your code:
"public Product (int itemNumber, String itemName, int stock, double price)"
Where's that string I wondered... you're extending class Plastic and the constructor for class plastic is from your code:
"public Plastic ( int itemNumber, String itemName, int stock, double price, String itemColor )"
So I believe you wanted to type: - Product item1 = new Plastic (1003182, "Polytrope TPP", 7, 1.81, "Black" );
-
Product item2 = new Plastic (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
-
Product item3 = new Plastic (1013972, "Polyvin PVC-DC", 8, 1.75, "Beige" );
-
Product item4 = new Plastic (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
Hope that helped...
-Asaf Erlich
Hi! I've been trying to get my inventory program to compile, but haven't had any luck. My instructor tells me that my arguments in the Inventory3 class need to match the datatype of the constructor in my Product class. I'm sure this is something rather simple that I should be able to figure out, but I'm at my wits end trying to make this work. Does anyone have any suggestions? My code is below. Thanks so much to anyone with advice in advance!! - Product item1 = new Product (1003182, "Polytrope TPP", 7, 1.81, "Black" );
-
Product item2 = new Product (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
-
Product item3 = new Product (1013972, "Polyvin PVC-DC", 8, 1.75, "Beige" );
-
Product item4 = new Product (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
You don't have Product constructors taking five parameters. That's what your
instructor is trying to tell you. Carefully check your Product constructors.
kind regards,
Jos
You guys are super ... thanks - that worked! My program compiles now, but it's not giving me the correct ouput. This is what I get rather than a list of inventory items with their respective details:
[LProduct;@42e816
[LProduct;@42e816
[LProduct;@42e816
[LProduct;@42e816
The Value of all items combined is: $45.96
Press any key to continue . . . .
Any suggestions? I don't even know where to begin with this one!
You guys are super ... thanks - that worked! My program compiles now, but it's not giving me the correct ouput. This is what I get rather than a list of inventory items with their respective details:
[LProduct;@42e816
[LProduct;@42e816
[LProduct;@42e816
[LProduct;@42e816
The Value of all items combined is: $45.96
Press any key to continue . . . .
Any suggestions? I don't even know where to begin with this one!
yes add a boolean statement
with a while statement
use a scanner to prompt the user to make a choice.
if yes then continue
if no close the program.
nomad
yes add a boolean statement
with a while statement
use a scanner to prompt the user to make a choice.
if yes then continue
if no close the program.
nomad
I tried this and it didn't help. I'm still getting the same output except now with the option
for the user to enter a choice. Any other suggestions?
You guys are super ... thanks - that worked! My program compiles now, but it's not giving me the correct ouput. This is what I get rather than a list of inventory items with their respective details:
[LProduct;@42e816
[LProduct;@42e816
[LProduct;@42e816
[LProduct;@42e816
The Value of all items combined is: $45.96
Press any key to continue . . . .
Any suggestions? I don't even know where to begin with this one!
You're printing the wrong object; you're printing 'item' four times; you should
print myProduct instead. Note that the output is the same four times which
matches the fact that you're printing the same object four times.
kind regards,
Jos
You're printing the wrong object; you're printing 'item' four times; you should
print myProduct instead. Note that the output is the same four times which
matches the fact that you're printing the same object four times.
kind regards,
Jos
Thanks Jos. That fixed my output errors, but it's not printing the additional attribute (color) or restock fee that's in my "Plastic" class. How do I link the two up so everything prints?
You will probably have to override the toString() method in Plastic. It can be very simple - call Product's .toString() to get all that information, concatenate the additional information that Plastic has, and return the resultant String.
You will probably have to override the toString() method in Plastic. It can be very simple - call Product's .toString() to get all that information, concatenate the additional information that Plastic has, and return the resultant String.
I feel so dumb when it comes to this stuff! How do I call the toString from the Product class into the Plastic class?
By using the super keyword: - super.superclassMethodHere()
In this case, it's super.toString()
By using the super keyword: - super.superclassMethodHere()
In this case, it's super.toString()
I added this plus the additional information, but now I'm getting a new error ... - class Plastic extends Product
-
{
-
private String itemColor; //color of product
-
-
public Plastic () //begin constructor
-
{
-
super();
-
itemColor = "";
-
}
-
-
public Plastic ( int itemNumber, String itemName, int stock, double price, String itemColor )
-
{
-
super.toString();
-
return "Item Number: "+itemNumber + "\nProduct Name: "+itemName+"\nQuantity in Stock: "+stock+"\nPrice per Unit: $"+price+"\nValue of Entire Stock: $"+value+"\nProduct Color"+itemColor+"\nValue of Inventory with a restocking fee: $:"+restockFee();
-
this.itemColor = itemColor; //add new attribute
-
}
-
-
public void setItemColor(String itemColor) //sets and gets the item color
-
{
-
this.itemColor = itemColor;
-
}
-
public String getItemColor()
-
{
-
return itemColor;
-
}
-
-
public double restockFee()
-
{
-
return getPrice() * 0.05;
-
}
-
-
} //end class Plastic
error:
java:96: cannot return a value from method whose result type is void
return "Item Number: "+itemNumber + "\nProduct Name: "+itemName+"\nQuantity in Stock: "+stock+"\nPrice per Unit: $"+price+"\nValue of Entire Stock: $"+value+"\nProduct Color"+itemColor+"\nValue of Inventory with a restocking fee: $:"+restockFee();
the ^ is below the final + before 'restockFee'
You stuck that piece of code in your constructor. You're supposed to override
the superclass's toString() method and call it too; like this: -
public String toString() { return super.toString()+" my additional info"; }
kind regards,
Jos
You stuck that piece of code in your constructor. You're supposed to override
the superclass's toString() method and call it too; like this: -
public String toString() { return super.toString()+" my additional info"; }
kind regards,
Jos
Perfect - Thank you!! Is there anywhere that I can put in the %.2f argument to keep my restock fee to 2 decimal places? I've tried putting it in several places, but end up either having it print or with an error.
Perfect - Thank you!! Is there anywhere that I can put in the %.2f argument to keep my restock fee to 2 decimal places? I've tried putting it in several places, but end up either having it print or with an error.
Not sure but I think you want to add %2f to the
public PrintStream format(String format, Object... args)
nomad
Thanks for the tips. I still haven't been able to get the 2-decimal point to work. But I've moved on to bigger and better and things for the moment.... I just have to make my items show up in a frame one at a time, which I've managed to figure out. But I want the entire application to close when I finish going through each window. I'm trying to use EXIT_ON_CLOSE, but it's not working properly. Can anyone tell me what I'm missing? This assignment is due tonight so any quick help would be GREATLY appreciated!! - public class InventoryGui extends JFrame //begin class InventoryGui
-
{
-
public static void main( String args[])
-
{
-
Product[] item = new Product [4];
-
-
Product item1 = new Plastic (1003182, "Polytrope TPP", 7, 1.81, "Black" );
-
Product item2 = new Plastic (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
-
Product item3 = new Plastic (1013972, "Polyvin PVC-DC", 5, 1.15, "Beige" );
-
Product item4 = new Plastic (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
-
-
item[0] = item1;
-
item[1] = item2;
-
item[2] = item3;
-
item[3] = item4;
-
-
double all = 0.00;
-
-
for (int total= 0; total < 4;total++)
-
{
-
all = all + item[total].value();
-
}
-
-
for (Product myProduct: item)
-
{
-
System.out.println(myProduct);
-
System.out.println();
-
}
-
-
System.out.println("The Value of all items combined is: $"+all);
-
-
JOptionPane.showMessageDialog( null, "The first item is: "+item1, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display first item
-
JOptionPane.showMessageDialog( null, "The second item is: "+item2, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display second item
-
JOptionPane.showMessageDialog( null, "The third item is: "+item3, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display third item
-
JOptionPane.showMessageDialog( null, "The fourth item is: "+item4, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display fourth item
-
JOptionPane.showMessageDialog( null, "The value of the entire inventory is: "+all, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display inventory value total
-
-
InventoryGui inventoryGui = new InventoryGui(); //create InventoryGui
-
inventoryGui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); //set frame to close on exit
-
-
} //end main method
-
-
} //end class InventoryGui
Thanks for the tips. I still haven't been able to get the 2-decimal point to work. But I've moved on to bigger and better and things for the moment.... I just have to make my items show up in a frame one at a time, which I've managed to figure out. But I want the entire application to close when I finish going through each window. I'm trying to use EXIT_ON_CLOSE, but it's not working properly. Can anyone tell me what I'm missing? This assignment is due tonight so any quick help would be GREATLY appreciated!! - public class InventoryGui extends JFrame //begin class InventoryGui
-
{
-
public static void main( String args[])
-
{
-
Product[] item = new Product [4];
-
-
Product item1 = new Plastic (1003182, "Polytrope TPP", 7, 1.81, "Black" );
-
Product item2 = new Plastic (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
-
Product item3 = new Plastic (1013972, "Polyvin PVC-DC", 5, 1.15, "Beige" );
-
Product item4 = new Plastic (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
-
-
item[0] = item1;
-
item[1] = item2;
-
item[2] = item3;
-
item[3] = item4;
-
-
double all = 0.00;
-
-
for (int total= 0; total < 4;total++)
-
{
-
all = all + item[total].value();
-
}
-
-
for (Product myProduct: item)
-
{
-
System.out.println(myProduct);
-
System.out.println();
-
}
-
-
System.out.println("The Value of all items combined is: $"+all);
-
-
JOptionPane.showMessageDialog( null, "The first item is: "+item1, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display first item
-
JOptionPane.showMessageDialog( null, "The second item is: "+item2, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display second item
-
JOptionPane.showMessageDialog( null, "The third item is: "+item3, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display third item
-
JOptionPane.showMessageDialog( null, "The fourth item is: "+item4, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display fourth item
-
JOptionPane.showMessageDialog( null, "The value of the entire inventory is: "+all, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display inventory value total
-
-
InventoryGui inventoryGui = new InventoryGui(); //create InventoryGui
-
inventoryGui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); //set frame to close on exit
-
-
} //end main method
-
-
} //end class InventoryGui
I think you better read up on swings...
couple of things you have some of your codes in the wrong order, and you are not using them correctly.
you also need to add(item),
setVisible(true)
Public static void main (String[] argements){
InventoryGui inventoryGui = new InventoryGui();
}
Sandy
Here's what I added but I can still see it's wrong. Do I need to call each item[ ] ... and how do I do that if so? -
-
public class InventoryGui extends JFrame //begin class InventoryGui
-
{
-
private JLabel items; // JLabel with just text
-
public InventoryGui()
-
{
-
setLayout( new FlowLayout() );
-
items = new JLabel("text");
-
add(items);
-
}
-
public static void main( String args[])
-
{
-
InventoryGui inventoryGui = new InventoryGui(); //create InventoryGui
-
inventoryGui.setVisible( true ); // display frame
-
inventoryGui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); //set frame to close on exit
-
Product[] item = new Product [4];
-
Product item1 = new Plastic (1003182, "Polytrope TPP", 7, 1.81, "Black" );
-
Product item2 = new Plastic (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
-
Product item3 = new Plastic (1013972, "Polyvin PVC-DC", 5, 1.15, "Beige" );
-
Product item4 = new Plastic (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
-
item[0] = item1;
-
item[1] = item2;
-
item[2] = item3;
-
item[3] = item4;
-
double all = 0.00;
-
for (int total= 0; total < 4;total++)
-
{
-
all = all + item[total].value();
-
}
-
for (Product myProduct: item)
-
{
-
System.out.println(myProduct);
-
System.out.println();
-
}
-
System.out.println("The Value of all items combined is: $"+all);
-
JOptionPane.showMessageDialog( null, "The first item is: "+item1, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display first item
-
JOptionPane.showMessageDialog( null, "The second item is: "+item2, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display second item
-
JOptionPane.showMessageDialog( null, "The third item is: "+item3, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display third item
-
JOptionPane.showMessageDialog( null, "The fourth item is: "+item4, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display fourth item
-
JOptionPane.showMessageDialog( null, "The value of the entire inventory is: "+all, "ASI Product Inventory", JOptionPane.PLAIN_MESSAGE ); //display inventory value total
-
} //end main method
-
} //end class InventoryGui
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Matt Eberts |
last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object
instantiated via a constructor using the arguments object and have it
expanded, so to speak, so that it doesn't appear as...
|
by: The Directive |
last post by:
This code will not compiled:
In main function:
Dot temp = new Dot( *(new Point( 5, 5 )) );
In Dot class:
//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5...
|
by: Andy Buckley |
last post by:
Hi,
A friend and I have recently had trouble getting code to compile when
using temporary objects in constructors. A minimal example is below:
This code fragment is used to construct seven ROD...
|
by: Hanika |
last post by:
Hello All,
I onced used a very efficient method in VBScript to retrive nammed arguments
passed on the commanline using:
WScript.Arguments.Named.Item ("ArgumenName").
That is, the call to the...
|
by: Allan Ebdrup |
last post by:
I have the following class in C# with two constructors
--------------
public class JobfolderEmailRecord
{
public JobfolderEmailRecord(int Id)
{
//code for constructor
}
public...
|
by: Telmo Costa |
last post by:
Hi. I have the following code:
--------------------------------------
function Tunnel() {
//arguments(???);
}
function Sum() {
var sum = 0;
for (i=0; i<arguments.length; i++) sum +=...
|
by: Pacific Fox |
last post by:
Hi all,
haven't posted to this group before, but got an issue I can't work
out... and hoping to get some help here ;-)
I've got a base object that works fine with named arguments when called...
|
by: saxenavaibhav17 |
last post by:
Actually at some place I am creating object of outcome class.
its taking five argument.
i don't want to give 3 and 4 th argument.
i want to give 1,2 and 5 th argument.
Outcome...
|
by: subramanian100in |
last post by:
I am copying the following text as it is from Stroustrup's TC++PL 3rd
edition, page 450.
It says:
"Note that a constructor given two arguments of the same type can be a
match for more than...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |