猿创征文 |【JAVA小程序】图书管理系统(继承 多态 接口 链表 一应俱全)附源码

谢谢你陪着稀奇古怪的我

猿创征文 |【JAVA小程序】图书管理系统(继承 多态 接口 链表 一应俱全)附源码_第1张图片
大家好,这里是新一,请多关照。在本篇博客中,新一将会为大带来JAVA小程序——图书管理系统,干货满满哟。(以下结果均在IDEA中编译)希望在方便自己复习的同时也能帮助到大家。

废话不多说,直接进入我们的文章。


文章目录

  • 图书管理系统
    • 1. 基础框架介绍
    • 2. book包
      • 2.1 Book类——数据有关信息
      • 2.2 BookList类——链表存储书籍
      • 2.3 ListNode类——链表节点信息
    • 3. user包
      • 3.1 User类——用户接口
      • 3.2 AdminUser类——管理员
      • 3.3 NormalUser类——普通用户
      • 3.4 Main类——主函数入口
    • 4. operation包
      • 4.1 AddOperation类——添加图书
      • 4.2 BorrowOperation类——借阅图书
      • 4.3 DelOperation类——删除图书
      • 4.4 DisplayOperation类——显示图书
      • 4.5 ExitOperation类——退出系统
      • 4.6 FindOperation类——查找图书
      • 4.7 RemoveOperation类——清空书架
      • 4.8 ReturnOperation类——清空书架
      • 4.9 IOperation类——接口
    • 5. 源码下载
  • 想对大家说的话


图书管理系统

既然我们前面介绍了继承,多态,接口等等一系列知识,那么今天新一就带大家来实战一波,面向对象—— 图书管理系统

1. 基础框架介绍

首先,我们想到,既然是图书管理系统,那肯定必须要有书,书——就是一个对象,一本书吗?当然不是,我们需要的是一堆书——链表存储,存储对象为书
还有用户——对象,以及我们形形色色的功能——对象

于是我们整理出了以下内容:

一、简单的登录
1.用户名和密码输入(简易)
2.管理员判定

二、管理端
1.整理书籍(该功能为可扩展功能)
2.查阅书籍
3.增加书籍
4.删除书籍
5.打印书籍列表
6.退出

三、用户端
1.查询书籍
2.借阅书籍
3.归还书籍
4.退出

于是我们便有了思路,建立三个包,分别是book包——用来存放图书信息,operation包——用来存放功能,user包——用来存放用户这个对象与main函数入口,包以及各函数接口如下:

2. book包

这里主要存放我们图书有关的类

2.1 Book类——数据有关信息

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 ? ", 该书已借出 " : ", 未借出 ") +
                '}';
    }
}

2.2 BookList类——链表存储书籍

package book;

public class BookList {
    ListNode head;

    public BookList(){
        ListNode listNode1 = new ListNode(new Book("未闻花名","长井龙雪",45,"爱情"));
        ListNode listNode2 = new ListNode(new Book("你的名字","新海诚",48,"爱情"));
        ListNode listNode3 = new ListNode(new Book("天气之子","新海诚",56,"爱情"));
        ListNode listNode4 = new ListNode(new Book("咒术回战","芥见下下",46,"战斗"));
        ListNode listNode5 = new ListNode(new Book("一拳超人","ONE",43,"战斗"));
        ListNode listNode6 = new ListNode(new Book("千与千寻","宫崎骏",42,"奇幻"));
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        listNode5.next = listNode6;
        this.head = listNode1;
    }

    public ListNode getHead() {
        return head;
    }

    public void setHead(ListNode head) {
        this.head = head;
    }

    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
}

2.3 ListNode类——链表节点信息

package book;
public class ListNode {
    public Book val;
    public ListNode next;

    public ListNode(Book val) {
        this.val = val;
    }
}

3. user包

这里主要存放我们面向用户的功能以及主函数main

3.1 User类——用户接口

package user;

import book.BookList;
import operation.IOperation;

public abstract class User {
    protected String name;
    protected IOperation[] iOperations;
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doWork(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
}

3.2 AdminUser类——管理员

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(),
                new RemoveOperation(),
        };
    }
    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("5.清空书架");
        System.out.println("0.退出系统");
        System.out.println("==============================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

3.3 NormalUser类——普通用户

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.4 Main类——主函数入口

package user;

import book.BookList;

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("请输入图书馆登录密码:");
        int count = 0;
        while (count <= 3){
        String key = scanner.nextLine();
            if (key.equals("bityyds")) {
                System.out.println("登录成功,请输入你的身份:one - 管理员,else number - 普通用户");
                int choice = scanner.nextInt();
                if (choice == 1) {
                    String namekey[] = {"ljk", "msr"};
                    for (int i = 0; i < namekey.length; i++) {
                        if (name.equals(namekey[i])) {
                            return new AdminUser(name);
                        }
                    }
                    System.out.println("抱歉,您非管理员,以下为普通用户界面");
                    return new NormalUser(name);
                } else {
                    return new NormalUser(name);
                }
            }
            if (count < 3){
                System.out.println("密码错误,请重新输入: ");
            }
            count++;
        }
        System.out.println("密码错误次数过多,退出系统");
        System.exit(0);
        return null;
    }

    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包

这里主要是我们的功能包。

4.1 AddOperation类——添加图书

package operation;

import book.Book;
import book.BookList;
import book.ListNode;

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("新增图书");
        System.out.println("请输入书名");
        String bookname = scanner.nextLine();
        System.out.println("请输入作者");
        String bookauthor = scanner.nextLine();
        System.out.println("请输入类型");
        String booktype = scanner.nextLine();
        System.out.println("请输入定价");
        int bookprice = scanner.nextInt();
        Book book = new Book(bookname, bookauthor, bookprice, booktype);

        ListNode newHead = bookList.getHead();
        ListNode node = new ListNode(book);
        node.next = newHead;
        bookList.setHead(node);
        System.out.println("新增成功!");
    }
}

4.2 BorrowOperation类——借阅图书

package operation;

import book.Book;
import book.BookList;
import book.ListNode;

public class BorrowOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        System.out.println("请输入您要借阅的图书名:");
        String bookName = scanner.nextLine();
        ListNode cur = bookList.getHead();
        while (cur != null){
            Book book = cur.val;
            if (bookName.equals(cur.val.getName())){
                book.setBorrowed(true);
                System.out.println("借阅成功");
                System.out.println(book);
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有你要借阅的书");
    }
}

4.3 DelOperation类——删除图书

package operation;

import book.BookList;
import book.ListNode;

public class DelOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除的图书名字:");
        String bookName = scanner.nextLine();
        ListNode cur = bookList.getHead();
        if (cur == null) {
            System.out.println("图书余量为空,不能删除!");
            return;
        }
        if (bookName.equals(cur.val.getName())) {
            bookList.setHead(cur.next);
            System.out.println("删除成功");
            return;
        }
        while (cur.next != null){
            if (bookName.equals(cur.next.val.getName())){
                cur.next = cur.next.next;
                System.out.println("删除成功");
                return;
            }
            cur = cur.next;
        }
        System.out.println("未找到删除图书");
    }
}

4.4 DisplayOperation类——显示图书

package operation;

import book.Book;
import book.BookList;
import book.ListNode;

public class DisplayOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("显示图书");
        ListNode cur = bookList.getHead();
        while (cur != null){
            System.out.println(cur.val);
            cur = cur.next;
        }
    }
}

4.5 ExitOperation类——退出系统

package operation;

import book.BookList;

public class ExitOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

4.6 FindOperation类——查找图书

package operation;

import book.BookList;
import book.ListNode;


public class FindOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("查找图书");
        System.out.println("请输入你要查找的图书名字:");
        String bookName = scanner.nextLine();
        ListNode cur = bookList.getHead();
        while (cur != null){
            if (bookName.equals(cur.val.getName())){
                System.out.println(cur.val);
                return;
            }
            cur = cur.next;
        }
        System.out.println("查找书名不存在");
    }
}

4.7 RemoveOperation类——清空书架

package operation;

import book.BookList;
import book.ListNode;

public class RemoveOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("清空书架!系统提示您:请谨慎操作");
        System.out.println("您是否要清空整个书架: one - 是  other number - 否");
        int index = scanner.nextInt();
        if (index == 1){
            bookList.setHead(null);
            System.out.println("清空成功");
            return;
        }
        System.out.println("您的清空操作未执行!");
    }
}

4.8 ReturnOperation类——清空书架

package operation;

import book.Book;
import book.BookList;
import book.ListNode;

public class ReturnOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("归还图书");
        System.out.println("请输入您要归还的图书名:");
        String bookName = scanner.nextLine();
        ListNode cur = bookList.getHead();
        while (cur != null){
            Book book = cur.val;
            if (bookName.equals(cur.val.getName())){
                book.setBorrowed(false);
                System.out.println("归还成功");
                System.out.println(book);
                return;
            }
            cur = cur.next;
        }
        System.out.println("归还信息不存在,如果您想为图书馆尽一份力,请联系管理员ljk & msr");
    }

4.9 IOperation类——接口

package operation;

import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-08
 * Time: 13:04
 */
public interface IOperation {
    Scanner scanner = new Scanner(System.in);
    public void work(BookList bookList);
}

5. 源码下载

需要源码的兄弟们点这里就OK咯,免费的哦 图书管理系统源码

想对大家说的话

家人们,学到这里我们已经肝完了JAVA图书管理系统后续新一会持续更JAVA的有关内容,学习永无止境,技术宅,拯救世界!
猿创征文 |【JAVA小程序】图书管理系统(继承 多态 接口 链表 一应俱全)附源码_第2张图片

你可能感兴趣的:(小程序,java专项突破,链表,java,小程序)