Educoder-Java类和对象-图书管理系统

第1关:设计Book类

​ 本关任务:定义一个Book类,描述图书的信息。

/**
 * 要求定义一个Book类,描述图书的信息。
 每一本书都有以下信息:
 - 书名(title),String 类型
 - 作者(author), String 类型
 - 出版社(press ),String 类型
 - 书号(ISBN), String 类型
 - 定价(price), double 类型

 * @author DELL
 *
 */
public class Book {
    //留意这里的权限是private
    private String title;
    private String author;
    private String press;
    private String ISBN;
    private double price;

    public Book(String title, String author, String press, String ISBN, double price)
    {
        //***********Begin************
        //请在此处完成构造函数的内容
        this.title=title;
        this.author=author;
        this.press=press;
        this.ISBN=ISBN;
        this.price=price;

        //************End*************
    }
    //获取各个字段的值
    //【思考】为什么需要以下5个getter函数     如果不用getter函数就获取不了值,因为用了private修饰
    public String getTitle() {
        return title;
    }
    public String getAuthor() {
        return author;
    }

    //***********Begin************
    //仿造上面两个函数,写出getPress()、getPrice()、getISBN()函数
    public String getPress(){
        return press;
    }
    public String getISBN(){
        return ISBN;
    }
    public double getPrice(){
        return price;
    }

    //************End*************

    public void printInfo() {
        System.out.println(title + "\t" + author + "\t" + press + "\t"+ ISBN + "\t"+ price);
    }

    public static void main(String[] args) {

        //下面是参考代码,请认真阅读
        Book b1 = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
        //输出书本信息的方法一:
        System.out.println(b1.getTitle()+"\t"+b1.getAuthor()+"\t"
                +b1.getPress() + "\t"+b1.getISBN()+"\t"+b1.getPrice());

        //输出书本信息的方法二:
        b1.printInfo();

        System.out.println("-------------我是分割线---------------");

        Book[] booklist = new Book[5];
        booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
        booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社",  "9787145513853", 45.6);
        booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
        booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
        booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);

        //***********Begin************
        //仿造上面方法一,输出5本书的信息(记得使用循环)
        for (int i = 0; i < 5; i++) {
            System.out.println(booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"+booklist[i].getPress()+"\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
        }
        /*for(Book e:booklist){
            System.out.println(e.getTitle()+"\t"+e.getAuthor()+"\t"+e.getPress()+"\t"+e.getISBN()+"\t"+e.getPrice());
        }*/
        //************End*************

        System.out.println("-------------我是分割线---------------");

        //***********Begin************
        //仿造上面方法二,输出5本书的信息(记得使用循环)
        for(Book e:booklist){
            e.printInfo();
        }
        /*for(int i = 0; i<booklist.length; i++){
            booklist[i].printInfo();
        }*/
        //************End*************

    }
}

第2关:设计Library类

​ 本关任务:设计一个能管理图书的Library类

package Jc;

public class Book {

    private String title;
    private String author;
    private String press;
    private String ISBN;
    private double price;

    public Book(String title, String author, String press, String ISBN, double price)
    {
        this.title = title;
        this.author = author;
        this.press = press;
        this.ISBN = ISBN;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }
    public String getAuthor() {
        return author;
    }
    public String getPress() {
        return press;
    }
    public double getPrice() {
        return price;
    }
    public String getISBN() {
        return ISBN;
    }

    public void setBook(String title, String author, String press, String ISBN, double price) {
        this.title = title;
        this.author = author;
        this.press = press;
        this.ISBN = ISBN;
        this.price = price;
    }

    public void printInfo() {
        System.out.println(title + "\t" + author + "\t" + press + "\t"+ ISBN + "\t"+ price);
    }

}


import java.util.Scanner;

public class Library {

    public static final int SIZE = 100; //图书库最大藏书量
    Book booklist[]; //图书库
    int count;    //当前图书库的图书数量
    Scanner scan;

