aboutsummaryrefslogtreecommitdiff
path: root/src/Client/Book.java
blob: 6d23e270b41ab9e576848a07a5bcb0f5ea6abda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
    }
}