Java实现图书管理系统
??前言??
本篇文章是对目前Java专栏已有内容的一个总结练习,希望各位小主们在学习完面向对象的知识后,可以阅览本篇文章后,自己也动手实现一个这样的demo来加深总结应用已经学到知识并进行巩固。
🍉博客主页: 🍁【如风暖阳】🍁 🍉精品Java专栏【Javase】、【Java数据结构】 🍉欢迎点赞 👍 收藏 ?留言评论 📝私信必回哟😁
🍉本文由 【如风暖阳】 原创,首发于 CSDN🙉
🍉博主将持续更新学习记录收获,友友们有任何问题可以在评论区留言
🍉博客中涉及源码及博主日常练习代码均已上传码云(gitee)
🍉1.系统导读
Java最主要的特征便是面向对象,这个小练习就是让我们加强对面向对象的理解,所以系统的实现肯定是通过分多个包和类来共同实现系统功能的。
该系统的通过四部分来实现: 1.book包下为图书信息的存储 2.user包下为用户的信息及操作逻辑 3.Main文件下为整个系统的逻辑实现 4.operation包下为系统功能的具体实现
首先是登陆页面,输入姓名后选择身份,然后跳转到不同的菜单(管理员和普通用户两份菜单),然后在用户进一步选择功能以后去实现对应的功能。
🍉2.创建系统所需类
2.1图书信息(book包)
该系统是通过顺序表来实现的,在顺序表的数组中存储的是每一本书的信息,然后通过顺序表进行增删查改功能的实现(顺序表了解可以博主之前的文章【Java数据结构之顺序表】)
Book.java
书需要包含书名、作者、价格、类型、是否被借出等信息
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
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 isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true)?"已被借出":"未被借出") +
'}';
}
}
BookList.java
package book;
public class BookList {
private Book [] books=new Book[10];
private int useSize;
public BookList() {
books[0] = new Book("三国演义","罗贯中",17,"小说");
books[1] = new Book("西游记","吴承恩",47,"小说");
books[2] = new Book("水浒传","施耐庵",37,"小说");
this.useSize=3;
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
public Book getPos(int pos) {
return this.books[pos];
}
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
}
2.2用户信息(user包)
因为使用者有管理者和普通用户两个对象,但两者多有相同属性且有细致区别的方法,所以将相同代码抽取出来创建抽象类User,再建立AdminUser和NormalUser分别继承User。
User.java
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
String name;
IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doWork(int choice, BookList bookList) {
iOperations[choice].work(bookList);
}
}
AdminUser.java
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu() {
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("==============================");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
NormalUser.java
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu() {
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("==============================");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
🍉3.系统逻辑(Main.java)
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
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();
User user=login();
while (true) {
int choice = user.menu();
user.doWork(choice, bookList);
}
}
}
🍉4.功能实现(operation包)
因为系统有多项功能,所以在这个包下,将每一个功能都独立创建为一个类来具体清晰实现,为了这些类中的代码复用,又抽象出来一个接口(多个类共有的功能)
Ioperation.java
package operation;
import book.BookList;
import java.util.Scanner;
public interface IOperation {
Scanner scanner = new Scanner(System.in);
void work(BookList bookList);
}
AddOperation.java
新增图书需要new图书并将信息初始化,然后将其放在顺序表最后位置
package operation;
import book.Book;
import book.BookList;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入作者:");
String author= scanner.next();
System.out.println("请输入类型:");
String type=scanner.next();
System.out.println("请输入价格:");
int price=scanner.nextInt();
Book book=new Book(name, author,price,type);
int size= bookList.getUseSize();
bookList.setBook(size,book);
bookList.setUseSize(size+1);
System.out.println("新增图书成功!");
}
}
BorrowOperation.java
输入借阅图书书名后遍历数组查找,找到后将图书借阅情况更改
package operation;
import book.Book;
import book.BookList;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入要借阅图书的名字");
String name=scanner.nextLine();
int size=bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if (name.equals(book.getName())) {
book.setBorrowed(true);
System.out.println("借阅成功");
System.out.println(book);
return;
}
}
System.out.println("没有你要借阅的书");
}
}
DelOperation.java
package operation;
import book.Book;
import book.BookList;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入要删除的图书");
String name=scanner.next();
int size= bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if (name.equals(book.getName())) {
for (int j = i; j < size-1; j++) {
Book bookNext=bookList.getPos(j+1);
bookList.setBook(j,bookNext);
}
bookList.setBook(size,null);
bookList.setUseSize(size-1);
System.out.println("删除完成");
return;
}
}
System.out.println("该书不存在");
}
}
DisplayOperation.java
把已有书目信息全部打印
package operation;
import book.Book;
import book.BookList;
import java.util.Arrays;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("浏览图书!");
int size= bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
System.out.println(book);
}
}
}
ExitOperation.java
退出系统
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
FindOperation.java
package operation;
import book.Book;
import book.BookList;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入要查找的图书:");
String name=scanner.next();
int size= bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book = bookList.getPos(i);
if (name.equals(book.getName())) {
System.out.println("找到了这本书,书的信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没找到");
}
}
ReturnOperation.java
package operation;
import book.Book;
import book.BookList;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入要归还图书的名字");
String name=scanner.nextLine();
int size=bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getPos(i);
if (name.equals(book.getName())) {
book.setBorrowed(true);
System.out.println("归还成功");
System.out.println(book);
return;
}
}
System.out.println("没有你要归还的书");
}
}
??最后的话??
总结不易,希望uu们不要吝啬你们的👍哟(^U^)ノ~YO!!如有问题,欢迎评论区批评指正😁
|