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

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

慕粉4130242
关注TA
已关注
手记 1
粉丝 1
获赞 2
自定义异常类
package com.Borrowbooks;
public class BookException extends Exception{
    public BookException (String messg) {
    super(messg)    ;
    }
}
定义图书类
package com.Borrowbooks;

public class Books {
    private int id;
    private String bookName;
    public Books(int id,String bookName){
        this.setId(id);
        this.setBookName(bookName);
    }
    public int getId() {
        return id;
    }
    private void setId(int id) {
        this.id = id;
    }
    public String getBookName() {
        return bookName;
    }
    private void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String toString() {
        return getBookName();
    }   
}
主程序
package com.Borrowbooks;
import java.util.InputMismatchException;
import java.util.Scanner;
public class RunningStart {
    Scanner UseInput=new Scanner(System.in);
    Books[] books={new Books(1, "java"),
                       new Books(2, "C语言"),
                       new Books(3, "PHP"),
                       new Books(4, "数学"),
                       new Books(5, "语文"),
                       new Books(6, "历史"),
                       new Books(7, "化学"),
                       new Books(8, "地理"),
                       new Books(9, "生物") };
    private void start()throws BookException {
        System.out.println("***欢迎进入借书系统****\n查阅方式:1-按名称  2-按序号\n请输入数字已确认:");
        while (true) {
            boolean On$Off=true;            
            try {
                int seMode=UseInput.nextInt();
                if(seMode==1){
                    FindName findBooks=new FindName();
                    findBooks.start();    / /调用按名称查找
                }else if(seMode==2){
                    FindNum findNum=new FindNum();
                    findNum.start();    / /调用按ID查找
                }else {
                    throw new BookException("请输入数字1或2:");
                }            
            }catch (BookException e){
                System.out.println(e.toString());
                On$Off=false;
            }catch (InputMismatchException e) {
                On$Off=false;
                System.out.println(e.toString()+"请输有效数字:");
                UseInput.next();
            }catch (Exception e) {
                On$Off=false;
                System.out.println(e.toString());
            }
            if(On$Off){break;}
        }
    }
    public static void main(String[] args) {
        RunningStart runningStart=new RunningStart();
        try {
            runningStart.start();
        } catch (Exception e) {
            System.out.println(e);
    }
   }
}
按ID查找
package com.Borrowbooks;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FindNum {
    RunningStart runningStart=new RunningStart();
    Scanner useInput= new Scanner(System.in);
    public void start()throws BookException {
        System.out.println("请输入图书序列号:");
        while (true) {
            boolean on$off=true;
            try {
                int bookID=useInput.nextInt();
                if(bookID>runningStart.books.length){
                    throw new BookException("序列号超出范围,请重新输入:");
                }else if (bookID<=0){
                    throw new BookException("序列号不能是负数和零,请重新输入:");
                }else {
                    System.out.println("找到序号为"+bookID+"的图书:"
                                        +runningStart.books[bookID-1].getBookName()+"---请点击查看");
                    break;
                }
            } catch (InputMismatchException e) {
                on$off=false;
                   useInput.next();
                System.out.println(e.toString()+"请输有效数字:");
            }catch (BookException e) {
                on$off=false;
                System.out.println(e.toString());
            }catch (Exception e) {
                on$off=false;
                System.out.println(e.toString());
            }
            if (on$off) {break;}
        }
    }
}
按名称查找
package com.Borrowbooks;
import java.util.Scanner;
public class FindName {
    Scanner useScanner=new Scanner(System.in);
    RunningStart runningStart=new RunningStart();
    public void start()throws BookException{
        System.out.println("请输入图书名称:");
        while (true){
            String bookname=useScanner.next();
            try {
                for (int i = 0; i < runningStart.books.length; i++) {
                if(runningStart.books[i].getBookName().equalsIgnoreCase(bookname)){
                        System.out.println("已经找到第"+(i+1)+"本图书"+runningStart.books[i].getBookName()+"---请点击查看");
                        break;
        }else {
                        throw new BookException("图书不存在,请重新输入:");
                      }
                }
        } catch (BookException e) {
                System.out.println(e.toString());
            }
        }
    }
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

boolean on$off=true;
请问这是什么意思啊

查看全部评论