• 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

Need help with java program to add more than one item on list

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i choose more than one item from my list, i can't seem to find out how to do it, i want to be able to choose multiple items and to also add up the prices to a total
     this is my code is far:





Also is this considered an array, if not what is it called, i'm not experience with the naming in coding
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you went too far with coding exercise and lost the track somewhere way back.

I usually solve problems in small chunks, and those chunks being represented by methods. In normal conditions they are small. About 5 lines long each.

In order to be able start with methods, you need to know what is the exercise about, so you could devide the task into many smaller tasks, those smaller tasks into smaller again, until you have pieces which are managable to solve.

Would you like to describe what is the coding exercise about?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A number of things:

1. You keep starting new threads but you're asking about the same problem. Please stop doing that as you're likely to get on the wrong side of a moderator and you don't want that to happen.

2. Understand that nobody gets paid to answer questions here; this is a volunteer site and people here help out and answer questions for many reasons but none of them are about money because there is none.

3. You seem to be trying to tackle a problem that is out of the range of your current knowledge of Java and programming constructs. You might want to go back and review some of the concepts that you've been taught and work on understanding each one first.

That said, I'll post some followups to walk through some issues that are making it hard for people to give you advice. Again, some of that goes back to your apparent lack of understanding of certain aspects of programming that are needed to write the program you seem to want to write.



 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing that's making it difficult for people to help you: your code formatting and indentation is atrocious. Reading code that is not formatted properly is very difficult because it's harder to understand the organization and flow of the logic. Also, you have way too much code in main(). That's bad. Good programs are broken down into small logical chunks of functionality where each chunk does one and only one thing. Ideally, your main() method should consist of only a few lines of code to get the program started and then to shut it down gracefully.

Here's your code formatted properly. It took me just a few seconds to do this since all I had to do was to copy it all, then paste it into my IDE and hit the keyboard shortcut to reformat the code. You should do this too, going forward. If you keep posting poorly formatted code, people will be less inclined to read it and therefore less inclined to help you. That's probably one of the main reasons you're not getting as many responses.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One concept that you appear to be lacking some understanding of is that of classes and objects. Here's the Java Tutorial for that: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

If I were to solve this problem you're working on, I'd probably try to have at least two classes to help: a Product class and an OrderItem class.

The Product class would help manage information about each product you want to sell, its description, and perhaps its retail price. An OrderItem would help manage information about a product being purchased, the quantity and the cost to purchase, based on the product's price.

I would also probably use a List instead of an array. Here are a couple of tutorials that will help you understand the difference between the two:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Liu's earlier point about breaking up your program into small chunks, here's what a decent main() method might look like:

That's it.

This is what the execute() method might look like:

As you can see, there's no detail in this method. It's just composed of calls to other methods that do specific tasks. Reading this code, however, can give a good idea of the flow of the program logic and what's being done overall.

This is called functional decomposition of programs and it's an important concept to follow if you're going to write any sort of non-trivial program. Otherwise you'll drown yourself in details and complexity.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line:

is super bad.

For one, it's all hard-coded. There are product names and prices in there. What happens if you decide to change the price of a camera? You'd have to change your code in multiple places to make what you display agree with the calculations being made when the user chooses to buy a camera. Having to reflect a change in a product's price in multiple places in your code is a bad thing and a potential source of bugs. Ideally, you would use a Product object to manage that information and just use it where you need to. That way, you'd only need to tell a product object to update its price and no other piece of code needs to be edited but the program would still adjust to the new price and work correctly.

Also, that line of code really should be broken up and put into a method like showMenu():

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here's what the data structures you'd use to manage product and order information might look like and how you might write a chooseItem() method:

That's it.

Here's how you might initialize the list of products:

Are you starting to see how functional decomposition works and how it helps you deal with complex problems?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One final followup. This line

is again not very good. I would replace it with something like this:

Notice how that little piece of code speaks for itself and can stand on its own, obviating the need for a redundant comment like "// shows the user products chose".

And this is how you might write the showCart() method:

Do you see any similarity of this to some other method I showed above? When you break your program down into small chunks, little patterns of symmetry like this become more obvious and your program starts getting easier to write and maintain.

Finally, to get what you've been asking about all along, this is the skeleton of the code you might write:

Of course, you'd have to implement the logic of the OrderItem.getCost() method but that's a simple multiplication of the Product.price and the OrderItem.quantity.

Again, the point of all this is to manage complexity by breaking things down into such small chunks of work that each chunk become trivial to write.
reply
    Bookmark Topic Watch Topic
  • New Topic