|
| 1 | +/* Create a program to handle total earning (like you want to create a application for hotel |
| 2 | +booking and suppose 10 rooms book on that day). |
| 3 | +
|
| 4 | +Client want to enter the room number which are available in the hotel at that time while customer visit, |
| 5 | +also want to enter the bookable room as per the customer request and number of person want to stay in the room, |
| 6 | +charging amount as per the person per day, enter the day count for stay in the hotel room, after the above manually enter the variable client |
| 7 | +want to show the result as booked room and with its room number and also show the result of avaiable room after booking and amount for customer. |
| 8 | +*/ |
| 9 | + |
| 10 | +package practice; |
| 11 | + |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +// Creating a class that tracks the booked room count. |
| 15 | +class RoomCount extends Thread { |
| 16 | + int roomsBooked = 0; |
| 17 | + int availableRooms; |
| 18 | + int roomsToBook; |
| 19 | + int daysStayed; |
| 20 | + |
| 21 | + // Constructor to initialize the number of rooms to book, available rooms, and |
| 22 | + // day stayed |
| 23 | + public RoomCount(int roomsToBook, int availableRooms, int daysStayed) { |
| 24 | + this.roomsToBook = roomsToBook; |
| 25 | + this.availableRooms = availableRooms; |
| 26 | + this.daysStayed = daysStayed; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void run() { |
| 31 | + // Check if enough rooms are available |
| 32 | + if (roomsToBook > availableRooms) { |
| 33 | + System.out.println("Sorry, we don't have enough rooms to book " + roomsToBook |
| 34 | + + " rooms. We can only book " + availableRooms + " rooms."); |
| 35 | + } else { |
| 36 | + // Book rooms in a single line |
| 37 | + roomsBooked = roomsToBook; |
| 38 | + // If you want to store the value of for loop in a variable so you should |
| 39 | + // decalre the variable outside the for loop |
| 40 | + int roomNumber = 100; |
| 41 | + System.out.println("Now Booked rooms are: " + roomsBooked); |
| 42 | + System.out.println("Room numbers booked: "); |
| 43 | + |
| 44 | + // Print room numbers (assuming room numbers start from 1) |
| 45 | + for (int i = 1; i <= roomsBooked; i++) { |
| 46 | + System.out.print(roomNumber + i + "," + " "); |
| 47 | + } |
| 48 | + System.out.println("\n\nRooms loading...\n"); |
| 49 | + |
| 50 | + try { |
| 51 | + sleep(1000); |
| 52 | + } catch (InterruptedException e) { |
| 53 | + System.out.println(e); |
| 54 | + } |
| 55 | + |
| 56 | + System.out.println("Number of available rooms left: " + (availableRooms - roomsBooked)); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Class to calculate total earning based on people per room |
| 62 | +class AmountCount extends Thread { |
| 63 | + int initialEarning = 0; |
| 64 | + int roomsToBook; |
| 65 | + int peoplePerRoom; |
| 66 | + int amountPerPersonPerDay; |
| 67 | + int daysStayed; |
| 68 | + |
| 69 | + // Constructor to initialize the number of rooms to book, amount per room per |
| 70 | + // day, and days stayed and amount per person for a day |
| 71 | + public AmountCount(int roomsToBook, int peoplePerRoom, int amountPerPersonPerDay, int daysStayed) { |
| 72 | + this.roomsToBook = roomsToBook; |
| 73 | + this.peoplePerRoom = peoplePerRoom; |
| 74 | + this.amountPerPersonPerDay = amountPerPersonPerDay; |
| 75 | + this.daysStayed = daysStayed; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void run() { |
| 80 | + initialEarning = roomsToBook * peoplePerRoom * amountPerPersonPerDay * daysStayed; |
| 81 | + System.out.println("Total earning from booked rooms for " + daysStayed + " days: " + initialEarning); |
| 82 | + |
| 83 | + try { |
| 84 | + sleep(1000); |
| 85 | + } catch (InterruptedException e) { |
| 86 | + System.out.println(e); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +public class Practice1 { |
| 92 | + public static void main(String[] args) { |
| 93 | + Scanner scanner = new Scanner(System.in); |
| 94 | + |
| 95 | + // Get the number of available rooms |
| 96 | + System.out.print("Enter the number of available rooms: "); |
| 97 | + int availableRooms = scanner.nextInt(); |
| 98 | + |
| 99 | + // Get the number of rooms to book |
| 100 | + System.out.print("Enter the number of rooms to book: "); |
| 101 | + int roomsToBook = scanner.nextInt(); |
| 102 | + |
| 103 | + // Check if enough rooms are available before asking for earnings |
| 104 | + if (roomsToBook > availableRooms) { |
| 105 | + System.out.println("Sorry, we don't have enough rooms to book " + roomsToBook |
| 106 | + + " rooms. We can only book " + availableRooms + " rooms."); |
| 107 | + } else { |
| 108 | + // Get number of people per room |
| 109 | + System.out.print("Enter the number of people staying per room: "); |
| 110 | + int peoplePerRoom = scanner.nextInt(); |
| 111 | + |
| 112 | + // Get the earning per person per day |
| 113 | + System.out.print("Enter the amount charged per person per day: "); |
| 114 | + int amountPerPersonPerDay = scanner.nextInt(); |
| 115 | + |
| 116 | + // Get the number of days stayed |
| 117 | + System.out.print("Enter the number of days stayed: "); |
| 118 | + int daysStayed = scanner.nextInt(); |
| 119 | + |
| 120 | + // Start room booking thread |
| 121 | + RoomCount rc = new RoomCount(roomsToBook, availableRooms, daysStayed); |
| 122 | + rc.start(); |
| 123 | + |
| 124 | + // Wait for room booking to complete |
| 125 | + try { |
| 126 | + rc.join(); |
| 127 | + } catch (InterruptedException e) { |
| 128 | + System.out.println(e); |
| 129 | + } |
| 130 | + |
| 131 | + // Start earnings calculation thread |
| 132 | + AmountCount ac = new AmountCount(roomsToBook, peoplePerRoom, amountPerPersonPerDay, daysStayed); |
| 133 | + ac.start(); |
| 134 | + } |
| 135 | + |
| 136 | + scanner.close(); |
| 137 | + } |
| 138 | +} |
0 commit comments