6-2 图书列表

构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。 Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。

构建一个书单类BookList,该类中用一个列表类对象存放书单,提供添加图书(addBook)、查找图书(searchBook)的函数
main函数从键盘输入多个Book添加到书单中,(添加时,提供书的名称、价格、作者、版本号),而后从键盘读入一本书,查找该列表对象中是否包含该书,若包含,输出”found: 该书在列表中的序号”,若不包含,输出“not found”,查找时,提供书的名称、作者、版本号。

输入描述:

添加书的个数
添加的书
查找的书

输出描述:

查找结果

裁判测试程序样例:

import java.util.Scanner;

/* 你的答案被嵌在这里 */

public class Main{
    
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        BookList bl = new BookList();
        int n = s.nextInt();
        for (int i=0; i<n;i++) {
            bl.addBook(new Book(s.next(),
                    s.nextInt(),
                    s.next(),
                    s.nextInt()));
        }
        bl.searchBook(new Book(s.next(),
                    0,
                    s.next(),s.nextInt()));
    }
}

输入样例:

在这里给出一组输入。例如:

2
ThinkingInJava
86
BruceEckel
4
CoreJava
95
CayS.Horstmann
10
CoreJava
CayS.Horstmann
10

输出样例:

在这里给出相应的输出。例如:

found: 1 

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Book{
    String name;
    int price;
    String author;
    int edition;
    public Book(String name, int price, String author, int edition){
        this.name=name;
        this.price=price;
        this.author=author;
        this.edition=edition;
    }
    @Override
    public String toString(){
        return "name: "+name+", price: "+price+", author: "+author+", edition: "+edition;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass()!= obj.getClass()) return false;
        /*// 如果传入的对象为 null 或者两个对象的类不同则返回 false*/
        Book other = (Book) obj;
        /*// 将传入的对象强制转换为 Book 类型*/
        return name.equalsIgnoreCase(other.name.replaceAll("\\s", "")) &&
                /*replaceAll("\\s", "")表示将字符串中所有的空白字符替换为空字符串(即删除空白字符)。例如,如果有一个字符串 "Java Book",经过replaceAll("\\s", "")操作后,会得到"JavaBook"*/
                /*name.equalsIgnoreCase(other.name.replaceAll("\\s", "")):比较两个对象的书名,先去除书名中的空格,然后不区分大小写进行比较。如果两个书名相同,则返回true,否则返回false*/
                author.equalsIgnoreCase(other.author.replaceAll("\\s", "")) &&
                edition == other.edition;
        /* // 比较两个 Book 对象的名称(忽略大小写并且去除空格后比较)、作者(忽略大小写并且去除空格后比较)以及版本号是否都相同,相同则返回 true,不同则返回 false*/
    }
}
class BookList{
    private List<Book> books=new ArrayList<>();
    public void addBook(Book book) {
        books.add(book); //列表增加对象的方法
    }

    public void searchBook(Book book) {
        for(int i=0;i<books.size();i++){
            if(books.get(i).equals(book)){
                System.out.println("found: "+i+" ");
                return;
            }
        }
        System.out.println("not found");
    }
}
public class Main{

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        BookList bl = new BookList();
        int n = s.nextInt();
        for (int i=0; i<n;i++) {
            bl.addBook(new Book(s.next(), s.nextInt(), s.next(), s.nextInt()));
        }
        bl.searchBook(new Book(s.next(), 0, s.next(),s.nextInt()));
    }
}

另一种解法

class Book{

    String name;
    int price;
    String author;
    int edition;

    public Book(String name,int price,String author,int edition){
        this.name=name;
        this.price=price;
        this.author=author;
        this.edition=edition;
    }//name: xxx, price: xxx, author: xxx, edition: xxx

    @Override
    public String toString(){
        return ("name: "+this.name+", price: "+this.price+", author: "+this.author+", edition: "+this.edition);
    }

    @Override
    public boolean equals(Object o){
        if(o==null)return false;
        Book b=(Book)o;
        if(b.name.equalsIgnoreCase(this.name)&&b.author.equalsIgnoreCase(this.author)&&b.edition==this.edition){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public int hashCode(){
        int result=11;
        result=result*31+this.name.hashCode();
        result=result*31+this.author.hashCode();
        result=result*31+this.edition;
        return result;
    }
}

class BookList{

    Book[] list;
    int count;
    public BookList(){
        list=new Book[999];
        count=0;
    }

    public void addBook(Book b){
        list[count++]=b;
    }

    public void searchBook(Book b){
        for(int i=0;i<count;i++){
            if(list[i].equals(b)==true){
                System.out.println("found: "+i);//found: 1
                return ;
            }
        }
        System.out.println("not found");
        return ;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值