2012-07-27 49 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();时,将创建列表的新实例,而不是使用现有列表的实例。

希望我的一些帮助。最后一点非常重要。