Java入门第三季1-9模拟借书系统

父类Book

package com.imooc;

public class Book {
    protected String name;
    protected int number;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }

}

子类English

package com.imooc;

public class English extends Book{
    public English(String newname,int newnumber){
        name = newname;
        number = newnumber;
    }

}

子类social

package com.imooc;

public class Social extends Book{
    public Social(String newname,int newnumber){
        name = newname;
        number = newnumber;

    }

}

测试

package com.imooc;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {
        public static void main(String[] args) {
            Book[] lib ={new English("六级词汇", 1),new English("托福考试", 2),new Social("社会工作导论", 3),new Social("社会保障概论", 4)};
            System.out.println("欢迎来到图书馆借书系统,按书名搜索请按1,按图书序号搜索请按2");
            Scanner scan = new Scanner(System.in);
            try{
                int orderID = scan.nextInt();

                if(orderID == 1){
                    nameSerch(lib);
                    scan.close();
                }
                else if(orderID ==2){
                    numberSerch(lib);
                    scan.close();
                }
                else{
                    main(null);
                }
            }catch (Exception e){
                    RuntimeException newExc = new RuntimeException("输入有误!请根据提示输入有效的数字命令!");
                    newExc.initCause(e);
                    throw newExc;
            }
        }

        public static void nameSerch(Book[] lib ) throws Exception{
            Scanner scan2 = new Scanner(System.in);
            System.out.println("请输入书名进行搜索");
            String input2 = scan2.next();
            boolean mark = false;
            try{
                for(Book lib2 : lib){
                    if(lib2.name.equals(input2)){
                        System.out.println("您搜索的书为:"+lib2.name);
                        mark = true;
                    scan2.close();
                    }
                }
                if(!mark){
                    System.out.println("您输入的书名有误或者图书馆无此书");
                    main(null);
                }
            }catch(InputMismatchException e){
                scan2.close();
                throw new Exception("输入类型有误!");
            }

        }

        public static void numberSerch(Book[] lib) throws Exception{
            Scanner scan3 = new Scanner(System.in);
            System.out.println("请输入书号进行搜索");
            int input3 = scan3.nextInt();
            boolean mark2 = false;
            try{
                for(Book lib3 : lib){
                    if(lib3.number == input3){
                        System.out.println("您搜索的书名为:"+lib3.name);
                        mark2= true;
                    scan3.close();
                    }
                }
                if(!mark2){
                    System.out.println("您输入的号码有误或者图书馆无此书");
                    main(null);
                }
            }catch(InputMismatchException e){
                scan3.close();
                throw new Exception("输入类型有误");
            }
        }
}
点赞