五分钟”手撕“图书管理系统

前言:

图书馆管理系统需要结合JavaSE的绝大部分知识,是一个很好的训练项目。

为了让大家更加方便的查阅与学习,我把代码放开头,供大家查询。

还有对代码的分析,我将以类为单位分开讲解。

目录

全部代码

Main类

User类

BookList类

Book类

NormalUser类


全部代码

java 复制代码
/*一共有三个包*/

//User包
//User.java
package user;
import Book.BookList;
import Operation.IOparetion;

public abstract class User {
    protected String name;
    protected IOparetion[] iOparetions;

    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doIOparetion(int chioce, BookList bookList){
        this.iOparetions[chioce].work(bookList);
    }
}







//User包
//NormalUser.java
package user;
import Operation.*;
import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOparetions= new IOparetion[] {
                new ExitOparetion(),
                new FindOparetion(),
                new BorrowedOparetion(),
                new ReturnOparetion()
    };
    }
    @Override
    public int menu() {
        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包
//Administrators.java
package user;
import Operation.*;
import java.util.Scanner;

public class Administrators extends User {
    public Administrators(String name) {
        super(name);
        this.iOparetions = new IOparetion[]{
                new ExitOparetion(),
                new FindOparetion(),
                new AddOparetion(),
                new DelOparetion(),
                new ShowOparetion()
        };
    }
    public int menu() {
        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;
    }
}





//Book包
//Book.java
package Book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isLend;

    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 isLend() {
        return isLend;
    }

    public void setLend(boolean lend) {
        isLend = lend;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                (isLend==true?"已借出":"未借出")+
                '}';
    }
}





//Book包
//BookList.java
package Book;

public class BookList {
    private Book[] book=new Book[10];
    private int usedSize;

    public Book getBook(int pos) {
        return book[pos];
    }

    public void setBook(int pos,Book book) {
        this.book[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public BookList() {
        book[0]=new Book("西游记","吴承恩",19,"小说");
        book[1]=new Book("三国演义","罗贯中",36,"小说");
        book[2]=new Book("红楼梦","曹雪芹",13,"小说");
        this.usedSize=3;
    }
    public boolean isFull(){
        if(usedSize==book.length){
            return true;
        }return false;
    }
}





//Oparetion包
//Ioparetion.java
package Operation;

import Book.BookList;

public interface IOparetion {
    void work(BookList bookList);
}





//Oparetion包
//AddOparetion.java
package Operation;

import Book.Book;
import Book.BookList;
import java.util.Scanner;

public class AddOparetion implements IOparetion {
    @Override
    public void work(BookList bookList) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你要添加的书籍名字");
        String name=scanner.nextLine();
        System.out.println("请输入作者的名字");
        String author=scanner.nextLine();
        System.out.println("请输入书的种类");
        String type=scanner.nextLine();
        System.out.println("请输入书的价格");
        int price=scanner.nextInt();
        Book book=new Book(name,author,price,type);
        if(bookList.isFull()){
            System.out.println("书架满了,借阅失败");
            return;
        }
        bookList.setBook(bookList.getUsedSize(), book);
        bookList.setUsedSize(bookList.getUsedSize()+1);
        System.out.println("添加成功");
    }
}




//Oparetion包
//BorrowedOparetion.java
package Operation;

import Book.BookList;
import java.util.Scanner;

public class BorrowedOparetion implements IOparetion{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要找的书名");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        for(int i=0;i<bookList.getUsedSize();i++){
            if(name.equals(bookList.getBook(i).getName())){
                bookList.getBook(i).setLend(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("借阅失败");
    }
    }




//Oparetion包
//DelOparetion.java
package Operation;

import Book.BookList;
import java.util.Scanner;

public class DelOparetion implements IOparetion{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要删除的书籍名字");
        Scanner scanner=new Scanner(System.in);
        String s=scanner.nextLine();
        int pos=-1;
        int i=0;
        for (;i<bookList.getUsedSize();i++){
            if(s.equals(bookList.getBook(i))){
                pos=i;
            }
            if(i>=bookList.getUsedSize()){
                System.out.println("没有你要找的书");
                return;
            }
        }for(int j=i;i<bookList.getUsedSize()-1;i++){
            bookList.setBook(j,bookList.getBook(j+1));

        }
        bookList.setBook(bookList.getUsedSize(), null);
        bookList.setUsedSize(bookList.getUsedSize()-1);
        System.out.println("删除成功");
    }
}





//Oparetion包
//ExitOparetion.java
package Operation;
import Book.BookList;

public class ExitOparetion implements IOparetion {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统中....");
        for(int i=0;i<bookList.getUsedSize()-1;i++){
            bookList.setBook(i,null);
        }
        System.exit(0);
    }
}




//Oparetion包
//FindOparetion.java
package Operation;

import Book.BookList;
import java.util.Scanner;

public class FindOparetion implements IOparetion{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要找的书名");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        for(int i=0;i<bookList.getUsedSize();i++){
            if(name.equals(bookList.getBook(i).getName())){
                System.out.println("找到了,下标为:"+i);
                System.out.println(bookList.getBook(i));
                return;
            }
        }
        System.out.println("没找到");
    }
}




//Oparetion包
//ReturnOparetion.java
package Operation;

import Book.BookList;
import java.util.Scanner;

