我的个人主页
欢迎来到我的Java图书管理系统,接下来让我们一同探索如何书写图书管理系统吧!
1管理端和用户端
2建立相关的三个包(book、operation、user)
3建立程序入口Main类
4程序运行
1.首先图书馆管理系统分为管理员端和用户端
1.1管理员端:AdminUser::menu()
------------------------------------------------------管理员菜单------------------------------------------------------
- 1.查找图书
- 2.新增图书
- 3.删除图书
- 4.显示图书
- 0.退出图书
------------------------------------------------------------------------------------------------------------------------
1.2用户端:NormalUser::menu()
------------------------------------------------------用户端----------------------------------------------------------
- 1.查找图书
- 2.借阅图书
- 3.归还图书
- 0.退出系统
------------------------------------------------------------------------------------------------------------------------
2.建立三个包(book(书相关的包),operation(操作相关的包),user(使用者相关的包))
2.1再在book包里面建立Book(书籍对象)和BookList(书架)类
Book
package book;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:12:39
*/
public class Book{
private String name;//书籍名称
private String author;//作者
private int price;//价格
private String taye;//类型
private boolean isBorrowed;;//是否被借出
public Book(String name, String author, int price, String taye) {
this.name = name;
this.author = author;
this.price = price;
this.taye = taye;
}
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;
}
这里可以通过快捷键alt+insert快速构造成员方法,也可以快速构建get和set
BookList
package book;
import java.security.PrivilegedAction;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:12:38
*/
public class BookList {
private Book[] books; //存放书籍
private static final int DEFAULT_SIZE = 10;
private int usedSize; // 有效书籍的个数
//private int xxxx;
public BookList() {
this.books = new Book[DEFAULT_SIZE];
//预先存3本书
this.books[0] = new Book("三国演义","罗贯中",19,"武侠");
this.books[1] = new Book("西游记","吴承恩",20,"神话");
this.books[2] = new Book("红楼梦","曹雪芹",15,"小说");
this.usedSize = 3;
}
public Book[] getBooks() {
return books;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//.....还有待补充的
public Book getBook(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
}
2.2在operation包里面建立AddBook、 BorrowedBook、DelBook、ExitBook、FindBook ReturnBook、ShowBook类,IOPeration(接口);
AddBook
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:17
*/
public class AddBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("新增图书....");
/*if(isFull(bookList)) {
System.out.println("书架满了....");
return;
}*/
/* if(isFull(bookList)) {
//扩容的代码 Arrays.copyOf();
}*/
//1. 整理书籍的信息
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书籍的名称:");
String name = scanner.nextLine();
System.out.println("请输入书籍的作者:");
String author = scanner.nextLine();
System.out.println("请输入书籍的价格:");
int price = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入书籍的类型:");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
//2.如果书架有这本书 则不能上架
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
System.out.println("有这本书,可以不上架!!");
return;
}
}
//3.没有这本书 则放到书籍数组当中
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
private boolean isFull(BookList bookList) {
return bookList.getBooks().length ==
bookList.getUsedSize();
}
}
BorrowedBook
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:22
*/
public class BorrowedBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书....");
System.out.println("请输入你要借阅的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//1.先遍历数组 查找是否存在要借阅的图书
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
//2.如果存在 检查 是否已经被借出
if(book.isBorrowed()) {
System.out.println("这本书已经被借出了!");
}else {
//3. 如果没有借出 可以借
book.setBorrowed(true);
System.out.println(book);
System.out.println("借阅成功!!");
}
return;
}
}
//4.如果不存在 则不能借阅
System.out.println("没有你要找的这本书,无法借阅!!");
}
}
DelBook
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:24
*/
public class DelBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("删除图书....");
Scanner scanner = new Scanner(System.in);
//1、输入你要删除的图书名称:
System.out.println("请输入要删除书籍的名称:");
String name = scanner.nextLine();
//2.查看当前书籍是否存在
int index = -1;
int i = 0;
int currentSize = bookList.getUsedSize();
for (; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
index = i;
break;
}
}
//没有遇到break
if(i >= currentSize) {
System.out.println("没有你要删除的书....");
return;
}
//遇到break 此时开始真正删除
for (int j = index; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1]; error
Book tmp = bookList.getBook(j+1);
bookList.setBooks(j,tmp);
}
//修改usedSize的值
bookList.setUsedSize(currentSize-1);
bookList.setBooks(currentSize-1,null);
System.out.println("删除图书成功了.....");
}
}
ExitBook
package operation;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:12
*/
public class ExitBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统.....");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
//bookList[i] = null;
bookList.setBooks(i,null);
}
bookList.setUsedSize(0);
System.exit(0);
}
}
FindBook
package operation;
import book.BookList;
import book.Book;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:25
*/
public class FindBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("查找图书...");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查找的图书的名字:");
String name = scanner.nextLine();
//1.先确定 当前数组当中 有效的书籍个数
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("找到这本书籍了,书籍内容如下:");
System.out.println(book);
return;
}
}
System.out.println("没有你要查找的图书的书籍!!");
}
}
ReturnBook
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:27
*/
public class ReturnBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("归还图书....");
System.out.println("请输入你要归还的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//1.先遍历数组 查找是否存在要归还的图书
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
if(!book.isBorrowed()) {
System.out.println("这本书没有被借出过,不用还!");
}else {
System.out.println("归还成功");
book.setBorrowed(false);
System.out.println(book);
}
return;
}
}
//4.如果不存在 则不能归还
System.out.println("没有你要找的这本书,无法归还!!");
}
}
ShowBook
package operation;
import book.Book;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:27
*/
public class ShowBook implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("打印图书....");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
System.out.println(tmp);
}
}
}
IOPeration
package operation;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:13:09
*/
public interface IOPeration {
void work(BookList books);
}
2.3 user包中建立AdminUser、NormalUser、User类;
AdminUser
package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:12:43
*/
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitBook(),
new FindBook(),
new AddBook(),
new DelBook(),
new ShowBook()
};
}
@Override
public int menu() {
System.out.println("欢迎 "+this.name+" 来到图书管理系统!");
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 scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
NormalUser
package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:12:42
*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitBook(),
new FindBook(),
new BorrowedBook(),
new ReturnBook()
};
}
@Override
public int menu() {
System.out.println("欢迎 "+this.name+" 来到图书管理系统!");
System.out.println("**********普通用户菜单*********");
System.out.println("1. 查找图书");
System.out.println("2. 借阅图书");
System.out.println("3. 归还图书");
System.out.println("0. 退出系统");
System.out.println("*****************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
User
package user;
import book.BookList;
import operation.IOPeration;
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:12:42
*/
public abstract class User {
public String name;
public IOPeration[] ioPerations; //没有初始化
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperations(int choice, BookList books) {
//return this.ioPerations[choice];
IOPeration ioPeration = this.ioPerations[choice];
//IOPeration ioPeration = new FindBook();
ioPeration.work(books);
}
}
3整个程序的入口Main类
Main
/**
* Created with IntelliJ IDEA.
* Description:
* User:Lenovo
* Date:2024-10-26
* Time:12:44
*/
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你的姓名: ");
String name=scanner.nextLine();
System.out.println("请输入你的身份:1:管理员 2:普通用户 ");
int choice=scanner.nextInt();
if(choice==1){
return new AdminUser(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
/* Book[] books=new Book[10];
books[0]=new Book{"三国演义","罗贯中",19,"武侠"};
books[1]=new Book{"西游记","吴承恩",20,"神话"};
books[2]=new Book{"红楼梦","曹雪芹",15,"小说"};
usedsize=3;*/
User user=login();
while(true){
int choice=user.menu();
user.doOperations(choice,bookList);
}
}
}
这样一个简单的图书管理系统就实现了
4程序运行:
4.1以管理员身份运行:
4.2以用户身份运行:
这次的图书管理系统之旅就到这里了;
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » Java-图书管理系统
发表评论 取消回复