Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Adding object into list through user input

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone its my first post and im in desperate need of help. I'm currently working on a project in school. My instructor wants us to create an array with hard coded values and then add the objects from the array into the list. I've already created the hard-coded array and have added the objects into the list that I have created. Now what I want to do next is ask the user if they want to add more items, if yes then I have to add the items they have entered into my list, if no, I need to output a total for the cost.

Here is my code, I have an Item class and Sales class

So far this what I have for my Salclass


Thanks in advance for your input.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can receive all 4 inputs from user, and then create an Item object and populate its fields. Thereafter, you can add this new Item object to the list.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Val Simon Wrote:
I've already created the hard-coded array and have added the objects into the list that I have created.


there is no need for hard-coding. That may not help for adding the objects. Hope this code helps you.

 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know of companies ill-treating employees... but selling them outright! God help us all.

[EDIT] Trying to be more politically correct.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Aditya Jha Good evening,

It is not to ill -treat. i Just didn't have time to change few things. and i am tied %')
 
Val Simon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepika deepi wrote:hi

Val Simon Wrote:
I've already created the hard-coded array and have added the objects into the list that I have created.


there is no need for hard-coding. That may not help for adding the objects. Hope this code helps you.



My instructor wants the values to be hard-coded, its one of the specifications in this project. Thank you for the code, I will try to incorporate it with what I have.

Is there any way to add the user's input directly into my list?
I know this is wrong, but just for demonstration sake:



I need to keep adding stuff to my list until the user enters 'N'. Thanks
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Val Simon Wrote:
I need to keep adding stuff to my list until the user enters 'N'.



you can use loop for checking the statement it it's true do some thing, when user types N break the statement



Hope this will help you to get the stuff till the user enters N.
 
Val Simon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepika deepi wrote:

Val Simon Wrote:
I need to keep adding stuff to my list until the user enters 'N'.



you can use loop for checking the statement it it's true do some thing, when user types N break the statement



Hope this will help you to get the stuff till the user enters N.



I get an error on this bit of code

obj.add(answer);
**The method add(Item) in the type list is not applicable for the arguments(String)

Is it because answer is a String while my List is from my Item class? Is there any way around this? Thanks.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Jha wrote:You can receive all 4 inputs from user, and then create an Item object and populate its fields. Thereafter, you can add this new Item object to the list.


Does this sound feasible to you? If you have any doubts on any of these steps, do let us know.
 
Marshal
Posts: 77523
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Jha wrote: . . .] Trying to be more politically correct.

It never works

Careful about spelling: you have written quantity in one place and quality in another.
Have you really been told to create an Item class with a quantity field? The word item precludes any argument about quantity: an item is always one. You might buy several items of the same sort when shopping, but if I buy two bags of flour, for example, my receipt will look like this

cheese
butter
flour
flour
sugar

I think you need a class intermediate between item and order, which you might call line, which encapsulates the type of goods (item) and quantity, as long as you don’t buy more than 2147483647 at a time.
 
Campbell Ritchie
Marshal
Posts: 77523
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Val Simon wrote: . . .

I get an error on this bit of code

obj.add(answer);
**The method add(Item) in the type list is not applicable for the arguments(String)

Is it because answer is a String while my List is from my Item class? Is there any way around this? Thanks.

No.
It is because you told the compiler you would only add Items to that list, and you are adding something different. Look at line 60 in your code. You need to pass the String or whatever to the Item constructor, and add the new Item object.
 
It's never done THAT before. Explain it to me tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic