Java图书管理系统(第一次编写)

第二次修正版在这里:Java图书管理系统(第二次修正)

1 运行程序显示主界面

 

2 图书查询

不需要登录,可以查询图书

输入图书编号精确查询,如果有就把书的信息显示出来,没有就提示没有的信息

按图书名称查询, 输入书名, 把包含该书名的相关图书都显示出来

3 读者登录

输入读者证编号, 密码登录, 登录成功后,  读者可以查看个人的信息(不显示密码), 修改个人信息, 查看当前借阅图书的信息, 修改密码, 借书, 还书

4 管理员操作

系统初始化一个管理员, 管理员登录后, 修改密码;  添加读者就是给读者办理借书证, 删除读者信息,  查询读者信息(包括读者密码)  , 添加图书就是新书上架, 删除图书...

最后把读者信息, 图书信息, 借阅记录都序列化到文件中

Test.java(测试类)

package bookstore;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * jian
 * 2020/11/20 0020
 */
public class Test {
    public static void main(String[] args) {
        //创建图书并初始化
        List<Book> booklist = new LinkedList<>();
        booklist.add(new Book().setId("book001").setName("java入门").setAuthor("张三").setPrice(40));
        booklist.add(new Book().setId("book002").setName("java提高").setAuthor("李四").setPrice(30));
        booklist.add(new Book().setId("book003").setName("java入门").setAuthor("王五").setPrice(20));
        //创建读者并初始化
        List<Reader> readerList = new LinkedList<>();
        readerList.add(new Reader().setReaderName("小明").setReaderId("reader001").setReaderPassword("123456"));
        //创建管理员并初始化
        Admin admin = new Admin();
        //初始化一个管理员
        admin.getMap().put("admin", "666");
        mainInterface(booklist, readerList, admin);
        save(booklist, readerList, admin);
    }

