分享一个图书馆管理系统的java实战项目,建议做java开发的都看看!

前程有光 2021-01-21 13:38:24
分享 一个 图书 书馆 图书馆


前言

该项目核心需求:

实现简单登录

管理员端实现下列功能

①查找书籍
②增加书籍
③删除书籍
④展示全部书籍
⑤退出系统

通用户实现下列功能

①查询书籍
②借阅书籍
③归还书籍
④退出系统

项目类的设计展示

图书相关的类

Book:定义书籍的信息 BookList:表示书库,里面存放书籍

package book;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:54
*/
public class Book {
//定义成员变量
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed = false; //表示书的借阅状态
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
// this.isBorrowed = isBorrowed;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getPrice() {
return price;
}
public String getType() {
return type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(int price) {
this.price = price;
}
public void setType(String type) {
this.type = type;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
// @Override
// public String toString() {
// return "Book{" +
// "name='" + name + '\'' +
// ", author='" + author + '\'' +
// ", price=" + price +
// ", type='" + type + '\'' +
// ", isBorrowed=" + isBorrowed +
// '}';
// }
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true) ? " 借阅状态: 已借出" : " 借阅状态: 未借出") +
'}';
}
}
package book;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:54
*/
public class BookList {
public int usedSize = 3; //书架
public Book[] books = new Book[10]; //书的类型为Book,用顺数组book去存储
public BookList() {
books[0] = new Book("三国演义","罗贯中", 100, "小说");
books[1] = new Book("水浒传", "施耐庵", 100, "小说");
books[2] = new Book("西游记", "吴承恩", 100, "小说");
}
//给指定位置放书
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
//拿到指定位置的书
public Book getBooks(int pos){
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}

对书库(顺序表)操作的类

//新增
public class AddOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("添加书籍");
Scanner sc = new Scanner(System.in);
System.out.println("请输入书名");
String name = sc.nextLine();
System.out.println("请输入书的作者");
String author = sc.nextLine();
System.out.println("请输入书的价格");
int price = sc.nextInt();
System.out.println("请输入书的类型");
String type = sc.next();
Book newBook = new Book(name,author,price,type); //构建新书(对象)
int size = booklist.getUsedSize(); //通过bookList引用访问当前顺序表长度
booklist.setBooks(size,newBook); //将新书放在顺序表最后面
booklist.setUsedSize(size+1); //顺序表放了新书之后,长度加1
}
}
//借阅
public class BorrowOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("借阅书籍");
Scanner sc = new Scanner(System.in);
System.out.println("请输入书名");
String name = sc.nextLine(); //name为所要借阅书名
for (int i = 0; i < booklist.getUsedSize(); i++) {
//通过booklist下标遍历每一本书
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
//如果为true,说明要借阅的书存在,我们需要做的是修改书的借阅状态
book.setBorrowed(true); //为true表示书已经结出
return ;
}
}
System.out.println("非常抱歉,本馆没有您要借阅的书!"); //
}
}
//删除
public class DelOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("删除书籍");
Scanner sc = new Scanner(System.in);
System.out.println("请输入书名");
String name = sc.nextLine(); //name为所要删除的书名
int i = 0;
for (; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
break;
}
}
if(i >= booklist.getUsedSize()){
System.out.println("没有要删除的这本书!");
return ;
}
//此时i为所要删除书的下标
for (int j = i; j < booklist.getUsedSize()-1; j++) {
Book book = booklist.getBooks(j+1); //获得j+1位置的书
booklist.setBooks(j,book); //将j+1位置的书给j位置
}
int size = booklist.getUsedSize(); //获得顺序表长度
booklist.setUsedSize(size-1); //删除书后,长度减去1
System.out.println("书已被删除!");
}
}
//展示
public class DisplayOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("展示书籍");
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBooks(i);
System.out.println(book);
}
}
}
//查找
public class FindOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("查找书籍");
Scanner sc = new Scanner(System.in);
System.out.println("请输入书名");
String name = sc.nextLine(); //name为所要查找书名
for (int i = 0; i < booklist.getUsedSize(); i++) {
//通过booklist下标遍历每一本书
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
System.out.println("该书存在!");
System.out.println(book); //直接打印书的信息,toString方法已被重写
return ;
}
}
System.out.println("没有这本书!");
}
}
//归还
public class ReturnOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("归还书籍");
Scanner sc = new Scanner(System.in);
System.out.println("请输入书名");
String name = sc.nextLine(); //name为所要归还的书名
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println(book); //直接打印书的信息,toString方法已被重写
return ;
}
}
System.out.println("没有你要归还的这本书!");
}
}
//退出
public class ExitOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("退出系统");
System.exit(1); //表示退出系统
}
}
//总接口
public interface IOperation {
void work(BookList booklist);
}

用户相关类

<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">用户类</mark>

package user;
import book.BookList;
import operation.IOperation;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:55
*/
abstract public class User {
public String name;
public IOperation[] operations;
public User(String name){
this.name = name;
}
/*
新建menu方法,理解为用户菜单
因为SpecialPerson和OrdinaryPerson继承了User,所以让两个子类重写menu方法,二者的菜单展示不一致
此时menu可以没有具体实现,因而将它设计为抽象方法,因此User类成为抽象类
*/
abstract public int menu();
//operations中存放的是哪些操作方法,得看子类
public void doOperation(int choice, BookList bookList){
this.operations[choice].work(bookList);
}
}

<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">管理员</mark>

package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:56
* 管理员
*/
public class SpecialPerson extends User{
//构造方法
public SpecialPerson(String name){
super(name);
this.operations = new IOperation[]{
new ExitOperation(), //0退出系统
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
/*
重写父类User的menu方法
menu作用:返回你要执行的操作
*/
@Override
public int menu() {
System.out.println("管理员菜单!");
System.out.println("=================================");
System.out.println("hello " + 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("请按照提示选择相应操作: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}

<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">普通用户</mark>

package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:57
* 普通用户
*/
public class OrdinaryPerson extends User{
//构造方法
public OrdinaryPerson(String name){
super(name);
this.operations = new IOperation[]{
new ExitOperation(), //0退出系统
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
//重写父类User的menu方法
@Override
public int menu() {
System.out.println("普通用户菜单!");
System.out.println("=================================");
System.out.println("hello" + this.name + "热诚欢迎使用本校图书馆管理系统!");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("================================");
System.out.println("请按照提示选择相应操作: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}

主函数程序

import book.BookList;
import user.OrdinaryPerson;
import user.SpecialPerson;
import user.User;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:30
*/
public class TrqTest {
//login(登录)方法返回用户实例,类型为User
public static User login(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名: ");
String name = sc.nextLine();
System.out.println("请输入你的身份> 1:管理员 0:普通用户");
int number = sc.nextInt();
if(number == 1){
return new SpecialPerson(name);
}else{
return new OrdinaryPerson(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
//登录
User user = login(); // 父类引用指向子类实例(向上转型)
while(true) {
//user.menu() 调用哪个menu方法,根据登录身份决定
int choice = user.menu();
//此doOperation方法是当前user自己的方法
//choice选择当前用户operations数组中的相应的类,该类产生的对象调用自己的work方法
user.doOperation(choice, bookList);
}
}
}

运行一下看看具体怎么样吧
在这里插入图片描述


感谢你看到这里,文章有什么不足还请指正,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!
欢迎关注公众号:前程有光,领取一线大厂Java面试题总结+各知识点学习思维导+一份300页pdf文档的Java核心知识点总结!

版权声明
本文为[前程有光]所创,转载请带上原文链接,感谢
https://segmentfault.com/a/1190000039048307

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云