package com.company;
import java.util.Scanner;
class MiNiBookMgrSys1 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
? ? ? ? //初始化書(shū)本信息
? ? ? ? String[] bookNames =new String[6];
? ? ? ? int[] borrowDates =new int[6];
? ? ? ? int[] borrowCounts =new int[6];
? ? ? ? int[] states =new int[6];
? ? ? ? //初始化三本書(shū)
? ? ? ? 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開(kāi)發(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("************歡迎使用天天圖書(shū)管理系統(tǒng)**********");
? ? ? ? ? ? System.out.println("\t\t\t\t\t1.新增圖書(shū)");
? ? ? ? ? ? System.out.println("\t\t\t\t\t2.查看圖書(shū)");
? ? ? ? ? ? System.out.println("\t\t\t\t\t3.借閱圖書(shū)");
? ? ? ? ? ? System.out.println("\t\t\t\t\t4.歸還圖書(shū)");
? ? ? ? ? ? System.out.println("\t\t\t\t\t5.刪除圖書(shū)");
? ? ? ? ? ? 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.新增圖書(shū)**********");
? ? ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入新增圖書(shū)的名字:");
? ? ? ? ? ? ? ? ? ? String addBook = sc.next();
? ? ? ? ? ? ? ? ? ? boolean isAdd =false;//false:不能添加圖書(shū)? true:可以添加圖書(shū)
? ? ? ? ? ? ? ? ? ? //遍歷數(shù)組,查找新增圖書(shū)的位置
? ? ? ? ? ? ? ? ? ? for(int i =1;i
//數(shù)組沒(méi)滿,可以新增圖書(shū)
? ? ? ? ? ? ? ? ? ? ? ? if(bookNames[i] ==null){
isAdd =true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? bookNames[i] = addBook;
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("新增圖書(shū)成功?。?!");
break;
? ? ? ? ? ? ? ? ? ? ? ? }else{
isAdd =false;
? ? ? ? ? ? ? ? ? ? ? ? }
}
//循環(huán)遍歷輸出數(shù)組內(nèi)圖書(shū)信息
? ? ? ? ? ? ? ? ? ? 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ì)不起,圖書(shū)已滿,不能新增!");
break;
? ? ? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? ? ? case 2:
System.out.println("2.查看圖書(shū)");
? ? ? ? ? ? ? ? ? ? System.out.println("序號(hào)"+"\t\t\t"+"書(shū)名"+"\t\t\t"+"借閱狀態(tài)"+"\t\t"+"借閱次數(shù)"+
"\t\t"+"借閱日期");
? ? ? ? ? ? ? ? ? ? //循環(huán)遍歷輸出數(shù)組內(nèi)圖書(shū)信息
? ? ? ? ? ? ? ? ? ? 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.借閱圖書(shū)");
? ? ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入您要借閱圖書(shū)的名字:");
? ? ? ? ? ? ? ? ? ? String borrowName = sc.next();
? ? ? ? ? ? ? ? ? ? boolean isBorrow =false;//true:能借閱
? ? ? ? ? ? ? ? ? ? for(int i =0;i
//如果可以找到書(shū),且書(shū)的狀態(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("借閱成功?。?!");
? ? ? ? ? ? ? ? ? ? ? ? ? ? //更改借閱日期
? ? ? ? ? ? ? ? ? ? ? ? ? ? borrowDates[i] = borrowRiQi;
? ? ? ? ? ? ? ? ? ? ? ? ? ? //更新借閱狀態(tài)
? ? ? ? ? ? ? ? ? ? ? ? ? ? states[i] =1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? //更新借閱次數(shù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? borrowCounts[i]++;
break;
? ? ? ? ? ? ? ? ? ? ? ? }else if(bookNames[i] !=null && bookNames[i].equals(borrowName) && states[i] ==1){
//如果可以找到書(shū),且書(shū)的狀態(tài)是已借出,不能借閱
? ? ? ? ? ? ? ? ? ? ? ? ? ? isBorrow =true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("書(shū)已經(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,說(shuō)明 找不到書(shū)
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("抱歉,找不到該書(shū)!??!");
break;
? ? ? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? ? ? case 4:
System.out.println("4.歸還圖書(shū)");
? ? ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入您要?dú)w還圖書(shū)的名字:");
? ? ? ? ? ? ? ? ? ? String returnName = sc.next();
? ? ? ? ? ? ? ? ? ? boolean isReturn =false;//true:能歸還
? ? ? ? ? ? ? ? ? ? for(int i =0;i
//if(!bookNames[i].equals(borrowName))
/*if(bookNames[i] == null){
? ? ? ? ? ? ? //找不到書(shū)isReturn = false;
break;
}else*/
? ? ? ? ? ? ? ? ? ? ? ? //如果可以找到書(shū),且書(shū)的狀態(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){
//如果可以找到書(shū),且書(shū)的狀態(tài)是可借閱,不能歸還
? ? ? ? ? ? ? ? ? ? ? ? ? ? isReturn =true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("書(shū)可借閱,不能歸還!");
break;
? ? ? ? ? ? ? ? ? ? ? ? }
}
if(!isReturn){
//bookNames[i] == null.說(shuō)明找不到書(shū),不可以歸還
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("抱歉,不是本圖書(shū)館的書(shū),不用歸還?。。?);
break;
? ? ? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? ? ? case 5:
System.out.println("5.刪除圖書(shū)");
? ? ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入您要?jiǎng)h除圖書(shū)的名字:");
? ? ? ? ? ? ? ? ? ? String deleteName = sc.next();
? ? ? ? ? ? ? ? ? ? boolean isDelete =false;//true:能刪 false:不能刪
? ? ? ? ? ? ? ? ? ? for(int i =0;i
/*//if(!bookNames[i].equals(borrowName))
if(bookNames[i] == null){
? ? ? ? ? ? ? //找不到書(shū),不能刪isDelete = false;
break;
}else*/
? ? ? ? ? ? ? ? ? ? ? ? if(bookNames[i] !=null && bookNames[i].equals(deleteName) && states[i] ==1){
//如果可以找到書(shū),且書(shū)的狀態(tài)是已借出,不能刪除
? ? ? ? ? ? ? ? ? ? ? ? ? ? isDelete =true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("書(shū)已借出,不能刪除!");
break;
? ? ? ? ? ? ? ? ? ? ? ? }else if(bookNames[i] !=null && bookNames[i].equals(deleteName) && states[i] ==0){
//如果可以找到書(shū),且書(shū)的狀態(tài)是可借閱,可以刪除
? ? ? ? ? ? ? ? ? ? ? ? ? ? isDelete =true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? //初始化呢index,用來(lái)記錄下需要?jiǎng)h除的位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? int index = -1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(bookNames[i].equals(deleteName)){
index = i;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
//判斷index是-1還是i
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(index != -1){
//index不是-1,說(shuō)明找到了圖書(shū),可以刪除
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(i = index;i
//從下標(biāo)index1開(kāi)始,后面的整體向前移一位
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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("刪除圖書(shū)成功!");
break;
? ? ? ? ? ? ? ? ? ? ? ? }
}
if(!isDelete){
//bookNames[i] == null.說(shuō)明找不到書(shū),不能刪除
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("不是本圖書(shū)館的書(shū),不能刪除");
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("謝謝使用!??!");
? ? }
}