图书管理系统,有没有写的简单点的答案?
写了个马马虎虎的异常链:
package stu.exrise.demo;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Library {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Library lib=new Library();
String books[][]={{"100","童话镇"},{"101","托福单词"},{"102","历史百科全书"},{"103","JAVA基础教程"},{"104","游戏大全"}};
Scanner scan=new Scanner(System.in);
String num;
String name;
int a=1;
int b=1;
int c=1;
System.out.println("欢迎进入图书借书系统!");
while(a!=0){
a=0;
System.out.println("请选择查找图书的方式:1 书名查找 2图书序号查找");
int n=scan.nextInt();
int flag=-1;
switch(n){
case 1:
while(b!=0){
b=0;
System.out.println("请输入要查找的书名:");
name=scan.next();
for(int i=0;i<books.length;i++){
for(int j=0;j<books[i].length;j++){
if(books[i][j].equals(name)){
System.out.println("books:"+books[i][j]+"图书序号为:"+books[i][j-1]);
flag=1;
break;
}
}
if(flag==1){
break;
}
}
if(flag!=1){
lib.testHandle2();
b++;
}
}
break;
case 2:
while(c!=0){
c=0;
System.out.println("请输入要查找书的序号:");
num=scan.next();
for(int i=0;i<books.length;i++){
for(int j=0;j<books[i].length;j++){
if(books[i][j].equals(num)){
System.out.println("books:"+books[i][j+1]+"图书序号为:"+books[i][j]);
flag=1;
break;
}
}
if(flag==1){
break;
}
}
if(flag!=1){
lib.testHandle2();
c++;
}
}
break;
default:
lib.testHandle1();
a++;
continue;
}
}
}
public void test1() throws BookException{
throw new BookException("输入命名错误,请按要求的重新输入");
}
public void test2() throws BookException{
throw new BookException("图书不存在,请重新输入");
}
public void testHandle1() throws BookException{
try{
test1();
}catch(BookException e){
//e.getCause();
//System.out.println(e);
};
}
public void testHandle2() throws BookException{
try{
test2();
}catch(BookException e){
//throw e;
};
}
}
/*
* 定义异常
*/
class BookException extends Exception{
BookException(){};
BookException(String message){
System.out.println(message);
};
}
求大神,有没有用异常链写的,或者写的简单些的?
写这段代码的时候只有上帝和我知道什么意思。 现在,只有上帝知道。
package com.imooc;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String[] s = {"唐诗300首","格林童话","java编程思想","计算机操作系统"};
System.out.println("欢迎来到图书管理系统");
while(true) {
int number = 0;
while(true) {
System.out.println("选择菜单 :输入1:进入书名查找模式 输入2:进入图书序列号查找模式");
Scanner sc = new Scanner(System.in);
try{
number = sc.nextInt();
}catch(Exception a) {
System.out.println("错误命令异常,请重新输入");
sc = null;
continue;
}
if(number >=1 && number <=2) {
break;
}else {
System.out.println("没有该选项!请重新输入");
}
}
if(number == 1) {
while(true){
System.out.print("请输入要查找的书名:");
Scanner sc2 = new Scanner(System.in);
String book = null;
try{
book = sc2.nextLine();
}catch(Exception b) {
System.out.println("错误命令异常,请重新输入!");
sc2 = null;
continue;
}
boolean b = true;
for(int x=0;x<s.length;x++) {
if(s[x].equals(book)) {
System.out.println(s[x]+"这本书存在!");
b = false;
break;
}
}
if(b) {
System.out.println(book+"这本书不存在");
System.out.println("是否重新输入:是/否");
Scanner sc5 = new Scanner(System.in);
if(sc5.nextLine().equals("是")) {
continue;
}
}
break;
}
}else if(number == 2) {
while(true) {
System.out.print("请输入要查找的序列号:");
int y;
Scanner sc4 = new Scanner(System.in);
try{
y = sc4.nextInt();
}catch(Exception c) {
System.out.println("错误命令异常,请重新输入");
continue;
}
boolean flag = false;
for(int x=0;x<s.length;x++) {
try{
if(s[x].equals(s[y])) {
System.out.println("序列号"+y+"为"+s[x]+"这本书存在!");
System.out.println("是否继续查询:是/否");
Scanner sc6 = new Scanner(System.in);
String choose3 = sc6.nextLine();
if(choose3.equals("是")) {
break;
}else if(choose3.equals("否")){
flag = true;
}
break;
}
}catch(Exception e) {
System.out.println("图书不存在异常!");
System.out.println("是否重新输入:是/否");
Scanner sc5 = new Scanner(System.in);
String choose2 = sc5.nextLine();
if(choose2.equals("是")) {
break;
}else if(choose2.equals("否")){
flag = true;
break;
}
}
}
if(flag) {
break;
}
}
}
Scanner i = new Scanner(System.in);
String choose = null;
while(true) {
System.out.println("是否返回选择菜单? 是/否");
try{
choose = i.nextLine();
break;
}catch(Exception e) {
System.out.println("输入的数值异常,请重新输入");
i = null;
continue;
}
}
if(!choose.equals("是")) {
break;
}
}
}
}尴尬,没有。。。。