    public Library() {
        booklist = new Book[SIZE];
        //初始化图书库,默认里面有5本书,信息如下
        booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
        booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社",  "9787145513853", 45.6);
        booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
        booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
        booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);

        count = 5;
        scan = new Scanner(System.in);
    }

    public void run(){

        int userChoice = 0;
        //循环处理,直到用户选择了“0...退出”
        do{
            displayMenu();

            //读取用户输入
            userChoice = scan.nextInt();

            switch(userChoice){
                case 0:
                    System.out.println("成功退出系统,欢迎再次使用!");
                    break;
                case 1:
                    printAllBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    modifyBook();  //本次任务不要求实现
                    break;
                case 5:
                    findBook();       //本次任务不要求实现
                    break;
                default:
                    System.out.println("输入非法,请重新输入!");

            }
        }while(userChoice != 0);
        scan.close();
    }

    void displayMenu(){

        //打印菜单
        System.out.println("---------------------");
        System.out.println("    图书管理系统     ");
        System.out.println("---------------------");
        System.out.println("|   0...退出系统   |");
        System.out.println("|   1...显示图书   |");
        System.out.println("|   2...增加图书   |");
        System.out.println("|   3...删除图书   |");
        System.out.println("|   4...修改图书   |");
        System.out.println("|   5...查询图书   |");
        System.out.println("---------------------");
        System.out.print("请输入选项:");


    }

    void printAllBook(){ //循环打印所有的图书
        System.out.println("---------------------------------------------------------------------------");
        System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
        System.out.println("---------------------------------------------------------------------------");
        //***********Begin************
        //输出booklist数组中count本图书的信息,记得前面需要有个序号(序号从1开始)
        //每项信息请用"\t"隔开
        
        /*int i=1;
        for(Book e:booklist){
            System.out.print(i+"\t");e.printInfo();
            i++;
            if(i>count)break;
        }*/
        for (int i = 0; i < count; i++) {
            System.out.print((i+1)+"\t");booklist[i].printInfo();
        }

        //************End*************
        System.out.println("---------------------------------------------------------------------------");
    }

    void addBook(){  //增加图书
        if (count < SIZE){
            System.out.println("----------------------------------------------");

            System.out.print("请输入图书名:");
            String title = scan.next();

            //***********Begin************
            //仿造上两行代码,提示用户输入各项信息
            //创建一个图书对象,加入到booklist数组中
            System.out.print("请输入作者:");
            String author = scan.next();
            System.out.print("请输入出版社:");
            String press = scan.next();
            System.out.print("请输入ISBN:");
            String ISBN= scan.next();
            System.out.print("请输入单价:");
            double price = scan.nextDouble();

            //************End*************);

            booklist[count] =new Book(title,author,press,ISBN,price);
            count++;
            System.out.println("成功增加1本图书!当前图书库信息如下:");
            printAllBook();

        }
        else{
            System.out.println("图书库已满!");
        }
    }

    void deleteBook(){   //删除图书

        int id = -1;
        while(true){
            printAllBook();
            System.out.print("请输入序号删除图书,输入0返回主菜单: ");
            int userChoice = scan.nextInt();
            if(userChoice == 0){ //输入0跳出循环,即返回主菜单
                break;
            }
            else {
                //***********Begin************
                //删除用户选择的图书,并输出"删除成功!"
                //若输入有误,请提示:"输入错误!请重新输入!"
                id = findByOrder(userChoice);
                if(id != -1)   {
                    for(int j = id; j < count-1; j++){  //用for循环的形式实现对数组的删除
                        booklist[j] = booklist[j+1];
                    }
                    count --;
                    System.out.println("删除成功!");
                }

                else
                    System.out.println("输入错误!请重新输入!");
                //************End*************);
            }
        }
    }

    int findByOrder(int number){   //按序号(1~count)返回数组下标id
        if(number > 0 && number <= count ){
            return number - 1;
        }
        else
            return -1;
    }

    void modifyBook() {   //修改图书
        System.out.println("功能建设中...");
    }

    void findBook(){ //查询图书
        System.out.println("功能建设中...");
    }

    public static void main(String[] args) {
        Library myLibrary = new Library();
        //***********Begin************
        //调用run()方法
        myLibrary.run()    ;
        //************End*************);
    }
}

第3关:完成“修改图书”功能

​ 本关任务:完善Library类,完成“修改图书”功能

package Jc;

public class Book {

    private String title;
    private String author;
    private String press;
    private String ISBN;
    private double price;

    public Book(String title, String author, String press, String ISBN, double price)
    {
        this.title = title;
        this.author = author;
        this.press = press;
        this.ISBN = ISBN;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }
    public String getAuthor() {
        return author;
    }
    public String getPress() {
        return press;
    }
    public double getPrice() {
        return price;
    }
    public String getISBN() {
        return ISBN;
    }

    public void setBook(String title, String author, String press, String ISBN, double price) {
        this.title = this.title;
        this.author = this.author;
        this.press = this.press;
        this.ISBN = this.ISBN;
        this.price = this.price;
    }

    public void printInfo() {
        System.out.println(title + "\t" + author + "\t" + press + "\t"+ ISBN + "\t"+ price);
    }

}


package step3.question;

import java.util.Scanner;

public class Library {

    public static final int SIZE = 100; //图书库最大藏书量
    Book[] booklist; //图书库
    int count;		//当前图书库的图书数量
    Scanner scan;

    public Library() {
        booklist = new Book[SIZE];
        //初始化图书库,默认里面有5本书,信息如下
        booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
        booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社",  "9787145513853", 45.6);
        booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
        booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
        booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);

