Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve task #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/java/mate/academy/TicketBookingSystem.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
package mate.academy;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TicketBookingSystem {
private final Semaphore semaphore;
private int totalSeats;
private final Lock lock;

public TicketBookingSystem(int totalSeats) {

this.totalSeats = totalSeats;
this.semaphore = new Semaphore(totalSeats);
this.lock = new ReentrantLock();
}

public BookingResult attemptBooking(String user) {
return null;
BookingResult bookingResult;
lock.lock();
try {
semaphore.acquire();
if (totalSeats > 0) {
bookingResult = new BookingResult(user, true, "Booking successful.");
totalSeats--;
} else {
bookingResult = new BookingResult(user, false, "No seats available.");
}
semaphore.release();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
return bookingResult;
}
}
Loading