Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HotelConfigure {
  4.  
  5. public static void main(String[] args) {
  6. System.out.print("Input the name of your hotel: ");
  7. Scanner sc = new Scanner(System.in);
  8. String name = sc.nextLine();
  9. int roomAmount = getInt("How many rooms should the hotel have? ", sc);
  10. Room[] rooms = new Room[roomAmount];
  11. for (int i = 0; i < roomAmount; i++) {
  12. String q = "How many beds does room " + (i+1) + " have? ";
  13. int bedAmount = getInt(q, sc);
  14. boolean valid = false;
  15. String size = "";
  16. while (!valid) {
  17. System.out.print("Should the bed(s) in room " + (i+1) + " be (a) single or double bed(s)? ");
  18. size = sc.next().toLowerCase();
  19. if (size.equals("single") || size.equals("double")) {
  20. valid = true;
  21. } else {
  22. sc.nextLine();
  23. System.err.println("Error: You need to input \"Single\" or \"Double\" (case insensitive).");
  24.  
  25. }
  26.  
  27. }
  28. rooms[i] = HotelTest.newRoom(bedAmount, size);
  29. }
  30. Hotel hotel = new Hotel(name, rooms);
  31. HotelReport.printReport(hotel);
  32.  
  33. sc.close();
  34. }
  35.  
  36. private static int getInt(String input, Scanner sc) {
  37. boolean valid = false;
  38. int res = -1;
  39. while (!valid) {
  40. System.out.print(input);
  41. if (sc.hasNextInt()) {
  42. res = sc.nextInt();
  43. }
  44. if (res > 0) {
  45. valid = true;
  46. } else {
  47. sc.nextLine();
  48. System.err.println("That's not a valid number! Please try again.");
  49. }
  50. }
  51. return res;
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement