Goodiny777

Hotel/Room/Guest Exr. 9.2

Apr 2nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.58 KB | None | 0 0
  1. Hotel================================================================================================
  2. package Hotel;
  3.  
  4. import java.util.Random;
  5.  
  6. public class Hotel {
  7.     private int numOfUsedRooms;
  8.     private Room[][] allRooms;
  9.     final int MAX_FLOORS = 10, MAX_ROOMS = 100;
  10.  
  11.     public Hotel(int floors, int rooms) {
  12.         if (floors < MAX_FLOORS && rooms < MAX_ROOMS) {
  13.             this.allRooms = new Room[floors][rooms];
  14.             //generate amount of beds for all rooms in hotel
  15.             for (int i = 0; i < floors; i += 1) {
  16.                 for (int j = 0; j < rooms; j += 1) {
  17.                     //amount of beds could be from 1 to 4
  18.                     this.allRooms[i][j] = new Room(new Random().nextInt(4) + 1);
  19.                 }
  20.             }
  21.         }
  22.     }
  23.  
  24.     public int checkIn(Guest[] guests) {
  25.         for (int i = 1; i < allRooms.length + 1; i += 1) {
  26.             for (int j = 1; j < allRooms[0].length + 1; j += 1) {
  27.                 //check if room with beds and if there are no one there
  28.                 if (this.allRooms[i - 1][j - 1] != null && this.allRooms[i - 1][j - 1].getAllGuests() == null) {
  29.                     if (this.allRooms[i - 1][j - 1].getNumOfBeds() >= guests.length) {
  30.                         //set guests into the room
  31.                         allRooms[i - 1][j - 1].setGuests(guests);
  32.                         //increase number of used rooms
  33.                         numOfUsedRooms += 1;
  34.                         //floor*100+room to get a right room number
  35.                         return i * 100 + j;
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.         //in case there is no valid room
  41.         return -1;
  42.     }
  43.  
  44.     public int isGuestExist(int id) {
  45.         //should start for from 1 for to get right number of room
  46.         for (int i = 1; i < allRooms.length + 1; i += 1) {
  47.             for (int j = 1; j < allRooms[0].length + 1; j += 1) {
  48.                 //check guest by id if he is from room and returns rooms number
  49.                 if (this.allRooms[i-1][j-1].getAllGuests() != null) {
  50.                     if (this.allRooms[i - 1][j - 1].isGuestFromRoom(id)) {
  51.                         return i * 100 + j;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         return -1;
  57.     }
  58.  
  59.     public int floorWithMaxEmptyRoom() {
  60.         int emptyOnFloor = 0, floor, maxRooms;
  61.         //set first floor as max
  62.         for (int i = 0; i < this.allRooms[0].length; i += 1) {
  63.             emptyOnFloor += this.allRooms[0][i].isRoomEmpty() ? 1 : 0;
  64.         }
  65.         maxRooms = emptyOnFloor;
  66.         floor = 1;
  67.         //should start for from 1 for to get right number of room
  68.         for (int i = 1; i < this.allRooms.length; i += 1) {
  69.             for (int j = 0; j < this.allRooms[0].length; j += 1) {
  70.                 //get all empty on one floor
  71.                 emptyOnFloor += this.allRooms[i][j].isRoomEmpty() ? 1 : 0;
  72.             }
  73.             //compare current max amount of rooms with amount on a floor. Set as max and save floor's index
  74.             if (maxRooms < emptyOnFloor) {
  75.                 maxRooms = emptyOnFloor;
  76.                 floor = i + 1;
  77.             }
  78.         }
  79.         return floor;
  80.     }
  81.  
  82.     @Override
  83.     public String toString() {
  84.         String str = "Hotel" + "\n" +
  85.                 "Amount of full rooms: " + numOfUsedRooms + "\n";
  86.         //should start for from 1 for to get right number of room
  87.         for (int i = 1; i < allRooms.length + 1; i += 1) {
  88.             for (int j = 1; j < allRooms[0].length + 1; j += 1) {
  89.                 if (!this.allRooms[i - 1][j - 1].isRoomEmpty()) {
  90.                     str += "room: " + (i * 100 + j) + "\n" + this.allRooms[i - 1][j - 1].toString();
  91.                 } else {
  92.                     str += "room: " + (i * 100 + j) + "-Is empty\n";
  93.                 }
  94.             }
  95.         }
  96.         return str;
  97.     }
  98. }
  99.  
  100. Room=======================================================================================================================
  101. package Hotel;
  102.  
  103. public class Room {
  104.     private int numOfBeds;
  105.     private Guest[] allGuests;
  106.  
  107.     public Room(int numOfBeds) {
  108.         this.numOfBeds = numOfBeds;
  109.     }
  110.  
  111.     public void setGuests(Guest[] guests) {
  112.         this.allGuests = guests;
  113.     }
  114.  
  115.     public Guest[] getAllGuests() {
  116.         return allGuests;
  117.     }
  118.  
  119.     public int getNumOfBeds() {
  120.         return numOfBeds;
  121.     }
  122.  
  123.     //check if no one live in the room
  124.     public boolean isRoomEmpty() {
  125.         if (this.allGuests == null) {
  126.             return true;
  127.         }
  128.         return false;
  129.     }
  130.  
  131.     //check if guest with id living in the room
  132.     public boolean isGuestFromRoom(int id) {
  133.         for (int i = 0; i < allGuests.length; i += 1) {
  134.             if (allGuests[i].getPassportNumber() == id) {
  135.                 return true;
  136.             }
  137.         }
  138.         return false;
  139.     }
  140.  
  141.     @Override
  142.     public String toString() {
  143.         String str = "beds: " + numOfBeds + "\n";
  144.         for (Guest guests : allGuests) {
  145.             str += guests.toString();
  146.         }
  147.         return str;
  148.     }
  149. }
  150.  
  151. Guest====================================================================================================================
  152. package Hotel;
  153.  
  154. public class Guest {
  155.     private String name;
  156.     private int passportNumber;
  157.  
  158.     public Guest(String name, int passportNumber) {
  159.         this.name = name;
  160.         this.passportNumber = passportNumber;
  161.     }
  162.  
  163.     public int getPassportNumber() {
  164.         return passportNumber;
  165.     }
  166.  
  167.     @Override
  168.     public String toString() {
  169.         return "name: " + name + '\n' +
  170.                 "passport: " + passportNumber + '\n';
  171.     }
  172. }
  173.  
  174. Main============================================================================================================
  175. import Hotel.*;
  176.  
  177. import java.io.File;
  178. import java.io.FileNotFoundException;
  179. import java.io.PrintWriter;
  180. import java.util.Scanner;
  181.  
  182. public class Main {
  183.     public static void main(String[] args) throws FileNotFoundException {
  184.         Scanner s = new Scanner(System.in);
  185.         int menuButtons = 0;
  186.  
  187.         //default filename
  188.         String fileName = "default.txt";
  189.         System.out.println("Do you want to read information from some file? 1-yes 0-no");
  190.         if (s.nextInt() == 1) {
  191.             System.out.println("Write a name of a file");
  192.             fileName = s.next() + ".txt";
  193.             //Read info from file
  194.             Hotel read = readFromFile(fileName);
  195.         }
  196.  
  197.         //initialize new hotel
  198.         Hotel hilton = new Hotel(3, 5);
  199.         //looped menu
  200.         System.out.println("You're in the menu.Enter anything to resume. Enter \"0\" for finish");
  201.         System.out.println("1 for Check in\n2 for find guest in hotel\n3 for print info about rooms \n4 for print a floor with most amount of empty rooms");
  202.         menuButtons = s.nextInt();
  203.         while (menuButtons != 0) {
  204.             switch (menuButtons) {
  205.                 case 1:
  206.                     //check in for guests
  207.                     findRoomForGuests(hilton);
  208.                     break;
  209.                 case 2:
  210.                     //find guest in hotel
  211.                     findGuest(hilton);
  212.                     break;
  213.                 case 3:
  214.                     //print all info of hotel
  215.                     System.out.println(hilton.toString());
  216.                     break;
  217.                 case 4:
  218.                     //print floor with max empty rooms
  219.                     System.out.printf("Floor with max empty rooms is %d\n", hilton.floorWithMaxEmptyRoom());
  220.                     break;
  221.             }
  222.             System.out.println("Enter \"0\" for finish or 1 to 4 for resume");
  223.             menuButtons = s.nextInt();
  224.         }
  225.         //function printing college to file
  226.         printToFile(fileName, hilton);
  227.     }
  228.  
  229.     public static void findGuest(Hotel hotel) {
  230.         System.out.println("Enter guest's id: ");
  231.         int guestId = new Scanner(System.in).nextInt();
  232.         if (hotel.isGuestExist(guestId) == -1) {
  233.             System.out.println("This men is not from hotel");
  234.         } else {
  235.             System.out.printf("Room number: %d\n", hotel.isGuestExist(guestId));
  236.         }
  237.     }
  238.  
  239.     public static void findRoomForGuests(Hotel hotel) {
  240.         Scanner s = new Scanner(System.in);
  241.         int roomForGuests;
  242.         Guest[] guests;
  243.  
  244.         System.out.println("Enter amount of guests(from 1 to 4): ");
  245.         //create an array of guests
  246.         guests = new Guest[s.nextInt()];
  247.         //link our array to Room's inner array and returns room's number or -1 if there are no empty room
  248.         roomForGuests = hotel.checkIn(guests);
  249.         //send massage for cases
  250.         if (roomForGuests == -1) {
  251.             System.out.println("Sorry we don't have an empty room");
  252.         } else {
  253.             for (int i = 0; i < guests.length; i += 1) {
  254.                 System.out.println("Enter guests name and his id separated by space");
  255.                 guests[i] = new Guest(s.next(), s.nextInt());
  256.             }
  257.             System.out.printf("You'r room: %d\n", roomForGuests);
  258.  
  259.         }
  260.     }
  261.  
  262.     public static void printToFile(String fileName, Hotel hotel) throws FileNotFoundException {
  263.         PrintWriter pw = new PrintWriter(new File(fileName));
  264.         //write all data from class by toString() into the file
  265.         pw.println(hotel.toString());
  266.         pw.close();
  267.     }
  268.  
  269.     public static Hotel readFromFile(String fileName) throws FileNotFoundException {
  270.         //to do this need to add some functions in the Hotel.java so if you want I can do this
  271.       return new Hotel(1,1);
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment