继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java第二季模拟租书系统

ruigenihao
关注TA
已关注
手记 6
粉丝 3
获赞 54

一共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){

        }

        }

    }

}

运行效果如图
图片描述

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

super(Message);

请问一下这句的含义是什么?

查看全部评论