【Java】分享一个图书馆管理系统的java实战项目,建议做java开发的都看看!
分享一个图书馆管理系统的java实战项目,建议做java开发的都看看!
前程有光发布于 37 分钟前
前言
该项目核心需求:
实现简单登录
管理员端实现下列功能
①查找书籍
②增加书籍
③删除书籍
④展示全部书籍
⑤退出系统
通用户实现下列功能
①查询书籍
②借阅书籍
③归还书籍
④退出系统
项目类的设计展示
图书相关的类
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>用户类</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>管理员</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>普通用户</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核心知识点总结!
面试java程序员spring后端
阅读 21发布于 37 分钟前
本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
前程有光
欢迎关注我的公众号【前程有光】
749 声望
107 粉丝
前程有光
欢迎关注我的公众号【前程有光】
749 声望
107 粉丝
宣传栏
前言
该项目核心需求:
实现简单登录
管理员端实现下列功能
①查找书籍
②增加书籍
③删除书籍
④展示全部书籍
⑤退出系统
通用户实现下列功能
①查询书籍
②借阅书籍
③归还书籍
④退出系统
项目类的设计展示
图书相关的类
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>用户类</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>管理员</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>普通用户</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核心知识点总结!
以上是 【Java】分享一个图书馆管理系统的java实战项目,建议做java开发的都看看! 的全部内容, 来源链接: utcz.com/a/105254.html
得票时间