一个借书系统,主要是使用异常来对借书时的情况进行把控。
基本可以跑。写的还是有点乱。
不足的地方还是很多的,希望大家指教,代码如下:
package com.Library;
import java.util.*;
public class BookNewSystem {
final Book[] books={new Book(1,"数据结构"),new Book(2,"高数"),new Book(3,"C语言")};
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BookNewSystem BNS = new BookNewSystem();
while(true){
System.out.println("输入命令:1-按照书名查找图书;2-按照序号查找图书");
try{
int input_number = inputNumber();
switch(input_number){
case 1:
System.out.println("输入图书名称:");
String bookname = input.next();
String temp = BNS.FindBookByName(bookname);
// BNS.FindBookByName(bookname);
System.out.println("book:"+bookname);
break;
case 2:
BNS.FindBookByOrder();
// System.out.println("输入图书序号:");
// int booknumber = inputNumber();
break;
default:
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
break;
}
catch(Exception me){
System.out.println(me.getMessage());
continue;
}
}
}
int FindBookByOrder(){
int j=0;
while(true){
System.out.println("输入图书序号:");
int booknumber = inputNumber();
if(booknumber==-1){
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
else{
if(booknumber>books.length){
System.out.println("图书不存在");
break;
}
else{
System.out.println("book:"+books[booknumber-1].BookName);
break;
}
}
}
return 0;
}
String FindBookByName(String name) throws Exception {
int i=0;
for(;i<books.length;++i){
if(name.equals(books[i].BookName))
return name;
}
throw new Exception("图书真的不存在!"+name);
}
static int inputNumber(){
int order;
Scanner inp = new Scanner(System.in);
try{
order = inp.nextInt();
return order;
}
catch(Exception e){
return -1;
}
}
}