Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In Booking menu, i have main with 3 options:a,b and c. Option 'a' - selection of room types. 5 different room types to choose from. Option 'b'- selection of add-ons for each room.Each add-on can be selected for additional cost. Option 'c' - return from make booking page to main page

My program needs to display 5 different types of room. At this point customer is only allowed to select option 'a', 'b' or 'c'.

If customer enters and invalid number which is not 1 to 5 inclusive when selecting room ,system should give error message and prompt user to enter again.Once the user entered the type of room he/she wants, they can proceed to enter quantity for each type of room. After the selection of room type, the system will prompt you to select add-ons for each room type.

This is my code below.I cant to get my number of add-ons i stored in the integer arraylist from option 'a' when the user selection option 'b'.

I initialized 2 arraylist but im not sure whether they are correct. The first arraylist is to store strings which is roomtype and date. The second arraylist is integer, which stores room price,number of room require, number of add-ons,number of nights,add-on option the user selects and quantity of the add-on.

I'm quite new to coding so need help on it.

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("==============
");
        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
");
                    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
");



                    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;
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
119 views
Welcome To Ask or Share your Answers For Others

1 Answer

I had a look at your code and there are some things you don't need and that you need to improve.

  1. Instead of reading an integer input.nextInt(); you should read the input string and try to perform Integer.parseInt(yourString) and use the resulting it (if it throws a NumberFormatException you'll know the string isn't a valid integer).
  2. for the while loops you should exclude the case for value 0
  3. eliminate the if statements from inside the while statements as the while condition incorporate the if in it.
  4. Create an array list of objects that have the features that you require instead of having two array lists:
    • create and object that has as fields the type of room, cost, ....
    • define and allocate this array list of objects outside the RoomSelection() function such that you will have access to it all the time.
    • if you define it in the RoomSelection() as you do with the lists, when you call new RoomSelection(); you create new instances of the lists instead of using the existing ones.

Hope I was of some help. The last point is very important.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...