因为文件过多每个文件之间的关系如下(每个文件中都只有一个类):
因为JAVA属于面向对象编程的语言,所以我们想要实现图书管理系统就得分以下几步:
- 找出其中的所有的对象
- 实现所有的对象
- 完成对象之间的交互
在图书管理系统中我们可以想到这几个对象:书,用户(用户还可以分为普通用户和管理员),还得有存放书的书架。
找出对象之后我们将它们分别放在两个包中(一个放书和书架,另一个放普通用户和管理员)。
我们可以在书架类中创建一个书的数组,这样就相当于把书放在了书架中。
package book; public class BookList { public Book[] books;//书架 public int numBooks;//书架实际存放的书的本书 }
此时你还可以将书类中的属性都用private修饰因为后面我们的所有操作都是针对书架(如果用private修饰就需要对每个属性都实现get 和 set 方法)。
书类包含以下属性:
String name;
String author;
int money;
String type;
boolean isBorrow;
以下方法:
publicBook(){}; publicBook(Stringname, Stringauthor, intmoney, Stringkind) {} publicStringgetName() {} publicvoidsetName(Stringname) {} publicStringgetAuthor() {} publicvoidsetAuthor(Stringauthor) {} publicintgetMoney() {} publicvoidsetMoney(intmoney) {} publicStringgetType() {} publicvoidsetType(Stringkind) {} publicbooleangetBorrow() {} publicvoidsetBorrow(booleanborrow) {} publicStringtoString() { return"name='"+name+'\''+", author='"+author+'\''+", money="+money+", kind='"+type+'\''+", 状态:"+(isBorrow==false?"未被借出" : "已被借出"); }
此处重写toString是为了可以直接使用System.out.println();输出book
因为普通用户和管理员都属于用户所以可与使用继承让 普通用户和管理员都继承用户这个父类。
这样我们就可以利用多态的思想,创建一个用户类,由于引用的对象不同调用相同的方法结果也不相同。
可是现在又有一个新问题就是管理员和普通用户所打印出来的菜单是不同的,所以就不能在父类中实现,所以父类就不能是一个普通的父类,它必须是一个抽象类。
因为用户的操作太多而且有重复方法所以这里推荐新建一个包将其全部放入新的包中,并将所有的操作都单独封装成一个类。
操作属于用户所以写成如下格式:
//用户类abstractpublicclassUser { protectedStringname; protectedWork[] work;//存放用户所有的操作publicintcounst; //work中的元素个数} //管理员publicclassAdvancedUserextendsUser{ publicAdvancedUser(Stringname){ this.name=name; this.work=newWork[]{ newExitOperation(), newFindOperation(), newAddOperation(), newDelOperation(), newShowOperation() }; this.counst=5; } } //普通用户publicclassNormalUserextendsUser { publicNormalUser(Stringname){ this.name=name; this.work=newWork[]{ newExitOperation(), newFindOperation(), newBorrowOperation(), newRetOperation(), }; this.counst=4; } }
这样就可以用数组来调用方法
publicvoidoperation(intchoice, BookListbookList){ work[choice].work(bookList); }
完整代码:
packagemain; importbook.BookList; importuser.AdvancedUser; importuser.NormalUser; importuser.User; importjava.util.Scanner; publicclassMain { publicstaticUserlogin(){ Scannerin=newScanner(System.in); System.out.println("请输入你的姓名:"); Stringname=in.nextLine(); System.out.println("请选择你的身份:1.管理员 0.普通用户"); if (in.nextInt() ==1){ returnnewAdvancedUser(name); }else { returnnewNormalUser(name); } } publicstaticvoidmain(String[] args) { BookListbookList=newBookList(); Useruser=login(); while(true){ intchoice; do{ choice=user.menu(); }while(choice<0||choice>=user.counst); user.operation(choice, bookList); } } }
packagebook; publicclassBook { privateStringname; privateStringauthor; privateintmoney; privateStringtype; privatebooleanisBorrow; publicBook(){}; publicBook(Stringname, Stringauthor, intmoney, Stringkind) { this.name=name; this.author=author; this.money=money; this.type=kind; } publicStringgetName() { returnname; } publicvoidsetName(Stringname) { this.name=name; } publicStringgetAuthor() { returnauthor; } publicvoidsetAuthor(Stringauthor) { this.author=author; } publicintgetMoney() { returnmoney; } publicvoidsetMoney(intmoney) { this.money=money; } publicStringgetType() { returntype; } publicvoidsetType(Stringkind) { this.type=kind; } publicbooleangetBorrow() { returnisBorrow; } publicvoidsetBorrow(booleanborrow) { isBorrow=borrow; } publicStringtoString() { return"name='"+name+'\''+", author='"+author+'\''+", money="+money+", kind='"+type+'\''+", 状态:"+(isBorrow==false?"未被借出" : "已被借出"); } }
packagebook; publicclassBookList { publicBook[] books;//书架publicintnumBooks;//书架实际存放的书的本书publicBookList() { this.books=newBook[10]; //默认书架存放两本书this.books[0] =newBook("西游记","吴承恩",39,"小说"); this.books[1] =newBook("红楼梦","曹雪芹",39,"小说"); this.numBooks=2; } }
packageoperation; importbook.Book; importbook.BookList; importjava.util.Scanner; publicclassAddOperationextendsWork{ publicvoidwork(BookListbookList) { Scannerin=newScanner(System.in); bookList.books[bookList.numBooks] =newBook(); System.out.print("请输入书名:"); bookList.books[bookList.numBooks].setName(in.nextLine()); System.out.print("请输入作者:"); bookList.books[bookList.numBooks].setAuthor(in.nextLine()); System.out.print("请输入所属书类:"); bookList.books[bookList.numBooks].setType(in.nextLine()); System.out.print("请输入书价:"); bookList.books[bookList.numBooks].setMoney(in.nextInt()); bookList.numBooks++; } }
packageoperation; importbook.BookList; importjava.util.Scanner; publicclassBorrowOperationextendsWork{ publicvoidwork(BookListbookList) { intnumBooks=bookList.numBooks; Scannerin=newScanner(System.in); System.out.print("请输入书名:"); Stringname=in.nextLine(); for (inti=0; i<numBooks; i++) { if (name.equals(bookList.books[i].getName())) { if (bookList.books[i].getBorrow() ==false){ System.out.println("借阅成功"); bookList.books[i].setBorrow(true); return; }else{ System.out.println("该书已被借出"); return; } } } System.out.println("未收录该书"); } }
packageoperation; importbook.BookList; importjava.util.Scanner; publicclassDelOperationextendsWork{ publicvoidwork(BookListbookList) { intnumBooks=bookList.numBooks; Scannerin=newScanner(System.in); System.out.print("请输入书名:"); Stringname=in.nextLine(); for (inti=0; i<numBooks; i++) { if (name.equals(bookList.books[i].getName())){ intj=0; for (j=i; j<numBooks-1; j++) { bookList.books[j] =bookList.books[j+1]; } bookList.books[j] =null; bookList.numBooks--; return; } } } }
packageoperation; importbook.BookList; publicclassExitOperationextendsWork{ publicvoidwork(BookListbookList) { System.exit(0); } }
packageoperation; importbook.BookList; importjava.util.Scanner; publicclassFindOperationextendsWork{ publicvoidwork(BookListbookList) { intnumBooks=bookList.numBooks; Scannerin=newScanner(System.in); System.out.print("请输入书名:"); Stringname=in.nextLine(); for (inti=0; i<numBooks; i++) { if (name.equals(bookList.books[i].getName())) { System.out.println(bookList.books[i]); return; } } System.out.println("未收录该书"); } }
packageoperation; importbook.BookList; importjava.util.Scanner; publicclassRetOperationextendsWork{ publicvoidwork(BookListbookList) { intnumBooks=bookList.numBooks; Scannerin=newScanner(System.in); System.out.print("请输入书名:"); Stringname=in.nextLine(); for (inti=0; i<numBooks; i++) { if (name.equals(bookList.books[i].getName())) { if (bookList.books[i].getBorrow()){ System.out.println("归还成功"); bookList.books[i].setBorrow(false); return; }else{ System.out.println("该书未被借出"); return; } } } System.out.println("未收录该书"); } }
packageoperation; importbook.BookList; publicclassShowOperationextendsWork{ publicvoidwork(BookListbookList) { for (inti=0; i<bookList.numBooks; i++) { System.out.println(bookList.books[i]); } } }
packageoperation; importbook.BookList; abstractpublicclassWork { abstractpublicvoidwork(BookListbookList); }
packageuser; importoperation.*; importjava.util.Scanner; publicclassAdvancedUserextendsUser{ publicAdvancedUser(Stringname){ this.name=name; this.work=newWork[]{ newExitOperation(), newFindOperation(), newAddOperation(), newDelOperation(), newShowOperation() }; this.counst=5; } publicintmenu() { System.out.println("###########################"); System.out.println(" Hello "+"AdvancedUser:"+this.name); System.out.println("1.查找图书"); System.out.println("2.新增图书"); System.out.println("3.删除图书"); System.out.println("4.显示图书"); System.out.println("0.退出系统"); System.out.println("###########################"); System.out.println("请选择你的操作:"); Scannerin=newScanner(System.in); returnin.nextInt(); } }
packageuser; importoperation.*; importjava.util.Scanner; publicclassNormalUserextendsUser { publicNormalUser(Stringname){ this.name=name; this.work=newWork[]{ newExitOperation(), newFindOperation(), newBorrowOperation(), newRetOperation(), }; this.counst=4; } publicintmenu() { System.out.println("###########################"); System.out.println(" Hello "+"NormalUser:"+this.name); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("###########################"); System.out.println("请选择你的操作:"); Scannerin=newScanner(System.in); returnin.nextInt(); } }
packageuser; importbook.BookList; importoperation.Work; abstractpublicclassUser { protectedStringname; protectedWork[] work;//存放用户所有的操作publicintcounst; //work中的元素个数abstractpublicintmenu(); publicvoidoperation(intchoice, BookListbookList){ work[choice].work(bookList); } }
运行结果: