2017-04-05 7 views
0

これと一直線になっています。私が書いたプログラムが実際にハッシュマップで正しく動作するかどうか、一般的なタスクを完了しようとする私のアプローチについての一般的なガイダンスが必要です。(コンパイルしようとしましたが、else文に関して90行目でエラーがスローされ、ピザオーダーシステムハッシュマップと一般的な構文

最初の機能の目的は、ユーザーが1行に最大5文字のオーダーを入力することです(これをチェックするためには何も書きません)。最初の文字はMまたはLでなければなりません中型または大型ピザ用。それに続いて、トッピングのための0〜4文字が続きます。

第2の機能の目的は、第1の機能の目的と同じですが、それだけで3つ以上の同じトッピングが可能です。あなたは同じキー最後に追加された値の上書き内容で2つの値を入れしようとしているとき

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(); 
    } 
} 
+0

「辞書」とは何ですか? – px06

+0

また、文字列を期待しているときに、 'String'を' put() 'に渡そうとしているようです。あなたの 'HashMap'オブジェクトの作成を二重チェックします。 – Logan

+0

私はこれを完全に見落としました。以前はハッシュマップの代わりに辞書を使用していましたが、文字列を追加する方法を変更するのを忘れました – blockoblock

答えて

0

をトッピング

//ピザ

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

//。例えば、Pizzaクラスを作成して、ピザをセットアップすることができます(サイズの決定とトッピングの追加)。トッピングのリストはList<Topping>となります。Toppingは列挙型です。価格
そして、あなたは何とかHashMapで、例えば、どこかの価格を保存する必要があるの保存

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); 
    } 
} 

。しかし、希望する場合は、Toppingクラスでプライスプロパティを定義することもできます。ピザサイズと各トッピングの組み合わせにはそれぞれ独自の価格が必要であると仮定すると、おそらくHashMapのようにリストを維持し、すべての組み合わせがリストにあることを確認する必要があります。もしそうでなければ、消費者はそれを無料で持っているかもしれない。 :-) Scannerからの入力を処理するには、入力
を処理

、あなただけの二つの方法、ピザのサイズを定義するための1、およびトッピングを定義するための1つを作ることができます。これらはあなたのピザクラスにはいけません。入力は、ちょうど最初の文字を想定し、注文プロセスで

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

は大きさであり、残りの文字がトッピングされている:

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

ます。また、これを考慮する必要があります。

  • 以外の変数名al方法は小文字で始まります。
  • Java 7以降、ダイヤモンド演算子を使用できます。ジェネリック型への参照を定義する場合、ジェネリッククラスのコンストラクタのジェネリックパラメータは、推測できるので省略することができます。たとえば、HashMap<Character, String> toppings = new Hashmap<Character, String>()は、HashMap<Character, String> toppings = new Hashmap<>()で置き換えることができます。
  • 浮動小数点演算は金銭的な値に使用しないでください。それをintに置き換え、セントを数えます。 this postを参照してください。
+0

詳細な応答をお寄せいただきありがとうございます。このメソッドを使用して書き直してみてください。 – blockoblock

0

キーがそうHashMapでユニークです。

あなたのコードでは、キーmを2回ピザとトッピングの値を入力するのに同じHashMapオブジェクト辞書を使用しています。あなたはすべてのあなたの情報に文字をマップするためにHashMapを使用すべきではない

dictionary.put("m", "mozzerella"); 
+0

この場合、ピザサイズのハッシュマップを追加するだけですか? – blockoblock

+0

はい、できますが、1つの 'HashMap'だけ永続化したい場合は、各エントリに対して異なるキーを使用する必要があります。 –

関連する問題