package com.company;
import java.util.Scanner;
public class MiNiBookMgrSys1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//初始化书本信息
String[] bookNames = new String[6];
int[] borrowDates = new int[6];
int[] borrowCounts = new int[6];
int[] states = new int[6];
//初始化三本书
bookNames[0] = "安徒生童话";
borrowDates[0] = 15;
borrowCounts[0] = 16;
states[0] = 0;//0:可借阅 1:已借出
bookNames[1] = "格林童话";
borrowDates[1] = 0;
borrowCounts[1] = 0;
states[1] = 1;//0:可借阅 1:已借出
bookNames[2] = "Java开发";
borrowDates[2] = 0;
borrowCounts[2] = 0;
states[2] = 0;//0:可借阅 1:已借出
int num = -1;//初始化用户输入的数字
boolean flag = true;//true:不退出系统 false:退出系统
do{
System.out.println("****欢迎使用MiNi图书管理系统**");
System.out.println("\t\t\t\t\t1.新增图书");
System.out.println("\t\t\t\t\t2.查看图书");
System.out.println("\t\t\t\t\t3.借阅图书");
System.out.println("\t\t\t\t\t4.归还图书");
System.out.println("\t\t\t\t\t5.删除图书");
System.out.println("\t\t\t\t\t6.退出系统");
System.out.print("\n请选择:");
int choose = sc.nextInt();
while(choose<0 || choose>6){
System.out.print("输入有误,重新输入:");
choose = sc.nextInt();
}
switch(choose){
case 1:
System.out.println("****1.新增图书**");
System.out.print("请输入新增图书的名字:");
String addBook = sc.next();
boolean isAdd = false;//false:不能添加图书 true:可以添加图书
//遍历数组,查找新增图书的位置
for(int i =1;i<bookNames.length;i++){
//数组没满,可以新增图书
if(bookNames[i] == null){
isAdd = true;
bookNames[i] = addBook;
System.out.println("新增图书成功!!!");
break;
}else{
isAdd = false;
}
}
//循环遍历输出数组内图书信息
for(int i = 0;i<bookNames.length;i++){
//如果为空,不输出
if(bookNames[i] != null){
String state = (states[i]==0)?"可借阅":"已借出"; //把借阅状态的0 1 变成易看的“可借阅 已借出”
String borrowCount = borrowCounts[i]+"次";
String borrowDate = borrowDates[i] + "日";
System.out.println((i+1)+"\t\t"+bookNames[i]+"\t\t"+state+"\t\t"+
borrowCount+"\t\t"+borrowDate);
}
}
if(!isAdd){
System.out.println("对不起,图书已满,不能新增!");
break;
}
break;
case 2:
System.out.println("2.查看图书");
System.out.println("序号"+"\t\t\t"+"书名"+"\t\t\t"+"借阅状态"+"\t\t"+"借阅次数"+
"\t\t"+"借阅日期");
//循环遍历输出数组内图书信息
for(int i = 0;i<bookNames.length;i++){
//如果为空,不输出
if(bookNames[i] != null){
String state = (states[i]==0)?"可借阅":"已借出"; //把借阅状态的0 1 变成易看的“可借阅 已借出”
String borrowCount = borrowCounts[i]+"次";
String borrowDate = borrowDates[i] + "日";
System.out.println((i+1)+"\t\t"+bookNames[i]+"\t\t"+state+"\t\t"+
borrowCount+"\t\t"+borrowDate);
}
}
break;
case 3:
System.out.println("3.借阅图书");
System.out.print("请输入您要借阅图书的名字:");
String borrowName = sc.next();
boolean isBorrow = false;//true:能借阅
for(int i = 0;i<bookNames.length;i++){
//如果可以找到书,且书的状态是可借阅,就可以借阅
if(bookNames[i] != null && bookNames[i].equals(borrowName) && states[i] == 0){
isBorrow = true;
//输入借阅日期
System.out.print("请输入借阅的日期:");
int borrowRiQi = sc.nextInt();
//判断输入的借阅日期是否合法
while(borrowRiQi < 1 || borrowRiQi > 31){
System.out.print("输入有误,重新输入:");
borrowRiQi = sc.nextInt();
}
System.out.println("借阅成功!!!");
//更改借阅日期
borrowDates[i] = borrowRiQi;
//更新借阅状态
states[i] = 1;
//更新借阅次数
borrowCounts[i]++;
break;
}else if(bookNames[i] != null && bookNames[i].equals(borrowName) && states[i] == 1){
//如果可以找到书,且书的状态是已借出,不能借阅
isBorrow = true;
System.out.println("书已经借出,不能重复借阅!");
break;
//if(bookNames[i] == null)
//if(!bookNames[i].equals(borrowName))
}/*else if(bookNames[i] == null){
isBorrow = false;
break;
}*/
}
if(!isBorrow){
//bookNames[i] == null,说明 找不到书
System.out.println("抱歉,找不到该书!!!");
break;
}
break;
case 4:
System.out.println("4.归还图书");
System.out.print("请输入您要归还图书的名字:");
String returnName = sc.next();
boolean isReturn = false;//true:能归还
for(int i = 0;i<bookNames.length;i++){
//if(!bookNames[i].equals(borrowName))
/*if(bookNames[i] == null){
//找不到书
isReturn = false;
break;
}else*/
//如果可以找到书,且书的状态是已借出,就可以归还
if(bookNames[i]!=null && bookNames[i].equals(returnName) && states[i] == 1){
isReturn = true;
//输入归还日期
System.out.print("请输入归还的日期:");
int returnRiQi = sc.nextInt();
//判断输入的归还日期是否合法
while(returnRiQi < 1 || returnRiQi > 31 || returnRiQi<borrowDates[i]){
//输入的归还日期不合法
if(returnRiQi < 1 || returnRiQi > 31){
System.out.print("输入的日期有误,重新输入:");
returnRiQi = sc.nextInt();
}else if(returnRiQi<borrowDates[i]){
System.out.print("归还日期必须大于借阅日期,从新输入:");
returnRiQi = sc.nextInt();
}
}
System.out.println("归还成功!!!");
//更新借阅状态
states[i] = 0;
/*//更新借阅次数
borrowCounts[i]++;*/
break;
}else if(bookNames[i]!=null && bookNames[i].equals(returnName) && states[i] == 0){
//如果可以找到书,且书的状态是可借阅,不能归还
isReturn = true;
System.out.println("书可借阅,不能归还!");
break;
}
}
if(!isReturn){
//bookNames[i] == null.说明找不到书,不可以归还
System.out.println("抱歉,不是本图书馆的书,不用归还!!!");
break;
}
break;
case 5:
System.out.println("5.删除图书");
System.out.print("请输入您要删除图书的名字:");
String deleteName = sc.next();
boolean isDelete = false;//true:能删 false:不能删
for(int i = 0;i<bookNames.length;i++){
/*//if(!bookNames[i].equals(borrowName))
if(bookNames[i] == null){
//找不到书,不能删
isDelete = false;
break;
}else*/
if(bookNames[i] != null && bookNames[i].equals(deleteName) && states[i] == 1){
//如果可以找到书,且书的状态是已借出,不能删除
isDelete = true;
System.out.println("书已借出,不能删除!");
break;
}else if(bookNames[i] != null && bookNames[i].equals(deleteName) && states[i] == 0){
//如果可以找到书,且书的状态是可借阅,可以删除
isDelete = true;
//初始化呢index,用来记录下需要删除的位置
int index = -1;
if(bookNames[i].equals(deleteName)){
index = i;
}
//判断index是-1还是i
if(index != -1){
//index不是-1,说明找到了图书,可以删除
for(i = index;i<bookNames.length-1;i++){
//从下标index1开始,后面的整体向前移一位
bookNames[i] = bookNames[i+1];
borrowDates[i] = borrowDates[i+1];
borrowCounts[i] = borrowCounts[i+1];
states[i] = states[i+1];
//最后一位置空
bookNames[bookNames.length-1] = null;
borrowDates[bookNames.length-1] = 0;
borrowCounts[bookNames.length-1] = 0;
states[bookNames.length-1] = 0;//0:可借阅 1:已借出
}
}
System.out.println("删除图书成功!");
break;
}
}
if(!isDelete){
//bookNames[i] == null.说明找不到书,不能删除
System.out.println("不是本图书馆的书,不能删除");
break;
}
break;
case 6:
flag = false; //退出系统
break;
default:
flag = false; //退出系统
break;
}
if(flag){
System.out.print("\n输入0返回:");
num = sc.nextInt();
}else{
break;
}
}while(num == 0);
System.out.println("谢谢使用!!!");
}
}