首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比萨订单系统哈希映射和通用语法

比萨订单系统哈希映射和通用语法
EN

Stack Overflow用户
提问于 2017-04-05 09:39:35
回答 2查看 602关注 0票数 1

直截了当地说了这个。我只需要一些一般性的指导,说明我编写的程序是否能够正确地使用Hashmap,以及我尝试完成一般任务的方法。(我试过编译它,它抛出了一个错误,第90行有关are语句,认为我的括号被弄乱了)。

第一个函数的目的是让用户在一行中输入最多5个字符的顺序(还没有写任何东西来检查这一点),第一个字符必须是M或L,对于中型或大型比萨饼。然后以0到4个字符作为顶部的后缀。

第二个功能的目的与第一个相同,只是它不应该允许三个或更多相同的配料。

代码语言:javascript
复制
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();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-05 10:14:52

您不应该使用HashMap将字符映射到所有信息。例如,您可以创建一个Pizza类,您可以在其中设置披萨(确定大小并添加配料)。顶部的列表可以是List<Topping>,其中Topping是枚举。

代码语言:javascript
复制
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);
    }
}

存储价格

然后你需要在某个地方以某种方式节省价格,例如,在HashMap中。但是,如果您愿意,也可以在Topping类中定义价格属性。假设比萨饼大小和每个顶部的每一个组合都必须有自己的价格,那么您应该在某个地方保留一个列表--也许是一个HashMap --并确保列表中有所有的组合。如果没有,那么消费者可能会免费拥有它。:-)

处理输入

要处理来自Scanner的输入,只需创建两个方法,一个用于定义披萨大小,另一个用于定义配料。这些不应该出现在你的披萨课上。

代码语言:javascript
复制
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.
}

为了处理输入,只需假设第一个字符是大小,其余字符是顶部:

代码语言:javascript
复制
consumeSizeToken(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
    consumeToppingToken(input.charAt(i);
}

你还应该考虑到这一点:

  • 变量名(常量除外)应该始终以小写字母开头。
  • 从Java 7开始,您可以使用菱形操作符。如果定义泛型类型的引用,则可以省略泛型类构造函数的泛型参数,因为可以推断它们。例如,HashMap<Character, String> toppings = new Hashmap<Character, String>()可以被HashMap<Character, String> toppings = new Hashmap<>()取代。
  • 浮点算法不应用于货币价值。用int代替它,数美分。见this post
票数 0
EN

Stack Overflow用户

发布于 2017-04-05 09:45:23

键在HashMap中是唯一的,所以当您试图在相同的键中放置两个值时,最后一个附加值会覆盖内容。

在您的代码中,您必须两次使用键m将必胜客的值放在同一个HashMap对象字典中。

//比萨

代码语言:javascript
复制
dictionary.put("m", "meduim");

//顶盖

代码语言:javascript
复制
dictionary.put("m", "mozzerella");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43227422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档