1.Book类
class Book {
final int id;
final String name;
public Book(int id,String name){
this.id = id;
this.name = name;
}
}
2.bookUnfoundException类
class bookUnfoundException extends Exception {
public bookUnfoundException(){}
public bookUnfoundException(String message){
super(message);
}
}
3.commandNotFoundException类
class commandNotFoundException extends Exception {
public commandNotFoundException(){}
public commandNotFoundException(String message){
super(message);
}
}
4.Main类
import java.util.*;
public class Main {
private final Book[] library = {new Book(1, "高等数学"), new Book(2, "线性代数"), new Book(3,"大学英语"), new Book(4, "C程序设计"), new Book(5, "Java程序设计"), new Book(6, "市场营销学")};
public static void main(String[] args) throws commandNotFoundException, bookUnfoundException {
Main main = new Main();
main.searchEngine();
}
private void searchEngine() throws commandNotFoundException, bookUnfoundException {
Main main = new Main();
System.out.println("*********欢迎您使用借书系统*********");
System.out.println("本系统可实现以下功能");
System.out.println("1.按照名称查询图书");
System.out.println("2.按照序号查找图书");
System.out.print("请输入您要使用的功能:");
int choose = main.getChoose();
switch (choose) {
case 1: {
main.sortByName();
break;
}
case 2: {
main.sortByID();
break;
}
}
}
private int getChoose() throws commandNotFoundException, bookUnfoundException {
Main main = new Main();
Scanner input = new Scanner(System.in);
try {
int choose = input.nextInt();
if(choose != 1 && choose != 2) {
throw new commandNotFoundException();
}
return choose;
}catch(InputMismatchException e){
System.out.println("命令输入错误(您需要输入数字),请重新输入");
main.searchEngine();
}catch(commandNotFoundException e){
System.out.println("命令输入错误(命令不存在),请重新输入");
main.searchEngine();
}
return 0;
}
private void sortByName() throws bookUnfoundException, commandNotFoundException {
Main main = new Main();
Scanner input = new Scanner(System.in);
System.out.print("请输入您要查询的书籍名称:");
try {
String search = input.next();
int i;
for(i = 0; i < library.length;i++) {
if (search.equals(library[i].name)) {//比较两个字符串不能使用==
break;
}
}
if(i == library.length){
throw new bookUnfoundException();
}
System.out.println("已找到您需要的图书");
System.out.println("序号:"+library[i].id);
System.out.println("名称:"+library[i].name);
System.out.println("谢谢您的使用!");
main.searchEngine();
} catch (bookUnfoundException e) {
System.out.println("很抱歉,没有找到您需要的书籍。");
main.searchEngine();
}
}
private void sortByID() throws bookUnfoundException,commandNotFoundException{
Main main = new Main();
Scanner input = new Scanner(System.in);
System.out.print("请输入您要查询的书籍的序号:");
try {
int choose = input.nextInt();
if(choose > library.length){
throw new bookUnfoundException();
}
System.out.println("已找到您需要的图书");
System.out.println("序号:"+library[choose-1].id);
System.out.println("名称:"+library[choose-1].name);
System.out.println("谢谢您的使用!");
main.searchEngine();
}catch(InputMismatchException e){
System.out.println("您的输入有误(您需要输入数字),请重新输入");
main.searchEngine();
}catch(bookUnfoundException e){
System.out.println("很抱歉,没有找到您需要的书籍。");
main.searchEngine();
}
}
}
热门评论
我有点愚见。。就是我个人觉得在定义Book的时候加上get和set和方法后面取值会方便很多