Description
构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。 Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。
Input
两本书信息
Output
两本书的打印信息 两本书是否相等
Sample Input
ThinkingInJava 86 BruceEckel 4 CoreJava 95 CayS.Horstmann 10
Sample Output
name: ThinkingInJava, price: 86, author: BruceEckel, edition: 4 name: CoreJava, price: 95, author: CayS.Horstmann, edition: 10 false
submit
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Book b1 = new Book(s.next(),
s.nextInt(),
s.next(),
s.nextInt());
Book b2 = new Book(s.next(),s.nextInt(),s.next(),s.nextInt());
System.out.println(b1);
System.out.println(b2);
System.out.println(b1.equals(b2));
}
}
class Book{
public String name;
public int price;
public String author;
public int edition;
public Book(String n, int p, String a, int e){
name = n;
price = p;
author = a;
edition = e;
}
public String toString() {
return "name: "+name+", price: "+price+", author: "+author+", edition: "+edition;
}
public boolean equals(Book b) {
if(name.equalsIgnoreCase(b.name) && author.equalsIgnoreCase(b.author) && edition == b.edition) {
return true;
}
return false;
}
}