Skip to content

Commit

Permalink
RSWW-134 fix errors in random selection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzigerrr committed Jun 3, 2024
1 parent d2e885a commit 5b85468
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import cloud.project.datagenerator.hotels.domain.Hotel;
import cloud.project.datagenerator.hotels.domain.Room;
import cloud.project.datagenerator.hotels.repositories.HotelRepository;
import cloud.project.datagenerator.hotels.repositories.RoomRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand All @@ -17,14 +16,13 @@
public class HotelsDataGenerator {

private final HotelRepository hotelRepository;
private final RoomRepository roomRepository;
private final Random random = new Random();

@Scheduled(fixedDelay = 5000, initialDelay = 5000)
public void generateRandomHotelData() {
System.out.println("Generating Hotel Data...");

int action = random.nextInt(3); // Generate a random number: 0, 1, or 2
int action = random.nextInt(3);

switch (action) {
case 0:
Expand Down Expand Up @@ -55,7 +53,7 @@ private void createNewRoom() {
System.out.println("Creating new room: " + newRoom);

// Todo: send the room using rabbitmq

}

private void updateRandomRoom() {
Expand All @@ -64,10 +62,10 @@ private void updateRandomRoom() {

Room randomRoom = randomHotel.getRooms().get(random.nextInt(randomHotel.getRooms().size()));
int currentGuestCapacity = randomRoom.getGuestCapacity();
int newGuestCapacity = random.nextInt(3, currentGuestCapacity + 1);
int newGuestCapacity = random.nextInt(1, currentGuestCapacity + 10);

System.out.println("Updating room " + randomRoom.getName() + " in hotel " + randomHotel.getName() +
" with new guest capacity: " + newGuestCapacity);
" - old guest capacity: " + currentGuestCapacity + " new guest capacity: " + newGuestCapacity);

// Todo: send the room using rabbitmq
randomRoom.setGuestCapacity(newGuestCapacity);
Expand All @@ -86,21 +84,26 @@ private void deleteRandomRoom() {
}

private Hotel getRandomHotel() {
// Fetch all hotels
List<Hotel> hotels = hotelRepository.findAll();

if (hotels.isEmpty()) {
System.out.println("No hotels found.");
return null;
}

// Select a random hotel
Hotel randomHotel = hotels.get(random.nextInt(hotels.size()));
Hotel randomHotel;
int attempts = 0;
do {
randomHotel = hotels.get(random.nextInt(hotels.size()));
attempts++;
} while (randomHotel.getRooms().isEmpty() && attempts < hotels.size());

if (randomHotel.getRooms().isEmpty()) {
System.out.println("No rooms found in the selected hotel.");
return null;
}

return randomHotel;
}

}

0 comments on commit 5b85468

Please sign in to comment.