今天博主再給你們分享一個小項目:MiNi圖書管理系統。用的是Java語言開發的,代碼很少,大概260行左右吧,系統是實現圖書的新增圖書、刪除圖書、借閱圖書、歸還圖書、查看圖書等簡單的功能(後附源代碼)!java
而後讓用戶選擇,若是用戶不當心或者誤輸入了錯誤的數,會再次讓用戶進行輸入。數組
1 package cn.kgc.sg.MiTuSys; 2
3 import java.util.Scanner; 4
5 public class MiNiBookMgrSys { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 //初始化書本信息
9 String[] bookNames = new String[6]; 10 int[] borrowDates = new int[6]; 11 int[] borrowCounts = new int[6]; 12 int[] states = new int[6]; 13
14 //初始化三本書
15 bookNames[0] = "安徒生童話"; 16 borrowDates[0] = 15; 17 borrowCounts[0] = 16; 18 states[0] = 0;//0:可借閱 1:已借出
19
20 bookNames[1] = "格林童話"; 21 borrowDates[1] = 0; 22 borrowCounts[1] = 0; 23 states[1] = 1;//0:可借閱 1:已借出
24
25 bookNames[2] = "Java開發"; 26 borrowDates[2] = 0; 27 borrowCounts[2] = 0; 28 states[2] = 0;//0:可借閱 1:已借出
29
30 int num = -1;//初始化用戶輸入的數字
31 boolean flag = true;//true:不退出系統 false:退出系統
32 do{ 33 System.out.println("************歡迎使用MiNi圖書管理系統**********"); 34 System.out.println("\t\t\t\t\t1.新增圖書"); 35 System.out.println("\t\t\t\t\t2.查看圖書"); 36 System.out.println("\t\t\t\t\t3.借閱圖書"); 37 System.out.println("\t\t\t\t\t4.歸還圖書"); 38 System.out.println("\t\t\t\t\t5.刪除圖書"); 39 System.out.println("\t\t\t\t\t6.退出系統"); 40
41 System.out.print("\n請選擇:"); 42 int choose = sc.nextInt(); 43 while(choose<0 || choose>6){ 44 System.out.print("輸入有誤,從新輸入:"); 45 choose = sc.nextInt(); 46 } 47 switch(choose){ 48 case 1: 49 System.out.println("************1.新增圖書**********"); 50 System.out.print("請輸入新增圖書的名字:"); 51 String addBook = sc.next(); 52 boolean isAdd = false;//false:不能添加圖書 true:能夠添加圖書 53 //遍歷數組,查找新增圖書的位置
54 for(int i =1;i<bookNames.length;i++){ 55 //數組沒滿,能夠新增圖書
56 if(bookNames[i] == null){ 57 isAdd = true; 58 bookNames[i] = addBook; 59 System.out.println("新增圖書成功!!!"); 60 break; 61 }else{ 62 isAdd = false; 63 } 64 } 65 //循環遍歷輸出數組內圖書信息
66 for(int i = 0;i<bookNames.length;i++){ 67 //若是爲空,不輸出
68 if(bookNames[i] != null){ 69 String state = (states[i]==0)?"可借閱":"已借出"; //把借閱狀態的0 1 變成易看的「可借閱 已借出」
70 String borrowCount = borrowCounts[i]+"次"; 71 String borrowDate = borrowDates[i] + "日"; 72 System.out.println((i+1)+"\t\t"+bookNames[i]+"\t\t"+state+"\t\t"+
73 borrowCount+"\t\t"+borrowDate); 74 } 75 } 76 if(!isAdd){ 77 System.out.println("對不起,圖書已滿,不能新增!"); 78 break; 79 } 80 break; 81 case 2: 82 System.out.println("2.查看圖書"); 83 System.out.println("序號"+"\t\t\t"+"書名"+"\t\t\t"+"借閱狀態"+"\t\t"+"借閱次數"+
84 "\t\t"+"借閱日期"); 85 //循環遍歷輸出數組內圖書信息
86 for(int i = 0;i<bookNames.length;i++){ 87 //若是爲空,不輸出
88 if(bookNames[i] != null){ 89 String state = (states[i]==0)?"可借閱":"已借出"; //把借閱狀態的0 1 變成易看的「可借閱 已借出」
90 String borrowCount = borrowCounts[i]+"次"; 91 String borrowDate = borrowDates[i] + "日"; 92 System.out.println((i+1)+"\t\t"+bookNames[i]+"\t\t"+state+"\t\t"+
93 borrowCount+"\t\t"+borrowDate); 94 } 95 } 96 break; 97 case 3: 98 System.out.println("3.借閱圖書"); 99 System.out.print("請輸入您要借閱圖書的名字:"); 100 String borrowName = sc.next(); 101 boolean isBorrow = false;//true:能借閱
102 for(int i = 0;i<bookNames.length;i++){ 103 //若是能夠找到書,且書的狀態是可借閱,就能夠借閱
104 if(bookNames[i] != null && bookNames[i].equals(borrowName) && states[i] == 0){ 105 isBorrow = true; 106 //輸入借閱日期
107 System.out.print("請輸入借閱的日期:"); 108 int borrowRiQi = sc.nextInt(); 109 //判斷輸入的借閱日期是否合法
110 while(borrowRiQi < 1 || borrowRiQi > 31){ 111 System.out.print("輸入有誤,從新輸入:"); 112 borrowRiQi = sc.nextInt(); 113 } 114 System.out.println("借閱成功!!!"); 115 //更改借閱日期
116 borrowDates[i] = borrowRiQi; 117 //更新借閱狀態
118 states[i] = 1; 119 //更新借閱次數
120 borrowCounts[i]++; 121 break; 122 }else if(bookNames[i] != null && bookNames[i].equals(borrowName) && states[i] == 1){ 123 //若是能夠找到書,且書的狀態是已借出,不能借閱
124 isBorrow = true; 125 System.out.println("書已經借出,不能重複借閱!"); 126 break; 127 //if(bookNames[i] == null) 128 //if(!bookNames[i].equals(borrowName))
129 }/*else if(bookNames[i] == null){ 130 isBorrow = false; 131 break; 132 }*/
133 } 134 if(!isBorrow){ 135 //bookNames[i] == null,說明 找不到書
136 System.out.println("抱歉,找不到該書!!!"); 137 break; 138 } 139 break; 140 case 4: 141 System.out.println("4.歸還圖書"); 142 System.out.print("請輸入您要歸還圖書的名字:"); 143 String returnName = sc.next(); 144 boolean isReturn = false;//true:能歸還
145 for(int i = 0;i<bookNames.length;i++){ 146 //if(!bookNames[i].equals(borrowName))
147 /*if(bookNames[i] == null){ 148 //找不到書 149 isReturn = false; 150 break; 151 }else*/
152 //若是能夠找到書,且書的狀態是已借出,就能夠歸還
153 if(bookNames[i]!=null && bookNames[i].equals(returnName) && states[i] == 1){ 154 isReturn = true; 155 //輸入歸還日期
156 System.out.print("請輸入歸還的日期:"); 157 int returnRiQi = sc.nextInt(); 158 //判斷輸入的歸還日期是否合法
159 while(returnRiQi < 1 || returnRiQi > 31 || returnRiQi<borrowDates[i]){ 160 //輸入的歸還日期不合法
161 if(returnRiQi < 1 || returnRiQi > 31){ 162 System.out.print("輸入的日期有誤,從新輸入:"); 163 returnRiQi = sc.nextInt(); 164 }else if(returnRiQi<borrowDates[i]){ 165 System.out.print("歸還日期必須大於借閱日期,重新輸入:"); 166 returnRiQi = sc.nextInt(); 167 } 168 } 169 System.out.println("歸還成功!!!"); 170 //更新借閱狀態
171 states[i] = 0; 172 /*//更新借閱次數 173 borrowCounts[i]++;*/
174 break; 175 }else if(bookNames[i]!=null && bookNames[i].equals(returnName) && states[i] == 0){ 176 //若是能夠找到書,且書的狀態是可借閱,不能歸還
177 isReturn = true; 178 System.out.println("書可借閱,不能歸還!"); 179 break; 180 } 181 } 182 if(!isReturn){ 183 //bookNames[i] == null.說明找不到書,不能夠歸還
184 System.out.println("抱歉,不是本圖書館的書,不用歸還!!!"); 185 break; 186 } 187 break; 188 case 5: 189 System.out.println("5.刪除圖書"); 190 System.out.print("請輸入您要刪除圖書的名字:"); 191 String deleteName = sc.next(); 192 boolean isDelete = false;//true:能刪 false:不能刪
193 for(int i = 0;i<bookNames.length;i++){ 194 /*//if(!bookNames[i].equals(borrowName)) 195 if(bookNames[i] == null){ 196 //找不到書,不能刪 197 isDelete = false; 198 break; 199 }else*/
200 if(bookNames[i] != null && bookNames[i].equals(deleteName) && states[i] == 1){ 201 //若是能夠找到書,且書的狀態是已借出,不能刪除
202 isDelete = true; 203 System.out.println("書已借出,不能刪除!"); 204 break; 205 }else if(bookNames[i] != null && bookNames[i].equals(deleteName) && states[i] == 0){ 206 //若是能夠找到書,且書的狀態是可借閱,能夠刪除
207 isDelete = true; 208 //初始化呢index,用來記錄下須要刪除的位置
209 int index = -1; 210 if(bookNames[i].equals(deleteName)){ 211 index = i; 212 } 213 //判斷index是-1仍是i
214 if(index != -1){ 215 //index不是-1,說明找到了圖書,能夠刪除
216 for(i = index;i<bookNames.length-1;i++){ 217 //從下標index1開始,後面的總體向前移一位
218 bookNames[i] = bookNames[i+1]; 219 borrowDates[i] = borrowDates[i+1]; 220 borrowCounts[i] = borrowCounts[i+1]; 221 states[i] = states[i+1]; 222
223 //最後一位置空
224 bookNames[bookNames.length-1] = null; 225 borrowDates[bookNames.length-1] = 0; 226 borrowCounts[bookNames.length-1] = 0; 227 states[bookNames.length-1] = 0;//0:可借閱 1:已借出
228 } 229 } 230 System.out.println("刪除圖書成功!"); 231 break; 232 } 233 } 234 if(!isDelete){ 235 //bookNames[i] == null.說明找不到書,不能刪除
236 System.out.println("不是本圖書館的書,不能刪除"); 237 break; 238 } 239 break; 240 case 6: 241 flag = false; //退出系統
242 break; 243 default: 244 flag = false; //退出系統
245 break; 246 } 247 if(flag){ 248 System.out.print("\n輸入0返回:"); 249 num = sc.nextInt(); 250 }else{ 251 break; 252 } 253 }while(num == 0); 254 System.out.println("謝謝使用!!!"); 255 } 256 }
ps:本人也在學Java開發,也是剛入門不久,因此也有些Java開發的資料,以及計算機相關的電子書等資料,因此有想要的小夥伴能夠多多互動哦,一塊兒多交流交流!!!O(∩_∩)Ospa