package edu.xcdq;
/**
@author huyuansong
@date 2021/4/9 10:19
*/
import java.util.Scanner;
public class BookMgr {
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開發(fā)";
borrowDates[2] =0;
borrowCounts[2] =0;
states[2] =0;//0:可借閱 1:已借出
int num = -1;//初始化用戶輸入的數(shù)字
boolean flag =true;//true:不退出系統(tǒng) false:退出系統(tǒng)
do{
System.out.println("************歡迎使用MiNi圖書管理系統(tǒng)**********");
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.退出系統(tǒng)");
System.out.print("\n請(qǐng)選擇:");
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("請(qǐng)輸入新增圖書的名字:");
String addBook = sc.next();
boolean isAdd =false;//false:不能添加圖書 true:可以添加圖書
//遍歷數(shù)組伸辟,查找新增圖書的位置
for(int i =1;i
//數(shù)組沒滿麻惶,可以新增圖書
if(bookNames[i] ==null){
isAdd =true;
bookNames[i] = addBook;
System.out.println("新增圖書成功!P欧颉窃蹋!");
break;
}else{
isAdd =false;
}
}
//循環(huán)遍歷輸出數(shù)組內(nèi)圖書信息
for(int i =0;i
//如果為空,不輸出
if(bookNames[i] !=null){
String state = (states[i]==0)?"可借閱":"已借出"; //把借閱狀態(tài)的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("對(duì)不起静稻,圖書已滿警没,不能新增!");
break;
}
break;
case 2:
System.out.println("2.查看圖書");
System.out.println("序號(hào)"+"\t\t\t"+"書名"+"\t\t\t"+"借閱狀態(tài)"+"\t\t"+"借閱次數(shù)"+
"\t\t"+"借閱日期");
//循環(huán)遍歷輸出數(shù)組內(nèi)圖書信息
for(int i =0;i
//如果為空振湾,不輸出
if(bookNames[i] !=null){
String state = (states[i]==0)?"可借閱":"已借出"; //把借閱狀態(tài)的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("請(qǐng)輸入您要借閱圖書的名字:");
String borrowName = sc.next();
boolean isBorrow =false;//true:能借閱
for(int i =0;i
//如果可以找到書杀迹,且書的狀態(tài)是可借閱,就可以借閱
if(bookNames[i] !=null && bookNames[i].equals(borrowName) && states[i] ==0){
isBorrow =true;
//輸入借閱日期
System.out.print("請(qǐng)輸入借閱的日期:");
int borrowRiQi = sc.nextInt();
//判斷輸入的借閱日期是否合法
while(borrowRiQi <1 || borrowRiQi >31){
System.out.print("輸入有誤押搪,重新輸入:");
borrowRiQi = sc.nextInt();
}
System.out.println("借閱成功J骼摇!大州!");
//更改借閱日期
borrowDates[i] = borrowRiQi;
//更新借閱狀態(tài)
states[i] =1;
//更新借閱次數(shù)
borrowCounts[i]++;
break;
}else if(bookNames[i] !=null && bookNames[i].equals(borrowName) && states[i] ==1){
//如果可以找到書续语,且書的狀態(tài)是已借出,不能借閱
isBorrow =true;
System.out.println("書已經(jīng)借出厦画,不能重復(fù)借閱疮茄!");
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("抱歉,找不到該書8睢Aκ浴!");
break;
}
break;
case 4:
System.out.println("4.歸還圖書");
System.out.print("請(qǐng)輸入您要?dú)w還圖書的名字:");
String returnName = sc.next();
boolean isReturn =false;//true:能歸還
for(int i =0;i
//if(!bookNames[i].equals(borrowName))
/*if(bookNames[i] == null){
//找不到書isReturn = false;
break;
}else*/
//如果可以找到書排嫌,且書的狀態(tài)是已借出畸裳,就可以歸還
if(bookNames[i]!=null && bookNames[i].equals(returnName) && states[i] ==1){
isReturn =true;
//輸入歸還日期
System.out.print("請(qǐng)輸入歸還的日期:");
int returnRiQi = sc.nextInt();
//判斷輸入的歸還日期是否合法
while(returnRiQi <1 || returnRiQi >31 || returnRiQi
//輸入的歸還日期不合法
if(returnRiQi <1 || returnRiQi >31){
System.out.print("輸入的日期有誤舀射,重新輸入:");
returnRiQi = sc.nextInt();
}else if(returnRiQi
System.out.print("歸還日期必須大于借閱日期炬丸,從新輸入:");
returnRiQi = sc.nextInt();
}
}
System.out.println("歸還成功=枘颉!薇芝!");
//更新借閱狀態(tài)
states[i] =0;
/*//更新借閱次數(shù)borrowCounts[i]++;*/
break;
}else if(bookNames[i]!=null && bookNames[i].equals(returnName) && states[i] ==0){
//如果可以找到書,且書的狀態(tài)是可借閱丰嘉,不能歸還
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("請(qǐng)輸入您要?jiǎng)h除圖書的名字:");
String deleteName = sc.next();
boolean isDelete =false;//true:能刪 false:不能刪
for(int i =0;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){
//如果可以找到書晃听,且書的狀態(tài)是已借出百侧,不能刪除
isDelete =true;
System.out.println("書已借出,不能刪除能扒!");
break;
}else if(bookNames[i] !=null && bookNames[i].equals(deleteName) && states[i] ==0){
//如果可以找到書佣渴,且書的狀態(tài)是可借閱,可以刪除
isDelete =true;
//初始化呢index初斑,用來記錄下需要?jiǎng)h除的位置
int index = -1;
if(bookNames[i].equals(deleteName)){
index = i;
}
//判斷index是-1還是i
if(index != -1){
//index不是-1辛润,說明找到了圖書,可以刪除
for(i = index;i
//從下標(biāo)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; //退出系統(tǒng)
break;
default:
flag =false; //退出系統(tǒng)
break;
}
if(flag){
System.out.print("\n輸入0返回:");
num = sc.nextInt();
}else{
break;
}
}while(num ==0);
System.out.println("謝謝使用;尴!挣跋!");
}
}