1、
【问题描述】编写一个Book类,该类至少有name和price两个属性。该类要实现Comarable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等。
【输入形式】每一行输入一个Book类的对象,名字和价格之间用逗号分隔,集合类的输入结束符号是#,然后输入一个新的Book类对象。
【输出形式】
显示查找结果,如果找到了,就显示具体对象的信息,没找到,就不显示。
【样例输入1】
input several Book,in the end #
yuwen,10
shuxue,12
yingyu,11
input a new Book:
kexue,12
【样例输出1】
new book:as same books
shuxue,12.0
【样例输入2】
input several Book,in the end #
yuwen,10
shuxue,12
waiyu,11
input a new Book:
kexue,13
【样例输出2】
new book:as same books
package learn;
public class Book implements Comparable<Book>{
public String name;
public int price;
public Book(){
}
public Book(String name,int price){
this.name=name;
this.price=price;
}
@Override
public int compareTo(Book book){
return this.price-book.price;
}
@Override
public String toString(){
String r=String.format("%s,%.1f",name,(double)price);//格式化字符串
return r;
}
}
package learn;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("input several Book,in the end #"