package com.skyL;
public class Book {
private int id;
private String name;
public Book (int id, String name){
this .id = id;
this .name = name;
}
public int getId (){
return id;
}
public String getName (){
return name;
}
}
package com.skyL;
public class BookExc extends Exception {
public BookExc (String str){
super (str);
}
}
package com.skyL;
import java.util.Scanner;
public class BookTest {
public static void main (String[] args) {
BookTest bt = new BookTest();
Book[] books = {new Book(1 , "大学英语" ), new Book(2 , "高等数学" ),
new Book(3 , "概率论" ), new Book(4 , "C++编程思想" ),
new Book(5 , "C++ primer" ), new Book(6 , "Java核心技术卷1" )};
int res = bt.borrowBook(books);
System.out .println("借书系统关闭,返回值为:" + res);
}
public boolean isExsit (Book[] books, int id){
for (Book book : books){
if (book.getId() == id)
return true ;
}
return false ;
}
public void showBooks (Book[] books){
for (Book book : books){
System.out .println(book.getId() + ":\t" + book.getName());
}
}
public int getUserInput () throws BookExc{
Scanner input = new Scanner(System.in );
int UserIn = input.nextInt();
if (UserIn != 1 && UserIn != 2 ){
throw new BookExc("输入错误!" );
}
return UserIn;
}
public int getUserBookId (Book[] books) throws BookExc{
Scanner input = new Scanner(System.in );
int bookId = input.nextInt();
if (!isExsit(books, bookId)){
throw new BookExc("该书不存在!" );
}
return bookId;
}
public int getUserIsBorow () throws BookExc{
Scanner input = new Scanner(System.in );
int isBorow = input.nextInt();
if (isBorow != 1 && isBorow != 2 ){
throw new BookExc("输入错误!" );
}
return isBorow;
}
public int borrowBook (Book[] books){
Scanner input = new Scanner(System.in );
int UserIn = 0 ;
int bookId = -1 ;
int isBorow = 0 ;
while (true ){
try {
System.out .println("是否进入借书页面" );
System.out .println("1: 是, 2: 否" );
UserIn = getUserInput();
}catch (Exception e){
System.out .println("输入错误!" );
continue ;
}
if (UserIn == 2 ){
System.out .println("已退出借书系统" );
break ;
}
while (true ){
try {
System.out .println("请输入书籍的编号:" );
bookId = getUserBookId(books);
}catch (Exception e){
System.out .println("该书不存在!" );
continue ;
}
System.out .println("你要借的书籍是:" + books[bookId].getName());
while (true ){
try {
System.out .println("是否要借阅该书:" );
System.out .println("1: 是, 2: 否" );
isBorow = getUserIsBorow();
}catch (Exception e){
System.out .println("输入错误! 请重新输入" );
continue ;
}
if (isBorow == 1 )
System.out .println("您已成功借书,书籍为:" + books[bookId].getName());
else
System.out .println("您已取消对" + books[bookId].getName() + "的借阅" );
while (true ){
try {
System.out .println("是否要继续借书:" );
System.out .println("1: 是, 2: 否" );
isBorow = getUserInput();
}catch (Exception e){
System.out .println("输入错误! 请重新输入" );
continue ;
}
break ;
}
break ;
}
if (isBorow == 1 )
continue ;
else
break ;
}
if (isBorow == 2 ){
System.out .println("已退出借书系统" );
break ;
}
}
return 0 ;
}
}