• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help with program

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:(




........................this is my second class

 
Marshal
Posts: 79710
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
     
    sandy skiathitis
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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!



    package final2;

    /**
    *
    * @author kyriakhskiathitis
    */

    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {
    String Mealm1 = new Meal("Peperoni Pizza", 9.99);
    String Mealm2 = new Meal("Onion Rings", 4.55);
    String Mealm3 = new Meal("Plain Pizza", 7.99);
    String Mealm4 = new Meal("Plain Steak", 5.20);
    String Mealm5 = new Meal("Cheese Steak", 6.00);
    String Mealm6 = new Meal("French Fries", 2.25);
    String Mealm7 = new Meal("Mozzarella Sticks", 4.79);
    String Mealm8 = new Meal("20 ounces Bottled Soda-Any", 1.85);
    String Mealm9 = new Meal("Cheese Cake plain", 2.50);
    String Mealm10 = new Meal("Cherry Cheese Cake", 2.50);

    System.out.println(Mealm1);
    System.out.println(Mealm2);
    System.out.println(Mealm3);
    System.out.println(Mealm4);
    System.out.println(Mealm5);
    System.out.println(Mealm6);
    System.out.println(Mealm7);
    System.out.println(Mealm8);
    System.out.println(Mealm9);
    System.out.println(Mealm10);

    Scanner keyboard=new Scanner(System.in);

    System.out.println("Please enter your items");
    String answer=keyboard.nextLine();

    int sum=0;
    int i=0;
    for( i=0; i<sum; i++)
    {
    if(sum!=0)
    {
    System.out.println("You have entered nothing.");
    }

    else
    {
    //calculate the order total.

    }


    }

    }

    }
     
    Bartender
    Posts: 4116
    72
    Mac TypeScript Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Sandy,
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 79710
    381
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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! . . .

    You're welcome.

    Beware of names of packages like "final2"; since final is a keyword, you can get some strange errors like that.
     
    I was her plaything! And so was this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic