2012-07-27 51 views
-1

在預約菜單中,我主要有3個選項:a,b和c。 選項'a' - 選擇房間類型。 5種不同的房間類型可供選擇。 選項'b'選擇每個房間的附加組件。可以選擇每個附加組件來增加成本。 選項'c' - 從預訂頁面返回到主頁ArrayList循環

我的程序需要顯示5種不同類型的房間。此時客戶只能選擇'a','b'或'c'選項。

如果客戶在選擇房間時輸入了不是1到5的非法號碼,系統應該給出錯誤信息並提示用戶再次輸入。一旦用戶輸入了他/她想要的房間類型,他們就可以繼續爲每種類型的房間輸入數量。 選擇房間類型後,系統會提示您選擇每種房型的加載項。

這是我的代碼below.I無法讓我的數量的附加組件存儲在整數陣列列表中從選項'a'時,用戶選擇選項'B'。

我初始化了2個arraylist,但我不知道他們是否正確。第一個數組列表是存儲房間類型和日期的字符串。第二個數組列表是整數,用於存儲房間價格,房間需求數量,附加組件數量,夜晚數量,用戶選擇的附加選項以及附加組件的數量。

我很新的編碼,所以需要幫助。

import java.util.*; 
import java.io.*; 

public class RoomSelection { 

    public RoomSelection() throws InputMismatchException { 

     String choiceStr;//initialize choiceStr which is use for reading lines from scanner input 
     char choiceChar;//initialize choiceStr which is use for reading character from scanner input 
     int choice;//initialize choiceStr which is use for reading integer from scanner input 
     String datee; 
     String[] roomType = {"Single Room", "Double Room", "Deluxe Room", "Junior Room", "Suite"}; //Initialize a array for room type 
     Integer[] priceRoom = {160, 200, 280, 380, 500}; //Initialize a array for room prices 
     Integer[] priceAdd = {25, 60, 70, 100}; //Initialize a array for add-on prices 

     ArrayList<String> roomAndDate = new ArrayList<String>(); 
     ArrayList<Integer> all = new ArrayList<Integer>(); 
     Scanner input = new Scanner(System.in); //Initialize a scanner input 

     System.out.println("Room Selection"); 
     System.out.println("==============\n"); 
     System.out.println("[a] Room Type"); 
     System.out.println("[b] Add-Ons"); 
     System.out.println("[c] Main Menu"); 
     System.out.println("Type 'a' to select Room Type and state the desire quantity for each type."); 
     System.out.println("Type 'b' to select the Add-Ons."); 
     System.out.println("Type 'c' to exit from the Booking Menu."); 
     System.out.println("Please enter your option (a, b or c): "); 

     choiceStr = input.nextLine(); 
     choiceChar = choiceStr.charAt(0); 
     while (true) { 

      switch (choiceChar) { 
       case 'a': 
        System.out.println("Room Type"); 
        System.out.println("====================================================="); 
        System.out.println("(1) Single Room (1 person) - Price: S$160"); 
        System.out.println("(2) Double Room (2 persons) - Price: S$200"); 
        System.out.println("(3) Deluxe Room (2 persons) - Price: S$280"); 
        System.out.println("(4) Junior Suite (2 persons) - Price: S$380"); 
        System.out.println("(5) Suite (2 persons) - Price: S$500\n"); 
        System.out.println("Enter Room types (Enter '1' to '5')"); 
        choice = input.nextInt(); 
        while (choice > 5) { 
         if (choice > 5) { 
          System.out.println("Please enter number between '1' to '5'!"); 
          choice = input.nextInt(); 
         } 
        } 

        String roomTypess = roomType[choice - 1]; 
        roomAndDate.add(roomTypess); 
        int storePricee = priceRoom[choice - 1]; 
        all.add(storePricee); 

        System.out.println("Number of rooms required (maximum 10): "); 
        choice = input.nextInt(); 
        while (choice > 10) { 
         if (choice > 10) { 
          System.out.println("Please enter again!"); 
          choice = input.nextInt(); 
         } 
        } 
        all.add(choice); 

        for (int i = 0; i < choice; i++) { 
         System.out.println("Enter the date of checked-in (dd/mm/yy) for " + roomAndDate.get(0) + " " + (i + 1)); 
         choiceStr = input.nextLine(); 
         choiceStr = input.nextLine(); 
         roomAndDate.add(choiceStr); 
         System.out.println(roomAndDate); 
         System.out.println("Enter number of Add-on for " + roomAndDate.get(0) + " " + (i + 1) + ": "); 
         choice = input.nextInt(); 
         while (choice > 4) { 
          if (choice > 4) { 
           System.out.println("Please enter again! Choose only option 1 to 4"); 
           choice = input.nextInt(); 
          } 
         } 
         all.add(choice); 
         System.out.println("Number of night(s) required (maximum 30) for " + roomAndDate.get(0) + " " + (i + 1) + ": "); 
         choice = input.nextInt(); 
         while (choice > 30) { 
          if (choice > 30) { 
           System.out.println("Please enter again! Maximum is 30 days!"); 
           choice = input.nextInt(); 
          } 
         } 
         all.add(choice); 


        } 

        new RoomSelection(); 
        break; 
       case 'b': 
        System.out.println("Add-Ons"); 
        System.out.println("====================================================="); 
        System.out.println("(1) Breakfast voucher (1 person) per day - Price: S$25"); 
        System.out.println("(2) Spa voucher (1 person) - Price: S$60"); 
        System.out.println("(3) Half Day Tour voucher (1 person) - Price: S$70"); 
        System.out.println("(4) Full Day Tour voucher (1 person) - Price: $100\n"); 



        for (int i = 0; i < (Integer) all.get(3); i++) { 

         System.out.println("Enter Add-On option"); 
         choice = input.nextInt(); 
         while (choice > 4) { 
          if (choice > 4) { 
           System.out.println("Please enter again! Choose only option 1 to 4"); 
           choice = input.nextInt(); 
          } 
         } 
         all.add(choice); 
         System.out.println("Enter quantity required for Add-On option " + (i + 1) + ": "); 
         choice = input.nextInt(); 
         all.add(choice); 
        } 


        break; 
       case 'c': 
        new MainPage1(); 
        break; 
       default: 
        continue; 
      } 
     } 
    } 
} 
+3

這看起來像一個家庭作業。你能解釋一下嗎:'new RoomSelection()';在'case'b'之前。 – kinaesthesia 2012-07-27 07:37:49

+0

是的,這是一項功課,新房間選擇是在完成輸入值後重新進入房間選擇菜單。但我juz發現,如果我把新的infront,用戶存儲到我的數組中的值早些時候,所以我堅持在那裏 – 2012-07-28 09:55:26

回答

1

我看了一下你的代碼,有些東西你不需要,你需要改進。

  1. 而不是閱讀的整數input.nextInt();你應該閱讀輸入字符串並嘗試執行Integer.parseInt(yourString)和使用得到它(如果它拋出一個NumberFormatException你就會知道該字符串不是有效的整數)。
  2. while循環,你應該排除的情況下對價值0
  3. 消除來自while語句中的if語句作爲while條件它納入if
  4. 創建有你需要的不是有兩個數組列表功能對象的數組列表:
    • 創建並具有作爲字段的房間,成本類型的對象,...
    • 定義和在RoomSelection()函數之外分配此對象的數組列表,以便您可以始終訪問它。
    • 如果您在RoomSelection()中對列表進行定義,則在調用new RoomSelection();時,將創建列表的新實例,而不是使用現有列表的實例。

希望我的一些幫助。最後一點非常重要。