信息网站开发合同,网站建设客户管理系统,郑州手机网站建设多少钱,秦皇岛市教育考试院引言 在我们学习了一些java的基础语法之后#xff0c;需要把这些知识点可以串起来#xff0c;这里使用一个简单的小项目可以很好的帮助我们牢记这些知识点#xff0c;今天就带大家学习一个有关java的小项目#xff0c;很多学校也经常把这个项目作为他们的课程设计——经典的…引言 在我们学习了一些java的基础语法之后需要把这些知识点可以串起来这里使用一个简单的小项目可以很好的帮助我们牢记这些知识点今天就带大家学习一个有关java的小项目很多学校也经常把这个项目作为他们的课程设计——经典的图书管理系统。ps不懂的知识可以看往期文章 这里必须提前给大家说一下要想看懂这篇文章必须得掌握我往期的文章带你玩转java封装和继承(上)JAVA如何利用接口实现多继承问题。只有看明白这些文章才能理解本次图书管理系统的代码。链接给大家放这里了~~ http://t.csdnimg.cn/vYS14 http://t.csdnimg.cn/DQynp 图书管理系统界面
普通用户界面菜单
1.查找图书
2.新增图书
3.删除图书
4.显示图书
0.退出系统
---------------------------------------------------------------------------------------------------------------------------------
管理员界面菜单
1.查找图书
2.借阅图书
3.归还图书
0.退出系统
如何搭建基本框架 首先我们先创建一些项目里一些我们需要用到基础的类来完善我们整个逻辑框架 首先右击src按步骤创建一个User的包这个包里首先放的就是我们的的用户类的一些功能和管理员类的一些功能。根据我们上面的需求两者都需要对应的功能菜单。
AdminUser
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(*****************************);System.out.println(请输入你的操作);Scanner scannernew Scanner(System.in);int choicescanner.nextInt();return choice;}
NormalUser
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 scannernew Scanner(System.in);int choicescanner.nextInt();return choice;} 这里有一个细节不知道你有没有发现这两段代码的菜单打印我都使用的是int返回值这是为什么呢原因其实仔细思考一下就可以得出答案由于二者的菜单内容的不同我们需要根据不同的对象打印不同的菜单为了区分这两个对象我们根据返回值的不同进行区分。现在看不懂没关系看到后面的代码你就明白了。 User 添加这个类的原因是在AdminUser和NormalUser中有许多共性的部分我们可以把这些部分抽取出来创建一个新的类User只需要继承就好了。 我们还需要一个main方法来测试我们代码的正确性这里创建一个main类
public class Main {public static User login(){Scanner scannernew Scanner(System.in);System.out.println(请输入你的姓名);String namescanner.nextLine();System.out.println(请输入你的身份1管理员 2普通用户);int choicescanner.nextInt();if(choice1){AdminUesr adminUesrnew AdminUesr(name);return adminUesr;}else{NormalUser normalUsernew NormalUser(name);return normalUser;}}public static void main(String[] args) {User user login();BookList bookList new BookList();while(true) {int choice user.menu();user.doIoperations(choice, bookList);}}
}那么我们有了用户的编写接下来就得管理图书了同样的我们新建一个Book包里面存放有关书的一些类大体上是book类和bookList类
book public class book {private String name;private String author;private int price;private String type;private boolean isLend;public String getName() {return name;}public void setName(String name) {this.name name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author author;}public int getPrice() {return price;}public void setPrice(int price) {this.price price;}public String getType() {return type;}public void setType(String type) {this.type type;}public boolean isLend() {return isLend;}public void setLend(boolean lend) {isLend lend;}public book(String name, String author, int price, String type) {this.name name;this.author author;this.price price;this.type type;}Overridepublic String toString() {return book{ name name \ , author author \ , price price , type type \ (isLendtrue ? 已借出 : 未借出)
// , isLend isLend };}} 好了有了书我们还得有一个书架把书管理起来这里我们创建一个bookList类 我们给这里直接放了三本书并且设置了此时书的个数usedSize这样操作起来比较方便。 好了上面把基础的一些信息打印好了现在我们需要实现具体的功能了即查找图书添加图书...,那么我如何写这些功能呢由于用户与管理员的不同直接以类的形式写比较麻烦我们这里将所有的功能都以接口的形式实现你需要哪个功能直接引用就好了。 这里我们创建一个IOperation接口里面实现一个抽象方法(work)当每个功能类引用这个接口之后都可以用这个work方法来对书架上的书进行操作。
package Operation;
import book.BookList;
public interface IOperation {void work(BookList bookList);
}实现业务:
接下来就实现每个功能了
AddOperation
public class AddOperation implements IOperation{public void work(BookList bookList){System.out.println(新增图书.....);if (bookList.isFull()) {System.out.println(书架满了不能新增图书了);return;}Scanner scannernew Scanner(System.in);System.out.println(请输入你要新增的书名);String bookNamescanner.nextLine();System.out.println(请输入你要新增的图书的作者);String authorscanner.nextLine();System.out.println(请输入你要新增的图书价格);int pricescanner.nextInt();System.out.println(请输入你要新增的图书的类型);String typescanner.nextLine();book book1new book(bookName,author,price,type);int currentSize bookList.getUsedSize();bookList.setBook(currentSize,book1);bookList.setUsedSize(currentSize1);System.out.println(新增图书成功);}
}这里提前给大家说一下为了管理方法方便我们将所有的功能都引用一个IOperation接口后面创建一个IOperation数组你的对象需要什么功能直接方到数组里面就好了这样就完成了不同对象不同功能的实现。 ShowOperation 思路给方法的参数传一个书架类型bookList获取当前的书的个数利用for循环进行打印总体来说比较简单。 package Operation;import book.BookList;
import book.book;public class ShowOperation implements IOperation{public void work(BookList bookList){System.out.println(显示图书.....);int currentSize bookList.getUsedSize();for (int i0;icurrentSize;i) {book book1 bookList.getbooks(i);System.out.println(book1);}}
}BorrowOperation 思路首先还是得获取一个书架这样才可以操作数组里的书然后输入你要查找的书名获取书架上书的个数使用for循环找到你要的书然后我们这里是将你借走后的书的状态改为true代表这本书被借走。(这里说一下寻找书的时候我们使用equls函数用于判别两个字符串是否相同)。 package Operation;import book.BookList;import java.util.Scanner;import book.book;
public class BorrowOperation implements IOperation{public void work(BookList bookList){System.out.println(借阅图书.....);System.out.println(请输入你要借阅图书的书名);Scanner scannernew Scanner(System.in);String bookNamescanner.nextLine();int currentSize bookList.getUsedSize();for(int i0;icurrentSize;i){book book1bookList.getbooks(i);if(bookName.equals(book1.getName())){book1.setLend(true);System.out.println(找到你要借阅的图书了);System.out.println(book1);return;}}System.out.println(没有你要借阅的图书);}
}DelOperation 这里我们对于删除的理解是在这个书架上找到删除的书的下一本然后依次往上面覆盖这样完成的删除操作同时别忘了删除后一定要将图书的个数减一哦~ 比如此时我们要删除红楼梦 package Operation;import book.BookList;
import book.book;import java.util.Scanner;public class DelOperation implements IOperation{public void work(BookList bookList){System.out.println(删除图书.....);System.out.println(请输入你要删除的书名);Scanner scannernew Scanner(System.in);String bookNamescanner.nextLine();int currentSize bookList.getUsedSize();int pos-1;for (int i0;icurrentSize;i) {book book1bookList.getbooks(i);if(book1.getName().equals(bookName)){posi;break;}if(icurrentSize){System.out.println(没有你要删除的图书);return;}for (int jpos;jcurrentSize-1;j) {book book2bookList.getbooks(j1);bookList.setBook(j,book2);}bookList.setUsedSize(currentSize-1);bookList.setBook(currentSize-1,null);System.out.println(删除成功);}}
}FindOperation 找书的业务有很多种按书名找按作者找.....这里我们统一采用按书名去找那么我们选择一个for循环找到我们需要的书名最后打印出来。 package Operation;import book.BookList;
import book.book;
import java.util.Scanner;public class FindOpertion implements IOperation{public void work(BookList bookList) {System.out.println(查找图书.....);System.out.println(请输入你要查找的图书名);Scanner scanner new Scanner(System.in);String bookName scanner.nextLine();int currentSize bookList.getUsedSize();for (int i 0; i currentSize; i) {//Book book bookList[i];book book1 bookList.getbooks(i);if (book1.getName().equals(bookName)) {System.out.println(找到了这本书);System.out.println(book1);return;}}System.out.println(没有你要找的书....);}
}ReturnOperation 其实这里和借书的操作基本一致都是先找到你要操作的书名后面将书的状态改为false代表这本书仍然在书架上。 package Operation;import book.BookList;
import book.book;import java.util.Scanner;public class ReturnOPeration implements IOperation{public void work(BookList bookList){System.out.println(归还图书.....);System.out.println(请输入你要归还图书的书名);Scanner scannernew Scanner(System.in);String bookNamescanner.nextLine();int currentSize bookList.getUsedSize();for(int i0;icurrentSize;i){book book3bookList.getbooks(i);if(bookName.equals(book3.getName())){book3.setLend(false);return;}}System.out.println(没有你要归还的图书);return;}
}Showoperation 查看图书也是比较简单的我们使用for循环将书架上面的书依次打印出来就好了。 package Operation;import book.BookList;
import book.book;public class ShowOperation implements IOperation{public void work(BookList bookList){System.out.println(显示图书.....);int currentSize bookList.getUsedSize();for (int i0;icurrentSize;i) {book book1 bookList.getbooks(i);System.out.println(book1);}}
}
文章转载自: http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.fddfn.cn.gov.cn.fddfn.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.dighk.com.gov.cn.dighk.com http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.ycnqk.cn.gov.cn.ycnqk.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.nngq.cn.gov.cn.nngq.cn