手记

JAVA第三季1-9(模拟借书系统)

Book父类:

package com.comit;

public class Book {
    protected int bookId;
    protected String bookName;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
}

EnglishBook子类:

package com.comit;

public class EnglishBook extends Book {

    public EnglishBook() {

        bookId = 1;
        bookName = "EnglishBook";

    }

    @Override
    public String toString() {
        return bookId + "." + bookName;
    }

}

Math子类:

package com.comit;

public class MathBook extends Book {
    public MathBook() {

        bookId = 2;
        bookName = "MathBook";

    }

    @Override
    public String toString() {
        return bookId + "." + bookName;
    }
}

BookRentalSystemInitial 主类:

package com.comit;

import java.util.InputMismatchException;
import java.util.Scanner;

public class BookRentalSystemInitial {
    public static void main(String[] args) {
        System.out.println("输入命令:1-按照序号查找图书;2-按照书名查找图书。");
        try {
            systemInitial();
        } catch (RuntimeException e) {
//          e.printStackTrace();
            System.out.println("命令数字输入有误,请根据提示输入数字命令!");
            main(null);
        }
    }

    //系统初始化
    public static void systemInitial() {
        Book[] book = { new EnglishBook(), new MathBook() };
        Scanner sn = new Scanner(System.in);
        try {
            int orderId = sn.nextInt();
            //选择借书查询方式
            if (orderId == 1) {
                findById(book);
            } else if (orderId == 2) {
                findByName(book);
            } else {
                //重新初始化系统
                main(null);
            }
        } catch (Exception e) {
            RuntimeException newExc = new RuntimeException("输入类型有误!");
            newExc.initCause(e);
            throw newExc;
        }
    }

    //按图书Id查询图书名称
    public static void findById(Book[] book) throws Exception {
        System.out.print("输入图书序号:");
        Scanner sn = new Scanner(System.in);
        int id = sn.nextInt();
        boolean mark = false;
        try {
            for (Book book2 : book) {
                if (book2.bookId == id) {
                    System.out.println("book:" + book2.bookName);
                    mark = true;
                    sn.close();
                }
            }
            if (!mark) {
                System.out.println("图书不存在!");
                main(null);
            }
        } catch (InputMismatchException e) {
            sn.close();
            throw new Exception("输入类型有误!");
        }
    }

    //按图书名称查询图书名称
    public static void findByName(Book[] book) throws Exception {
        System.out.print("输入图书名称:");
        Scanner sn = new Scanner(System.in);
        String name = sn.next();
        boolean mark = false;
        try {
            for (Book book2 : book) {
                if (book2.bookName.equals(name)) {
                    System.out.println("book:" + book2.bookName);
                    mark = true;
                    sn.close();
                }
            }
            if (!mark) {
                System.out.println("图书不存在!");
                main(null);
            }
        } catch (InputMismatchException e) {
            sn.close();
            throw new Exception("输入类型有误!");
        }
    }
}
33人推荐
随时随地看视频
慕课网APP

热门评论

main(null)真是神器啊!!!

sn.close();是什么意思


boolean mark =false; //为什么要加入一个布尔变量?
  try {
   for (Book book2 : book) {
    if (book2.bookId == id) {
     System.out.println("book:" + book2.bookName);
     mark =true;
     sn.close();//这句有什么作用?
     }
   }
   if (!mark) {
     System.out.println("图书不存在!");
     main(null);
     }
   
   }

查看全部评论