2012-07-27 4 views
-1

예약 메뉴에서 a, b 및 c의 3 가지 옵션이있는 주 메뉴가 있습니다. 옵션 'a'- 객실 유형 선택. 선택할 수있는 5 가지 객실 유형. 옵션 'b'- 각 룸에 대한 추가 기능 선택. 추가 비용을 위해 각 추가 기능을 선택할 수 있습니다. 옵션 'c'- 예약 페이지에서 기본 페이지로 돌아 가기ArrayList looping

내 프로그램은 5 가지 유형의 회의실을 표시해야합니다. 이 시점에서 고객은 'a', 'b'또는 'c'옵션 만 선택할 수 있습니다.

방을 선택했을 때 고객이 1 ~ 5가 아닌 번호를 입력하면 시스템에서 오류 메시지를 표시하고 사용자가 다시 입력하도록 요청해야합니다. 사용자가 원하는 유형의 방을 입력하면 진행할 수 있습니다. 각 유형의 객실에 대한 수량을 입력하십시오. 방 유형을 선택하면 각 방 유형에 대한 추가 기능을 선택하라는 메시지가 표시됩니다.

이것은 아래 코드입니다. 사용자 선택 옵션 'b'에서 정수 arraylist에 저장 한 추가 기능을 'a'옵션에서 가져올 수 없습니다.

2 명의 arraylist를 초기화했지만 올바른지 확실하지 않습니다. 첫 번째 arraylist는 roomtype과 date 인 문자열을 저장하는 것입니다. 두 번째 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

+0

네, 숙제입니다. 새 룸 선택은 값 입력을 마치면 룸 선택 메뉴로 다시 들어가는 것입니다.하지만 나는 주스가 새로운 infront 넣어 경우 이전에 사용자 배열을 내 배열에 값이 너무 인스턴트 메신저 거기에 붙어 갈 것이라고 알게되었습니다. –

답변

1

나는 당신의 코드를 보았다. 당신이 필요로하지 않고 개선해야 할 것이있다.

  1. 대신 당신이 입력 문자열을 읽고 Integer.parseInt(yourString)을 수행하려고하고 (그것이 NumberFormatException를 throw하면 문자열이 유효한 정수가 아닌 당신이 알고있는 것)을 결과를 사용한다 input.nextInt(); 정수를 읽고. while에 대한
  2. 당신이 while 조건이 그 안에 if을 통합으로 0
  3. while 문 안쪽에서 if 문을 제거 값의 경우를 제외한다 반복합니다.
  4. 가 대신 두 개의 배열 목록 필요없이 필요한 기능이 객체의 배열 목록 만들기 : 이
    • 생성, 룸, 비용의 유형을 필드로이 객체 ....
    • 정의 및 RoomSelection() 함수 외부에있는이 배열 목록을 할당하면 항상 액세스 할 수 있습니다.
    • new RoomSelection();을 호출 할 때 목록과 마찬가지로 RoomSelection()에 정의하면 기존 목록을 사용하는 대신 목록의 새 인스턴스를 만들 수 있습니다.

희망 나는 약간의 도움이 있었다. 마지막 요점은 매우 중요합니다.