public class ReturnOparetion implements IOparetion{
    @Override
    public void work(BookList bookList) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你要归还的书名");
        String s=scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize()-1; i++) {
            if(s.equals(bookList.getBook(i).getName())){
                bookList.getBook(i).setLend(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("归还失败,没有你要归还的书");
    }
}




//Oparetion包
//ShowOparetion.java
package Operation;
import Book.BookList;

public class ShowOparetion implements IOparetion{
    @Override
    public void work(BookList bookList) {
        for(int i=0;i<bookList.getUsedSize();i++){
            System.out.println(bookList.getBook(i));
        }
    }
    }

首先,我们需要打个框架出来,由于java是一门面向对象的编程,所以我们应该要多考虑对象,而不是过程。

我将它分为三个包:User、Book、operation包 ,其实对象有:书架、书、用户、管理员。

总体请看下图:

这里我主要讲解大致结构和细微的细节 ,因为方法的思路在数据结构讲解更加清晰

Main类

java 复制代码
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){
            NormalUser normalUser=new NormalUser(name);
            return normalUser;
        }else{
            Administrators administrators=new Administrators(name);
            return administrators;
        }
    }
    public static void main(String[] args) {
        BookList bookList=new BookList();
        User user=login();
        while (true){
            int ret=user.menu();
            user.doIOparetion(ret,bookList);
        }
    }
}

上述代码中:

Scanner来new一个对象来输入,int一个choice来接收选1还是2,选择是管理人还是普通用户。

由于要返回一个对象,所以login的返回值是user类型的,user与管理员和普通用户是父子关系,在main中用user类型接收,属于向上转型(好处就是发生多态)

User类

java 复制代码
package user;
import Book.BookList;
import Operation.IOparetion;

public abstract class User {
    protected String name;
    protected IOparetion[] iOparetions;

    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doIOparetion(int chioce, BookList bookList){
        this.iOparetions[chioce].work(bookList);
    }
}

上述代码中:

User作为父类,被管理员和普通用户继承,被abstract修饰主要是为了menu这个方法被继承,实现多态

IOparetion是一个接口,是为了被那些add和del等等方法实现的,还是实现多态,

它现在作为一个数组来接收,是为了存放这些方法 ,这样就不用一个个在用户或者管理员里面书写了,可以直接定义为一个类(这也是为什么方法可以另外书写的原因),这体现了面向对象的特点,大大缩短了工程量和分模块的简洁性(即使下次有人需要用,不需要再写,直接调用就好了)。

BookList类

java 复制代码
package Book;

public class BookList {
    private Book[] book=new Book[10];
    private int usedSize;

    public Book getBook(int pos) {
        return book[pos];
    }

    public void setBook(int pos,Book book) {
        this.book[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public BookList() {
        book[0]=new Book("西游记","吴承恩",19,"小说");
        book[1]=new Book("三国演义","罗贯中",36,"小说");
        book[2]=new Book("红楼梦","曹雪芹",13,"小说");
        this.usedSize=3;
    }
    public boolean isFull(){
        if(usedSize==book.length){
            return true;
        }return false;
    }
}

对于上述代码:

由于java是面向对象编程,所以对于一些信息不需要用户来知道内部是怎么实现的 ,所以用private修饰,这样的话需要set和get方法来获取,这样之后的代码都需要调用set和get来获取或者是赋值。

BookList是书架,所以里面是摆书的,所以用Book类型作为数组,来收取book。可以放很多本。

usedsize是有效书籍数,就是记录里面有多少本。

Book类

java 复制代码
package Book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isLend;

    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 isLend() {
        return isLend;
    }

    public void setLend(boolean lend) {
        isLend = lend;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                (isLend==true?"已借出":"未借出")+
                '}';
    }
}

对于上述代码:

Book是类,它代表的是一本书,所以你需要定义名字作者价格什么什么的。

也是利用封装,只把必要的信息给用户,所以用privat密封起来,更加安全一点,所以需要set和get方法。

这里的toString方法里面有个三目操作符,如果isLend被借出的话,就是true,就会打印已借出,否则为借出。

NormalUser类

java 复制代码
package user;
import Operation.*;
import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOparetions= new IOparetion[] {
                new ExitOparetion(),
                new FindOparetion(),
                new BorrowedOparetion(),
                new ReturnOparetion()
    };
    }
    @Override
    public int menu() {
        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;
    }
}

对于上述代码:

如果继承了父类,使用构造方法时候需要先把父类的构造方法实现。

因为IOparetion是user里面的,user被普通用户继承,所以可以直接访问把他变成一个数组,这样就可以存方法了,不用在这普通用户这个类里一个一个方法写,直接脱离出来,非常充分体现了面向对象的特点!

相关推荐
菠萝咕噜肉i几秒前
MyBatis是什么?为什么有全自动ORM框架还是MyBatis比较受欢迎?
java·mybatis·框架·半自动
林的快手15 分钟前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
FeboReigns17 分钟前
C++简明教程(10)(初识类)
c语言·开发语言·c++
学前端的小朱18 分钟前
处理字体图标、js、html及其他资源
开发语言·javascript·webpack·html·打包工具
向阳12181 小时前
mybatis 缓存
java·缓存·mybatis
上等猿1 小时前
函数式编程&Lambda表达式
java
摇光931 小时前
js高阶-async与事件循环
开发语言·javascript·事件循环·宏任务·微任务
沐泽Mu1 小时前
嵌入式学习-QT-Day09
开发语言·qt·学习
蓝染-惣右介1 小时前
【23种设计模式·全精解析 | 行为型模式篇】11种行为型模式的结构概述、案例实现、优缺点、扩展对比、使用场景、源码解析
java·设计模式
小猿_001 小时前
C语言实现顺序表详解
c语言·开发语言