181 - 图书类

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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值