package Client; import UI.MainWindow; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; public class Book { private int id; private String title; private String author; private int issuedBy; public Book(int id, String title, String author, int issuedBy) { this.id = id; this.title = title; this.author = author; this.issuedBy = issuedBy; } public static ArrayList fromObjects(Object[] array) throws JSONException { ArrayList books = new ArrayList<>(); JSONArray jsonArray = (JSONArray) array[0]; for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); // Extract fields from JSON object int id = jsonObject.getInt("id"); String title = jsonObject.getString("title"); String author = jsonObject.getString("author"); int issuedBy = jsonObject.getInt("issuedBy"); // Create a Book object and add it to the list Book book = new Book(id, title, author, issuedBy); books.add(book); } return books; } public void issue() { SocketClient.socket.emit("issue", "{" + "\"id\":" + MainWindow.userId + "," + "\"bookId\":" + this.id + "}"); } public void returnBook() { SocketClient.socket.emit("return", "{" + "\"id\":" + MainWindow.userId + "," + "\"bookId\":" + this.id + "}"); } public String getTitle() { return title; } public String getAuthor() { return author; } public int getIssuedBy() { return issuedBy; } }