1

straight to the point with this one. I just need some general guidance on whether the program i've written will actually work correctly with Hashmaps and just my approach to trying to complete tasks in general.(ive tried compiling it and it throws an error with line 90 regarding the else statement, think my brackets are messed up)

The purpose of the first function is to have the user input an order of upto 5 characters in one line(havent written anything to check this yet), the first character must be M or L for medium or large pizza. Its then followed by 0 to 4 characters for the toppings.

the second function purpose is the same as the first, only it shoudnt allow 3 or more of the same toppings.

public class Exercise_1{
    public static void pizzaServiceA(String args[]){

        HashMap <Character, String> Toppings = new Hashmap <Character, String>();

        //pizza
        dictionary.put("m", "meduim");
        dictionary.put("l", "large");

        //topping
        dictionary.put("h", "ham");
        dictionary.put("m", "mozzerella");
        dictionary.put("o", "olives");
        dictionary.put("p", "pineapple");
        dictionary.put("s", "spinach");

        dictionary.put("H", "ham");
        dictionary.put("M", "mozzerella");
        dictionary.put("O", "olives");
        dictionary.put("P", "pineapple");
        dictionary.put("S", "spinach");

        HashMap <Character, Double> Prices = new Hashmap <Character, Double>();


        //pizza price 
        dictionary.put("m", 4.00);
        dictionary.put("l", 5.00);

        //topping price medium
        dictionary.put("h", 1.40);
        dictionary.put("m", 1.00);
        dictionary.put("o", 0.80);
        dictionary.put("p", 1.00);
        dictionary.put("s", 1.20);

        //topping price large
        dictionary.put("H", 2.10);
        dictionary.put("M", 1.50);
        dictionary.put("O", 1.20);
        dictionary.put("P", 1.50);
        dictionary.put("S", 1.20);


        System.out.println("Enter a pizza order: ");
        Scanner reader = new Scanner(System.in);
        String orders = reader.nextLine();
        Char[] orderLetters = orders.toCharArray();


        String fullOrder = "";
        Double fullPrice = 0.0;


        //check if sequence enters it more than 5 characters

        if (input.equals("quit")) {
                System.out.println("Quitting.");
                System.exit(0);
            } 

        else if (!(order[0].equals('l')))
        {
            System.out.println("Please enter the size of your pizza, m or l");

        }

        else if (!(order[0].equals('m')))
        {
            System.out.println("Please enter the size of your pizza, m or l");
        }


        for(Char orderLetters : c.toCharArray())
        {
            Double price = Prices.get(orderLetters);
            fullPrice += price;

            String type = Toppings.get(orderLetters);
            if(type == 'm' || type == 'l')
            {
                fullOrder += type + " pizza with ";
            }
            else
            {
                fullOrder += type + ",";
            }


        }
        fullOrder += fullPrice;
        System.out.printf("%.2f", "£", fullOrder);

    }
    public static void pizzaServiceB(){
        Map<Character, Integer> map = new Hashmap<Character, Integer>();
        for(int i = 0; i <s.length(); i++){
            char orderLetters = c.charAt(i); //s.charAt?
            if (map.containsKey(orderLetters)){
                int c = map.get(orderLetters); //counts letters in orderletters
                map.put(orderLetters, ++c);
                {
                    else 
                    {
                        map.put(orderLetters, 1);
                    }
                }
            }
        }

        if (c.equals() = 3){
            System.out.println("You cannot order "); //if topping occurs 3 times print
        }

        //same functionality of A but orders with more than 3 toppings shoudlnt be allowed
    }



    public static void main(){
        Exercise_1 ex1 = null;
        ex1.testpizzaServiceA();
        //ex1.testpizzaServiceB();
    }
}
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
blockoblock
  • 53
  • 10
  • What is `dictionary`? – px06 Apr 05 '17 at 09:43
  • Also, it seems like you're trying to pass a `String` to `put()`, when it is expecting a `Character`. I would double check the creation of your `HashMap` objects. – Logan Apr 05 '17 at 09:44
  • yeh i completely overlooked this. I was using a dictionary instead of hashmaps before and forgot to change how i was adding the strings – blockoblock Apr 05 '17 at 09:56

2 Answers2

0

Keys are unique in HashMap so when you are trying to put two value in same key last added value override contents .

in your code you have use key m two times to put values for Pizza and topping in same HashMap Object dictionary.

//pizza

dictionary.put("m", "meduim");

//topping

dictionary.put("m", "mozzerella");
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

You should not be using a HashMap to map characters to all your information. You could, for instance, create a Pizza class where you can set up the pizza (determine the size and add the toppings). A list of topping could be List<Topping>, where Topping is an enum.

class Pizza {

    public static enum Topping {
        HAM, MOZZARELLA, OLIVES, PINEAPPLE, SPINACH,
        CHEESE; // I added cheese. I love cheese.
    }

    public static enum Size {
        MEDIUM, LARGE;
    }

    private List<Topping> toppings = new ArrayList<>();

    public void addTopping(Topping topping) {
        // First, test if the list with toppings does not contain
        // the given topping more than once.
        this.toppings.add(topping);
    }
}

Storing the prices
And then you need to somehow save the prices somewhere, for instance, in a HashMap. But you can also define a price property in the Topping class if you want to. Assuming each combination between the pizza size and each topping must have its own price, you should maintain a list somewhere – perhaps a HashMap – and make sure all combinations are present in the list. If not, then the consumer might have it for free. :-)

Processing the input
To process the input from the Scanner, you can just make two methods, one for defining the pizza size, and one for defining the toppings. These should not be in your pizza class.

private void consumeSizeToken(char token) {
    // Obtain the pizza object somewhere, otherwise add it to the
    // method's parameter list.
    switch (token) {
        case 'm':
            // I assume the Pizza class to have a setSize() method.
            pizza.setSize(Size.MEDIUM);
            break;
        case 'l':
            pizza.setSize(Size.LARGE);
            break;
        default:
            throw new IllegalArgumentException("Invalid pizza size");
    }
}

private void consumeToppingToken(char token) {
    // Do the same as the consumeSizeToken() method, but, of course,
    // handle the toppings.
}

In order process the input, just assume the first character is the size, and the remaining characters are toppings:

consumeSizeToken(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
    consumeToppingToken(input.charAt(i);
}

You should also take this into consideration:

  • Variable names, other than constants, should always start with a lowercase letter.
  • Since Java 7, you can use the diamond operator. If you define a reference to a generic type, you can omit the generic parameters for constructors of generic classes, because they can be inferred. For example HashMap<Character, String> toppings = new Hashmap<Character, String>() can be replaced by HashMap<Character, String> toppings = new Hashmap<>().
  • Floating point arithmetic should not be used for monetary values. Replace it with int, counting the cents. See this post.
Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130