站长家园(原代码之家)(www.adminjie.com)网站源码,微信源码,游戏源码,商业源码分享平台。
当前位置:网站首页 技术文章 网络编程 正文

Java图文详解之实现图书管理系统

时间:2022-06-08 [网络编程]作者:fabuyuan 浏览:4 次

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于怎么实现图书管理系统的相关问题,包括了查找图书、添加图书、删除图书、借阅图书等等内容,下面一起来看一下,希望对大家有帮助。

推荐学习:《java视频教程

一. 功能介绍

1.1 使用介绍

1.输入姓名

2.选择身份

3.根据身份打印不同功能菜单

1.2 基本框架


二、具体思路

book(包)

operation(功能包)

user(使用者包)


三、代码实现

book(包)

Book(书类)

package book;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 14:45
 * Description:书
 */public class Book {
    private String name;//书名
    private String author;//作者
    private double price;//价格
    private String type;//类型
    private boolean state;//书的状态(已借出or未借出)
    
	//对书进行初始化,书的状态不用进行初始化(默认未借出)
    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        this.state = state;
    }

    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 double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

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

BookList(书架类)

package book;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 14:52
 * Description:书架
 */public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;

    public BookList() {
        books[0] = new Book("三国演义","罗贯中",19.9,"小说");
        books[1] = new Book("西游记","吴承恩",29.9,"小说");
        books[2] = new Book("红楼梦","曹雪芹",26.9,"小说");
        usedSize = 3;
    }

    public Book getBooks(int pos) {//输入下标获取该下标的这本书
        return books[pos];
    }

    public void setBooks(int pos, Book book) {//输入下标和一本书,在该下标放入一本书
        this.books[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

operation(功能包)

IOperation(功能接口)

package operation;import book.BookList;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:07
 * Description:接口
 */public interface IOperation {
    void work(BookList bookList);}

FindOperation(查找图书)

package operation;import book.Book;import book.BookList;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:09
 * Description:查找书籍信息
 */public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("查找图书");
        System.out.println("------------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入书的名称:");
        String name = scanner.nextLine();

        //获取书架中图书数量
        int size = bookList.getUsedSize();
        //根据名字查找
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                System.out.println(book);
                return;
            }
        }
        System.out.println("未找到该书。");
    }}

AddOperation(添加图书)

package operation;import book.Book;import book.BookList;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:04
 * Description:新增书籍
 */public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("添加图书");
        System.out.println("------------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入书名:");
        String name = scanner.nextLine();
        System.out.print("请输入作者:");
        String author = scanner.nextLine();
        System.out.print("请输入书的类型:");
        String type = scanner.nextLine();
        System.out.print("请输入价格:");
        double price = scanner.nextDouble();

        Book book = new Book(name, author, price, type);
        bookList.setBooks(bookList.getUsedSize(), book);//将书放入书架

        bookList.setUsedSize(bookList.getUsedSize()+1);//书籍数量+1
    }}

DelOperation(删除图书)

package operation;import book.Book;import book.BookList;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:05
 * Description:删除书籍
 */public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("删除图书");
        System.out.println("------------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的书名:");
        String name = scanner.nextLine();

        //获取书架中现在有几本书
        int size = bookList.getUsedSize();
        int i = 0;
        //根据书名找到这个本书的下标
        for (i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                break;
            }
        }
        if (i == size) {
            System.out.println("删除失败,未找到该书。");
            return;
        }
        //通过前移将书删除
        for (i = 0; i < size - 1; i++) {
            bookList.setBooks(i,bookList.getBooks(i + 1));
        }
        bookList.setUsedSize(bookList.getUsedSize()-1);//书架中图书数量-1
        System.out.println("删除成功。");
    }}

BorrowOperation(借阅图书)

package operation;import book.Book;import book.BookList;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:05
 * Description:借阅书籍
 */public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("借阅图书");
        System.out.println("------------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要借书的名称:");
        String name = scanner.nextLine();

        //获取书架中现在有几本书
        int size = bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            //判断在书架中是否有这本书
            if (book.getName().equals(name)) {
                //判断书是否被借走
                if (!book.isState()) {
                    System.out.println("借阅成功!");
                    book.setState(true);//将书的状态改为已借出
                    return;
                } else {
                    System.out.println("借阅失败,该书已被别人借走。");
                    return;
                }
            }
        }
        System.out.println("借阅失败,未找到该书。");
    }}

ReturnOperation(归还图书)

package operation;import book.Book;import book.BookList;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:10
 * Description:归还书籍
 */public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("归还图书");
        System.out.println("------------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要还书的名称:");
        String name = scanner.nextLine();

        //获取书架中现在有几本书
        int size = bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            //判断在书架中是否有这本书
            if (book.getName().equals(name)) {
                //判断书是否已被借走
                if (book.isState()) {
                    System.out.println("归还成功!");
                    book.setState(false);//将书的状态改为未借出
                    return;
                } else {
                    System.out.println("归还失败,该书已还未被借走。");
                    return;
                }
            }
        }
        System.out.println("归还失败,未找到该书。");
    }}

DisplayOperation(打印图书信息)

package operation;import book.Book;import book.BookList;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:06
 * Description:打印全部书籍信息
 */public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("显示全部图书");
        System.out.println("------------------------");
        int size = bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }}

ExitOperation(退出系统)

import book.BookList;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:08
 * Description:退出系统
 */public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("退出系统。");
        System.out.println("------------------------");
        System.exit(0);
    }}

user(使用者包)

User(使用者抽象类)

package user;import book.BookList;import operation.IOperation;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:11
 * Description:使用者
 */abstract public class User {
    protected String userName;//使用者姓名
    protected IOperation[] iOperations;//功能数组

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

    public abstract int menu();
    
    //输入选择,调用不同的功能
    public void doOperation(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);//动态绑定
    }}

AdminUser(管理员)

package user;import operation.*;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:13
 * Description:管理员
 */public class AdminUser extends User{
    public AdminUser(String userName) {
        super(userName);
        //管理员功能
        this.iOperations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println();
        System.out.println("hello " + this.userName + " 欢迎来到图书馆");
        while(true) {
            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.print("请输入你的操作:");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            //保证输入数字有效
            if (choice > 4 || choice < 0) {
                System.out.println("输入有误请重新输入。");
            } else {
                return choice;
            }
        }
    }}

NormalUser(普通用户)

package user;import operation.*;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:13
 * Description:学生
 */public class NormalUser extends User{
    public NormalUser (String userName) {
        super(userName);
        //用户功能
        this.iOperations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };
    }

    @Override
    public int menu() {
        System.out.println();
        System.out.println("hello "+this.userName+" 欢迎来到图书馆");
        while(true) {
            System.out.println("------------------------");
            System.out.println("1.查找图书");
            System.out.println("2.借阅图书");
            System.out.println("3.归还图书");
            System.out.println("0.退出系统");
            System.out.println("------------------------");
            System.out.print("请输入你的操作:");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            //保证输入数字有效
            if (choice > 3 || choice < 0) {
                System.out.println("输入有误请重新输入。");
            } else {
                return choice;
            }
        }
    }}

Test类

import book.BookList;import user.AdminUser;import user.NormalUser;import user.User;import java.util.Scanner;/**
 * User: gu'jiu
 * Date: 2022-05-18
 * Time: 15:20
 * Description:图书管理系统
 */public class Test {
    public static User login() {
        System.out.println("欢迎使用图书管理系统");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入您的姓名:");
        String name = scanner.nextLine();

        while(true){
            System.out.println("-----------------------------");
            System.out.println("请选择您的身份:1>管理员 2>普通用户");

            System.out.print("请输入:");
            int choice = scanner.nextInt();

            //保证输入数字有效
            if (choice == 1) {
                return new AdminUser(name);
            } else if (choice == 2) {
                return new NormalUser(name);
            } else {
                System.out.println("输入有误,请重新输入。");
            }
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();//向上转型

        while(true){
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }}

四、部分代码(doOperation)解释

主函数中的user.doOperation(choice, bookList);中的doOperation方法比较难理解,我们来解释一下

1.user.引用的可能是两个对象

2.在实例化这对象的时候,在构造方法中,我们已经将iOperations[]准备好了。

3.根据输入下标(例如:2)访问相应对象,再调用work方法。

推荐学习:《java视频教程》

以上就是Java图文详解之实现图书管理系统的详细内容,更多请关注站长家园其它相关文章!

本文标签:  java

转载请注明来源:Java图文详解之实现图书管理系统

本文永久链接地址:https://www.adminjie.com/post/13217.html

免责声明:
本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。

附:
二○○二年一月一日《计算机软件保护条例》第十七条规定:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬!鉴于此,也希望大家按此说明研究软件!

版权声明:
一、本站致力于为软件爱好者提供国内外软件开发技术和软件共享,着力为用户提供优资资源。
二、本站提供的部分源码下载文件为网络共享资源,请于下载后的24小时内删除。如需体验更多乐趣,还请支持正版。
三、我站提供用户下载的所有内容均转自互联网。如有内容侵犯您的版权或其他利益的,若有侵犯你的权益请:提交版权证明文件到邮箱 2225329873#qq.com(#换为@) 站长会进行审查之后,情况属实的会在三个工作日内为您删除。

  • 站长家园(原代码之家)会员升级
  • 最新文章
    热门文章