1

I've scoured google and SO with no luck so far. It may be due to my lack of knowledge also making it hard to know how to search properly for my answer.

Conceptually, I want to have a method that expects one string, then uses that string within its block of code to guide a basic System.out.println

import java.util.HashMap;

public class Restaurant {
    public static void main(String[] args) {
        HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();

        restaurantMenu.put("Turkey Burger",13);
        restaurantMenu.put("Naan Pizza",11);
        restaurantMenu.put("Cranberry Kale Salad",10);

        seePrice("Naan Pizza");    

        /* I want to create a method called seePrice, which expects a string input 
        (which will ultimately be a name of one of the food items on the menu). 
        Then it goes on to use that string input in:
        System.out.println(restaurantMenu.get(stringinputgoeshere));

        So that I can simply type seePrice("Naan Pizza");
        and it will display the price in the console
        */

    }
}

After many different attempts, I can't get something so basic to work for me. Please forgive my being such a novice. All help is appreciated.

Edited to the following (note some superfluous items like the sqft lines were added to simply further good practice on setters and getters)

    import java.util.HashMap;
public class Restaurant {

  //sqft section
  private double sqft = 0.0;
  public Restaurant(){
    sqft = 500.0;
                     }
  public Restaurant(double squareFeet){
        sqft = squareFeet;
                                      }
  public double getSqft(){
    return sqft;
                         }

  //Menu section
    private  HashMap<String, Double> restaurantMenu = new HashMap<String, Double>();
  public int setMenu(HashMap<String, Double> currentMenu){
    this.restaurantMenu = currentMenu;
    return 1;

                                                         }
  public HashMap<String, Double> getMenu(){
    return restaurantMenu;

                                                         }

  public static void seePrice (HashMap<String, Double> menu, String food){
    System.out.println(menu.get(food));
                                                                         }


  /////////Run Main Section Here  
    public static void main(String[] args) {

    Restaurant platefulOfCox = new Restaurant(2120.8);
    System.out.println("The square footage is "+platefulOfCox.getSqft());
    HashMap<String, Double> tempMenu = new HashMap <String, Double>();
      tempMenu.put("Pizza",10.99);
      tempMenu.put("Burger",5.95);
      tempMenu.put("Coke",1.99);
      platefulOfCox.setMenu(tempMenu);



      //Price Checker
      String foodItem = "Coke";
      seePrice(platefulOfCox.getMenu(),foodItem);

    }
}
1
  • Please describe you problem properly, if you just want a method that accept one string then (public void myMethod(string st){ System.out.println(st) }) will be your answer Commented May 8, 2017 at 1:27

3 Answers 3

1

The map restaurantMenu won't be visible inside any method seePrice(String) because it's a local variable to the main() method. You have two options:

  1. Pass the map as a second argument to the seePrice method so it can look up the price.
  2. Make restaurantMenu a field of the Restaurant class.

The second option has two sub-options: make restaurantMenu a static field (and seePrice() a static method) or make it an instance field and create an instance of Restaurant in the main() method. Making everything static is a bit easier, but much less flexible in the long run if you are going to evolve the program beyond a toy example.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the detailed response. I've edited the original question to include what I have now. It functions as I'd like it to, but curious if there are some things that could cause issues if this program became a larger undertaking and moved beyond the current toy example. (note that I fully plan to break the Restaurant class into its own file rather than have it all together like it is now).
@KenParsons - That's a big improvement already. You might want to separate the menu into a separate class as Joel suggests in his answer, moving the seePrice() method into that class. That way you could, for instance, share a single menu among multiple restaurants, or have several menus for a single restaurant (e.g., breakfast, seasonal, weekend, etc.) Also, I'd suggest replacing seePrice() with a getPrice() menu that returns a value; it's generally better to separate business logic ("what's the price?") from I/O.
1

Your problem is the scope of restaurantMenu. You can't access it outside of the main method. You need to declare it as a member of the class, or pass it in as a parameter to your seePrice method.

import java.util.HashMap;
public class Restaurant {
    private static HashMap<String, Integer> restaurantMenu;


    public static void main(String[] args) {
        restaurantMenu = new HashMap<String, Integer>();
        ....

1 Comment

Thank you for your answer. It led me down the path of learning how to break this apart to the way it rightfully should be. Certainly a lot more utility this way.
0

You're most of the way there. Just move your HashMap into its own class, maybe public class Menu. Something like:

import java.util.Map;
import java.util.HashMap;

public class Menu {
    private final Map<String, Integer> restaurantMenu = new HashMap<String, Integer>();

    public Menu() {
        restaurantMenu.put("Turkey Burger",13);
        restaurantMenu.put("Naan Pizza",11);
        restaurantMenu.put("Cranberry Kale Salad",10);
    }

    public void seePrice(final String item) {
        System.out.println(restaurantMenu.get(item));
    }
}

This is an incredibly simple implementation. Consider adding menu items through a method instead of in the constructor and you'll likely want to check to see if the item passed to seePrice is on the menu before printing to sys out.

Edit: From here, in your main simply create a final Menu menu = new Menu() and call menu.seePrice(item).

4 Comments

Thank you for all the insight. The great answers here, yours included, made it clear that the first step to tackle is getting the hashmap outside of the main. The problem now is I'm extremely quickly moving outside of my realm of knowledge. I've tried your implementation by including those lines of code in every possible place within the entire code, but I can't seem to make any of this work. For sake of helping me understand context, would you be able to include the entire copy of the code?
For reference, I'm getting a common error of 'error: cannot find symbol private final Map <String, Integer> restaurantMenu = new HashMap<String, Integer>();'
I've updated my code sample to include imports and fix a syntax error. I suspect my solution will take you outside of your comfort zone, but I'll see if I can help anyway. I'd suggest putting the Menu code into a separate Menu.java in the same directory as your Main.java (or Restaurant.java, whichever you've called it.) You should simply be able to create a new Menu object and call its seePrice method as described above.
Thank you again! I will, for sake of starting good practice early, move on to separating the Restaurant class from the main method (the Restaurant Driver, if you will). I'm seeing your updates now after I had continued to crack into the way it was laid out originally, and have now edited my original answer to include the updated code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.