library management system


Submit solution

Points: 40 (partial)
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

設計一個簡單的圖書管理系統,系統中有兩種類型的書籍:電子書(EBook)和印刷書(PrintedBook)。你需要設計類別來表示這些書籍,並創建一個圖書館(Library)類別來管理這些書籍。

類別說明

  • Book 類別:

屬性:title(書名)、author(作者) 方法:getDetails(),返回書籍的詳細信息

  • EBook 類別(繼承自 Book):

屬性:fileSize(文件大小,MB)、format(格式,例如:PDF、EPUB) 方法:覆寫 getDetails() 方法,返回包含電子書詳細信息的字符串

  • PrintedBook 類別(繼承自 Book):

屬性:pageCount(頁數)、publisher(出版商) 方法:覆寫 getDetails() 方法,返回包含印刷書詳細信息的字符串

  • Library 類別:

屬性:books(書籍列表) 方法:addBook(Book book),將書籍添加到書籍列表中

  • showAllBooks(),顯示所有書籍的詳細信息

底下提供程式碼版型供同學快速上手

import java.util.*;

// Book 類別
class Book {
    String title;
    String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    String getDetails() {
        return "Title=" + title + ", Author=" + author;
    }
}

// EBook 類別,繼承自 Book
class EBook extends Book {
    double fileSize;
    String format;

    EBook(String title, String author, double fileSize, String format) {
        super(title, author);
        this.fileSize = fileSize;
        this.format = format;
    }

    @Override
    String getDetails() {
        /*
        輸入在此
        */
    }
}

// PrintedBook 類別,繼承自 Book
class PrintedBook extends Book {
    /*
    參考以上後輸入在此
    */
}

// Library 類別
class Library {
    List<Book> books;

    Library() {
        books = new ArrayList<>();
    }

    void addBook(Book book) {
        books.add(book);
    }

    void showAllBooks() {
        for (Book book : books) {
            System.out.println(book.getDetails());
            System.out.println();
        }
    }
}

// 主程式
public class t05311 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Library library = new Library();

        while (true) {
            int command = scanner.nextInt();
            if (command == 0) {
                break;
            }
            if (command == 1) {
                String title = scanner.next();
                String author = scanner.next();
                double fileSize = scanner.nextDouble();
                String format = scanner.next();
                library.addBook(new EBook(title, author, fileSize, format));
            } else if (command == 2) {
                String title = scanner.next();
                String author = scanner.next();
                int pageCount = scanner.nextInt();
                String publisher = scanner.next();
                library.addBook(new PrintedBook(title, author, pageCount, publisher));
            } else if (command == 3) {
                library.showAllBooks();
            }
        }
        scanner.close();
    }
}

輸入

輸入包含多行,每行代表一個操作:

  • 1 title author fileSize format:添加一個電子書,包含書名、作者、文件大小和格式

  • 2 title author pageCount publisher:添加一個印刷書,包含書名、作者、頁數和出版商

  • 3:顯示所有書籍的詳細信息

輸入以 0 結束。

輸出

輸出包含所有書籍的詳細信息,每本書之間用空行分隔。

測試資料輸入

1 Java_Programming James_Gosling 2.5 PDF
2 Clean_Code Robert_C._Martin 464 Prentice_Hall
3
0

測試資料輸出

EBook: Title=Java_Programming, Author=James_Gosling, File Size=2.5MB, Format=PDF

PrintedBook: Title=Clean_Code, Author=Robert_C._Martin, Page Count=464, Publisher=Prentice_Hall

Comments


  • 0
    scu09156146  commented on June 4, 2024, 3:46 p.m.

    題解

    參考課堂範例A266A294

    import java.util.*;
    
    //Book 類別
    class Book {
        String title;
        String author;
    
        Book(String title, String author) {
            this.title = title;
            this.author = author;
        }
    
        String getDetails() {
            return "Title=" + title + ", Author=" + author;
        }
    }
    
    //EBook 類別,繼承自 Book
    class EBook extends Book {
        double fileSize;
        String format;
    
        EBook(String title, String author, double fileSize, String format) {
            super(title, author);
            this.fileSize = fileSize;
            this.format = format;
        }
    
        @Override
        String getDetails() {
            return "EBook: " + super.getDetails() + ", File Size=" + fileSize + "MB, Format=" + format;
        }
    }
    
    //PrintedBook 類別,繼承自 Book
    class PrintedBook extends Book {
        int pageCount;
        String publisher;
    
        PrintedBook(String title, String author, int pageCount, String publisher) {
            super(title, author);
            this.pageCount = pageCount;
            this.publisher = publisher;
        }
    
        @Override
        String getDetails() {
            return "PrintedBook: " + super.getDetails() + ", Page Count=" + pageCount + ", Publisher=" + publisher;
        }
    }
    
    //Library 類別
    class Library {
        List<Book> books;
    
        Library() {
            books = new ArrayList<>();
        }
    
        void addBook(Book book) {
            books.add(book);
        }
    
        void showAllBooks() {
            for (Book book : books) {
                System.out.println(book.getDetails());
                System.out.println();
            }
        }
    }
    
    //主程式
    public class Book_SuperClass {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Library library = new Library();
    
            while (true) {
                int command = scanner.nextInt();
                if (command == 0) {
                    break;
                }
                if (command == 1) {
                    String title = scanner.next();
                    String author = scanner.next();
                    double fileSize = scanner.nextDouble();
                    String format = scanner.next();
                    library.addBook(new EBook(title, author, fileSize, format));
                } else if (command == 2) {
                    String title = scanner.next();
                    String author = scanner.next();
                    int pageCount = scanner.nextInt();
                    String publisher = scanner.next();
                    library.addBook(new PrintedBook(title, author, pageCount, publisher));
                } else if (command == 3) {
                    library.showAllBooks();
                }
            }
            scanner.close();
        }
    }