• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with adding shopping items

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem with adding certain features to my code, my code is working fine at the moment but I still have to add in certain amount of features this is my current code:




I want to be able to add up each option to together even if the user wished to buy all items available and to get the sum of the total cost with all the items shown that are bought and how much each costs when the users chooses each item, so i want the output print out to look like a regular receipt, I also want to be able to not allow the program to crash if the user adds in a number that is not available on the list shown and for the user to be told to try again endlessly until he decides to quite, so a quite option as well. I have looked on the internet to find out a solution for numerous hours but I have only found simple adding coding for simple basic programs, and I don't even know how to implement them into my program, I honestly am not experienced enough to be able to simply start a program from scratch, so obviously most of the stuff in my coding is robbed from the internet but I modified it to the best of my coding abilities, so I will need more help than the average coder since I'm pretty new. Thanks
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jordan,

to let the user be able to select more than just one item, you would use line 48 in a loop, something like:

However, you now have to store all the inputted choices, to proces them later. You can use an array for this, but a small problem is that an array has a fixed length. So you must set upfront how many items a user can buy.
Are you familiar with Lists? Thse are kind of arrays too, but can adjust their length, if and when necessary.

By the way: how about creating a class Product, with fields: String name and int price? That will be much much more handy than having a separate array for each Product property like you have now. For instance:

and have an array Product[], that you can use in your total price and average methods.

 
Jordan Belford
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Jordan,

to let the user be able to select more than just one item, you would use line 48 in a loop, something like:

However, you now have to store all the inputted choices, to proces them later. You can use an array for this, but a small problem is that an array has a fixed length. So you must set upfront how many items a user can buy.
Are you familiar with Lists? Thse are kind of arrays too, but can adjust their length, if and when necessary.

By the way: how about creating a class Product, with fields: String name and int price? That will be much much more handy than having a separate array for each Product property like you have now. For instance:

and have an array Product[], that you can use in your total price and average methods.





I read what you have to say, and I understand what you're saying but I don't know how exactly to do it, what code i need to do that and where in my program i must place it
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jordan,

you can put that Product class code simply after your TaTASK1 code, so you would get:

And there are many advises to be given here. First of all, I would not use fixed length arrays, but flexible length Lists (for instance ArrayLists). If you have not encountered Lists so far, just think of a List as a very handy array. that you do not have to give a length. To add an element to a List, just used list.add(element), and to get one element from a list you can use list.get(index of the element that you want). Have a look at the API.

You can then make your code a bit neater. For instance: your main method could look like:


 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Jordan Belford
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to add up multiple items from an array shown in the output and for the user to be able to choose more than one item from the list, I also want to get a list shown of the items selected and the prices for each and the total sum of the prices at the end, i need to stick to an array list, this is my current program down below:



how can I prove on it? Don't forget that i must stick with a list array
 
Jordan Belford
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Jordan Belford,
I have merged your topic into this topic. I hope that helps.




How are people going to see that it's a new thread since they must think this is an old thread by now if you merged it and won't help anymore
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The forum is displayed in reverse order of past date, so the most recent reply comes to the top. It is also not a good idea to ask the same question twice.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic