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

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

爱芬芬
关注TA
已关注
手记 1
粉丝 0
获赞 19

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;
}

}

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