Simple Book Search


Submit solution

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

Authors:
Problem type

題目說明

本題已提供 BookBookStore 類別,請撰寫主程式完成下列功能:

使用者會輸入多筆書籍資料(書名 + 價格),請加入 BookStore 中。

接著輸入一個書名作為查詢關鍵字,請檢查該書名是否完全與某本書一致。

若該書存在,請輸出其資訊(Title: 書名, Price: 價格),否則輸出 The book is not in the store.

程式模板

import java.util.ArrayList;
import java.util.Scanner;

class Book {
    private String title;
    private int price;

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

    public String getTitle() {
        return title;
    }

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

class BookStore {
    private ArrayList<Book> books = new ArrayList<>();

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

    public Book find(String title) {
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                return book;
            }
        }
        return null;
    }
}

public class MyProgram {
    public static void main(String[] args) {
        // TODO: 請完成主程式
    }
}

輸入

第一行輸入一個整數 n,表示書的數量。

接下來 n 行,每行輸入一筆書籍資料:書名(不含空格)與 價格(整數)。

最後一行輸入一個書名,作為查詢用的關鍵字。

輸出

若查詢書籍存在,請印出該書籍資訊 格式:Title: 書名, Price: 價格

否則,輸出:The book is not in the store.

範例輸入 #1

3
JavaBasics 100
PythonCookbook 120
CppPrimer 80
PythonCookbook

範例輸出 #1

Title: PythonCookbook, Price: 120

說明:說明:查詢書名完全一致,成功找到書籍。

範例輸入 #2

2
Java101 90
PythonIntro 110
Java

範例輸出 #2

The book is not in the store.

Comments

There are no comments at the moment.