Searchable Bookstore


Submit solution

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

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

請撰寫一個書店管理程式,程式功能包括允許使用者新增書籍,依照關鍵字判斷書籍分類,
書店在收錄圖書時,只會收下書籍分類不為"Other"且價格大於0的書,
收錄完成後,會允許用戶根據類別與價格範圍列出符合條件的書籍,依照收錄順序排序,
若無符合的書籍,就顯示Not found

請利用以下的參考程式碼作為基礎,依照main()裡面的需求,補上BookStore這個類別,需填補的程式碼行數可能在30行以內。
若修改正確,當程式獲得以下每一組範例輸入時,應該產生該範例的對應輸出結果。

提示,BookStore這個類別應具備以下內容:

  • 需要一個變數 存放書籍的列表
  • 需要一個建構函數 初始化列表
  • 需要一個add()函數 允許新增書籍到列表中,新增書籍時,要利用Book類別提供的函數,檢查書籍「價格大於0」且「書籍分類不為"Other"」才能列入書庫
  • 需要一個searchForCategory()函數 根據指定書籍分類和價格範圍搜尋書籍,返回符合條件的書籍列表

在測試資料中,至少會有一本合格的書籍,且每一本書籍的標題至少有一個字。
書籍分類的結果以Book.getCategory()的回傳值為準。


範例輸入#1

Java Programming;500
Python Cookbook;300
Italian Cooking;250
Java Stories;150
World War II Biography;400
Programming;200;600

範例輸出#1

Title: Java Programming, Price: 500, Category: Programming
Title: Python Cookbook, Price: 300, Category: Programming

範例輸入#2

JavaScript Essentials;600
Mediterranean Adventure;501
Historical Figures;400
Sci-Fi Adventures;450
Mystery Tales;300
Fiction;200;500

範例輸出#2

Title: Sci-Fi Adventures, Price: 450, Category: Fiction

範例輸入#3

Java Programming;199
Italian Cooking;250
Java Stories;150
World War II Biography;400
Python Cookbook;601
Programming;200;600

範例輸出#3

Not found

參考程式碼
import java.util.ArrayList;
import java.util.Scanner;

class Book {
    private String title; // title of the book
    private int price; // price of the book

    public Book(String title, int price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public int getPrice() {
        return price;
    }

    public String toString() {
        return "Title: " + title + ", Price: " + price + ", Category: " + getCategory();
    }

    public String getCategory() {
        if (title.contains("Java") || title.contains("Python")) {
            return "Programming";
        } else if (title.contains("Cookbook") || title.contains("Food")) {
            return "Cooking";
        } else if (title.contains("Biography") || title.contains("Culture")) {
            return "History";
        } else if (title.contains("Adventure") || title.contains("Horror")) {
            return "Fiction";
        } else {
            return "Other";
        }
    }
}

public class MyProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // create a new BookStore object, where the library contains some books
        BookStore store = new BookStore();

        // Allow the user to add 5 books to the store
        for (int i = 1; i <= 5; i++) {
            // Example input: Java Programming;25
            // (title: Java Programming, price: 25)
            String input = scanner.nextLine();
            String title = input.split(";")[0];
            int price = Integer.parseInt(input.split(";")[1]);

            // create a new Book object with the given title and price
            store.add(new Book(title, price));
        }

        // Allow the user to search for books in a specific category and price range
        // Example input: Programming;20;30
        // (category: Programming, lower price: 20, upper price: 30)
        String input = scanner.nextLine();
        String category = input.split(";")[0];
        int lowerPrice = Integer.parseInt(input.split(";")[1]);
        int upperPrice = Integer.parseInt(input.split(";")[2]);

        ArrayList<Book> result = store.searchForCategory(category, lowerPrice, upperPrice);
        if (result.isEmpty()) {
            System.out.println("Not found");
        } else {
            for (Book book : result) {
                System.out.println(book);
            }
        }
    }
}

Comments

There are no comments at the moment.