查阅系统代码分享
package librarySearch;public class b1 extends Books { @Override public int num() { // TODO 自动生成的方法存根 return 1; } @Override public String title() { // TODO 自动生成的方法存根 return "红楼梦"; } }
package librarySearch;public class b1 extends Books { @Override public int num() { // TODO 自动生成的方法存根 return 1; } @Override public String title() { // TODO 自动生成的方法存根 return "红楼梦"; } }
2018-07-02
package librarySearch; import java.util.InputMismatchException; import java.util.Scanner; public class Search { static Books hlm = new b1(); static Books sgyy = new b2(); static Books shz = new b3(); static Books xyj = new b4(); final static Books [] bk= {hlm,sgyy,shz,xyj}; final static String [] bk2 = {"听说英语","精读英语","西方文化通论","大学语文","微积分","计算机程序设计基础","数据结构","线性代数","大学物理","电路分析","数字电路","概率统计"}; public class NoBookException extends Exception{ private static final long serialVersionUID = 1L; public NoBookException(){ } public NoBookException(String err){ super(err); } } public static void main (String [] args) throws InputMismatchException,NoBookException{ Search sch= new Search(); Scanner sc = new Scanner(System.in); outer: while (true){ try{ System.out.println("请输入你要查找的书库号: 1-四大名著 2-教科书 3-退出"); int i = sc.nextInt(); if (i==1){ System.out.println("请输入你要使用的查找方法: 1-按照名称查找图书 2-按照序号查找图书"); i = sc.nextInt(); if (i==1){ System.out.println("请输入图书名称"); String str = sc.next(); for ( int j = 0 ; j < 4 ; j++ ){ if (bk[j].title().equals(str)){ System.out.println("book:"+bk[j].title()); continue outer; } } throw sch.new NoBookException(); } else if (i==2){ System.out.println("请输入图书编号"); int num = sc.nextInt(); for ( int j = 0 ; j < 4 ; j++ ){ if (bk[j].num() == num){ System.out.println("book:"+bk[j].title()); continue outer; } } throw sch.new NoBookException(); } else{ throw new InputMismatchException(); } } else{ if (i==2){ System.out.println("请输入你要使用的查找方法: 1-按照名称查找图书 2-按照序号查找图书"); i = sc.nextInt(); if (i==1){ System.out.println("请输入图书名称"); String str = sc.next(); for ( int j = 0 ; j < 12 ; j++ ){ if (bk2[j].equals(str)){ System.out.println("book:"+bk2[j]); continue outer; } } throw sch.new NoBookException(); } else if (i==2){ System.out.println("请输入图书编号"); int num = sc.nextInt(); for ( int j = 0 ; j < 12 ; j++ ){ if (j == num-1){ System.out.println("book:"+bk2[j]); continue outer; } } throw sch.new NoBookException(); } else{ throw new InputMismatchException(); } } else{ if (i==3) break; else throw new InputMismatchException(); } } } catch(NoBookException e){ System.out.println("抱歉,书库中并没有这本书……"); sc.nextLine(); } catch(InputMismatchException e){ System.out.println("请输入正确的数据!"); sc.nextLine(); } catch(Exception e){ System.out.println("未知错误!"); sc.nextLine(); } } System.out.println("感谢使用本查阅系统,欢迎下次再来!"); sc.close(); } }
《Search.java》
举报