    public static void save(List<Book> booklist, List<Reader> readerList, Admin admin) {
        try {
            ObjectOutputStream readerOos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\reader.txt"));
                ObjectOutputStream bookOos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\book.txt"));
                ObjectOutputStream readerBookOos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\readerBook.txt"));
                ObjectOutputStream adminOos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\admin.txt"));
                for (Book book : booklist) {
                    bookOos.writeObject(book);
                }
                for (Reader reader : readerList) {
                    readerOos.writeObject(reader);
                    readerBookOos.writeObject(reader.getReaderBookList());
            }
            adminOos.writeObject(admin);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void mainInterface(List<Book> bookList, List<Reader> readerList, Admin admin) {
        Scanner input = new Scanner(System.in);
        while (true) {
            //运行程序显示主界面
            System.out.println("=================图书管理系统=================");
            System.out.println("1 图书查询");
            System.out.println("2 读者登陆");
            System.out.println("3 管理员登陆");
            System.out.println("0 退出");
            System.out.println("请输入你的操作");
            int option1 = input.nextInt();
            switch (option1) {
                case 1:
                    //图书查询
                    queryBook(bookList, input);
                    break;
                case 2:
                    //读者登陆
                    readerLogin(bookList, readerList, input);
                    break;
                case 3:
                    //管理员登陆
                    adminLogin(bookList, readerList, admin, input);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("选择操作有误");
            }
        }
    }

    public static void adminLogin(List<Book> bookList, List<Reader> readerList, Admin admin, Scanner input) {
        System.out.println("-------------管理员登录-------------");
        System.out.println("请输入用户名");
        String username = input.next();
        System.out.println("请输入密码");
        String password1 = input.next();
        if (admin.login(username, password1)) {
            System.out.println("登陆成功");
            adminHandle(admin, readerList, bookList, input);
        }else {
            System.out.println("账号名或密码错误,登录失败");
        }
    }

    public static void readerLogin(List<Book> bookList, List<Reader> readerList, Scanner input) {
        System.out.println("-------------读者登录-------------");
        System.out.println("请输入读者编号");
        String id = input.next();
        System.out.println("请输入密码");
        String password = input.next();
        for (Reader reader : readerList) {
            if (reader.login(id, password, readerList)) {
                System.out.println("登陆成功");
                readerHandle(reader, bookList, input);
            }else {
                System.out.println("账号名或密码错误,登录失败");
            }
        }
    }

    public static void adminHandle(Admin admin, List<Reader> readerList, List<Book> bookList, Scanner input) {
        while (true) {
            //管理员操作界面
            System.out.println("-=-=-=-=-=-=-=管理员操作界面=-=-=-=-=-=-");
            System.out.println("1 添加读者信息");
            System.out.println("2 删除读者信息");
            System.out.println("3 查询读者信息");
            System.out.println("4 添加图书信息");
            System.out.println("5 删除图书信息");
            System.out.println("6 退出");
            System.out.println("请输入你的选择");
            int option = input.nextInt();
            switch (option) {
                case 1:
                    System.out.println("请输入添加的读者姓名");
                    String name = input.next();
                    System.out.println("请输入读者证编号");
                    String id1 = input.next();
                    System.out.println("请输入读者证密码");
                    String password2 = input.next();
                    admin.addReader(readerList, new Reader().setReaderName(name).setReaderId(id1).setReaderPassword(password2));
                    break;
                case 2:
                    System.out.println("请输入删除的读者证编号");
                    String id2 = input.next();
                    admin.deleteReader(id2, readerList);
                    break;
                case 3:
                    System.out.println("请输入要查询的读者证编号");
                    String id3 = input.next();
                    admin.seeReader(id3, readerList);
                    break;
                case 4:
                    System.out.println("----------------添加图书----------------");
                    System.out.println("请输入编号");
                    String addBookId = input.next();
                    System.out.println("请输入名称");
                    String addBookName = input.next();
                    System.out.println("请输入作者");
                    String addBookAuthor = input.next();
                    System.out.println("请输入价格");
                    double addBookPrice = input.nextDouble();
                    admin.addBook(bookList, new Book().setId(addBookId).setName(addBookName).setAuthor(addBookAuthor).setPrice(addBookPrice));
                    break;
                case 5:
                    System.out.println("请输入要删除的图书编号");
                    String delBookId = input.next();
                    admin.deleteBook(delBookId, bookList);
                    break;
                case 6:
                    return;
                default:
                    System.out.println("选择有误");
            }
        }
    }

    public static void readerHandle(Reader reader, List<Book> bookList, Scanner input) {
        while (true) {
            System.out.println("***********读者操作界面***********");
            System.out.println("1 查看个人信息");
            System.out.println("2 查看借阅信息");
            System.out.println("3 修改密码");
            System.out.println("4 借书");
            System.out.println("5 还书");
            System.out.println("6 退出");
            System.out.println("请输入你的操作");
            int option = input.nextInt();
            switch (option) {
                case 1:
                    //查看个人信息
                    reader.seeInformation();
                    break;
                case 2:
                    //查看借阅信息
                    reader.lookBorrow();
                    break;
                case 3:
                    //修改密码
                    System.out.println("请输入新密码");
                    String newPassword = input.next();
                    reader.changePassword(reader, newPassword);
                    break;
                case 4:
                    //借书
                    System.out.println("请输入要借阅书的编号");
                    String borrowBookId = input.next();
                    reader.borrowBooks(borrowBookId, bookList);
                    break;
                case 5:
                    //还书
                    System.out.println("请输入要还的书的编号");
                    String returnBookId = input.next();
                    reader.returnBook(returnBookId, bookList);
                    break;
                case 6:
                    //退出
                    return;
                default:
                    System.out.println("选择有误");
            }
        }
    }

    public static void queryBook(List<Book> bookList, Scanner input) {
        System.out.println("-------------图书查询-------------");
        System.out.println("1 按图书编号查询    2 按图书名称查询");
        int i = input.nextInt();
        switch (i) {
            case 1:
                System.out.println("请输入图书编号");
                String id = input.next();
                queryBookById(id, bookList);
                break;
            case 2:
                System.out.println("请输入图书名称");
                String name = input.next();

                queryBookByName(name, bookList);
                break;
            default:
                System.out.println("选择有误");
        }
    }

    //按图书编号查询图书
    public static void queryBookById(String id, List<Book> bookList) {
        boolean isFlag = false;
        for (Book book : bookList) {
            if (id.equals(book.getId())) {
                System.out.println(book);
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("不存在图书编号:" + id);
        }
    }

    //按图书名称查询
    public static void queryBookByName(String name, List<Book> bookList) {
        boolean isFlag = false;
        for (Book book : bookList) {
            if (book.getName().contains(name)) {
                System.out.println(book);
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("不存在图书名称:" + name);
        }

    }

}

Book.java(图书类)

package bookstore;
import java.io.Serializable;
import java.util.Objects;

/**
 * 定义图书类
 * 图书具有编号,名称,作者,价格
 * jian
 * 2020/11/20 0020
 */
public class Book implements Serializable {
    private static final long serialVersionUID = 3899956163457761738L;
    private String id;
    private String name;
    private String author;
    private double price;

    public String getId() {
        return id;
    }

    public Book setId(String id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public Book setName(String name) {
        this.name = name;
        return this;
    }

    public String getAuthor() {
        return author;
    }

    public Book setAuthor(String author) {
        this.author = author;
        return this;
    }

    public double getPrice() {
        return price;
    }

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

    @Override
    public String toString() {
        return "图书信息:" +
                "图书编号:" + id +
                ", 图书名称" + name +
                ", author = " + author +
                ", price = " + price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Double.compare(book.price, price) == 0 &&
                Objects.equals(id, book.id) &&
                Objects.equals(name, book.name) &&
                Objects.equals(author, book.author);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, author, price);
    }
}

Reader.java(读者类)

package bookstore;

import java.io.Serializable;
import java.util.*;

/**
 * 定义读者类
 * 读者有姓名,编号,密码
 * jian
 * 2020/11/20 0020
 */
public class Reader implements Serializable {
    private static final long serialVersionUID = -6103001049590268858L;
    //读者姓名
    private String readerName;
    //存储读者证编号
    private String readerId;
    //存储读者证密码
    private String readerPassword;
    //定义List集合存储借阅的书
    private List<Book> readerBookList = new LinkedList<>();

    //登陆
    public boolean login(String id, String password, List<Reader> readerList) {
        for (Reader reader : readerList) {
            if (id.equals(reader.readerId) && password.equals(reader.readerPassword)) {
                return true;
            }
        }
        return false;
    }

    //查看个人信息
    public void seeInformation() {
        System.out.println("个人信息:" + "\n姓名:" + readerName + "\n读者证编号:" + readerId);
    }

    //查看借阅信息
    public void lookBorrow() {
        for (Book book : readerBookList) {
            System.out.println(book);
        }
    }

    //修改密码
    public void changePassword(Reader reader, String password) {
        reader.setReaderPassword(password);
    }

    //借书
    public void borrowBooks(String id, List<Book> bookList) {
        boolean isFlag = false;
        for (int i = 0; i < bookList.size(); i++) {
            if (id.equals(bookList.get(i).getId())) {
                readerBookList.add(bookList.remove(i));
                System.out.println("借书成功");
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("不存在图书编号:" + id);
        }
    }

    //还书
    public void returnBook(String id, List<Book> bookList) {
        boolean isFlag = false;
        for (int i = 0; i < readerBookList.size(); i++) {
            if (id.equals(readerBookList.get(i).getId())) {
                bookList.add(readerBookList.remove(i));
                System.out.println("还书成功");
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("不存在图书编号:" + id);
        }
    }

    public String getReaderName() {
        return readerName;
    }

    public Reader setReaderName(String readerName) {
        this.readerName = readerName;
        return this;
    }

    public String getReaderId() {
        return readerId;
    }

    public Reader setReaderId(String readerId) {
        this.readerId = readerId;
        return this;
    }

    public String getReaderPassword() {
        return readerPassword;
    }

    public Reader setReaderPassword(String readerPassword) {
        this.readerPassword = readerPassword;
        return this;
    }

    public List<Book> getReaderBookList() {
        return readerBookList;
    }

    public Reader setReaderBookList(List<Book> readerBookList) {
        this.readerBookList = readerBookList;
        return this;
    }

    @Override
    public String toString() {
        return "个人信息:" +
                "\n姓名:" + readerName +
                "\n读书证编号:" + readerId +
                "\n读书证密码:" + readerPassword;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Reader reader = (Reader) o;
        return Objects.equals(readerName, reader.readerName) &&
                Objects.equals(readerId, reader.readerId) &&
                Objects.equals(readerPassword, reader.readerPassword) &&
                Objects.equals(readerBookList, reader.readerBookList);
    }

    @Override
    public int hashCode() {
        return Objects.hash(readerName, readerId, readerPassword, readerBookList);
    }
}

Admin.java(管理员类)

package bookstore;

import java.io.Serializable;
import java.util.*;

/**
 * 定义管理员类
 * jian
 * 2020/11/20 0020
 */
public class Admin implements Serializable {
    private static final long serialVersionUID = -1466625283886374865L;
    private Map map = new HashMap();

    public Map getMap() {
        return map;
    }

    public Admin setMap(Map map) {
        this.map = map;
        return this;
    }

    //登陆
    public boolean login(String username, String password) {
        if (password.equals(map.get(username))) {
            return true;
        }
        return false;
    }

    //添加读者信息
    public void addReader(List<Reader> readerList,Reader reader) {
        readerList.add(reader);
        System.out.println("添加成功");
    }

    //删除读者信息
    public void deleteReader(String id, List<Reader> readerList) {
        boolean isFlag = false;
        for (Iterator<Reader> iterator = readerList.iterator(); iterator.hasNext(); ) {
            Reader reader = iterator.next();
            if (id.equals(reader.getReaderId())){
                iterator.remove();
                System.out.println("删除成功");
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("删除失败,不存在该读者");
        }
    }

    //查询读者信息
    public void seeReader(String id, List<Reader> readerList) {
        boolean isFlag = false;
        for (Reader reader : readerList) {
            if (id.equals(reader.getReaderId())) {
                System.out.println("查询成功");
                System.out.println(reader);
                //查询读者借阅信息
                System.out.println("借阅记录:");
                List<Book> bookList = reader.getReaderBookList();
                for (Book book : bookList) {
                    System.out.println(book);
                }
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("查询失败,不存在该读者");
        }

    }

    //添加图书信息
    public void addBook(List<Book> bookList,Book book) {
        bookList.add(book);
        System.out.println("添加图书信息成功");
    }

    //删除图书信息
    public void deleteBook(String id, List<Book> bookList) {
        boolean isFlag = false;
        for (Iterator<Book> iterator = bookList.iterator(); iterator.hasNext(); ) {
            Book book = iterator.next();
            if (id.equals(book.getId())) {
                iterator.remove();
                System.out.println("删除图书信息成功");
                isFlag = true;
            }
        }
        if (!isFlag) {
            System.out.println("删除图书信息失败,不存在图书编号:" + id);
        }
    }

    @Override
    public String toString() {
        return "Admin{" +
                "map=" + map +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Admin admin = (Admin) o;
        return Objects.equals(map, admin.map);
    }

    @Override
    public int hashCode() {
        return Objects.hash(map);
    }
}

 

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java是一种面向对象的编程语言,它可以用于开发各种类型的应用程序,包括图书管理系统。下面是一个简单的Java图书管理系统的实现方式: 1. 首先,需要定义一个Book类,该类包含书籍的基本信息,如书名、作者、出版社、ISBN号等。可以使用以下代码定义Book类: ``` public class Book { private String title; private String author; private String publisher; private String isbn; public Book(String title, String author, String publisher, String isbn) { this.title = title; this.author = author; this.publisher = publisher; this.isbn = isbn; } public String getTitle() { return title; } public String getAuthor() { return author; } public String getPublisher() { return publisher; } public String getIsbn() { return isbn; } } ``` 2. 接下来,需要定义一个BookManager类,该类包含对图书进行管理的方法,如添加图书、删除图书、查找图书等。可以使用以下代码定义BookManager类: ``` import java.util.ArrayList; import java.util.List; public class BookManager { private List<Book> books; public BookManager() { books = new ArrayList<>(); } public void addBook(Book book) { books.add(book); } public void removeBook(Book book) { books.remove(book); } public Book findBookByTitle(String title) { for (Book book : books) { if (book.getTitle().equals(title)) { return book; } } return null; } public Book findBookByAuthor(String author) { for (Book book : books) { if (book.getAuthor().equals(author)) { return book; } } return null; } public Book findBookByIsbn(String isbn) { for (Book book : books) { if (book.getIsbn().equals(isbn)) { return book; } } return null; } } ``` 3. 最后,可以编写一个简单的控制台程序来测试图书管理系统。可以使用以下代码实现: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { BookManager manager = new BookManager(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加图书"); System.out.println("2. 删除图书"); System.out.println("3. 查找图书"); System.out.println("4. 退出"); int choice = scanner.nextInt(); scanner.nextLine(); switch (choice) { case 1: System.out.println("请输入书名:"); String title = scanner.nextLine(); System.out.println("请输入作者:"); String author = scanner.nextLine(); System.out.println("请输入出版社:"); String publisher = scanner.nextLine(); System.out.println("请输入ISBN号:"); String isbn = scanner.nextLine(); Book book = new Book(title, author, publisher, isbn); manager.addBook(book); System.out.println("添加成功!"); break; case 2: System.out.println("请输入书名:"); title = scanner.nextLine(); book = manager.findBookByTitle(title); if (book != null) { manager.removeBook(book); System.out.println("删除成功!"); } else { System.out.println("未找到该书!"); } break; case 3: System.out.println("请选择查找方式:"); System.out.println("1. 按书名查找"); System.out.println("2. 按作者查找"); System.out.println("3. 按ISBN号查找"); int searchChoice = scanner.nextInt(); scanner.nextLine(); switch (searchChoice) { case 1: System.out.println("请输入书名:"); title = scanner.nextLine(); book = manager.findBookByTitle(title); if (book != null) { System.out.println("书名:" + book.getTitle()); System.out.println("作者:" + book.getAuthor()); System.out.println("出版社:" + book.getPublisher()); System.out.println("ISBN号:" + book.getIsbn()); } else { System.out.println("未找到该书!"); } break; case 2: System.out.println("请输入作者:"); author = scanner.nextLine(); book = manager.findBookByAuthor(author); if (book != null) { System.out.println("书名:" + book.getTitle()); System.out.println("作者:" +

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值