小码哥杯java程序比赛复习(八)异常与断言(下)

阶段练习--模拟借书系统

要求:

1.定义字符串数组保存图书信息;

2.提示用户输入,分别按"书名"和"图书序号"查找图书;

3.根据输入信息进行适当的异常处理

  a.如果输入类型错误,抛出"错误命令异常",并提示重新输入;

  b.如果书名不存在,抛出"图书不存在异常",并提示重新输入;

  c.如果图书序号超过字符串数组范围,抛出"图书不存在异常",并提示重新输入;

  博主把慕课上老师的代码和自己的做了下对比,有不同的意见欢迎大家拍砖:

  慕课上老师的代码

  

<span style="font-family:SimSun;font-size:18px;">package ExceptionTest;
import java.util.Scanner;
public class BookManagerEasy {
	private static Scanner console = new Scanner(System.in);

	public static void main(String[] args) {
		//定义”图书“数组
		String[] books = { "C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概" };
		while (true) {
			System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
			String book;
			try {
				//取得整型命令
				int command = inputCommand();
				//根据不同命令值,进行不同操作
				switch (command) {
				case 1://按照图书名称选择图书
					book = getBookByName(books);
					System.out.println("book:" + book);
					break;
				case 2://按照图书序号(数组下标)选择图书
					book = getBookByNumber(books);
					System.out.println("book:" + book);
					break;
				case -1://返回值为-1,说明输入有误
					System.out.println("命令输入错误!请根据提示输入数字命令!");
					continue;
				default://其他值的命令均认为是错误命令
					System.out.println("命令输入错误!");
					continue;
				}
				break;//退出程序
			} catch (Exception bne) {
				//捕获”图书不存在异常“时,要求重新输入命令
				System.out.println(bne.getMessage());
				continue;
			} 
		}
	}

	//按照图书名称查询图书
	private static String getBookByName(String[] books)
			throws Exception {
		System.out.println("输入图书名称:");
		//获取输入的图书名称
		String name = console.next();
		for (int i = 0; i < books.length; i++) {
			if (name.equals(books[i]))
				//输入的名称与某一图书名称匹配,返回该图书
				return books[i];
		}
		//若无匹配,抛出”图书不存在异常“
		throw new Exception("图书不存在!");
	}

	//根据图书序号(数组下标)查询图书
	private static String getBookByNumber(String[] books)
			throws Exception {
		while (true) {
			System.out.println("输入图书序号:");
			try {
				//获取输入的图书序号(数组下标)
				int index = inputCommand();
				//若返回值为-1
				if(index == -1){
					System.out.println("命令输入错误!请根据提示输入数字命令!");
					continue;
				}
				//若不出现”数组下标越界异常“,则返回相应位置的图书
				String book = books[index];
				return book;
			} catch (ArrayIndexOutOfBoundsException e) {
				//输入的序号不存在(引发”数组下标越界异常“),则抛出”图书不存在异常“
				Exception bookNotExists = new Exception("图书不存在!");
				bookNotExists.initCause(e);
				throw bookNotExists;
			}
		}
	}

	//从控制台输入命令,用于输入命令和输入图书序号
	private static int inputCommand(){
		int command;
		try {
			command = console.nextInt();
			return command;
		} catch (Exception e) {
			//若输入字符型或者字符串,则抛出异常,捕获该异常,抛出”错误命令异常“
			console = new Scanner(System.in);
			//返回-1
			return -1;
		}
	}
}</span><span style="font-family:KaiTi_GB2312;font-size:24px;">
</span>

    博主的案例:

Book类:

package BookManger;

public class Book {
	protected int bookId;
	protected String bookName;
	public Book(int bookId,String bookName){
		this.bookId = bookId;
		this.bookName = bookName;
	}
	public int getBookId() {
		return bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	@Override
	public String toString() {
		return "书号" + bookId + ", 书名: " + bookName;
	}
}

CompturBook类:

package BookManger;

public class CompturBook extends Book{
	public CompturBook(int bookId,String bookName){
		super(bookId,bookName);
	}
	
	@Override
	public String toString() {
		return "书号" + bookId + ", 书名: " + bookName;
	}
	
}

MathBook 类:

package BookManger;

public class MathBook extends Book{
	public MathBook(int bookId,String bookName){
		super(bookId,bookName);
	}
	
	public String toString() {
		return "书号" + bookId + ", 书名: " + bookName;
	}
}

BookMangerEasy类(主类):

package BookManger;
import java.util.*;
public class BookMangerEasy {
	private static Scanner console = new Scanner(System.in);
	public static void main(String[] args){
		System.out.println("输入命令:1.按照书名查找图书;2.按照序号查找图书");
		try{
			sysInital();
		}catch(RuntimeException e){
			e.printStackTrace();
			System.out.println("命令数字输入有误,请根据提示输入数字命令!");
			main(null);
		}
	}
	
	//系统初始化
	public static void sysInital(){
		Book[] book = {new CompturBook(1,"疯狂java"),new MathBook(2,"汤家凤高数")};
		try{
			int command = inputCommand();
			//选择借书查询方式
			//根据不同命令值,进行不同操作
			switch (command) {
			case 1://按照图书名称选择图书
				getBookByName(book);
				break;
			case 2://按照图书序号(数组下标)选择图书
				getBookByNumber(book);
				break;
			case -1: //返回值为-1,说明输入有误
				System.out.println("命令输入错误!请根据提示输入数字命令!");
			default://其他值的命令均认为是错误命令
				System.out.println("命令输入错误!");
				main(null);
			}
		}catch(Exception e){
			RuntimeException newExc = new RuntimeException(e);
			throw newExc;
		}
	}
	
	//按照图书的序号去查询图书
	public static void getBookByNumber(Book[] book) throws Exception {
		System.out.print("输入图书序号:"); 
		int id = inputCommand();
		boolean mark = false; 
		try { for (Book book2 : book) { 
			if (book2.bookId == id) {
				System.out.println("书名:" + book2.bookName);
				mark = true; 
				}
			}
		if (!mark)  System.out.println("图书不存在!"); 
		main(null); 
		} catch (InputMismatchException e) {  
			throw new Exception("输入类型有误!"); 
			} 
		}
	//按照图书的名称去查询图书
	public static void getBookByName(Book[] book) throws Exception{
		System.out.println("输入图书名称:");
		String name = sinputCommand();
		boolean mark = false;
		try{
			for(Book book2:book){
				if(book2.bookName.equals(name)){
					System.out.println("book:"+book2.bookName);
					mark = true;
				}
			}
			if(!mark)  System.out.println("图书不存在!");
			main(null);
		}catch(InputMismatchException e){
			System.out.println(name);
		}
	}
	
	//从控制台输入命令,用于输入命令和输入图书序号
	private static int inputCommand(){
		int command;
		try{
			command = console.nextInt();
			return command;
		}catch(Exception e){
			
			//若输入字符型或者字符串,则抛出异常,捕获该异常,抛出"错误命令异常"
			console = new Scanner(System.in);
			//返回-1
			return -1;
		}
	}
	//用于图书的名称中的输入
	private static String sinputCommand(){
		String command;
		try{
			command = console.next();
			return command;
		}catch(Exception e){
			console = new Scanner(System.in);
			return "查询出现错误!";
		}
	}
}



    在这里自己规避了while(true)..continue这种不爽的方法,用了main(null)这种循环方式,关于这个main(null)的解释见下一篇文章.

    今天任务结束,至此异常类总结完毕,断言那篇转载的文章已经介绍的很详细辣,

   总体来说,总结的还略粗糙,有错误的麻烦各位看官及时告知批评
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

精神抖擞王大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值