Anyway, I'm trying to make a program that can create and store tools at a hardware store. I have a Main class, Tool Class, Hammer Class (which extends Tool Class)
From user input, I want to create a Hammer, and put it into the array or ArrayList for future use. A tool, and a hammer for that matter have multiple attributes (String brand, double price, int sku)
One problem is that I don't know how to create a new object to put into the array with a unique identifier. Once I've created a new Hammer, how do I take user input and customize the hammer with brand, price, sku? Any help would be greatly appreciated.
This is what I have so far:
BTW I suggest you change the to so that it doesn't matter what case the user uses when entering the tool name.
Once you have got used to that sort of thing, you can reintroduce the other fields.
You do realise that the loop you have will fill your array entirely with hammers, even if you might want drills or saws in it?
There is something not quite right about entering types, e.g. Hammer, as text and then checking the names. I am not sure what the best solution would be.
I need to know how to make a tool with user input with several fields,
You store each of the input values in a different variable ie called type, brand, price, sku and code the constructor of your Hammer class so that it accepts three parameters ie brand, price and sku.
Then when create an instance of Hammer you pass in the values ie
J. Chris Miller wrote:I need to know how to make a tool with user input with several fields, and then add that to an array or ArrayList.
Note that this is (at least) three distinct problems:
1) How to make a tool with several fields
2) How to get user input
3) How to add your object to an array.
Each of these can (and SHOULD) be done completely independently. Assuming your tool has a type, brand, and cost, it doesn't matter where those values come from - they could come from user input, from a database read, from a file, or even be hard-coded. Figure out how to create your tools first.
THEN figure out how to get user input.
THEN figure out how to create the tool using your user's input.
Ashish Dutt wrote:Fred, can you tell how to make the ArrayList hold something like SimpleTool?
I thought my previous post spelled it out...
ArrayList t = new ArrayList<String>();
makes an ArrayList that can hold Strings,
what do you think you should do to make an ArrayList that can hold SimpleTools?
Marshall Brett wrote:
What you're doing here is creating a new object of type SimpleTool. The fact that you've made a class for this object means you can declare an array of type SimpleTool.
Well, what happened when you tried it? Did the compiler complain when you tried to add x to tools? Did you get an error when you ran the code?
IMO, the best way to learn something (at least so that it sticks) is hands-on experience.
[EDIT: I thought this post was from the OP -- it is not. And as such, it can be read as another rancher summarizing, and not asking for elaboration. So, apologies if this post makes an assumption that isn't true.]