5025231061_EASPBO

Nama : Nadin Nabil Hafizh Ayyasy
NRP : 5025231061

EAS PBO : Implementasi Perpustakaan

1. nheritance dalam Java adalah konsep di mana sebuah class dapat mewarisi properti dan metode dari class lain. Dengan inheritance, kode dapat digunakan kembali, mempermudah pengorganisasian, dan memperkuat hierarki objek. Dengan pewarisan ini, akan menciptakan hierarki class yang terstruktur. Implementasi inhertance di FP kami adalah menggunakan library JFrame untuk menampilkan tampilkan pada java

class BookDetailsPage extends JFrame {
   public BookDetailsPage(Book book) {
       setTitle("Book Details - " + book.getTitle());
       setSize(600, 400);
       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       setLayout(new BorderLayout(10, 10));

       // Panel Gambar Cover
       JLabel coverLabel = new JLabel(createImageIcon(book.getImagePath()));
       coverLabel.setHorizontalAlignment(SwingConstants.CENTER);
       add(coverLabel, BorderLayout.NORTH);
       
       String description = book.getDescription();
       String formattedDescription = description.replace("\n", "<br>");
       // Panel Informasi Buku
       JEditorPane bookInfoPane = new JEditorPane("text/html",
"<html>" +
"<b>Title:</b> " + book.getTitle() + "<br>" +
"<b>Author:</b> " + book.getAuthor() + "<br>" +
"<b>Year:</b> " + book.getYear() + "<br>" +
"<b>Genres:</b> " + String.join(", ", book.getGenres()) + "<br><br>" +
"<b>Description:</b><br>" + formattedDescription +
"</html>"
);

Extends JFrame

BookDetailsPage adalah kelas yang mewarisi JFrame.

JFrame adalah sebuah kelas dalam pustaka di Java Swing. Class ini digunakan untuk membuat antarmuka pengguna berbasis jendela yang dapat menampilkan komponen grafis seperti tombol, label, dan lainnya berbasis GUI.

Dengan mewarisi JFrame, kelas BookDetailsPage dapat memiliki seluruh fungsionalitas yang disediakan oleh JFrame, seperti pengaturan ukuran jendela, close jendela, pengaturan layout, dan menambah komponen UI.

2. Fitur yang ada di FP kami :

  • Searching Data
    Pengguna dapat mencari buku yang ingin dibaca
    searchButton.addActionListener(e -> {
       String query = searchField.getText().toLowerCase();
       gridPanel.removeAll();
       for (Book book : books) {
           if (book.getTitle().toLowerCase().contains(query) || book.getAuthor().toLowerCase().contains(query)) {
               gridPanel.add(createBookPanel(book));
           }
       }
       gridPanel.revalidate();
       gridPanel.repaint();
    });

  • Sorting Genre
    Pengguna dapat mfilter buku bedasarkan genre yang disukai
    private JPanel createGenrePanel() {
    JPanel genrePanel = new JPanel(new BorderLayout());
    JPanel genreOptionsPanel = new JPanel(new GridLayout(0, 5, 10, 10));
    String[] genres = {
       "Action",
       "Adventure",
       "Comedy",
       "Demons",
       "Drama",
       "Ecchi",
       "Fantasy",
       "Game",
       "Harem",
       "Historical",
       "Horror",
       "Josei",
       "Magic",
       "Martial Arts",
       "Mecha",
       "Military",
       "Music",
       "Mystery",
       "Psychological",
       "Parody",
       "Police",
       "Romance",
       "Samurai",
       "School",
       "Sci-Fi",
       "Seinen",
       "Shoujo",
       "Shoujo Ai",
       "Shounen",
       "Slice of Life",
       "Sports",
       "Space",
       "Super Power",
       "Supernatural",
       "Thriller",
       "Vampire"
    };

    JPanel genreBooksPanel = new JPanel(new GridLayout(0, 4, 10, 10));

    for (String genre : genres) {
       JLabel genreLabel = new JLabel(genre);
       genreLabel.setForeground(Color.BLUE);
       genreLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

       genreLabel.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               genreBooksPanel.removeAll();
               for (Book book : books) {
                   if (book.getGenres().contains(genre)) {
                       genreBooksPanel.add(createBookPanel(book));
                   }
               }
               genreBooksPanel.revalidate();
               genreBooksPanel.repaint();
           }
       });
       genreOptionsPanel.add(genreLabel);
    }

    genrePanel.add(genreOptionsPanel, BorderLayout.NORTH);
    genrePanel.add(new JScrollPane(genreBooksPanel), BorderLayout.CENTER);
    return genrePanel;
    }

  • Create, Read, Update, dan Delete
    Pengguna dapat menambahkan  mengedit dan menghapus daftar buku
    class AddBookListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       JTextField titleField = new JTextField();
       JTextField authorField = new JTextField();
       JTextField yearField = new JTextField();
       JTextField genreField = new JTextField();
       JTextField imagePathField = new JTextField();
       JTextArea description = new JTextArea(5, 20);
       description.setLineWrap(true);  // Agar teks terbungkus jika panjang
       description.setWrapStyleWord(true);  // Agar teks terbungkus di kata, bukan di tengah
       JScrollPane descriptionScroll = new JScrollPane(description);

       Object[] inputFields = {
           "Title:", titleField, "Author:", authorField, "Year:", yearField,
           "Genres (comma-separated):", genreField, "Image Path:", imagePathField, "Descripton:", description
       };

       if (JOptionPane.showConfirmDialog(frame, inputFields, "Add Book", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
           List<String> genres = Arrays.asList(genreField.getText().split(","));
           books.add(new Book(titleField.getText(), authorField.getText(), yearField.getText(), genres, imagePathField.getText(), description.getText()));
           updateGridPanel();
       }
    }
    }
    class EditBookListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       String title = JOptionPane.showInputDialog(frame, "Enter the title of the book to edit:");
       if (title == null || title.isEmpty()) return;
       for (Book book : books) {
           if (book.getTitle().equalsIgnoreCase(title)) {
               JTextField titleField = new JTextField(book.getTitle());
               JTextField authorField = new JTextField(book.getAuthor());
               JTextField yearField = new JTextField(book.getYear());
               JTextField genreField = new JTextField(String.join(",", book.getGenres()));
               JTextField imagePathField = new JTextField(book.getImagePath());
               JTextArea description = new JTextArea(book.getDescription());
               description.setPreferredSize(new Dimension(200, 100));

               Object[] inputFields = {
                   "Title:", titleField, "Author:", authorField, "Year:", yearField,
                   "Genres (comma-separated):", genreField, "Image Path:", imagePathField,
                   "Descriptoion:",  new JScrollPane(description)
               };

               if (JOptionPane.showConfirmDialog(frame, inputFields, "Edit Book", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                   book.genres = Arrays.asList(genreField.getText().split(","));
                   // books.set(books.indexOf(book), new Book(titleField.getText(), authorField.getText(), yearField.getText(), book.genres, imagePathField.getText()));
                   book.setTitle(titleField.getText());
                   book.setAuthor(authorField.getText());
                   book.setYear(yearField.getText());
                   book.setGenres(Arrays.asList(genreField.getText().split(",")));
                   book.setImagePath(imagePathField.getText());
                   book.setDescription(description.getText());

                   updateGridPanel();
               }
               return;
           }
       }
       JOptionPane.showMessageDialog(frame, "Book not found!");
    }
    }

    class DeleteBookListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       String title = JOptionPane.showInputDialog(frame, "Enter the title of the book to delete:");
       if (title == null || title.isEmpty()) return;
       books.removeIf(book -> book.getTitle().equalsIgnoreCase(title));
       updateGridPanel();
    }
    }

3. Diagram Class dari FP Kami

 

class LibraryManagerr {

   - JFrame frame

   - JTabbedPane tabbedPane

   - JPanel gridPanel

   - ArrayList<Book> books

   + LibraryManagerr()

   - JPanel createHomePanel()

   - JPanel createGenrePanel()

   - JPanel createAllBooksPanel()

   - void updateGridPanel()

   - JPanel createBookPanel(Book book)

   - void showBookDetails(Book book)

   - void updateBookListModel(DefaultListModel<String> bookListModel)

}


class Book {

   - String title

   - String author

   - String year

   - List<String> genres

   - String imagePath

   - String description

   + Book(String title, String author, String year, List<String> genres, String imagePath, String description)

   + String getTitle()

   + String getAuthor()

   + String getYear()

   + List<String> getGenres()

   + String getImagePath()

   + String getDescription()

   + void setTitle(String title)

   + void setAuthor(String author)

   + void setYear(String year)

   + void setGenres(List<String> genres)

   + void setImagePath(String imagePath)

   + void setDescription(String description)

}


class AddBookListener {

   + void actionPerformed(ActionEvent e)

}


class EditBookListener {

   + void actionPerformed(ActionEvent e)

}


class DeleteBookListener {

   + void actionPerformed(ActionEvent e)

}


class BookDetailsPage {

   + BookDetailsPage(Book book)

}


LibraryManagerr "1" *-- "1..*" Book

LibraryManagerr "1" *-- "1" AddBookListener

LibraryManagerr "1" *-- "1" EditBookListener

LibraryManagerr "1" *-- "1" DeleteBookListener

LibraryManagerr "1" *-- "1" BookDetailsPage

Comments

Popular posts from this blog

Otomata Church Turing oleh Nadin Nabil Hafizh Ayyasy