作者有话要说:
关于这篇博客,主要记录上课的一个作业,作业任务要求如下(一.图书管理系统任务书),该项目主要就是对JavaSE部分知识进行练习(主要涉及封装继承多态集合),没有和数据后端进行交互,也没有使用任何框架。
一.图书管理系统任务书:
描述: 图书管理系统会因为图书的数量、种类、提供的操作等不同而具有不同的复杂度。基本信息的维护、图书借阅、归还及查询等操作通常是图书管理系统的基本功能。在规模较大、业务较多的图书馆还需要图书的库存管理、销售管理等更加复杂的功能。 相关数据表格: 功能实现要求:
- 实现接口IAction,其包含add(插入)、del(删除)、getCount(数据大小 )、getlistInfo(列表输出)和Find(查找)五个方法。图书信息类、读者信息类、借阅信息类和用户信息类实现IAction接口的方法,要求使用集合框架技术。测试类中分别对各类进行插入、删除、大小 、列表输出、查询等相关方法进行测试。
2.实现图书管理系统的用户交互功能,包括用户菜单的显示与用户选择菜单后对数据的新增、删除、查询、列表等功能(分别实现对图书信息、读者信息、借阅信息和用户信息的操作)。具体实现的功能菜单如下: 0:系统退出 1:图书信息列表显示 2:图书信息新增 3:图书信息删除 4:图书信息查找(按书名查找) 6:读者信息列表显示 7:读者信息新增 8:读者信息删除 9:读者信息查找(按姓名查找) 10:借阅信息列表显示 11:借阅信息新增 12:借阅信息删除 13:借阅信息查找(按书名查找) 14:用户信息列表显示 15:用户信息新增 16:用户信息删除 17:用户信息查找(按用户名查找)
二.项目结构
1.项目框架 2.SystemData.java: 由于没有和数据库进行数据交互,该系统的数据通过静态属性进行对象的共享: 相关学习链接: java基础回顾—static关键字
3.LibraySystem.java LibraySystem.java就是该图书管理的整体框架: 通过start()启动该图书管理系统: 由于几个功能模块的功能就只有CRUD操作,所用使用一个复合方法,参数为IAction接口(多态的练习): 完整代码:
package com.zhumin.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import com.zhumin.librarysystem.ben.Book;
import com.zhumin.librarysystem.ben.Borrow;
import com.zhumin.librarysystem.ben.Reader;
import com.zhumin.librarysystem.ben.User;
import com.zhumin.service.IAction;
public class LibraySystem {
public static Map<Integer, String>MANAGERVIEW_MENU,LIBRAYSYSTE_MMENU;
static{
SystemData.BORROWLIST_SYSTEM=new ArrayList<>();
SystemData.TEST_USER=new User("10001", "admin", "123456", true);
ArrayList<Book>aBooks=new ArrayList<>();
aBooks.add(new Book("AA001001", "Java基础入门", "计算机", "张三", "无", "清华大学出版社", new Date(), 44.5,50));
aBooks.add(new Book("AA001002", "Java编程思想", "计算机", "李四", "无", "机械工业出版社", new Date(), 108,100));
aBooks.add(new Book("AA001003", "数据结构", "计算机", "王五", "无", "电子工业出版社", new Date(), 88,50));
SystemData.BOOKLIST_SYSTEM=aBooks;
ArrayList<User>aUsers=new ArrayList<>();
aUsers.add(SystemData.TEST_USER);
aUsers.add(new User("1001", "张三", "123456", false));
aUsers.add(new User("1002", "李四", "123456", false));
aUsers.add(new User("1003", "王麻子", "123456", false));
aUsers.add(new User("1004", "王宝强", "123456", false));
aUsers.add(new User("1005", "卢本伟", "123456", false));
SystemData.USERLIST_SYSTEM=aUsers;
ArrayList<Reader>aReaders=new ArrayList<>();
aReaders.add(new Reader("1001", "张三", "普通读者", "男", 5, 30));
aReaders.add(new Reader("1002", "李四", "普通读者", "男", 5, 30));
aReaders.add(new Reader("1003", "王麻子", "普通读者", "男", 5, 30));
aReaders.add(new Reader("1004", "王宝强", "普通读者", "男", 5, 30));
aReaders.add(new Reader("1005", "卢本伟", "普通读者", "男", 5, 30));
SystemData.READERLIST_SYSTEM=aReaders;
ArrayList<Borrow>aBorrows=new ArrayList<>();
aBorrows.add(new Borrow("AA001001", "123456789", "1001", "2021-10-27", "未还", false));
aBorrows.add(new Borrow("AA001001", "985522212", "1002", "2021-10-27", "未还", false));
aBorrows.add(new Borrow("AA001001", "464682223", "1002", "2021-10-27", "未还", false));
aBorrows.add(new Borrow("AA001001", "135656132", "1003", "2021-10-27", "未还", false));
MANAGERVIEW_MENU=new HashMap<Integer, String>();
MANAGERVIEW_MENU.put(1, "1:列表显示");
MANAGERVIEW_MENU.put(2, "2:信息新增");
MANAGERVIEW_MENU.put(3, "3:信息删除");
MANAGERVIEW_MENU.put(4, "4:信息查找");
MANAGERVIEW_MENU.put(5, "5:返回上一级");
LIBRAYSYSTE_MMENU=new HashMap<>();
LIBRAYSYSTE_MMENU.put(0, "你已进入退出模块~");
LIBRAYSYSTE_MMENU.put(1, "欢迎来到图书管理模块~");
LIBRAYSYSTE_MMENU.put(2, "欢迎来到借阅管理模块~");
LIBRAYSYSTE_MMENU.put(3, "欢迎来到读者管理模块~");
LIBRAYSYSTE_MMENU.put(4, "欢迎来到用户管理模块~");
}
public void start() {
System.out.println("----------------图书系统----------------");
System.out.println("欢迎来到图书系统!");
if (!SystemData.STATUS_USER) {
login();
}
System.out.println("欢迎"+SystemData.CURRENT_USER.getUsername()+"进入图书管理系统~");
while(SystemData.STATUS_USER){
System.out.println("图书系统功能面板:");
System.out.println("0:系统退出,1:图书管理页面,2:借阅管理页面,3:读者信息页面,4:用户信息页面");
System.out.println("输入你的选项:");
int num=new Scanner(System.in).nextInt();
if (num>=0&&num<=LIBRAYSYSTE_MMENU.size()) {
System.out.println(LIBRAYSYSTE_MMENU.get(num));
}
switch (num) {
case 0:
exit();
break;
case 1:
operateReuse(new Book(),MANAGERVIEW_MENU);
break;
case 2:
operateReuse(new Borrow(),MANAGERVIEW_MENU);
break;
case 3:
operateReuse(new Reader(),MANAGERVIEW_MENU);
break;
case 4:
operateReuse(new User(),MANAGERVIEW_MENU);
default:
System.out.println("无效输入,请重新输入!");
break;
}
}
}
private void login() {
SystemData.STATUS_USER=true;
if (SystemData.CURRENT_USER==null) {
while(true){
System.out.println("当前没有用户进行登入!");
System.out.println("1:登入用户,2:注册用户");
System.out.println("输入你的选项数字:");
int num=new Scanner(System.in).nextInt();
if (num==2) {
new User().add();
break;
}else if (num==1) {
String userName,passWord;
System.out.println("输入账号:");
userName=new Scanner(System.in).nextLine();
System.out.println("输入密码:");
passWord=new Scanner(System.in).nextLine();
User user=new User();
user.setUsername(userName);
user.setPassword(passWord);
if (user.userLogin(user)) {
System.out.println("登入成功!");
break;
}else {
System.out.println("登入失败!");
}
}else {
System.out.println("无效输入,请重新输入!");
}
}
}
}
private void exit() {
SystemData.STATUS_USER=false;
System.out.println("欢迎下次光临!");
System.out.println("已经退出系统!");
return;
}
private void operateReuse(IAction iAction,Map<Integer, String> menu) {
while (true) {
showMenu(menu);
switch (new Scanner(System.in).nextInt()) {
case 1:
iAction.getlistInfo();
break;
case 2:
iAction.add();
break;
case 3:
iAction.del();
break;
case 4:
iAction.find();
break;
case 5:
return;
default:
System.out.println("无效输入,请重新输入!");
break;
}
}
}
private void showMenu(Map<Integer, String> menu) {
System.out.println("菜单:");
for (Map.Entry<Integer, String> entry :menu.entrySet()) {
System.out.println(entry.getValue());
}
System.out.println("输入你的选项:");
}
}
4.Book,Borrow,Reader,User Book,Borrow,Reader,User实现IAction接口,并且实现接口的方法: (这里的CRUD操作其实就是集合的操作,对集合知识进行练习) Book.java:
package com.zhumin.librarysystem.ben;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import com.zhumin.controller.SystemData;
import com.zhumin.service.IAction;
public class Book implements IAction{
private String id="AA0000001";
private String bookname;
private String booktype="科技";
private String author;
private String translator;
private String publisher;
private Date publish_time;
private double price=28;
private int stock=1;
public Book(String id, String bookname, String booktype, String author, String translator, String publisher,
Date publish_time, double price, int stock) {
this.id = id;
this.bookname = bookname;
this.booktype = booktype;
this.author = author;
this.translator = translator;
this.publisher = publisher;
this.publish_time = publish_time;
this.price = price;
this.stock = stock;
}
public Book() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTranslator() {
return translator;
}
public void setTranslator(String translator) {
this.translator = translator;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Date getPublish_time() {
return publish_time;
}
public void setPublish_time(Date publish_time) {
this.publish_time = publish_time;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return "图书编号:" + id + ", 图书名:" + bookname + ", 图书类型:" + booktype + ", 作者:" + author
+ ", 翻译:" + translator + ", 出版社:" + publisher + ", 出版时间:" + new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒").format(publish_time)
+ ", 价格:" + price + ", 库存:" + stock + "]";
}
@Override
public boolean add() {
Scanner scanner=new Scanner(System.in);
Book book=new Book();
System.out.println("输入图书编号:");
book.setId(new Scanner(System.in).nextLine());
System.out.println("输入图书名称");
book.setBookname(new Scanner(System.in).nextLine());
System.out.println("输入图书类别");
book.setBooktype(new Scanner(System.in).nextLine());
System.out.println("输入图书作者");
book.setAuthor(new Scanner(System.in).nextLine());
System.out.println("输入图书译者");
book.setTranslator(new Scanner(System.in).nextLine());
System.out.println("输入图书出版社");
book.setPublisher(new Scanner(System.in).nextLine());
System.out.println("输入图书定价");
book.setPrice(new Scanner(System.in).nextDouble());
System.out.println("输入图书库存数量");
book.setStock(new Scanner(System.in).nextInt());
book.setPublish_time(new Date());
SystemData.BOOKLIST_SYSTEM.add(book);
return true;
}
@Override
public boolean del() {
System.out.println("输入你要删除图书的编号:");
String num=new Scanner(System.in).nextLine();
ArrayList<Book>aBooks=SystemData.BOOKLIST_SYSTEM;
for (int i = 0; i < aBooks.size(); i++) {
if (num.equals(aBooks.get(i).getId())) {
SystemData.BOOKLIST_SYSTEM.remove(i);
System.out.println("删除成功!");
return true;
}
}
System.out.println("删除失败!");
return false;
}
@Override
public int getCount() {
return SystemData.BOOKLIST_SYSTEM.size();
}
@Override
public void getlistInfo() {
ArrayList<Book>aBooks=SystemData.BOOKLIST_SYSTEM;
if (aBooks.size()>0) {
for(Book book:aBooks){
System.out.println(book.toString());
}
}else {
System.out.println("无任何信息~");
}
}
@Override
public boolean find() {
System.out.println("输入你要查询图书的编号:");
String num=new Scanner(System.in).nextLine();
ArrayList<Book>aBooks=SystemData.BOOKLIST_SYSTEM;
for(Book book:aBooks){
if (num.equals(book.getId())) {
System.out.println(book.toString());
return true;
}
}
System.out.println("当前图书系统不存在该图书!");
return false;
}
}
Borrow.java:
package com.zhumin.librarysystem.ben;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import com.zhumin.controller.SystemData;
import com.zhumin.librarysystem.util.CreateSerialNumberUtil;
import com.zhumin.service.IAction;
public class Borrow implements IAction{
private String book_id;
private String id;
private String reader_id;
private String borrow_date;
private String back_date;
private boolean if_back;
public Borrow(String book_id, String id, String reader_id, String borrow_date, String back_date,
boolean if_back) {
this.book_id = book_id;
this.id = id;
this.reader_id = reader_id;
this.borrow_date = borrow_date;
this.back_date = back_date;
this.if_back = if_back;
}
public Borrow() {
}
public String getBook_id() {
return book_id;
}
public void setBook_id(String book_id) {
this.book_id = book_id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getReader_id() {
return reader_id;
}
public void setReader_id(String reader_id) {
this.reader_id = reader_id;
}
public String getBorrow_date() {
return borrow_date;
}
public void setBorrow_date(String borrow_date) {
this.borrow_date = borrow_date;
}
public String getBack_date() {
return back_date;
}
public void setBack_date(String back_date) {
this.back_date = back_date;
}
public boolean isIf_back() {
return if_back;
}
public void setIf_back(boolean if_back) {
this.if_back = if_back;
}
@Override
public String toString() {
return "图书编号:" + book_id + ", 流水号:" + id + ", 读者编号:" + reader_id + ", 借书时间:"
+ borrow_date + ", 归还时间:" + back_date + ", 是否归还:" + (if_back==true?"归还":"未归还") + "";
}
@Override
public boolean add() {
Scanner scanner=new Scanner(System.in);
System.out.println("输入图书编号:");
String id=scanner.nextLine();
System.out.println("输入读者编号:");
String readerId=scanner.nextLine();
System.out.println("输入借书时间:");
String time1=scanner.nextLine();
System.out.println("输入还书时间:");
String time2=scanner.nextLine();
boolean data=false;
while (true) {
System.out.println("是否还书:1:是,2:否");
int if_data=scanner.nextInt();
if (if_data==1) {
data=true;
break;
}else if (if_data==2) {
data=false;
break;
}else {
System.out.println("请重新输入!");
}
}
Borrow borrow=new Borrow(id, CreateSerialNumberUtil.getSerialBorrowNumber(id), readerId,time1, time2,data);
SystemData.BORROWLIST_SYSTEM.add(borrow);
return false;
}
@Override
public boolean del() {
System.out.println("输入你要删除借阅信息的图书编号:");
String num=new Scanner(System.in).nextLine();
ArrayList<Borrow>aBorrows=SystemData.BORROWLIST_SYSTEM;
for (int i = 0; i < aBorrows.size(); i++) {
if (aBorrows.get(i).getBook_id().equals(num)) {
SystemData.BORROWLIST_SYSTEM.remove(i);
System.out.println("删除成功!");
return true;
}
}
System.out.println("删除失败!");
return false;
}
@Override
public int getCount() {
return SystemData.BORROWLIST_SYSTEM.size();
}
@Override
public void getlistInfo() {
ArrayList<Borrow>aBorrows=SystemData.BORROWLIST_SYSTEM;
if (aBorrows.size()>0) {
for(Borrow borrow:aBorrows){
System.out.println(borrow.toString());
}
}else {
System.out.println("无任何信息~");
}
}
@Override
public boolean find() {
System.out.println("输入你要查询借阅信息的读者编号:");
String num=new Scanner(System.in).nextLine();
ArrayList<Borrow>aBorrows=SystemData.BORROWLIST_SYSTEM;
for(Borrow borrow:aBorrows){
if (num.equals(borrow.getReader_id())) {
System.out.println(borrow.toString());
return true;
}
}
System.out.println("当前图书系统不存在该读者信息!");
return false;
}
}
Reader.java:
package com.zhumin.librarysystem.ben;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import com.zhumin.controller.SystemData;
import com.zhumin.service.IAction;
public class Reader implements IAction{
private String id="AA000001";
private String readername;
private String readertype;
private String sex;
private int max_num;
private int days_num;
public Reader(String id, String readername, String readertype, String sex, int max_num, int days_num) {
this.id = id;
this.readername = readername;
this.readertype = readertype;
this.sex = sex;
this.max_num = max_num;
this.days_num = days_num;
}
public Reader() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getReadername() {
return readername;
}
public void setReadername(String readername) {
this.readername = readername;
}
public String getReadertype() {
return readertype;
}
public void setReadertype(String readertype) {
this.readertype = readertype;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getMax_num() {
return max_num;
}
public void setMax_num(int max_num) {
this.max_num = max_num;
}
public int getDays_num() {
return days_num;
}
public void setDays_num(int days_num) {
this.days_num = days_num;
}
@Override
public String toString() {
return "读者编号:" + id + ", 读者姓名:" + readername + ", 读者类型:" + readertype + ", 读者性别:" + sex
+ ", 最大可借书数:" + max_num + ", 可借天数:" + days_num;
}
@Override
public boolean add() {
Reader reader=new Reader();
Scanner objScanner=new Scanner(System.in);
System.out.println("请输入读者编号:");
reader.setId(objScanner.nextLine());
System.out.println("请输入读者姓名:");
reader.setReadername(objScanner.nextLine());
reader.setReadertype("普通读者");
System.out.println("请输入读者性别:1:男,2:女");
while (true) {
int num=objScanner.nextInt();
if (num==1) {
reader.setSex("男");
break;
}else if (num==2) {
reader.setSex("女");
break;
}else {
System.out.println("输入有误,请重新输入!");
}
}
reader.setMax_num(5);
reader.setDays_num(15);
SystemData.READERLIST_SYSTEM.add(reader);
return SystemData.CURRENT_READER!=null?true:false;
}
@Override
public boolean del() {
System.out.println("输入你要删除读者的编号:");
String num=new Scanner(System.in).nextLine();
ArrayList<Reader>aReaders=SystemData.READERLIST_SYSTEM;
for (int i = 0; i < aReaders.size(); i++) {
if (aReaders.get(i).getId().equals(num)) {
SystemData.READERLIST_SYSTEM.remove(i);
System.out.println("删除成功!");
return true;
}
}
System.out.println("删除失败!");
return false;
}
@Override
public int getCount() {
return SystemData.READERLIST_SYSTEM.size();
}
@Override
public void getlistInfo() {
ArrayList<Reader>aReaders=SystemData.READERLIST_SYSTEM;
if (aReaders.size()>0) {
for(Reader reader:aReaders){
System.out.println(reader.toString());
}
}else {
System.out.println("无任何信息~");
}
}
@Override
public boolean find() {
System.out.println("输入你要查询读者的编号:");
String num=new Scanner(System.in).nextLine();
ArrayList<Reader>aReaders=SystemData.READERLIST_SYSTEM;
for(Reader reader:aReaders){
if (num.equals(reader.getId())) {
System.out.println(reader.toString());
return true;
}
}
System.out.println("当前图书系统不存在该读者信息!");
return false;
}
}
User.java:
package com.zhumin.librarysystem.ben;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import com.zhumin.controller.SystemData;
import com.zhumin.librarysystem.util.CreateSerialNumberUtil;
import com.zhumin.service.IAction;
public class User implements IAction{
private String id;
private String username;
private String password;
private boolean is_admin;
public User(String id, String username, String password, boolean is_admin) {
this.id = id;
this.username = username;
this.password = password;
this.is_admin = is_admin;
}
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isIs_admin() {
return is_admin;
}
public void setIs_admin(boolean is_admin) {
this.is_admin = is_admin;
}
@Override
public String toString() {
return "用户编号:" + id + ", 用户姓名:" + username + ", 用户密码:" + password + ", 是否为管理员:"
+ (is_admin==true?"是":"不是" );
}
@Override
public boolean add() {
User createUser=new User();
Scanner objScanner=new Scanner(System.in);
System.out.println("请输入账号:");
createUser.setUsername(objScanner.nextLine());
System.out.println("请输入密码:");
createUser.setPassword(objScanner.nextLine());
createUser.setId(CreateSerialNumberUtil.getSerialNumber());
while(true){
System.out.println("是否为管理员,1表示是,2表示不是");
int num=objScanner.nextInt();
if (num==1) {
createUser.setIs_admin(true);
break;
}else if (num==2) {
createUser.setIs_admin(false);
break;
}
System.out.println("格式不对,请重新输入!");
}
SystemData.USERLIST_SYSTEM.add(createUser);
return SystemData.CURRENT_USER!=null?true:false;
}
@Override
public boolean del() {
System.out.println("输入你要删除的用户账号:");
String num=new Scanner(System.in).nextLine();
ArrayList<User>aUsers=SystemData.USERLIST_SYSTEM;
for (int i = 0; i < aUsers.size(); i++) {
if (aUsers.get(i).getUsername().equals(num)) {
SystemData.USERLIST_SYSTEM.remove(i);
System.out.println("删除成功!");
return true;
}
}
System.out.println("删除失败!");
return false;
}
@Override
public int getCount() {
return SystemData.USERLIST_SYSTEM.size();
}
@Override
public void getlistInfo() {
ArrayList<User>aUsers=SystemData.USERLIST_SYSTEM;
if (aUsers.size()>0) {
for(User user:aUsers){
System.out.println(user.toString());
}
}else {
System.out.println("无任何信息~");
}
}
@Override
public boolean find() {
System.out.println("输入你要查询用户的账号:");
String num=new Scanner(System.in).nextLine();
User user=getUserInfoByUserName(num);
if (user!=null) {
System.out.println(user.toString());
return true;
}else {
System.out.println("当前图书系统不存在该用户信息!");
}
return false;
}
public boolean userLogin(User user) {
ArrayList<User>aUsers=SystemData.USERLIST_SYSTEM;
for (User user2 : aUsers) {
if (user2.getUsername().equals(user.getUsername())) {
if (user2.getPassword().equals(user.getPassword())) {
SystemData.CURRENT_USER=user2;
return true;
}
}
}
return false;
}
public User getUserInfoByUserName(String userName) {
ArrayList<User>aUsers=SystemData.USERLIST_SYSTEM;
for(User user:aUsers){
if (userName.equals(user.getUsername())) {
System.out.println(user.toString());
return user;
}
}
return null;
}
}
5:CreateSerialNumberUtil工具类用于生成流水号
package com.zhumin.librarysystem.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class CreateSerialNumberUtil {
public static String getSerialNumber() {
return new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+UUID.randomUUID().toString().replaceAll("-", "");
}
public static String getSerialBorrowNumber(String id) {
return new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+id;
}
}
7:IAction接口
package com.zhumin.service;
public interface IAction {
public boolean add();
public boolean del();
public int getCount();
public void getlistInfo();
public boolean find();
}
8:Test测试类
三.运行效果和源码下载地址
百度网盘下载地址: 链接:https://pan.baidu.com/s/170GZr24ohtp-EiqXujSenw 提取码:9999
|