import java.util.Scanner; import java.util.Arrays; public class book { public static void main(String[] args){ //初始化數據 String[] bookName = {"西遊記", "紅樓夢", "三國演義", "水滸傳","海賊王",null}; String[] state = {"可借","可借","可借","可借","可借","可借"}; int[] count = {0,0,0,0,0,0}; int num; do { System.out.println("歡迎使用圖書借閱系統"); System.out.println("-------------------------------"); System.out.println("1.查看圖書"); System.out.println("2.新增圖書"); System.out.println("3.刪除圖書"); System.out.println("4.借閱圖書"); System.out.println("5.歸還圖書"); System.out.println("6.退出圖書"); System.out.println("-------------------------------"); System.out.println("請選擇:"); Scanner scanner = new Scanner(System.in); num = scanner.nextInt(); //打印輸入的值 //System.out.println(num); switch(num) { case 1: System.out.println("---》查看圖書"); System.out.println("序號\t\t名稱\t\t狀態\t\t借出次數"); //int i = 1; for(int i = 1; i<=bookName.length; i++) { System.out.print(i); System.out.print("\t\t"+bookName[i-1]); System.out.print("\t\t"+state[i-1]); System.out.println("\t\t"+count[i-1]); } /* for(String name:bookName) { System.out.print(i); System.out.print("\t\t"+name); System.out.print("\t\t"+state[i-1]); System.out.println("\t\t"+count[i-1]); i++; }*/ break; case 2: System.out.println("---》新增圖書"); //判斷數組中是否有位置增長書名 if(bookName[bookName.length-1] == null) { System.out.println("請輸入書名:"); String name = scanner.next(); //檢測數組中是否已經存在該書名 //int index = Arrays.binarySearch(bookName, name); //默認數組中沒有用戶輸入的書 boolean flag = false; for(String name2:bookName) { if(name.equals(name2)){ flag = true; break; } } if(flag) { System.out.println("《"+name+"》"+"書名已經存在"); break; }else{ for(int i = 0; i < bookName.length; i++) { if(bookName[i] == null) { bookName[i] = name; break; } } System.out.println("已添加該書名"); } }else{ System.out.println("-----書架已滿-----"); } break; case 3: System.out.println("----》刪除圖書"); //遍歷圖書 for(int i = 1; i<=bookName.length; i++) { System.out.print(i); System.out.println("\t"+bookName[i-1]); } //檢測書架是否爲空 boolean flag = false; for(String name2:bookName) { if(name2 != null){ flag = true; break; } } if(flag) { System.out.println("-------請輸入你要刪除的書名------"); String name = scanner.next(); boolean flag2 = false; //證實該書是否存在 for(String name2:bookName) { if(name.equals(name2)) { flag2 = true; break; } } if(flag2) { //找到被刪除的書的數組下標 int index = 0; for(int i = 0; i< bookName.length; i++) { if(name.equals(bookName[i])) { index = i; break; } } //index以後的數據往前提 for(int i = index; i < bookName.length-1; i++) { bookName[i] = bookName[i+1]; } //將數組最後一位置爲null bookName[bookName.length-1] = null; System.out.println("刪除成功"); }else{ System.out.println("該書不存在"); } }else { System.out.println("-------空書架-------"); } //for(int i = 1; i<=bookName.length; i++) { // System.out.print(i); // System.out.println("\t"+bookName[i-1]); //} //num = scanner.nextInt(); //bookName[num-1] =null; break; case 4: System.out.println("借閱圖書"); //1.檢測bookName是否爲空 flag = false; for(String name:bookName) { if(name != null) { flag = true; break; } } if(flag) { //2.檢測用戶輸入的書是否存在 System.out.print("請輸入你要借閱的書名:"); String name = scanner.next(); boolean flag2 = false; for(String name2:bookName) { if(name.equals(name2)) { flag2 = true; break; } } if(flag2) { //將圖書對應的狀態改成已借 int index = 0; for(int i = 0; i < bookName.length; i++) { if(name.equals(bookName[i])) { index = i; break; } }
//判斷該書的狀態
if(state[index].equals("已借出")) {
System.out.println("------該圖書已借出------");
}else{
state[index] = "已借出";
//將圖書對應的借出數量加
count[index] = ++count[index];
System.out.println("該書已借閱成功");
}java
}else{ System.out.println("你輸入的書名不存在"); } }else{ System.out.println("書架已空,沒有能夠借閱的書籍"); } break; case 5: System.out.println("----》歸還圖書"); //2.檢測用戶輸入的書是否存在 System.out.print("請輸入你要歸還的書名:"); String name = scanner.next(); boolean flag2 = false; for(String name2:bookName) { if(name.equals(name2)) { flag2 = true; break; } } if(flag2) { int index = 0; for(int i = 0; i < bookName.length; i++) { if(name.equals(bookName[i])) { index = i; break; } } if(state[index].equals("可借")) { System.out.println("該書已歸還"); }else{ state[index] = "可借"; System.out.println("該書已歸還"); } }else{ System.out.println("該書不存在"); } break; case 6: System.out.println("退出系統"); return; } System.out.println("輸入0返回"); num = scanner.nextInt(); System.out.println(num); int bb = 0; while(num !=0) { System.out.println("----------輸入錯誤請繼續輸入-----------"); System.out.println("\t\t\t須要輸入0才能返回"); num = scanner.nextInt(); if(bb >= 3){ System.out.print("再輸入錯誤就要打屁屁了"); } bb++; } }while(num == 0 ); } }