Please help me with this two-part question. Here is the first part:
(Part 2: I updated the code since then - the requirements have changed a bit.)
I am trying to implement a library problem in Java. The Wikipedia Semaphore page provides a library analogy to Semaphore. In the first part, I try to simulate this problem. In my case, I use [Topic Expert] instead of a room as a resource.
Suppose the library has 10 identical classrooms designed for use by one student at a time. To prevent controversy, students should request a room from the front desk if they want to use the study. When the student has finished using the room, the student should return to the counter and indicate that one room has become free. If there are no rooms available, students wait at the counter until someone leaves the room.
Since the numbers are identical, the librarian at the reception does not keep track of which room is occupied, only the number of rooms available. When a student requests a room, the librarian decreases this number. When a student releases a room, the librarian increases this number. After access to the room is granted, the room can be used as long as possible, and therefore it is not possible to book rooms in advance.
The problem that I encountered in my implementation concerns the association of a student with a subject matter expert. How will you do this in the next secnario? All SubjectMatterExpert needs to do is print the student ID (for now).
Part 2: New Requirements:
- There is a fixed number of students, small and medium enterprises and bookcases
- Students have a certain number of books at the beginning (currently books are just numbers)
- SMEs add or check books from the Boook cabinet upon student request
- Students specify the action or number of copies, number of books, and bookcase
This is a modified (edited) Student class:
package librarysimulation; public class Student extends Thread { String studentId = ""; Librarian librarian = null; int bookCount = 0; public Student(String id, Librarian lib, int book) { studentId = id; librarian = lib; bookCount = book; } @Override public void run() { System.out.println("Student " + studentId + " is requesting SME..."); librarian.requestSME(); try {
This is a modified (edited) librarian class:
package librarysimulation; import java.util.concurrent.Semaphore; import java.util.logging.Level; import java.util.logging.Logger; public class Librarian { public Semaphore sme; public int bookClosetCount = 0; public Librarian(int smeCount, int bookCloset) { sme = new Semaphore(smeCount, true); bookClosetCount = bookCloset;
This is a modified (edited) Subject Matter Expert class:
package librarysimulation; public class SubjectMatterExpert extends Thread { String smeId = ""; SubjectMatterExpert(String id) { smeId = id; } @Override public void run(){
This is a modified (edited) simulator class:
package librarysimulation; public class Simulator extends Thread { public static final int STUDENT_COUNT = 50; public static final int SME_COUNT = 3; public static final int BOOKCLOSET_COUNT = 10; public static final int BOOK_PER_STUDENT_COUNT = 10; @Override public void run() {
a is the (new) Book Closet class:
package librarysimulation; public class BookCloset { int closetId; int bookCount = 0; public BookCloset(int id, int book) { closetId = id; bookCount = book; } public int addBook(int book){ return bookCount + book; } public int checkOutBook(int book){ int finalBookCount = bookCount - book;