您好,登錄后才能下訂單哦!
java控制臺輸出圖書館管理系統(tǒng)(只用java代碼不用數(shù)據(jù)庫和GUI,java入門的新手秒懂)
在個項目中,我只用數(shù)組保存數(shù)據(jù),和只用for循環(huán)和if條件語句來完成,連數(shù)組工具類都沒用,所以導(dǎo)致要用到很多的條件語句,雖然比較局限,但可以讓新手快速體驗一下做小項目的樂趣,也可以練練for和if這些語句,練練一下它們之間嵌套的邏輯關(guān)系等,因為時間有限所以沒有去優(yōu)化了,主要還是讓新手們體驗下做java項目的面向?qū)ο缶幊趟枷耄∣OP),給新手們體驗一下做項目的樂趣,嘿嘿!
話不多說了,直接切入正題。
設(shè)計思路如下圖:
首先用eclipse建立包和類(為了簡單明了我的項目結(jié)構(gòu)設(shè)計如下):
//在UserData.java里邊輸入以下代碼
package com.jiaxun.library.data; public class UserData { private String username = "小詢";//用戶名 private String password = "zengjiaxun";//密碼 public String getUser() { return username; } public String getPassword() { return password; } }
//在BookData.java中輸入以下代碼
package com.jiaxun.library.data; public class BookData { //書庫 public BookData() { { String noreaders= "無"; for (int i = 0; i < borrowreaders.length; i++) { borrowreaders[i]=noreaders; } for (int i = 0; i < 5; i++) { switch (i) { case 0: booknames[i]="神級強者在都市"; authors[i]="日當(dāng)午"; pubdates[i]="2016-03-31"; sumpaginations[i]="1320"; break; case 1: booknames[i]="都市修仙高手"; authors[i]="雪無淚"; pubdates[i]="2017-07-25"; sumpaginations[i]="656"; break; case 2: booknames[i]="護(hù)花神兵"; authors[i]="雞飛蛋打"; pubdates[i]="2016-01-31"; sumpaginations[i]="1120"; break; case 3: booknames[i]="七界仙尊 "; authors[i]="無良書生"; pubdates[i]="2017-04-04"; sumpaginations[i]="1310"; break; case 4: booknames[i]="戀上調(diào)皮公主"; authors[i]="殘落筱筱"; pubdates[i]="2012-08-20"; sumpaginations[i]="954"; break; } } } } public String[] booknames = new String[10];// 書名 public String[] authors = new String[10];// 作者 public String[] pubdates = new String[10];// 出版日期 public String[] sumpaginations = new String[10];// 總頁數(shù) public String[] borrowreaders = new String[10];// 借閱人 }
//在Bo.java中輸入以下代碼
package com.jiaxun.library.bo; import java.util.Scanner; import com.jiaxun.library.data.BookData; import com.jiaxun.library.data.UserData; public class Bo { public static BookData book = new BookData(); UserData user = new UserData(); // 管理員登錄驗證 public int login(String username, String password) { if (username.equals(user.getUser())) { if (password.equals(user.getPassword())) { return 1;// 賬號和密碼驗證通過 } else { return 0;// 密碼錯誤 } } else { return -1;// 賬號不存在 } } /* * 菜單功能展示列表 * * 圖書信息展示功能 */ public void showBook() { System.out.println("本圖書館共有" + (book.booknames.length - remainSpace()) + "本書"); int a = bookinfo(); if (a == 0) { // 展示所有圖書信息 showAll(); } else if (a > 0 && a <= book.booknames.length) { // 展示相關(guān)圖書信息 showPart(a); } else { System.out.println("輸入的編號不合法!"); showBook(); } } // 展示所有圖書信息 public void showAll() { for (int i = 0; i < book.booknames.length; i++) { if (book.booknames[i] != null) { System.out.println("---------------------------------------編號" + (i + 1) + "--------------------------------------------------------------"); System.out.println("書籍編號為" + (i + 1) + "的詳細(xì)信息為:"); System.out.print("書名:" + book.booknames[i] + " "); System.out.print("作者:" + book.authors[i] + " "); System.out.print("出版日期:" + book.pubdates[i] + " "); System.out.print("總頁數(shù):" + book.sumpaginations[i] + " "); System.out.println("借閱者:" + book.borrowreaders[i]); } } System.out.println( "---------------------------------------------------------------------------------------------------------"); } // 展示相關(guān)圖書信息 public void showPart(int a) { int i = a; System.out.println( "---------------分界線---------------------------------------------------------------------------------------------------"); System.out.println("書籍編號" + (i) + "的詳細(xì)信息為:"); System.out.print("書名:" + book.booknames[i - 1] + " "); System.out.println("作者:" + book.authors[i - 1] + " "); System.out.print("出版日期:" + book.pubdates[i - 1] + " "); System.out.print("總頁數(shù):" + book.sumpaginations[i - 1] + " "); System.out.println("借閱者:" + book.borrowreaders[i - 1]); System.out.println( "---------------------------------------------------------------分界線---------------------------------------------------"); } //根據(jù)編號判斷要全局展示還是部分展示 public int bookinfo() { for (int i = 0; i < book.booknames.length; i++) { if (book.booknames[i] != null) { System.out.println("書名編號" + (i + 1) + ":" + book.booknames[i] + " "); } } System.out.print("輸入相應(yīng)編號即可查看相應(yīng)書籍的詳細(xì)信息(輸入0則查看所有信息):"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); return i; } // 錄入新書籍判斷 public int addBook(String bookname, String author, String pubdate, String sumpagination) { int flag = 0;// 1為成功,0為圖書館已經(jīng)存在該新書,-1為倉庫爆滿 if (book.booknames[book.booknames.length - 1] != null) { flag = -1;// 爆滿 } if (book.booknames[book.booknames.length - 1] == null) { for (int i = 0; i < book.booknames.length; i++) { if (bookname.equals(book.booknames[i])) { flag = 0;// 已存在book.booknames[i] break; } if (!(bookname.equals(book.booknames[i]))) { // 添加書籍 add(bookname, author, pubdate, sumpagination); flag = 1;// 成功 break; } } } return flag; } // 錄入新書籍 public void add(String bookname, String author, String pubdate, String sumpagination) { for (int i = 0; i < book.booknames.length; i++) { if (book.booknames[i] == null) { book.booknames[i] = bookname; book.authors[i] = author; book.pubdates[i] = pubdate; book.sumpaginations[i] = sumpagination; break; } } } // 存放書本的剩余容量 public int remainSpace() { int count = 0; try { for (int i = 0; i < book.booknames.length; i++) { if (book.booknames[i] == null) { ++count; } } } catch (Exception e) { // TODO: handle exception } return count; } // 修改時根據(jù)書名查找相關(guān)的書籍信息 public int selectBook(String bookname) { int flag = 0; for (int i = 0; i < book.booknames.length; i++) { if (bookname.equals("")) { flag = -1;// -1為未輸入書名 break; } // 其他情況下為匹配館里的圖書 if (bookname.equals(book.booknames[i])) { flag = 1;// 1找到相關(guān)書名 showPart(i + 1); break; } if (!bookname.equals(book.booknames[i])) { if (i == book.booknames.length - 1) { flag = 0;// 0為未找到 } } } return flag; } // 根據(jù)書名查找索引,有此書返其索引,無返-1 public int selectIndex(String bookname) { int flag = -1; for (int i = 0; i < book.booknames.length; i++) { if (bookname.equals(book.booknames[i])) { flag = i; } } return flag;// 找不到此書 } // 修改書籍信息 1書名 2作者 3出版日期 4總頁數(shù) 5借閱者 public void modinfo(int a, String value, int index) { switch (a) { case 1: book.booknames[index] = value; break; case 2: book.authors[index] = value; break; case 3: book.pubdates[index] = value; break; case 4: book.sumpaginations[index] = value; break; case 5: book.borrowreaders[index] = value; break; } } // 刪除書籍功能 public void deleteBook(String bookname) { int a = selectIndex(bookname); book.booknames[a] = null; book.authors[a] = null; book.pubdates[a] = null; book.sumpaginations[a] = null; book.booknames[a] = null; } // 檢查書是否存在館里和是否已借出 public int testReader(String bookname) { // 館里有此書,查找此書的索引,有此書a>=0,無返-1 int a = selectIndex(bookname); // 館里有此書 if (a >= 0) { if ((book.borrowreaders[a].equals("無") || book.borrowreaders[a].equals("")))// 無人借此書,""為掃描器的直接回車值 return 0;//未借 else return 1;//已借 } else { return -1;// 館里沒有此書 } } //將借閱者設(shè)置為無或者為空 public void setBorrowReader(String bookname){ int a = selectIndex(bookname); book.borrowreaders[a]="無"; } //查詢指定書的借閱者 public String getBorrowReader(String bookname){ int a = selectIndex(bookname); return book.borrowreaders[a]; } //書籍借閱功能 public void borrow(String bookname,String readername){ int a = selectBook(bookname); book.borrowreaders[a]=readername; } }
//在LibraryUI.java中輸入以下代碼
package com.jiaxun.library.ui; import java.util.Scanner; import com.jiaxun.library.bo.Bo; public class LibraryUI { Bo bo = new Bo(); public static void main(String[] args) { LibraryUI ui = new LibraryUI(); ui.welcome();// 歡迎界面 } // 歡迎界面 public void welcome() { System.out.println("*********************************"); System.out.println("********小☆詢圖書館管理系統(tǒng)********"); System.out.println("*********************************"); loginBoundary();// 登錄界面 } // 管理員登錄界面 public void loginBoundary() { System.out.print("管理員賬號:"); Scanner scanner = new Scanner(System.in); String username = scanner.nextLine(); System.out.print("登 錄 密 碼 :"); Scanner scanner1 = new Scanner(System.in); String password = scanner1.nextLine(); int flag = bo.login(username, password);// 登錄驗證 switch (flag) { case 0:// 密碼錯誤 System.out.println("密碼錯誤,請重新輸入。"); loginBoundary(); break; case 1:// 驗證通過 System.out.println("登錄成功!"); showMenu();// 登錄成功后跳轉(zhuǎn)至菜單功能管理 break; case -1:// 賬戶不存在 System.out.println("您輸入的管理員賬號不存在,請確認(rèn)后再輸入。"); loginBoundary(); break; } } // 菜單功能展示列表 public void showMenu() { System.out.println("請輸入功能編號進(jìn)入相應(yīng)的功能:"); 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("退出系統(tǒng)——編號:7"); System.out.print("請輸入功能編號:"); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt();// switch (a) { case 1:// 圖書信息 showBook(); returnMethod();// 返回主菜單 break; case 2:// 錄入新書籍 addBook(); break; case 3:// 修改書籍基本信息 modBookInfo(); break; case 4:// 刪除書籍 delete(); break; case 5:// 還書 returnBook(); break; case 6:// 借閱 borrowBook(); break; case 7:// 退出登錄 welcome(); scanner.close(); break; } } // 返回主菜單功能 public void returnMethod() { System.out.print("按下ENTER鍵返回到功能主菜單:"); Scanner scanner = new Scanner(System.in); String i = scanner.nextLine(); showMenu(); } // 圖書信息展示功能 public void showBook() { bo.showBook(); } // 錄入新書籍功能 public void addBook() { System.out.print("請輸入要增加的書籍名稱:");// 書名 Scanner scanner = new Scanner(System.in); String bookname = scanner.nextLine(); System.out.print("請輸入" + bookname + "的作者:");// 作者 Scanner scanner1 = new Scanner(System.in); String author = scanner1.nextLine(); System.out.print("請輸入" + bookname + "的出版日期(格式如2000-01-01):");// 出版日期 Scanner scanner2 = new Scanner(System.in); String pubdate = scanner2.nextLine(); System.out.print("請輸入" + bookname + "的總頁數(shù)(頁):");// 總頁數(shù) Scanner scanner3 = new Scanner(System.in); String sumpagination = scanner3.nextLine(); int flag = bo.addBook(bookname, author, pubdate, sumpagination); // 1為成功,0為圖書館已經(jīng)存在該新書,-1為倉庫爆滿 switch (flag) { case 1: System.out.println("新書籍" + bookname + "添加成功,目前圖書館還有" + bo.remainSpace() + "個位置可以存放新書籍。"); returnMethod();// 書籍添加界面中的返回功能,可選擇繼續(xù)添加或者回到主菜單 break; case 0: System.out.println("添加書籍失敗," + bookname + "已存在該圖書館中!"); returnMethod();// 書籍添加界面中的返回功能,可選擇繼續(xù)添加或者回到主菜單 break; case -1: System.out.println("添加失敗,圖書館已經(jīng)存儲量已經(jīng)爆滿了,請刪除部分書籍后再進(jìn)行添加。"); showMenu(); break; } } // 修改書籍信息 public void modBookInfo() { System.out.print("請輸入您要操作的書名:"); Scanner scanner = new Scanner(System.in); String bookname = scanner.nextLine(); // 根據(jù)書名查找相關(guān)的書籍信息 int flag = bo.selectBook(bookname); switch (flag) { case 0: System.out.print("未找到相關(guān)書籍,輸入0回車進(jìn)入該層功能,輸入1回車返回到功能主菜單。"); int a = scanner.nextInt(); if (a == 0) { modBookInfo(); } else if (a == 1) { showMenu(); } break; case -1: System.out.println("書名為空,輸入0回車進(jìn)入該層功能,輸入1回車返回到功能主菜單。"); int a1 = scanner.nextInt(); if (a1 == 0) { modBookInfo(); } else { showMenu(); } break; case 1: System.out.print("以上信息為相關(guān)書名查找的結(jié)果,其中(數(shù)字“1”代表書名,數(shù)字“2”代表作者,數(shù)字“3”代表出版日期,數(shù)字“4”代表總頁數(shù),數(shù)字“5”代表借閱者)輸入相關(guān)數(shù)字可以修改相關(guān)信息:"); int a2 = scanner.nextInt();// 1書名 2作者 3出版日期 4總頁數(shù) 5借閱者 if (a2 > 0 && a2 < 6) { System.out.print("請輸入您想修改的" + sortName(a2) + ":"); Scanner scanner2 = new Scanner(System.in); String value = scanner2.nextLine(); int index = bo.selectIndex(bookname); bo.modinfo(a2, value, index); System.out.println(bookname + "這本書的相關(guān)信息修改成功!"); returnMethod(); } else { System.out.println("您輸入的數(shù)字不合法。請重試!"); modBookInfo(); } break; } } // 修改的分類名稱 public String sortName(int flag) { if (flag == 1) return "書名"; if (flag == 2) return "作者"; if (flag == 3) return "出版日期"; if (flag == 4) return "總頁數(shù)"; else return "借閱者"; } // 刪除書籍功能 public void delete() { System.out.print("請輸入要被刪除的書名:"); Scanner scanner = new Scanner(System.in); String bookname = scanner.nextLine(); int flag = bo.testReader(bookname); // 存在此書并無人借 if (flag == 0) { bo.deleteBook(bookname); System.out.println(bookname + "已從圖書館成功移除!輸入0回到主菜單,輸入1則繼續(xù)刪除"); delReturn(); } else if (flag == 1) {// 有人借 System.out.println("此書已借出,請等待歸還后再刪除。輸入0回到主菜單,輸入1則繼續(xù)刪除"); delReturn(); } else {// 不存在此書 System.out.println("本館不存在此書,請仔細(xì)較對。輸入0回到主菜單,輸入1則繼續(xù)刪除"); delReturn(); } } // 刪除的返回操作 public void delReturn() { Scanner scanner1 = new Scanner(System.in); int i = scanner1.nextInt(); if (i == 0) { showMenu(); } else if (i == 1) { delete(); } } // 還書功能 public void returnBook() { System.out.print("請輸入要還的書籍名稱:"); Scanner scanner = new Scanner(System.in); String bookname = scanner.nextLine(); int flag = bo.testReader(bookname); if (flag == 0) { System.out.println(bookname + "暫未借出。"); System.out.print("輸入0回車返回至主菜單,輸入其他鍵則繼續(xù)還書"); String a = scanner.nextLine(); if(a.equals("0")){ showMenu(); }else { returnBook(); } } else if (flag == -1) { System.out.println(bookname + "不存在該圖書館中。"); System.out.print("輸入0回車返回至主菜單,輸入其他鍵則繼續(xù)還書"); String a = scanner.nextLine(); if(a.equals("0")){ showMenu(); }else { returnBook(); } } else if (flag == 1) { System.out.println(bo.getBorrowReader(bookname) + "已經(jīng)還書成功!"); bo.setBorrowReader(bookname); System.out.print("輸入0回車返回至主菜單,輸入其他鍵則繼續(xù)還書"); String a = scanner.nextLine(); if(a.equals("0")){ showMenu(); }else { returnBook(); } } } // 書籍借閱功能 public void borrowBook() { System.out.print("請輸入要借的書名:"); Scanner scanner = new Scanner(System.in); String bookname = scanner.nextLine(); int flag = bo.testReader(bookname); switch (flag) { case 0://存在此書并無人借的情況 System.out.println("請輸入借閱者的名字:"); String readername = scanner.nextLine(); bo.borrow(bookname,readername); System.out.println(readername+"從該圖書館里借出了以"+bookname+"為名的書籍。"); System.out.print("輸入0回車返回至主菜單,輸入1回車則繼續(xù)借書:"); int a0 = scanner.nextInt(); if(a0==0) showMenu(); if(a0==1) borrowBook(); break; case 1://存在此書被借的情況 System.out.println("借書失敗," + bookname + "已被借!"); System.out.print("輸入0回車返回至主菜單,輸入1回車則繼續(xù)借書:"); int a = scanner.nextInt(); switch (a) { case 0: showMenu(); break; case 1: borrowBook(); break; } case -1://不存在此書的情況 System.out.println("借書失敗," + bookname + "不存在該圖書館中。"); System.out.print("輸入0回車返回至主菜單,輸入1回車則繼續(xù)借書:"); int a1 = scanner.nextInt(); switch (a1) { case 0: showMenu(); break; case 1: borrowBook(); break; } } } }
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。