Evaluasi Akhir Semester

Nama: Thopaz Givangkara Rosadi

NRP: 5025231050

Kelas: PBO (G) 

Source Code: https://github.com/rsthopaz/FP_PBO.git


Soal:

1. Apa yang dimaksud dengan Inheritance dalam Java. Bagaimana implementasi inheritance dalam Final Project yang sedang dikerjakan.

2. Jelaskan fitur Aplikasi yang ada dalam Final Project

3. Buatlah desain Diagram Kelas dari aplikasi Final Project


Jawaban:

1. Pewarisan (inheritance) adalah salah satu konsep dasar dalam pemrograman berorientasi objek (OOP) yang memungkinkan sebuah kelas (kelas turunan atau subclass) untuk mewarisi atribut dan metode dari kelas lain (kelas induk atau superclass). Dengan pewarisan, kita dapat menciptakan hierarki kelas yang lebih terstruktur dan meminimalkan pengulangan kode. Implementasi inhertance dalam FP yang kelompok kami kerjakan menggunakan library JFrame untuk menampilkan tampilan 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 Java Swing yang menyediakan jendela utama untuk aplikasi grafis berbasis GUI. Kelas ini digunakan untuk membuat antarmuka pengguna berbasis jendela yang dapat menampilkan komponen grafis seperti tombol, label, dan lainnya.

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


2. Fitur yang ada meliputi

Searching Data

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 by Genre and A-Z

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;
    }


CRUD (Create, Read, Update, and Delete)

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




@startuml


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


@enduml



Komentar

Postingan populer dari blog ini

The Busy Beaver Problem

Tugas 15 - Final Project

Church-Turing Thesis dan Kaitannya dengan Bahasa Pemrograman