        count = 5;
        scan = new Scanner(System.in);
    }

    public void run(){

        int userChoice = 0;
        //循环处理,直到用户选择了“0...退出”
        do{
            displayMenu();

            //读取用户输入
            userChoice = scan.nextInt();

            switch(userChoice){
                case 0:
                    System.out.println("成功退出系统,欢迎再次使用!");
                    break;
                case 1:
                    printAllBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    modifyBook();
                    break;
                case 5:
                    findBook();
                    break;
                default:
                    System.out.println("输入非法,请重新输入!");

            }
        }while(userChoice != 0);
        scan.close();
    }

    void displayMenu(){

        //打印菜单

        System.out.println("---------------------");
        System.out.println("    图书管理系统     ");
        System.out.println("---------------------");
        System.out.println("|   0...退出系统	|");
        System.out.println("|   1...显示图书	|");
        System.out.println("|   2...增加图书	|");
        System.out.println("|   3...删除图书	|");
        System.out.println("|   4...修改图书	|");
        System.out.println("|   5...查询图书	|");
        System.out.println("---------------------");
        System.out.print("请输入选项:");
    }

    void addBook(){  //增加图书
        if (count < SIZE){
            System.out.println("----------------------------------------------");

            System.out.print("请输入图书名:");
            String title = scan.next();
            System.out.print("请输入作者:");
            String author = scan.next();
            System.out.print("请输入出版社:");
            String press = scan.next();
            System.out.print("请输入ISBN:");
            String ISBN = scan.next();
            System.out.print("请输入单价:");
            double price = scan.nextDouble();
            Book book = new Book(title,author,press,ISBN,price);
            booklist[count] = book;
            count++;
            System.out.println("成功增加1本图书!当前图书库信息如下:");

            printAllBook();

        }
        else{
            System.out.println("图书库已满!");
        }
    }

    void deleteBook(){   //删除图书

        int id = -1;
        while(true){
            printAllBook();
            System.out.print("请输入序号删除图书,输入0返回主菜单: ");
            int userChoice = scan.nextInt();
            if(userChoice == 0){
                break;
            }
            else {
                id = findByOrder(userChoice);
                if(id > -1)	{
                    for(int j = id; j < count-1; j++){  //用for循环的形式实现对数组的删除
                        booklist[j] = booklist[j+1];
                    }
                    count --;
                    System.out.println("删除成功!");
                }
                else
                    System.out.println("输入错误!请重新输入!");
            }
        }
    }

    void modifyBook() {   //修改图书
        while(true)	{
            printAllBook();

            System.out.print("请输入序号修改图书,输入0返回主菜单: ");
            int userChoice = scan.nextInt();
            int id = -1;
            if(userChoice == 0){
                break;
            }
            else {
                id = findByOrder(userChoice);
                //***********Begin************
                //调用modifyBookById修改第id本图书
                //若修改成功,输出"修改成功!当前图书信息如下:"并打印图书信息
                //若输入有误,请提示:"输入错误!请重新输入!"
                if(modifyBookById(id)){
                    System.out.println("修改成功!当前图书信息如下:");
                    booklist[id].printInfo();
                }else{
                    System.out.println("输入错误!请重新输入!");
                }
                //************End*************);

            }
        }
    }
    boolean modifyBookById(int id){  //修改第id本图书(id为数组下标,从0开始)

        if(id > -1 && id < count)	{
            System.out.println("图书信息如下:");
            booklist[id].printInfo();
            //此处获得图书的原各项信息
            String titleOld = booklist[id].getTitle();
            String authorOld = booklist[id].getAuthor();
            String pressOld = booklist[id].getPress();
            String ISBNOld = booklist[id].getISBN();
            double priceOld = booklist[id].getPrice();
            scan.nextLine();  //释放前一个回车

            System.out.print("请输入新书名(直接回车保持不变):");
            String title = scan.nextLine();
            title = title.isEmpty() ? titleOld : title;		//三目运算符进行判断
            //***********Begin************
            //仿造上面三行代码,提示用户输入其它信息
            //变量名请用author,press,ISBN
            System.out.print("请输入作者(直接回车保持不变):");
            String author = scan.nextLine();
            author= author.isEmpty() ? authorOld : author;
            System.out.print("请输入出版社(直接回车保持不变):");
            String press = scan.nextLine();
            press = press.isEmpty() ? pressOld : press;
            System.out.print("请输入ISBN(直接回车保持不变):");
            String ISBN = scan.nextLine();
            ISBN = ISBN.isEmpty() ? ISBNOld : ISBN;
            //************End*************);

            System.out.print("请输入单价(直接回车保持不变):");
            String strPrice = scan.nextLine();
            double price = strPrice.isEmpty()? priceOld : Double.valueOf(strPrice).doubleValue();

            //***********Begin************
            //调用Book类的setBook函数,把当前图书的信息重新设置为新信息
            booklist[id].setBook(title,author,press,ISBN,price);
            //************End*************);

            return true;
        }
        else {
            return false;
        }
    }
    void printAllBook(){ //循环打印所有的图书
        System.out.println("---------------------------------------------------------------------------");
        System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
        System.out.println("---------------------------------------------------------------------------");
        for (int i = 0; i < count; i++){
            System.out.println((i+1)+"\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"
                    +booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
        }
        System.out.println("---------------------------------------------------------------------------");
    }
    int findByOrder(int number){	//按序号(1~count)返回数组下标id

        if(number > 0 && number <= count ){

            return number - 1;
        }
        else
            return -1;
    }
    void findBook(){ //查询图书
        System.out.println("功能建设中...");
    }
    public static void main(String[] args) {
        Library myLibrary = new Library();
        myLibrary.run();
    }
}

第4关:完成“查询图书”功能

​ 本关任务:完善Library类,完成“查询图书”功能

package Jc;

public class Book {

    private String title;
    private String author;
    private String press;
    private String ISBN;
    private double price;

