java第二季模拟租书系统

2019-06-15 00:00:00 模拟 第二季 租书

一共4个class,俩个异常类 一个功能类 一个main类
1.存在异常

package com.imooc.ruige;

public class ExistedException extends Exception {
    public ExistedException(String Message) {
        // TODO Auto-generated constructor stub
        super(Message);
    }
    public ExistedException() {
        System.out.println("图书不存在!");
    }
}

2.类型异常

package com.imooc.ruige;

public class TypeException extends Exception {
    public TypeException(String Message) {
        super(Message);
    }
    public TypeException() {
        System.out.println("命令输入有误!请根据提示输入数字命令!");
    }
}

3.功能类

package com.imooc.ruige;

import java.util.Scanner;

public class Book {
    String[] book ={"数据结构","Effective c","通信原理","信号与系统","高数"};
    Scanner in =new Scanner(System.in);
    int isFind;
    int orderNum;
    public void brobook () throws TypeException, ExistedException{

            isFind=0;
             System.out.println("输入命令:1-按照名称查找图书:2-按照序号查找图书");
            try{ orderNum=Integer.parseInt(in.next());}
            catch(Exception e){
                throw new TypeException();
            }
     if(orderNum==1){
                System.out.println("输入图书名称:"); 
            String name= in.next();

            for(String bookname:book){
                if(bookname.equals(name)){
                    isFind=1;
                    System.out.println("book:"+bookname);}
            }
            if(isFind==0)
                throw new ExistedException();

             }
      else if(orderNum==2){
                 System.out.println("输入图书序号");
                 int bookNum = in.nextInt();
                 if(0<=bookNum && bookNum<=book.length)
                     System.out.println("book:"+book[bookNum]);
                 else
                    throw new ExistedException();
                 } 
    }

             }

4.主类(main)

package com.imooc.ruige;

import java.util.Scanner;

public class BroBook {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Book book =new Book();
        while(true){
        try {
            book.brobook();
        } catch (TypeException e) {
            // TODO Auto-generated catch block

        }
        catch(ExistedException e){

        }
        catch(Exception e){

        }

        }

    }

}

运行效果如图
《java第二季模拟租书系统》

相关文章