2017-04-05 23 views
0

直接與這一點。我只需要一些關於我編寫的程序是否實際上能夠正確使用Hashmaps以及試圖完成一般任務的方法的一般指導(第一次嘗試編譯它,它會在第90行中拋出有關else語句的錯誤,認爲我的括號是搞砸了)比薩訂單系統哈希曲線和一般語法

第一個功能的目的是讓用戶在一行中輸入最多5個字符的順序(沒有寫任何東西來檢查這一點),第一個字符必須是M或L適合中型或大型披薩。然後接着0到4個字符的澆頭。

第二個功能用途與第一個功能相同,只有它可以允許3個或更多相同的澆頭。

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

你我完全忽略了這一點。我之前使用字典而不是hashmaps,並忘記改變我如何添加字符串 – blockoblock

回答

0

你不應該使用HashMap映射字符您的所有信息。例如,您可以創建一個Pizza類,您可以在其中設置披薩(確定尺寸並添加配料)。頂部列表可以是List<Topping>,其中Topping是枚舉。

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輸入,你可以做出兩種方法,一種用於定義比薩的尺寸,以及一個用於定義澆頭。這些不應該在你的披薩類。

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將Pizza和Topup的值設置爲相同的HashMap對象字典。

//比薩餅

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

//一流

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

我應該添加另一個hashmap只是爲了比薩尺寸在這種情況下? – blockoblock

+0

是的,你可以但如果你想只保留一個'HashMap',那麼你需要爲每個條目使用不同的鍵。 –