Book父类:
package com.comit;
public class Book {
protected int bookId;
protected String 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;
}
}
EnglishBook子类:
package com.comit;
public class EnglishBook extends Book {
public EnglishBook() {
bookId = 1;
bookName = "EnglishBook";
}
@Override
public String toString() {
return bookId + "." + bookName;
}
}
Math子类:
package com.comit;
public class MathBook extends Book {
public MathBook() {
bookId = 2;
bookName = "MathBook";
}
@Override
public String toString() {
return bookId + "." + bookName;
}
}
BookRentalSystemInitial 主类:
package com.comit;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BookRentalSystemInitial {
public static void main(String[] args) {
System.out.println("输入命令:1-按照序号查找图书;2-按照书名查找图书。");
try {
systemInitial();
} catch (RuntimeException e) {
// e.printStackTrace();
System.out.println("命令数字输入有误,请根据提示输入数字命令!");
main(null);
}
}
//系统初始化
public static void systemInitial() {
Book[] book = { new EnglishBook(), new MathBook() };
Scanner sn = new Scanner(System.in);
try {
int orderId = sn.nextInt();
//选择借书查询方式
if (orderId == 1) {
findById(book);
} else if (orderId == 2) {
findByName(book);
} else {
//重新初始化系统
main(null);
}
} catch (Exception e) {
RuntimeException newExc = new RuntimeException("输入类型有误!");
newExc.initCause(e);
throw newExc;
}
}
//按图书Id查询图书名称
public static void findById(Book[] book) throws Exception {
System.out.print("输入图书序号:");
Scanner sn = new Scanner(System.in);
int id = sn.nextInt();
boolean mark = false;
try {
for (Book book2 : book) {
if (book2.bookId == id) {
System.out.println("book:" + book2.bookName);
mark = true;
sn.close();
}
}
if (!mark) {
System.out.println("图书不存在!");
main(null);
}
} catch (InputMismatchException e) {
sn.close();
throw new Exception("输入类型有误!");
}
}
//按图书名称查询图书名称
public static void findByName(Book[] book) throws Exception {
System.out.print("输入图书名称:");
Scanner sn = new Scanner(System.in);
String name = sn.next();
boolean mark = false;
try {
for (Book book2 : book) {
if (book2.bookName.equals(name)) {
System.out.println("book:" + book2.bookName);
mark = true;
sn.close();
}
}
if (!mark) {
System.out.println("图书不存在!");
main(null);
}
} catch (InputMismatchException e) {
sn.close();
throw new Exception("输入类型有误!");
}
}
}
点击查看更多内容
33人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