为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过java一步实现租书系统

标签:
Java

package first;
import java.util.Scanner;

public class Hello {

public static void main(String[] args){
    String[] books = new String[5];
    books[0] = "高数";
    books[1] = "线代";
    books[2] = "英语";
    books[3] = "化学";
    books[4] = "历史";
    Hello h = new Hello();
    int num = h.welcome();
    while(true) {
        if (num == 1) {
            try {
                if (h.byName(books)){
                    break;
                }
            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("图书不存在!");
                num = h.welcome();
            }
        } else if (num == 2) {
            try {
                if (h.byNum(books)) {
                    break;
                }
            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("图书不存在!");
                num = h.welcome();
            }
        } else {
            System.out.println("NUM:"+num);
            break;
        }
    }

}

public int welcome(){
    System.out.println("输入命令: 1-按照名称查找图书 2-按照序号查找图书");
    Scanner sin = new Scanner(System.in);
    int num = 0;
    try {
        num =  sin.nextInt();   
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("命令错误!请根据提示输入数字命令!");
        num = welcome();
    }
    return num;
}
public boolean byName(String[] books) throws NotFoundException{
    Scanner sin = new Scanner(System.in);
    System.out.println("输入图书名称:");
    String name = sin.next();
    for (int i = 0; i < books.length;i++) {
        if (books[i].equals(name) == true) {
             System.out.println("book:"+name);
             return true;
        }
    }
    throw new NotFoundException("图书不存在!");

}
public boolean byNum(String[] books) throws NotFoundException{
    Scanner sin = new Scanner(System.in);
    System.out.println("输入图书序号:");
    int num;
    try {
        num = sin.nextInt();
    } catch (Exception e) {
        // TODO: handle exception
        throw new NotFoundException("图书不存在!");
    }

    if (num < 0 || num > books.length) {
        throw new NotFoundException("图书不存在!");
    }
    System.out.println("book:"+books[num]);
    return true;
}

}

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消