前言:該圖書管理系統實現了查找、添加、刪除、顯示、借閱、歸還等功能,分為兩個用戶群體:管理者和普通用戶。使用了類與對象,封裝繼承多態,抽象類和接口等Java基礎知識。
一.思路
面向對象三部曲:找對象,創建對象,使用對象。
1.找對象
圖書館管理系統中的對象有書和人。人通過一些方法操作書。操作方法是一個集合。
2.創建對象
這里可用使用包將它們整合在一起,人、書和功能各一個包。
書是一個類,還一個類是書架。書中是定義書的各種屬性,書架是放書的地方,也是操作書的地方。
人分為兩種,一種是管理員,一種是普通用戶。創建一個父類將兩人共有的屬性放在這里,另外創建兩個子類擴展父類。
功能這個包里放著各種各樣我們實現的包,
3.脈絡
public static void main(String[] args) {BookList bookList=new BookList();//實例化書架,形象的理解為創建了一個圖書館Person person=logic(); //判斷這個人是哪一個類,是管理員還是普通用戶while(true){int choice=person.menu();person.doFunction(choice,bookList);}}
這是main方法的全部。
我們可以看到,我們先實例化了書架,相當于建立起了一個書架,我們可以在這個書架上對書進行操作。
第二步,通過一個logic方法判斷出用戶是哪一類。
書架有了,身份有了,接下來就是人對書架進行操作,也就是下面while循環里的內容。
二.框架的具體實現
1.logic方法的實現
先上代碼:
public static Person logic(){System.out.println("歡迎來的圖書管理系統");System.out.println("請輸入您的姓名:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();System.out.println("輸入1:管理員 2:普通用戶");int choice=scanner.nextInt();if(choice==1){return new Manager(name);}else{return new Normal(name);}}
返回值是我們定義的父類Person。通過輸入的選擇,用if分出兩種情況(這里不是很嚴謹,沒有判別輸入其他數字的情況),直接new一個對象返回,new完類也實例化完了。
這樣我們就得到了我們要用戶身份。
2.person.menu方法的實現
person現在是某一個具體的身份,可以調用其類里的menu方法。
//普通用戶
public int menu(){System.out.println("普通用戶界面");System.out.println("1:查閱圖書");System.out.println("2:借閱圖書");System.out.println("3:歸還圖書");System.out.println("0.退出系統");System.out.println("請輸入你的操作:");Scanner scanner=new Scanner(System.in);int choice= scanner.nextInt();return choice;}
//管理員
public int menu(){System.out.println("管理員界面");System.out.println("1:查閱圖書");System.out.println("2:新增圖書");System.out.println("3:刪除圖書");System.out.println("4:顯示圖書");System.out.println("0.退出系統");System.out.println("請輸入你的操作:");Scanner scanner=new Scanner(System.in);int choice= scanner.nextInt();return choice;}
通過輸入數字來選擇我們要進行的操作,并將剛輸入的數字作為返回值去返回。
獲得具體數字后我們可以進行下面的操作來進行具體的實現。
3.person.doFunction方法的實現
public IFunction[] iFunctions;
public void doFunction(int choice, BookList bookList){iFunctions[choice].work(bookList);}
這是Person類中的內容,可以看到我們先定義了一個接口數組,使用了這個接口中的某一個方法。在一開始實例化兩種用戶時我們就用構造方法來實現了數組的“賦值”:
//普通用戶
public Normal(String name) {super(name); this.iFunctions = new IFunction[]{new Exit(),new Seek(),new Borrow(),new Back()};}
//管理員
public Manager(String name) {super(name); this.iFunctions=new IFunction[]{new Exit(),new Seek(),new Add(),new Del(),new Show()};}
這里的順序對應了menu方法里的順序,大家可以去上面查看。
4.接口的實現
public interface IFunction {void work(BookList bookList);
}
在接口里寫了一個work方法,這個就是上面調用的方法。參數是書架,目的是為了操作書架。
三.功能的具體實現
0.前置內容
每一個具體的功能都通過接口連上了IFunction,都要再次實現work方法
iFunctions[choice].work(bookList);
另外在書架上定義一個int變量來記錄書的數量
int size = 0;
1.查閱圖書
public void work(BookList bookList) {System.out.println("查找圖書");System.out.println("請輸入想要查找書的名稱:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();int size=bookList.getSize(); //利用BookList類的方法來獲得書架上書的數量for(int i=0;i<size;i++){String bname=bookList.getname(i);if(bname.equals(name)){ //比較字符串System.out.println("書找到了");return ;}}System.out.println("沒有這本書");}
bookList.getSize()這個方法是獲取現在書架上書的數量,然后進入循環去尋找是否有這本書,比較簡單。
2.借閱圖書
public void work(BookList bookList) {System.out.println("借閱圖書");System.out.println("輸入要借閱書的名稱:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();int choice=bookList.getSize();for(int i=0;i<choice;i++){String bname= bookList.getname(i);if(bname.equals(name)){bookList.changeType(i,true); //用人方法要說哪一個bookList.changeSize(-1);System.out.println("借閱成功");return ;}}System.out.println("沒有這本書無法借閱");}
借閱圖書要實現的細節:
1.把書的屬性設置為已借出;2.書架上書的數量減一。
3.歸還圖書
public void work(BookList bookList) {System.out.println("歸還圖書");System.out.println("輸入要歸還書的名稱:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();int choice=bookList.getSize();for(int i=0;i<choice;i++){String bname= bookList.getname(i);if(bname.equals(name)){bookList.changeType(i,false); //用人方法要說哪一個bookList.changeSize(1);System.out.println("歸還成功");return ;}}System.out.println("沒有這本書無法歸還");}
細節方面與上面的借閱相反。
4.新增圖書
public void work(BookList bookList) {System.out.println("添加書籍");int choice=bookList.getSize();if(choice==10){System.out.println("書架已滿無法繼續添加");return;}Scanner scanner=new Scanner(System.in);System.out.println("請輸入新增書的名稱:");String name=scanner.nextLine();System.out.println("請輸入新增書的作者:");String author=scanner.nextLine();System.out.println("請輸入新增書的類型:");String type=scanner.nextLine();System.out.println("請輸入新增書的價格:");int price=scanner.nextInt();Book newbook=new Book(name,author,price,type);for (int i = 0; i < choice; i++) {String name2=bookList.getname(i);if(newbook.equals(name2)){System.out.println("書架上已經有這本書了");return ;}}bookList.setBooks(choice,newbook);bookList.setSize(choice+1);System.out.println("添加成功");}
5.刪除圖書
public void work(BookList bookList) {System.out.println("刪除書籍");Scanner scanner=new Scanner(System.in);System.out.println("輸入要刪除的書的名稱");String name=scanner.nextLine();int choice=bookList.getSize();int i = 0;int flag=-1;for (; i < choice; i++) {String bname=bookList.getname(i);if(bname.equals(name)){flag=i;break;}}if(i==choice){System.out.println("沒有這本書");return ;}for (int j = flag; j < choice-1; j++) {Book book = bookList.getBooks(j+1);bookList.setBooks(j,book);}bookList.setBooks(choice-1,null);bookList.setSize(choice-1);System.out.println("刪除成功");}
6.退出系統
public void work(BookList bookList) {System.out.println("成功退出系統");System.exit(0);}
四.總結
以上功能的實現不是最主要的,代碼的框架是最主要的。怎么實現一個圖書管理系統,它的整個脈絡是怎樣的,這是重要的。上述功能代碼的實現可能有不嚴謹的地方,大家可以自行修改。