Ok so i need to make a program that allows a user to input what items they want from a menu that is displayed on screen. After their input, the user should get back a total price for their order. Now, I want a second class that shows the owner how much he made in one day, and also the number of items he sold. this is what I got so far. I am so lost first time ever programmed
I am afraid I can see some errors; for example item.indexof() won't work because item is an array and arrays don't have an indexof method. And $4.55 for onion rings? Nobody will ever eat there at those prices
Joking apart, you are going to have to start small. Very small. don't write more than about 5 lines without compiling the code and running it. Start with a Meal class, which can have two fields, the description and the price. Give it the standard methods of the Object class (well, at least toString() just for now), and set up a main method which creates a couple of Meal objects and prints them out. You will also have to give your class a constructor. There is a bit in the Java™ Tutorials about that. Remember to give the two fields private access.Now you can set up an array of Meals, and give each a number (remember arrays start with no 0). You can enter a number via your Scanner, and add the price to the total price.
Your for loop won't work, for two reasons:
You have declared i twice.
You are testing whether i < 0, and that loop will never actually start.
Anyway, try some of those stages, and see how far you get.
Write down on a piece of paper what you think your methods ought to do. Write a few lines and get them working before you think of writing any more. Good luck, and tell us how you get on.
Joined: Apr 22, 2011
ok so I did what you said, but i am still a little lost. The word Meal after the equals sign does not work, why?? Also, once I fix that how can I calculate the customers total? should i use a for loop or indexof method?? or what...... can you show me an example. Thanks!
Please UseCodeTags while posting code . (your original post has been edited by Campbell, I guess it's for code tags).
You are trying too many things here in one class. First try to implement a small part as already pointed out. Don't try everything at once it'll cause more problem than it solves.
Some points for you:
- Have a separate class which wraps the menu to be displayed to the user.
- You should not loop through the "sum", instead what the user entered. For example he may allow
to enter his preferred item as a comma separated string so you can extract them in your program.
sandy skiathitis wrote:o . . . The word Meal after the equals sign does not work, why??
It's not an equals sign, but an assignment operator. You are supposed to write new Meal()
Also, once I fix that how can I calculate the customers total? should i use a for loop or indexof method??
I would prefer a loop; you can put in some nonsense value like -1 to terminate the loop
or what...... can you show me an example.
I have already shown you how to create new Meal objects. I don't think I ought to show you any more at this stage.
I think you are going to have to put your Meal objects into an arrayNow you can loop through the array and print them out with numbers like a Chinese restaurant and you can order a No 3. Remember the first element of an array is No 0.
Thanks! . . .
Beware of names of packages like "final2"; since final is a keyword, you can get some strange errors like that.