作完這個eclipse控制檯版本的圖書管理系統,感受略有收穫,對類與對象的一些關係,接口的使用,以及HashMap,語句斷定等有了更深入的理解.java
而這個系統還會繼續進化. 加入數據庫, web展現.亦或者使用java圖形界面.git
1.首先第一步,我要作這個圖書管理系統,我更理解爲一個圖書館 管理系統, 而不單單是圖書. 首先java是面向對象的.那麼好吧,有一個圖書館接口,一些書架,一些書放在書架上.github
以後功能需求要能實現註冊,登錄,退出.web
登錄以後實現書的借閱,歸還.數據庫
2.這一步使用了大量的邏輯斷定判斷,由於以爲是練手的小項目,前期作得有點"冗餘",多餘的一些雜質讓我感受作不動.經過放低要求,才發現,原來簡單而美妙.eclipse
package 圖書管理系統2; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class LoginStuff { static Scanner in = new Scanner(System.in); // 登錄方式選區,經過卡號呢仍是用戶名密碼 public static void loginSelect() { System.out.println("1.刷卡登錄.<輸入卡號>\n2.用戶名密碼登錄.\n#.返回上級"); String loginSelectNum = in.next(); boolean numRight = loginSelectNum.equals("1") || loginSelectNum.equals("2") || loginSelectNum.equals("#"); if (numRight) { if (loginSelectNum.equals("1")) { LoginStuff.loginWithID(); } if (loginSelectNum.equals("2")) { LoginStuff.loginWithUserAndPassword(); } if (loginSelectNum.equals("#")) { Service.getHello(); } } else { System.out.println("輸入錯誤,請輸入1或2或#.💗"); System.out.println(); loginSelect(); } } // 執行login(ID)前必須執行該方法 // 使用ID卡號登錄 public static void loginWithID() { System.out.println("請輸入您的卡號,以#號結束💗,(或只輸入#返回上一層)"); String ID_numberStr = ""; String nextNumber = in.next(); int OriginalIDlength = Library.IDLength; ID_numberStr = nextNumber.trim(); boolean IDPassed = false; if (ID_numberStr.endsWith("#")) { if (ID_numberStr.length() == 1 && ID_numberStr.equals("#")) { loginSelect(); return; } ID_numberStr = ID_numberStr.replace("#", ""); if (ID_numberStr.length() != OriginalIDlength) { IDPassed = false; System.out.println("正確卡號長度爲" + OriginalIDlength + "位.請從新輸入:💗"); } if (ID_numberStr.length() == OriginalIDlength) { IDPassed = true; } } if (IDPassed) { long ID_number = (long) Long.valueOf(ID_numberStr); login(ID_number); } else { loginWithID(); } } // 經過用戶名及密碼登錄的方法,這一方法只是斷定其格式,以後再調用了 login(inputUserName, inputPassWord) public static void loginWithUserAndPassword() { System.out.println("請輸入用戶名及密碼登錄.💗,(或只輸入#返回上一層)"); String inputUserName = in.next(); if(inputUserName.equals("#")) { loginSelect(); return; } String inputPassWord = in.next(); boolean regexFailed = inputUserName == null || inputUserName == "" || inputUserName.length() == 0 || inputPassWord == null || inputPassWord == "" || inputPassWord.length() == 0; while (regexFailed) { System.err.println("用戶名或密碼不能爲空!💗"); loginWithUserAndPassword(); } if (!regexFailed) { login(inputUserName, inputPassWord); } } // 登錄方式的選擇2, 經過用戶名和密碼登錄 以前已經使用loginWithUserAndPassword()進行了用戶名和密碼的接受和格式斷定 // 而這一步是斷定用戶數據庫的userList中和輸入的用戶名密碼進行比對,若是正確則提示登錄成功, // 若是對比失敗則從新調用loginWithUserAndPassword()方法進行用戶名和密碼的接受和斷定,其後再這個login方法進行比對.循環往復 public static void login(String userName, String passWord) { Set<User> userList = UserDatabase.getUserInstance(); boolean userInList = false; boolean userAuthPassed = false; String thisNick = null; String pass = null; User thisUser_ = null; Iterator<User> userIt = userList.iterator(); while (userIt.hasNext()) { thisUser_ = userIt.next(); if (userName.equals(thisUser_.getUserName())) { pass = thisUser_.getPassWord(); userInList = true; } if (userInList) { if (passWord.equals(pass)) { userAuthPassed = true; thisNick = thisUser_.getNickName(); break; } } } // 登錄經過以後 if (userAuthPassed) { System.out.println(thisNick + "登錄成功!"); System.out.println("快和小夥伴們一塊兒玩耍(看書)吧!💗"); MemberService.showMemberSerivce(thisUser_); } else { System.out.println("帳號或密碼錯誤,請從新登錄!💗"); loginWithUserAndPassword(); } } // 經過用戶ID進行登錄,先經過User中的IDNumberSet這個Set集合進行取出, // 使用了IDNumberJudge(Long ID)方法進行了"是否有這個ID"的比對 public static void login(long ID_Number) { long inputID = User.getID_number(); // 該boolean值就是去肯定是否有這個ID號碼,有則爲true.沒有這個ID也就是 // 說明用戶ID是不存在的,不能登錄,其後能夠再設置讓其進行註冊 boolean IDReady = User.IDNumberJudge(inputID); if (IDReady) { HashMap<Long, User> userData = UserDatabase.getUserDataMap(); User thisUser = userData.get(inputID); System.out.println("------------------------"); System.out.println(thisUser); System.out.println("刷卡成功,能夠隨意看書啦.當您離開記得再次刷卡以扣除時間段內消費!💗"); System.out.println("------------------------"); MemberService.showMemberSerivce(thisUser); } else { System.out.println("抱歉😭,沒有找到關於您卡號的信息!請稍後再試.如需幫助請聯繫管理員.💗"); System.out.println("或者您也能夠進行使用面板的1號選項進行註冊.💗"); Service.getHello(); } } }
package 圖書管理系統2; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegisterStuff { static Scanner in = new Scanner(System.in); private static String userName; private static String passWord; private static String sex; private static String nickName; private static int age; public User newRegisterStuff(String username, String password1, String password2, String sex, String nickname, int age) { String regex = "^[a-zA-Z0-9\u4E00-\u9FA5]+$"; Pattern pattern = Pattern.compile(regex); Matcher match = pattern.matcher(username); boolean b = match.matches(); int alreadyHasUserName = 1; boolean userNamePassed = (username != "" || username != null || username.length() != 0); if (userNamePassed) { if (!b) { System.out.println("用戶名只能由數字字母和下劃線組成!請從新輸入!💗"); register(); // return; } else { // 用戶名格式經過後,再斷定用戶名是否存在於已有用戶的用戶名字段中,已存在則返回true,不存在返回false alreadyHasUserName = userNameAgain(username); if (alreadyHasUserName == 1) {// 已經存在該帳戶名,則從新輸入 // 去斷定用戶名有一個什麼標記才行呢?使用boolean和int去斷定最終仍是會不行. System.out.println("該用戶名已經存在,請選用其餘用戶名~💗"); username = null; register(); // return; } else if (alreadyHasUserName == 0) {// 不重複則經過,並賦值給this.userName this.userName = username; } } } else { while (!userNamePassed) { System.out.println("用戶名爲空,請從新填寫註冊信息.💗"); register(); // return; } } // 斷定密碼 boolean passwordPassed = password1.equals(password2); if (passwordPassed) { this.passWord = password1; } else { while (!passwordPassed) { System.out.println("密碼兩次不一致,請覈對後再次輸入!(只輸入兩次密碼💗)"); String password_ = in.next(); String password2_ = in.next(); passwordPassed = passWordAgain(password_, password2_); if (passwordPassed) { this.passWord = password_; } } } // 斷定性別輸入 (不足)英文只支持小寫. boolean sexPassed = (sex.equals("男") || sex.equals("女") || sex.equals("man") || sex.equals("man") || sex.equals("woman") || sex.equals("boy") || sex.equals("girl")); if (sexPassed) { this.sex = sex; } else { while (!sexPassed) { System.out.println("性別填寫錯誤,請再次填寫:"); String sex_ = in.next(); sexPassed = sexAgain(sex_); if (sexPassed) { this.sex = sex_; } } } // 斷定暱稱合法性 boolean nickNamePassed = (nickname != "" || nickname != null || nickname.length() != 0); if (nickNamePassed) { this.nickName = nickname; } else { while (!nickNamePassed) { System.out.println("暱稱爲空,請再次填寫:"); String nickname_ = in.next(); nickNamePassed = nickNameAgain(nickname_); if (nickNamePassed) { this.nickName = nickname_; } } } // 判斷年齡合法性 boolean agePassed = (age < 999 && age > 0); if (agePassed) { this.age = age; } else { while (!agePassed) { System.out.println("只接受0歲以上兒童以及999歲之內的銀河系生物!請再次輸入年齡!"); int age_ = in.nextInt(); agePassed = ageAgain(age_); if (agePassed) { this.age = age_; } } } // 結束了全部數據的判斷 ///////////////////////////////////////////////////////////////////////////////// boolean AuthPassed = alreadyHasUserName == 0 && userNamePassed && b && passwordPassed && sexPassed && nickNamePassed && agePassed; User newUser = null; // 若是註冊成功則建立新user對象. if (AuthPassed) { newUser = new User(this.userName, this.passWord, this.sex, this.nickName, this.age); } return newUser; }// RegisterStuff(.......註冊信息.......)_END private static String getUserName() { return userName; } private static String getPassWord() { return passWord; } private static String getSex() { return sex; } private static String getNickName() { return nickName; } private static int getAge() { return age; } public RegisterStuff() { } // 註冊 public static void register() { System.out.println("請輸入您的>>帳戶名<<>>密碼<<>>確認密碼<<>>性別<<>>暱稱<<>>年齡<<\n以空格隔開~💗(*^_^*)💗"); RegisterStuff rs = new RegisterStuff(); User newUser = rs.newRegisterStuff(in.next(), in.next(), in.next(), in.next(), in.next(), in.nextInt()); if (newUser != null) { registerToUser(newUser);// 在將註冊信息註冊入了用戶數據庫後. 可能會爲null 此時在這個方法中判斷 printOK(newUser);// 登錄成功提示,並提示用戶的暱稱等. } } // 註冊完成後用戶數據放入UserDatabase public static void registerToUser(User newUser) { UserDatabase.setUserNameList(newUser.getUserName()); UserDatabase.setUserInstance(newUser); HashMap<Long, User> userDatabaseInRegister = UserDatabase.getUserDataMap(); long ID = newUser.getID_number(); userDatabaseInRegister.put(ID, newUser); UserDatabase.setUserData(userDatabaseInRegister); // 註冊信息已送入UserDatabase 能夠將用戶數據使用IO寫入txt文件 } // // 最後補的 暫未加入 // public static void afterRegister() { // printOK(); // BookShelf bs = new BookShelf(); // bs.BookShelfInit(); // bs.getAllCategory(); // System.out.println("請問你要查閱哪些書籍?或哪一本?"); // } // 註冊成功以後 private static void printOK(User user) { System.out.println("註冊成功!"); HashMap<Long,User> userInstanceInRegister = UserDatabase.getUserDataMap(); User thisUser = userInstanceInRegister.get(user.getID_number()); System.out.println("***"+thisUser+"***"); } // 從數據庫取出用戶名,查看此次的用戶是否存在重複 public int userNameAgain(String userName) { // 從用戶數據庫List獲取已經存在的帳戶對象 Set<String> userAlreadyHave = UserDatabase.getUserNameList(); // 從數據庫取出,去斷定用戶名是否存在,單獨寫個方法 for (String alreadyHaveName : userAlreadyHave) { if (userName.equals(alreadyHaveName)) { return 1;// 重複用戶名返回1 } } // 不重複用戶名返回0 return 0; } // 從新寫入passWord屬性 public boolean passWordAgain(String password1, String password2) { if (password1.equals(password2)) { System.out.println("本次密碼覈對正確!"); return true; } return false; } // 從新寫入sex屬性 private boolean sexAgain(String sex_) { if ((sex_.equals("男") || sex_.equals("女") || sex_.equals("man") || sex_.equals("man") || sex_.equals("woman") || sex_.equals("boy") || sex_.equals("girl"))) { return true; } return false; } // 從新寫入nickName屬性 public boolean nickNameAgain(String nickname_) { if (nickname_ != "" || nickname_ != null || nickname_.length() != 0) { return true; } return false; } // 從新寫入age屬性 public boolean ageAgain(int age_) { if (age_ < 999 && age_ > 0) { return true; } return false; } }
3. 有了登錄註冊,在用戶登錄後要提供會員服務,我直接創建了一個MemberService類.其實還有管理員Service和計算機Service類,應該提取某些Service做爲一個Service接口,不過還沒想好.this
package 圖書管理系統2; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; import java.util.Set; public class MemberService { static Scanner in = new Scanner(System.in); public static void showMemberSerivce(User thisUser_) { System.out.println("您好," + thisUser_.getNickName() + ",這是會員服務欄目,您可享受一下服務,Enjoy it!💗"); System.out.println("1.借書\t\t2.還書\n3.點個咖啡\t\t4.上網衝浪\n5.健身運動\t\t6.圖書館留言\n#.返回上級目錄(退出登錄)"); String serNum = in.next(); switch (serNum) { case "1":borrowBookSer(thisUser_); break; case "2":giveBackBookSer(thisUser_); break; case "3": break; case "4": break; case "5": break; case "6": break; case "#":LoginStuff.loginSelect(); break; default: System.out.println("請輸入正確的服務選擇~!💗"); showMemberSerivce(thisUser_); break; } } public static void borrowBookSer(User user) { System.out.println("您好💗"+user.getNickName()+",請問您想借閱哪本書呢?💗"); HashMap<String, Books> books = BookShelf.getBookMap(); Set<String> bookNames = books.keySet(); for(String bns : bookNames) { System.out.print(bns+" : "+books.get(bns).beBorrowed); } System.out.println("輸入書名,借閱書籍.💗"); String wantedBook = in.next(); user.borrow(books.get(wantedBook)); showMemberSerivce(user); } public static void giveBackBookSer(User user) { System.out.println("您好💗"+user.getNickName()+",請問您想還哪本書呢?💗"); ArrayList<String> userBorrowedBooks = user.getBorrowedBooks(); if(userBorrowedBooks == null) { System.out.println("抱歉您沒有書要還~💗"); return; } System.out.println("您借閱的書此時還有以下:"); int hasBorrowedCount = 0; for(String books:userBorrowedBooks) { hasBorrowedCount++; System.err.println(books); } if(hasBorrowedCount != 0) { System.out.println("請輸入您要還的書的書名~:"); String giveBackBookName = in.next(); //從用戶已借閱列表中刪除這本書 boolean giveback = userBorrowedBooks.remove(giveBackBookName); HashMap<String, Books> bookInstanceMap =BookShelf.getBookMap(); Books backBookInstance = bookInstanceMap.get(giveBackBookName); user.giveBack(backBookInstance, giveback); showMemberSerivce(user); }else { System.err.println("Sorry,您沒有書要還~💗"); showMemberSerivce(user); } } }
還有不少代碼就不貼了直接上🔗https://www.processon.com/view/link/5b24b04ae4b06d15ca94aedb編碼
請見諒爲了方便查閱,包名使用了中文.spa
經過作這個系統,感受對類和對象的概念更清晰了,接口使用和一些冗餘仍是有待增強.
最近看<重構>這書以爲仍是有些許做用,好比當感受必需要規整一下編碼,把相似的放到一塊兒,或者按照某些原則,這些我仍是不太懂.
總之,收穫頗豐💗~~~