Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hotel================================================================================================
- package Hotel;
- import java.util.Random;
- public class Hotel {
- private int numOfUsedRooms;
- private Room[][] allRooms;
- final int MAX_FLOORS = 10, MAX_ROOMS = 100;
- public Hotel(int floors, int rooms) {
- if (floors < MAX_FLOORS && rooms < MAX_ROOMS) {
- this.allRooms = new Room[floors][rooms];
- //generate amount of beds for all rooms in hotel
- for (int i = 0; i < floors; i += 1) {
- for (int j = 0; j < rooms; j += 1) {
- //amount of beds could be from 1 to 4
- this.allRooms[i][j] = new Room(new Random().nextInt(4) + 1);
- }
- }
- }
- }
- public int checkIn(Guest[] guests) {
- for (int i = 1; i < allRooms.length + 1; i += 1) {
- for (int j = 1; j < allRooms[0].length + 1; j += 1) {
- //check if room with beds and if there are no one there
- if (this.allRooms[i - 1][j - 1] != null && this.allRooms[i - 1][j - 1].getAllGuests() == null) {
- if (this.allRooms[i - 1][j - 1].getNumOfBeds() >= guests.length) {
- //set guests into the room
- allRooms[i - 1][j - 1].setGuests(guests);
- //increase number of used rooms
- numOfUsedRooms += 1;
- //floor*100+room to get a right room number
- return i * 100 + j;
- }
- }
- }
- }
- //in case there is no valid room
- return -1;
- }
- public int isGuestExist(int id) {
- //should start for from 1 for to get right number of room
- for (int i = 1; i < allRooms.length + 1; i += 1) {
- for (int j = 1; j < allRooms[0].length + 1; j += 1) {
- //check guest by id if he is from room and returns rooms number
- if (this.allRooms[i-1][j-1].getAllGuests() != null) {
- if (this.allRooms[i - 1][j - 1].isGuestFromRoom(id)) {
- return i * 100 + j;
- }
- }
- }
- }
- return -1;
- }
- public int floorWithMaxEmptyRoom() {
- int emptyOnFloor = 0, floor, maxRooms;
- //set first floor as max
- for (int i = 0; i < this.allRooms[0].length; i += 1) {
- emptyOnFloor += this.allRooms[0][i].isRoomEmpty() ? 1 : 0;
- }
- maxRooms = emptyOnFloor;
- floor = 1;
- //should start for from 1 for to get right number of room
- for (int i = 1; i < this.allRooms.length; i += 1) {
- for (int j = 0; j < this.allRooms[0].length; j += 1) {
- //get all empty on one floor
- emptyOnFloor += this.allRooms[i][j].isRoomEmpty() ? 1 : 0;
- }
- //compare current max amount of rooms with amount on a floor. Set as max and save floor's index
- if (maxRooms < emptyOnFloor) {
- maxRooms = emptyOnFloor;
- floor = i + 1;
- }
- }
- return floor;
- }
- @Override
- public String toString() {
- String str = "Hotel" + "\n" +
- "Amount of full rooms: " + numOfUsedRooms + "\n";
- //should start for from 1 for to get right number of room
- for (int i = 1; i < allRooms.length + 1; i += 1) {
- for (int j = 1; j < allRooms[0].length + 1; j += 1) {
- if (!this.allRooms[i - 1][j - 1].isRoomEmpty()) {
- str += "room: " + (i * 100 + j) + "\n" + this.allRooms[i - 1][j - 1].toString();
- } else {
- str += "room: " + (i * 100 + j) + "-Is empty\n";
- }
- }
- }
- return str;
- }
- }
- Room=======================================================================================================================
- package Hotel;
- public class Room {
- private int numOfBeds;
- private Guest[] allGuests;
- public Room(int numOfBeds) {
- this.numOfBeds = numOfBeds;
- }
- public void setGuests(Guest[] guests) {
- this.allGuests = guests;
- }
- public Guest[] getAllGuests() {
- return allGuests;
- }
- public int getNumOfBeds() {
- return numOfBeds;
- }
- //check if no one live in the room
- public boolean isRoomEmpty() {
- if (this.allGuests == null) {
- return true;
- }
- return false;
- }
- //check if guest with id living in the room
- public boolean isGuestFromRoom(int id) {
- for (int i = 0; i < allGuests.length; i += 1) {
- if (allGuests[i].getPassportNumber() == id) {
- return true;
- }
- }
- return false;
- }
- @Override
- public String toString() {
- String str = "beds: " + numOfBeds + "\n";
- for (Guest guests : allGuests) {
- str += guests.toString();
- }
- return str;
- }
- }
- Guest====================================================================================================================
- package Hotel;
- public class Guest {
- private String name;
- private int passportNumber;
- public Guest(String name, int passportNumber) {
- this.name = name;
- this.passportNumber = passportNumber;
- }
- public int getPassportNumber() {
- return passportNumber;
- }
- @Override
- public String toString() {
- return "name: " + name + '\n' +
- "passport: " + passportNumber + '\n';
- }
- }
- Main============================================================================================================
- import Hotel.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner s = new Scanner(System.in);
- int menuButtons = 0;
- //default filename
- String fileName = "default.txt";
- System.out.println("Do you want to read information from some file? 1-yes 0-no");
- if (s.nextInt() == 1) {
- System.out.println("Write a name of a file");
- fileName = s.next() + ".txt";
- //Read info from file
- Hotel read = readFromFile(fileName);
- }
- //initialize new hotel
- Hotel hilton = new Hotel(3, 5);
- //looped menu
- System.out.println("You're in the menu.Enter anything to resume. Enter \"0\" for finish");
- 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");
- menuButtons = s.nextInt();
- while (menuButtons != 0) {
- switch (menuButtons) {
- case 1:
- //check in for guests
- findRoomForGuests(hilton);
- break;
- case 2:
- //find guest in hotel
- findGuest(hilton);
- break;
- case 3:
- //print all info of hotel
- System.out.println(hilton.toString());
- break;
- case 4:
- //print floor with max empty rooms
- System.out.printf("Floor with max empty rooms is %d\n", hilton.floorWithMaxEmptyRoom());
- break;
- }
- System.out.println("Enter \"0\" for finish or 1 to 4 for resume");
- menuButtons = s.nextInt();
- }
- //function printing college to file
- printToFile(fileName, hilton);
- }
- public static void findGuest(Hotel hotel) {
- System.out.println("Enter guest's id: ");
- int guestId = new Scanner(System.in).nextInt();
- if (hotel.isGuestExist(guestId) == -1) {
- System.out.println("This men is not from hotel");
- } else {
- System.out.printf("Room number: %d\n", hotel.isGuestExist(guestId));
- }
- }
- public static void findRoomForGuests(Hotel hotel) {
- Scanner s = new Scanner(System.in);
- int roomForGuests;
- Guest[] guests;
- System.out.println("Enter amount of guests(from 1 to 4): ");
- //create an array of guests
- guests = new Guest[s.nextInt()];
- //link our array to Room's inner array and returns room's number or -1 if there are no empty room
- roomForGuests = hotel.checkIn(guests);
- //send massage for cases
- if (roomForGuests == -1) {
- System.out.println("Sorry we don't have an empty room");
- } else {
- for (int i = 0; i < guests.length; i += 1) {
- System.out.println("Enter guests name and his id separated by space");
- guests[i] = new Guest(s.next(), s.nextInt());
- }
- System.out.printf("You'r room: %d\n", roomForGuests);
- }
- }
- public static void printToFile(String fileName, Hotel hotel) throws FileNotFoundException {
- PrintWriter pw = new PrintWriter(new File(fileName));
- //write all data from class by toString() into the file
- pw.println(hotel.toString());
- pw.close();
- }
- public static Hotel readFromFile(String fileName) throws FileNotFoundException {
- //to do this need to add some functions in the Hotel.java so if you want I can do this
- return new Hotel(1,1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment