diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-11-16 02:11:24 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-11-16 02:11:24 +0530 |
commit | ed3e023d1da3465bc79a91d38950a167004911b2 (patch) | |
tree | 08fb4de9af501612ccb85c8ee2f6beb60779c1f2 /src/Client |
First Commit
Diffstat (limited to 'src/Client')
-rw-r--r-- | src/Client/Book.java | 72 | ||||
-rw-r--r-- | src/Client/SocketClient.java | 19 |
2 files changed, 91 insertions, 0 deletions
diff --git a/src/Client/Book.java b/src/Client/Book.java new file mode 100644 index 0000000..6d23e27 --- /dev/null +++ b/src/Client/Book.java @@ -0,0 +1,72 @@ +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<Book> fromObjects(Object[] array) throws JSONException { + ArrayList<Book> 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; + } +} diff --git a/src/Client/SocketClient.java b/src/Client/SocketClient.java new file mode 100644 index 0000000..9ce80a3 --- /dev/null +++ b/src/Client/SocketClient.java @@ -0,0 +1,19 @@ +package Client; + +import io.socket.client.IO; +import io.socket.client.Socket; + +import java.net.URISyntaxException; + +public class SocketClient { + public static Socket socket; + + public SocketClient(String url) { + try { + socket = IO.socket(url); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + + } +} |