//Main 方法
package www.baidu.com;
import java.util.*;
public class Main {
/**
* @param args
* @throws NoBookException
*/
//命令输入错误异常,
public void test1() throws WrongException{
throw new WrongException("命令输入错误!请根据提示输入数字命令");
}
// 图书不存在异常
public void test2() throws NoBookException {
throw new NoBookException("图书不存在");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
show [] V = new show[100];
V [0] = new show ("高数");
V [1] = new show ("数据结构");
V [2] = new show ("西游记");
V [3] = new show ("水浒传");
V [4] = new show ("三国演义");
V [5] = new show ("红楼梦");
V [6] = new show ("英语");
V [7] = new show ("JAVA");
V [8] = new show ("语言编译");
V [9] = new show ("三毛流浪记");
//// System.out.println(V[2].getName());
do{
Scanner input = new Scanner(System.in);
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
String a = input.next();
if(a.equals("1")){
boolean flag=false;
System.out.println("输入图书名称");
String bookname = input.next();
for(int i = 0; i < V.length;i++){
// System.out.println(V[i].getName());
if (bookname.equals(V[i].getName())){
System.out.println("book:"+bookname);
flag=true;
break;
}
}
if(flag==false){
Main input2 = new Main ();
try {
input2.test2();
} catch (NoBookException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}else if(a.equals("2")) {
boolean flage=false;
System.out.println("输入图书序号");
String b = input.next();
// System.out.println(V[b].getName());
for(int i = 0; i < V.length;i++){
if(b.equals(i+1)){
System.out.println(V[i].getName());
flage = true;
break;
}
}
if(flage==false){
Main input3 = new Main();
try {
input3.test2();
}catch (NoBookException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}else{
Main input2 = new Main();
try {
input2.test1();
} catch (WrongException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
continue;
}
}while (true);
}
}
//show.java 这个类是习惯写成的set和get
package www.baidu.com;
public class show{
private String name;
public show(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public show(String name) {
// TODO Auto-generated constructor stub
setName(name);
}
}
//NoBookException.java 继承了Exception
package www.baidu.com;
public class NoBookException extends Exception{
public NoBookException(){
}
public NoBookException (String str){
super(str);
}
}
//WrongException.java 继承了Exception,
package www.baidu.com;
public class WrongException extends Exception{
public WrongException (){
}
public WrongException (String str){
super(str);
}
}
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