    public Book(String title, String author, String press, String ISBN, double price)
    {
        this.title = title;
        this.author = author;
        this.press = press;
        this.ISBN = ISBN;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }
    public String getAuthor() {
        return author;
    }
    public String getPress() {
        return press;
    }
    public double getPrice() {
        return price;
    }
    public String getISBN() {
        return ISBN;
    }

    public void setBook(String title, String author, String press, String ISBN, double price) {
        this.title = this.title;
        this.author = this.author;
        this.press = this.press;
        this.ISBN = this.ISBN;
        this.price = this.price;
    }

    public void printInfo() {
        System.out.println(title + "\t" + author + "\t" + press + "\t"+ ISBN + "\t"+ price);
    }

}


package Jc;

import java.util.Scanner;

public class Library {

    public static final int SIZE = 100; //图书库最大藏书量
    Book[] booklist; //图书库
    int count;		//当前图书库的图书数量
    Scanner scan;

    public Library() {
        booklist = new Book[SIZE];
        //初始化图书库,默认里面有5本书,信息如下
        booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
        booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社",  "9787145513853", 45.6);
        booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
        booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
        booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);

        count = 5;
        scan = new Scanner(System.in);
    }

    public void run(){

        int userChoice = 0;
        //循环处理,直到用户选择了“0...退出”
        do{
            displayMenu();

            //读取用户输入
            userChoice = scan.nextInt();

            switch(userChoice){
                case 0:
                    System.out.println("成功退出系统,欢迎再次使用!");
                    break;
                case 1:
                    printAllBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    modifyBook();
                    break;
                case 5:
                    findBook();
                    break;
                default:
                    System.out.println("输入非法,请重新输入!");

            }
        }while(userChoice != 0);
        scan.close();
    }

    void displayMenu(){

        //打印菜单
        System.out.println("---------------------");
        System.out.println("    图书管理系统     ");
        System.out.println("---------------------");
        System.out.println("|   0...退出系统	|");
        System.out.println("|   1...显示图书	|");
        System.out.println("|   2...增加图书	|");
        System.out.println("|   3...删除图书	|");
        System.out.println("|   4...修改图书	|");
        System.out.println("|   5...查询图书	|");
        System.out.println("---------------------");
        System.out.print("请输入选项:");
    }

    void printAllBook(){ //循环打印所有的图书
        System.out.println("---------------------------------------------------------------------------");
        System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
        System.out.println("---------------------------------------------------------------------------");
        for (int i = 0; i < count; i++){
            System.out.println((i+1)+"\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"
                    +booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
        }
        System.out.println("---------------------------------------------------------------------------");
    }

    void addBook(){  //增加图书
        if (count < SIZE){
            System.out.println("----------------------------------------------");

            System.out.print("请输入图书名:");
            String title = scan.next();
            System.out.print("请输入作者:");
            String author = scan.next();
            System.out.print("请输入出版社:");
            String press = scan.next();
            System.out.print("请输入ISBN:");
            String ISBN = scan.next();
            System.out.print("请输入单价:");
            double price = scan.nextDouble();
            Book book = new Book(title,author,press,ISBN,price);
            booklist[count] = book;
            count++;
            System.out.println("成功增加1本图书!当前图书库信息如下:");

            printAllBook();

        }
        else{
            System.out.println("图书库已满!");
        }
    }

    void deleteBook(){   //删除图书

        int id = -1;
        while(true){
            printAllBook();
            System.out.print("请输入序号删除图书,输入0返回主菜单: ");
            int userChoice = scan.nextInt();
            if(userChoice == 0){
                break;
            }
            else {
                id = findByOrder(userChoice);
                if(id > -1)	{
                    for(int j = id; j < count-1; j++){  //用for循环的形式实现对数组的删除
                        booklist[j] = booklist[j+1];
                    }
                    count --;
                    System.out.println("删除成功!");
                }
                else
                    System.out.println("输入错误!请重新输入!");
            }
        }
    }

    void modifyBook() {   //修改图书

        while(true)	{
            printAllBook();

            System.out.print("请输入序号修改图书,输入0返回主菜单: ");
            int userChoice = scan.nextInt();
            int id = -1;
            if(userChoice == 0){
                break;
            }
            else {
                id = findByOrder(userChoice);
                if(modifyBookById(id) == true) {
                    System.out.println("修改成功!当前图书信息如下:");
                    booklist[id].printInfo();;
                }
                else
                    System.out.println("输入错误!请重新输入!");
            }
        }
    }

    boolean modifyBookById(int id){  //修改第id本图书

        if(id > -1 && id < count)	{
            System.out.println("图书信息如下:");
            booklist[id].printInfo();

            String titleOld = booklist[id].getTitle();
            String authorOld = booklist[id].getAuthor();
            String pressOld = booklist[id].getPress();
            String ISBNOld = booklist[id].getISBN();
            double priceOld = booklist[id].getPrice();
            scan.nextLine();  //释放前一个回车

            System.out.print("请输入新书名(直接回车保持不变):");
            String title = scan.nextLine();
            title = title.isEmpty() ? titleOld : title;
            System.out.print("请输入作者(直接回车保持不变):");
            String author = scan.nextLine();
            author = author.isEmpty() ? authorOld : author;
            System.out.print("请输入出版社(直接回车保持不变):");
            String press = scan.nextLine();
            press = press.isEmpty() ? pressOld : press;
            System.out.print("请输入ISBN(直接回车保持不变):");
            String ISBN = scan.nextLine();
            ISBN = ISBN.isEmpty() ? ISBNOld : ISBN;
            System.out.print("请输入单价(直接回车保持不变):");
            String strPrice = scan.nextLine();
            double price = strPrice.isEmpty()? priceOld : Double.valueOf(strPrice).doubleValue();
            booklist[id].setBook(title,author,press,ISBN,price);

            return true;
        }
        else {

            return false;
        }
    }

    void findBook(){ //查询图书

        while(true){
            System.out.println("----------------------------------------------");
            System.out.println("请输入按哪种方法查找图书:0、返回主菜单    1、书名   2、作者名  ");
            int userChoice = scan.nextInt();
            if(userChoice == 0)	{
                break;
            }
            else if(userChoice >= 3) {
                System.out.println("输入错误,请重新输入!");
            }
            else {
                if(userChoice == 1) {
                    System.out.println("请输入您要查找的书名:");
                    String title = scan.next();
                    findByTitle(title);

                }
                else if(userChoice == 2) {
                    System.out.println("请输入您要查找的作者名:");
                    String author = scan.next();
                    findByAuthor(author);
                }
            }
        }
    }

    int findByOrder(int number){	//按序号(1~count)返回数组下标id

        if(number > 0 && number <= count ){

            return number - 1;
        }
        else
            return -1;
    }

    void findByTitle(String title){//按书名查找图书,返回id
        //***********Begin************
        //遍历booklist数组中进行查找,输出所有包含title的图书信息
        //如果一本都没找到,请输出"查找失败!"
        boolean found=false;
        for(int i=0;i<count;i++){

            if(booklist[i].getTitle().contains(title))  {
                booklist[i].printInfo();
                found = true;
            }

        }
        if(!found){
            System.out.println("查找失败!");
        }
        //************End*************
    }

    void findByAuthor(String author) {//按作者名查找图书,返回id
        //***********Begin************
        //遍历booklist数组中进行查找,输出所有作者名是author的图书信息
        //如果一本都没找到,请输出"查找失败!"
        int flag=0;
        for(int i=0;i<count;i++){
            if(booklist[i].getAuthor().contains(author)){
                booklist[i].printInfo();
                flag=1;
            }
        }
        if(flag!=1){
            System.out.println("查找失败!");
        }
        //************End*************
    }

    public static void main(String[] args) {

        Library myLibrary = new Library();
        myLibrary.run();
    }
}


posted @ 2021-07-29 15:53  颜骏  阅读(607)  评论(0编辑  收藏  举报