Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class HotelConfigure {
- public static void main(String[] args) {
- System.out.print("Input the name of your hotel: ");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- int roomAmount = getInt("How many rooms should the hotel have? ", sc);
- Room[] rooms = new Room[roomAmount];
- for (int i = 0; i < roomAmount; i++) {
- String q = "How many beds does room " + (i+1) + " have? ";
- int bedAmount = getInt(q, sc);
- boolean valid = false;
- String size = "";
- while (!valid) {
- System.out.print("Should the bed(s) in room " + (i+1) + " be (a) single or double bed(s)? ");
- size = sc.next().toLowerCase();
- if (size.equals("single") || size.equals("double")) {
- valid = true;
- } else {
- sc.nextLine();
- System.err.println("Error: You need to input \"Single\" or \"Double\" (case insensitive).");
- }
- }
- rooms[i] = HotelTest.newRoom(bedAmount, size);
- }
- Hotel hotel = new Hotel(name, rooms);
- HotelReport.printReport(hotel);
- sc.close();
- }
- private static int getInt(String input, Scanner sc) {
- boolean valid = false;
- int res = -1;
- while (!valid) {
- System.out.print(input);
- if (sc.hasNextInt()) {
- res = sc.nextInt();
- }
- if (res > 0) {
- valid = true;
- } else {
- sc.nextLine();
- System.err.println("That's not a valid number! Please try again.");
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement