diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Client/Book.java | 72 | ||||
-rw-r--r-- | src/Client/SocketClient.java | 19 | ||||
-rw-r--r-- | src/META-INF/MANIFEST.MF | 3 | ||||
-rw-r--r-- | src/Main.java | 20 | ||||
-rw-r--r-- | src/UI/Components/BookListItem.java | 47 | ||||
-rw-r--r-- | src/UI/Components/BooksList.java | 38 | ||||
-rw-r--r-- | src/UI/Components/InputBox.java | 39 | ||||
-rw-r--r-- | src/UI/Components/LoginPanel.java | 55 | ||||
-rw-r--r-- | src/UI/MainWindow.java | 69 | ||||
-rw-r--r-- | src/UI/SettingsPopupWindow.java | 31 |
10 files changed, 393 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); + } + + } +} diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..37197ef --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0
+Main-Class: Main
+
diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..c02430a --- /dev/null +++ b/src/Main.java @@ -0,0 +1,20 @@ +import UI.MainWindow; + +import javax.swing.*; +import java.awt.*; + +public class Main { + public static void main(String[] args) { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | + IllegalAccessException _) { + } + + EventQueue.invokeLater(() -> { + MainWindow w = new UI.MainWindow(); + w.setVisible(true); + }); + } +} diff --git a/src/UI/Components/BookListItem.java b/src/UI/Components/BookListItem.java new file mode 100644 index 0000000..9042762 --- /dev/null +++ b/src/UI/Components/BookListItem.java @@ -0,0 +1,47 @@ +package UI.Components; + +import Client.Book; +import UI.MainWindow; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.border.MatteBorder; +import java.awt.*; + +public class BookListItem extends JPanel { + BookListItem(Book book) { + this.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(6, 10, 6, 10), new MatteBorder(0, 0, 1, 0, Color.BLACK))); + + boolean issued = book.getIssuedBy() != 0; + + JLabel titleLabel = new JLabel(book.getTitle()); + titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.BOLD, titleLabel.getFont().getSize() + 2)); + this.add(titleLabel); + + JPanel issueButtonPanel = new JPanel(); + issueButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); + if (!issued || book.getIssuedBy() == MainWindow.userId) { + JButton issueButton = new JButton(issued ? "Return Book" : "Issue Book"); + issueButtonPanel.add(issueButton); + + issueButton.addActionListener(e -> { + if (issued) { + book.returnBook(); + } else { + book.issue(); + } + }); + } else { + issueButtonPanel.add(new JLabel("Book is Unavailable")); + } + this.add(issueButtonPanel); + + JLabel authorLabel = new JLabel(book.getAuthor()); + authorLabel.setForeground(Color.DARK_GRAY); + this.add(authorLabel); + + this.setMaximumSize(new Dimension(650, 100)); + this.setLayout(new GridLayout(2, 2)); + } +} + diff --git a/src/UI/Components/BooksList.java b/src/UI/Components/BooksList.java new file mode 100644 index 0000000..05a2f5f --- /dev/null +++ b/src/UI/Components/BooksList.java @@ -0,0 +1,38 @@ +package UI.Components; + +import Client.Book; + +import javax.swing.*; +import java.util.ArrayList; + +public class BooksList extends JPanel { + private static JPanel listPanel; + + public BooksList() { + listPanel = new JPanel(); + listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); + + JScrollPane scrollPane = new JScrollPane(listPanel); + scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + + this.add(scrollPane); + + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + } + + public static void refreshBooks(ArrayList<Book> books) { + new Thread(new Runnable() { + @Override + public void run() { + listPanel.removeAll(); + + for (Book book : books) { + listPanel.add(new BookListItem(book)); + } + + listPanel.revalidate(); + listPanel.repaint(); + } + }).start(); + } +} diff --git a/src/UI/Components/InputBox.java b/src/UI/Components/InputBox.java new file mode 100644 index 0000000..c15381e --- /dev/null +++ b/src/UI/Components/InputBox.java @@ -0,0 +1,39 @@ +package UI.Components; + +import javax.swing.*; +import java.awt.*; + +public class InputBox extends JPanel { + JLabel label; + JTextField textField; + + public InputBox(String label) { + this.label = new JLabel(label); + this.label.setPreferredSize(new Dimension(70, 30)); + this.add(this.label); + + this.add(Box.createHorizontalGlue()); + + this.textField = new JTextField(20); + this.add(this.textField); + + this.setLayout(new FlowLayout(FlowLayout.CENTER)); + } + + public String getLabel() { + return this.label.getText(); + } + + public String getValue() { + return this.textField.getText(); + } + + public void setLabel(String label) { + this.label.setText(label); + } + + public void setValue(String value) { + this.textField.setText(value); + this.textField.repaint(); + } +} diff --git a/src/UI/Components/LoginPanel.java b/src/UI/Components/LoginPanel.java new file mode 100644 index 0000000..e79326b --- /dev/null +++ b/src/UI/Components/LoginPanel.java @@ -0,0 +1,55 @@ +package UI.Components; + +import UI.MainWindow; +import UI.SettingsPopupWindow; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; + +public class LoginPanel extends JPanel { + public JButton loginButton; + private SettingsPopupWindow settingsPopupWindow; + + public InputBox usernameInput; + public InputBox passwordInput; + + public LoginPanel() { + usernameInput = new InputBox("Username:"); + usernameInput.setBorder(new EmptyBorder(140, 100, 0, 100)); + this.add(usernameInput); + + passwordInput = new InputBox("Password:"); + passwordInput.setBorder(new EmptyBorder(10, 100, 10, 100)); + this.add(passwordInput); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(Box.createRigidArea(new Dimension(140, 0))); + + JButton settingsButton = new JButton("Settings"); + settingsButton.addActionListener(_ -> { + if (settingsPopupWindow == null) { + settingsPopupWindow = new SettingsPopupWindow(); + settingsPopupWindow.saveButton.addActionListener(_ -> { + MainWindow.url = settingsPopupWindow.hostnameInput.getValue(); + settingsPopupWindow.setVisible(false); + }); + } + + settingsPopupWindow.hostnameInput.setValue(MainWindow.url); + + settingsPopupWindow.setVisible(true); + }); + buttonPanel.add(settingsButton); + + buttonPanel.add(Box.createRigidArea(new Dimension(10, 0))); + + loginButton = new JButton("Login"); + buttonPanel.add(loginButton); + + buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); + this.add(buttonPanel); + + this.setMaximumSize(new Dimension(350, 200)); + } +} diff --git a/src/UI/MainWindow.java b/src/UI/MainWindow.java new file mode 100644 index 0000000..8c3964e --- /dev/null +++ b/src/UI/MainWindow.java @@ -0,0 +1,69 @@ +package UI; + +import Client.Book; +import Client.SocketClient; +import UI.Components.BooksList; +import UI.Components.LoginPanel; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import javax.swing.*; +import java.awt.*; +import java.net.Socket; + +public class MainWindow extends JFrame { + public static String url = "http://localhost:8080"; + public static int userId = 0; + + public MainWindow() { + this.setTitle("User Panel - Library Management System"); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setSize(650, 500); + this.setResizable(false); + this.setLocationRelativeTo(null); + + CardLayout cardLayout = new CardLayout(); + JPanel cardPanel = new JPanel(cardLayout); + + LoginPanel loginPanel = new LoginPanel(); + loginPanel.loginButton.addActionListener(e -> { + new SocketClient(url); + SocketClient.socket.connect(); + + SocketClient.socket.emit("login", + "{" + + "\"userName\": \"" + loginPanel.usernameInput.getValue() + "\"," + + "\"password\": \"" + loginPanel.passwordInput.getValue() + "\"" + + "}"); + + SocketClient.socket.on("loggedIn", data -> { + MainWindow.userId = (int) data[0]; + + SocketClient.socket.on("allBooksList", d -> { + try { + BooksList.refreshBooks(Book.fromObjects(d)); + } catch (JSONException ex) { + throw new RuntimeException(ex); + } + }); + + SocketClient.socket.on("booksUpdated", d -> { + try { + BooksList.refreshBooks(Book.fromObjects(d)); + } catch (JSONException ex) { + throw new RuntimeException(ex); + } + }); + + SocketClient.socket.emit("getAllBooks"); + cardLayout.next(cardPanel); + }); + }); + cardPanel.add(loginPanel); + + cardPanel.add(new BooksList()); + + this.add(cardPanel); + } +} diff --git a/src/UI/SettingsPopupWindow.java b/src/UI/SettingsPopupWindow.java new file mode 100644 index 0000000..6c77e07 --- /dev/null +++ b/src/UI/SettingsPopupWindow.java @@ -0,0 +1,31 @@ +package UI; + +import UI.Components.InputBox; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; + +public class SettingsPopupWindow extends JFrame { + public InputBox hostnameInput; + public JButton saveButton; + + public SettingsPopupWindow() { + this.setTitle("Settings - Library Management System"); + this.setSize(350, 200); + this.setResizable(false); + this.setLocationRelativeTo(null); + + this.hostnameInput = new InputBox("Hostname:"); + this.hostnameInput.setBorder(new EmptyBorder(0, 10, 0, 10)); + this.add(this.hostnameInput); + + saveButton = new JButton("Save"); + JPanel buttonPanel = new JPanel(); + buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 12)); + buttonPanel.add(saveButton); + this.add(buttonPanel); + + this.setLayout(new FlowLayout(FlowLayout.TRAILING)); + } +} |