Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Hotel {
- private int numOfUsedRooms;
- private Room[][] allRooms;
- private int numOfFloors;
- private int totalRooms;
- private int numOfRooms;
- public Hotel(int floors, int roomsPerFloor) {
- this.allRooms = new Room[floors][roomsPerFloor];
- this.numOfFloors = floors;
- this.numOfRooms = roomsPerFloor;
- this.numOfUsedRooms = 0;
- for (int i = 0; i < floors; i++) {
- for (int j = 0; j < roomsPerFloor; j++) {
- int numOfBeds = (int) ((Math.random() * 3) + 1);
- this.allRooms[i][j] = new Room(numOfBeds);
- this.totalRooms++;
- }
- }
- }
- public Hotel(String fileName) throws FileNotFoundException {
- File f = new File(fileName);
- Scanner sc = new Scanner(f);
- this.numOfUsedRooms = sc.nextInt();
- this.numOfFloors = sc.nextInt();
- this.numOfRooms = sc.nextInt();
- this.allRooms = new Room[this.numOfFloors][this.numOfRooms];
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- int numOfBeds = sc.nextInt();
- this.allRooms[i][j] = new Room(numOfBeds);
- // this.allRooms[i][j].setNumOfGuests(sc.nextInt());
- }
- }
- for (int i = 0; i < this.numOfFloors; i++) {
- int roomNum = sc.nextInt();
- this.allRooms[roomNum / 100][roomNum % 10] = new Room(sc);
- }
- sc.close();
- }
- public Hotel(Scanner sc) throws FileNotFoundException {
- this.numOfUsedRooms = sc.nextInt();
- this.numOfFloors = sc.nextInt();
- this.numOfRooms = sc.nextInt();
- this.allRooms = new Room[this.numOfFloors][this.numOfRooms];
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- int numOfBeds = sc.nextInt();
- this.allRooms[i][j] = new Room(numOfBeds);
- }
- }
- for (int i = 0; i < this.numOfUsedRooms; i++) {
- int roomNum = sc.nextInt();
- allRooms[roomNum / 100][roomNum % 100] = new Room(sc);
- }
- }
- public String guestLookUp(int passportNumber) {
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- Room currentRoom = allRooms[i][j];
- if (currentRoom.getNumOfGuests() != 0) {
- for (int k = 0; k < currentRoom.getNumOfGuests(); k++) {
- if (currentRoom.getGuests()[k].getPassportNumber() == passportNumber) {
- return ("Guest found, resides in room: " + ((i * 100) + j) + ".");
- }
- }
- }
- }
- }
- return "Sorry, guest was not found in hotel registry.";
- }
- public void addGuestGroup(Guest[] group) {
- boolean freeRoomFound = false;
- int floor = 0;
- int freeRoom = 0;
- for (; floor < this.numOfFloors; floor++) {
- freeRoom = freeRoomFinder(floor, group.length);
- if (freeRoom != 0) {
- freeRoomFound = true;
- break;
- }
- }
- if (freeRoomFound == true) {
- for (int i = 0; i < group.length; i++) {
- this.allRooms[floor][freeRoom].addGuest(group[i]);
- }
- this.numOfUsedRooms++;
- if (floor == 0) {
- System.out.println("Guests were assigned to room: 0" + freeRoom);
- } else {
- System.out.println("Guests were assigned to room: " + ((floor * 100) + freeRoom));
- }
- } else {
- System.out.println("No free rooms were found for the guests.");
- }
- }
- public int freeRoomFinder(int floor, int numOfBedsRequired) {
- for (int i = 0; i < this.totalRooms; i++) {
- Room currentRoom = this.allRooms[floor][i];
- if ((currentRoom.getNumOfBeds() - currentRoom.getNumOfGuests()) >= numOfBedsRequired) {
- return i;
- }
- }
- return 0;
- }
- public int numOfFreeRooms(int floor) {
- int counter = 0;
- for (int i = 0; i < this.numOfRooms; i++) {
- if (this.allRooms[floor][i].getNumOfGuests() == 0) {
- counter++;
- }
- }
- return counter;
- }
- public int floorWithMaxFreeRooms() {
- int maxedFloor = 0;
- for (int i = 0; i < this.numOfFloors - 1; i++) {
- if (this.numOfFreeRooms(i) < this.numOfFreeRooms(i + 1)) {
- maxedFloor = i + 1;
- }
- }
- return maxedFloor;
- }
- public void displayStats() {
- for (int i = 0; i < this.numOfFloors; i++) {
- int counter = 0;
- System.out.println("\nFloor " + i + ".");
- for (int j = 0; j < this.numOfRooms; j++) {
- if (allRooms[i][j].getNumOfGuests() != 0) {
- System.out.println("\nRoom #" + j + ":");
- counter++;
- for (int k = 0; k < allRooms[i][j].getNumOfGuests(); k++) {
- allRooms[i][j].displayGuest(allRooms[i][j].getGuests()[k]);
- }
- }
- }
- if (counter == 0) {
- System.out.println("Floor is empty.\n");
- }
- }
- }
- public void saveHotel(PrintWriter pw) throws FileNotFoundException {
- pw.println(this.numOfUsedRooms);
- pw.println(this.numOfFloors);
- pw.println(this.numOfRooms);
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- pw.println(this.allRooms[i][j].getNumOfBeds());
- }
- }
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- if (this.allRooms[i][j].getNumOfGuests() != 0) {
- int num = (i * 100) + j;
- pw.println(num);
- this.allRooms[i][j].saveRoom(pw);
- }
- }
- }
- pw.close();
- }
- public void saveHotel(String fileName) throws FileNotFoundException {
- File f = new File(fileName);
- PrintWriter pw = new PrintWriter(f);
- pw.println(this.numOfUsedRooms);
- pw.println(this.numOfFloors);
- pw.println(this.numOfRooms);
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- pw.println(this.allRooms[i][j].getNumOfBeds());
- }
- }
- for (int i = 0; i < this.numOfFloors; i++) {
- for (int j = 0; j < this.numOfRooms; j++) {
- if (allRooms[i][j].getNumOfGuests() != 0) {
- int num = (i * 100) + j;
- pw.println(num);
- allRooms[i][j].saveRoom(pw);
- }
- }
- }
- pw.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement