CodeHS如何按作者过滤书籍?

问题描述 投票:0回答:1

这是我的任务: 你已经得到了一份暑期阅读的书籍清单,但你只想阅读某个作者的书。

写一个方法

公共列表filterBooks(列表阅读列表,字符串作者) 它以书籍列表作为参数,从阅读列表中删除所有不是由给定作者编写的书籍,然后返回结果列表。

您可以通过调用 book.getAuthor() 来访问书籍的作者。提供Book类供参考。

有两节课...

Book.java

public class Book
{
    private String title;
    private String author;

    public Book(String theTitle, String theAuthor)
    {
        title = theTitle;
        author = theAuthor;
    }

    public String getTitle()
    {
        return title;
    }

    public String getAuthor()
    {
        return author;
    }

    public String toString()
    {
        return title + " by " + author;
    }

    public boolean equals(Book other)
    {
        return title.equals(other.title) && author.equals(other.author);
    }
}

单元测试

public List<Book> filterBooks(List<Book> readingList, String author)
{
    // Remove all Books from the readingList that are not
    // written by the given author. Return the resulting List
}

我不太确定如何解决这个问题...我知道可能需要解释很多东西,但我真的需要认真思考这种逻辑编码...非常感谢帮助!

java database unit-testing
1个回答
0
投票

作业要求您删除非指定作者的书籍。编写代码来检查作者是否不是所选作者,然后从列表中删除该条目。

© www.soinside.com 2019 - 2024. All rights reserved.