我正在尝试获取 getBooksByAuthor 方法的输出,但它没有返回任何其他方法正在运行的内容
我需要一个不更改 getBooksByAuthor 方法而仅通过更改 Book 类的解决方案。
import java.util.*;
class Book {
// this class can modified if necessary, including the class definition
private String title;
private String author;
private double price;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
}
class Bookstore {
List<Book> books = new ArrayList<Book>();
public Bookstore(List<Book> books) {
this.books = books;
}
public List<Book> getBooksSortedByPrice(int order) {
if(order==0){
for(double outer=0; outer < books.size()-1; outer++)
{
for(double inner=outer; inner < books.size(); inner++)
{
if(Integer.toString((int) books.get((int) outer).getPrice()).compareTo(Integer.toString((int)books.get((int) inner).getPrice()))>0) {
Book temp = books.get((int) outer);
books.set((int) outer, books.get((int) inner));
books.set((int) inner, temp);
}
}
}}
if(order==1){
for(double outer=0; outer < books.size()-1; outer++)
{
for(double inner=outer; inner < books.size(); inner++)
{
if(Integer.toString((int) books.get((int) outer).getPrice()).compareTo(Integer.toString((int) books.get((int) inner).getPrice()))<0) {
Book temp = books.get((int) outer);
books.set((int) outer, books.get((int) inner));
books.set((int) inner, temp);
}
}
}}
return books;
}
public List<Book> getBooksSortedByTitle(int order) {
if(order==0){
for(int outer=0; outer < books.size()-1; outer++)
{
for(int inner=outer; inner < books.size(); inner++)
{
if(books.get(outer).getTitle().compareTo(books.get(inner).getTitle())>0) {
Book temp = books.get(outer);
books.set(outer, books.get(inner));
books.set(inner, temp);
}
}
}}
if(order==1){
System.out.println("isu = ");
for(int outer=0; outer < books.size()-1; outer++)
{
for(int inner=outer; inner < books.size(); inner++)
{
if(books.get(outer).getTitle().compareTo(books.get(inner).getTitle())<0) {
Book temp = books.get(outer);
books.set(outer, books.get(inner));
books.set(inner, temp);
}
}
}}
return books;
}
public List<Book> getBooksByAuthor(String author) {
// this method returns the list of books for a given author
// do not make any changes to this method
// change the Book class to make sure this method will work correctly
List<Book> result = new ArrayList<Book>();
Book helperBook = new Book("Dummy", author, 0.0);
for (Book b : books) {
if (b.equals(helperBook)) {
result.add(b);
}
}
return result;
}
}
public class Q2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
List<Book> book = new ArrayList<Book>();
book.add(new Book("Java Programming", "Joyce Farrell", 881.0));
book.add( new Book("Team Of Rivals", "Dorris Kearns Goodwin", 994));
book.add (new Book("1776", "Daivd McCullough", 400));
book.add (new Book("No Ordinary Time", "Dorris Kearns Goodwin", 768));
book.add( new Book("Steve Jobs", "Walter Isaacson", 656));
book.add( new Book("Dummy","Walter",0.0));
book.add( new Book("Dummy","Walter",0.0));
Bookstore df=new Bookstore(book);
List<Book> booksSorted = df.getBooksByAuthor("Walter");
for (Book number : booksSorted) {
System.out.println( number.getPrice()+"--as--"+number.getTitle()+"----"+number.getAuthor());
}
}
}
最佳答案
您应该通过实现 IComparable 将实体 Book
添加为 Comparable。
class Book implements IComparable {
您还应该指定您想要比较的内容。
因此,请将此方法添加到您的 Book
类中。
public int compareTo(object obj) {
Book objBook = (Book)obj;
return this.author.equals(objBook.author) ? 0 : -1;
}
这不是 compareTo
类的良好实现,但由于老师的奇怪要求,它已经足够了。
关于java - 更正 Book 类以获取 getBooksByAuthor 方法的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27439089/