8-4 图书借阅

编写图书借阅程序,可以处理简单的书籍借阅情况,包括借书和还书等。图书类的数据成员包括书名、书号和借书学生等;方法包括借书、还书和显示书籍信息等。学生类的数据成员包括姓名、学号和所借书籍等;方法包括显示学生信息等。将数据保存在数据库中或文件中,采用图形化用户界面,实现图书的入库,借阅和归还。

代码展示

package Test7.a8_4g;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Objects;

public class LibrarySystemGUI extends JFrame {

    // 初始化图书和学生信息
    static Book java = new Book("Java程序设计", "ISBN 978-7-5193-0612-0");
    static Book c = new Book("C语言程序设计", "ISBN 978-7-5682-8878-1");
    static Book python = new Book("Python程序设计", "ISBN 978-7-5620-9481-4");
    static Student zhang = new Student("张三", "001");
    static Student li = new Student("李四", "002");
    static Student wang = new Student("王五", "003");
    static Student zhao = new Student("赵六", "004");
    static String[] books = {java.getName(), c.getName(), python.getName()};
    static Book[] allBook = {java, c, python};
    static Student[] students = {zhang, li, wang, zhao};

    public LibrarySystemGUI() {
        setTitle("图书借阅系统");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建面板和布局管理器
        JPanel panel = new JPanel(new GridLayout(3, 2));

        // 创建按钮
        JButton borrowButton = new JButton("借书");
        JButton returnButton = new JButton("还书");

        // 添加按钮的事件监听器
        borrowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                borrowBook();
            }
        });

        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                returnBook();
            }
        });

        // 将按钮添加到面板
        panel.add(new JLabel("欢迎使用图书借阅系统!"));
        panel.add(new JLabel());
        panel.add(borrowButton);
        panel.add(returnButton);
        panel.add(new JLabel());
        panel.add(new JLabel());

        // 将面板添加到窗口
        add(panel);

        // 设置窗口居中显示
        setLocationRelativeTo(null);
    }

    private void borrowBook() {
        String selectedBook = (String) JOptionPane.showInputDialog(
                null, "请选择您要借阅的图书: ", "图书借阅",
                JOptionPane.PLAIN_MESSAGE, null, books, books[0]);

        if (selectedBook == null) {
            return;
        }

        Book book = findBook(selectedBook);
        if (book.getStudent() != null) {
            JOptionPane.showMessageDialog(null, "此书已被" + book.getStudent().getName() + "借走!",
                    "温馨提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        Student student = inputId(true);
        book.borrowBy(student);
        showResult(student, book, true);
    }

    private void returnBook() {
        Student student = inputId(false);
        if (student.getBorrowList()[0] == null) {
            JOptionPane.showMessageDialog(null, "您没有未归还的图书!",
                    "温馨提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String bookName = (String) JOptionPane.showInputDialog(
                null, "请选择您要归还的图书: ", "归还图书",
                JOptionPane.PLAIN_MESSAGE, null, student.getBorrowList(), student.getBorrowList()[0]);

        if (bookName == null) {
            return;
        }

        Book book = findBook(bookName);
        book.giveBackBy(student);
        showResult(student, book, false);
    }

    private static Book findBook(String s) {
        Book curBook = null;
        for (Book book : allBook) {
            if (Objects.equals(book.getName(), s)) {
                curBook = book;
            }
        }
        return curBook;
    }

    private static Student findStudent(String id) {
        Student curStudent = null;
        for (Student student : students) {
            if (Objects.equals(student.getId(), id)) {
                curStudent = student;
            }
        }
        return curStudent;
    }

    private static Student inputId(boolean isBorrow) {
        JPanel jPanel = new JPanel();
        JTextField id = new JTextField(10);
        jPanel.add(new JLabel("请输入学号: "));
        jPanel.add(id);
        String title = isBorrow ? "图书借阅" : "图书归还";
        Student student;
        while (true) {
            int option = JOptionPane.showConfirmDialog(null, jPanel, title, JOptionPane.OK_CANCEL_OPTION);
            if (option == JOptionPane.CANCEL_OPTION) {
                System.exit(0);
            }
            if (id.getText().matches("^[0-9]*$")) {
                student = findStudent(id.getText());
                if (student != null) {
                    break;
                }
            }
            JOptionPane.showMessageDialog(null, "您输入的学号有误或无此学生!", "温馨提示",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        return student;
    }

    private static void showResult(Student student, Book book, boolean isBorrow) {
        String suffix = isBorrow ? "借阅成功!" : "归还成功!";
        JOptionPane.showMessageDialog(null, book.toString() + "\n" + student.toString() + "\n" +
                book.getName() + suffix);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LibrarySystemGUI().setVisible(true);
            }
        });
    }
}




class Student {
    /** 学生姓名 */
    private final String name;
    /** 学生学号 */
    private final String id;
    /** 学生手中图书数量 */
    private int count;
    /** 学生借书列表 */
    private ArrayList<Book> borrowList;

    /**
     * 带参构造函数
     * @param name 学生姓名
     * @param id 学生学号
     */
    public Student(String name, String id){
        this.name = name;
        this.id = id;
        count = 0;
        borrowList = new ArrayList<>();
    }

    /**
     * 获取学生姓名
     * @return 姓名
     */
    public String getName(){
        return name;
    }

    /**
     * 获取学生学号
     * @return 学号
     */
    public String getId(){
        return id;
    }

    /**
     * 获取学生手中图书数量
     * @return 学生手中图书数量
     */
    public int getCount(){
        return count;
    }

    public String[] getBorrowList(){
        String[] books = new String[count + 1];
        int index = 0;
        for (Book book : borrowList){
            books[index++] = book.getName();
        }
        return books;
    }

    /**
     * 学生借书
     * @param book 所借图书
     */
    public void borrow(Book book) {
        count++;
        borrowList.add(book);
    }

    /**
     * 学生还书
     * @param book 所还图书
     */
    public void giveBack(Book book){
        borrowList.remove(book.getIndexInStudentBorrowList());
        count--;
    }

    @Override
    public String toString(){
        StringBuilder string = new StringBuilder("姓名: " + name + "\n" +
                "学号: " + id + "\n" +
                "已借图书: ");
        for (Book book : borrowList){
            string.append(book.getName()).append("; ");
        }
        return string.toString();
    }
}





class Book {
    /** 书名 */
    private String name;
    /** 书号 */
    private String id;
    /** 借阅此书的学生 */
    private Student student;
    /** 学生借阅此书时,此书在学生所借图书列表中的序号 */
    private int indexInStudentBorrowList;

    /**
     * 带参构造函数
     * @param name 书名
     * @param id 书号
     */
    public Book(String name, String id){
        this.name = name;
        this.id = id;
        student = null;
        indexInStudentBorrowList = 0;
    }

    /**
     * 获取书名
     * @return 书名
     */
    public String getName(){
        return name;
    }

    /**
     * 获取借阅此书的学生
     * @return 借书的学生
     */
    public Student getStudent(){
        return student;
    }

    /**
     * 获取学生借阅此书时,此书在学生所借图书列表中的序号
     * @return 图书所在列表中的序号
     */
    public int getIndexInStudentBorrowList(){
        return indexInStudentBorrowList;
    }

    /**
     * 图书被某个学生借走
     * @param student 借阅此书的学生
     */
    public void borrowBy(Student student){
        if(this.student != null){
            System.out.println(this.name + "已被" + this.student.getName() + "借阅!");
        }
        this.student = student;
        indexInStudentBorrowList = student.getCount();
        student.borrow(this);
    }

    /**
     * 图书被借阅此书的学生归还
     * @param student 借阅此书的学生
     */
    public void giveBackBy(Student student){
        if(!Objects.equals(this.student.getId(), student.getId())){
            System.out.println(student.getName() + "没有借阅" + this.name + "!");
            return;
        }
        student.giveBack(this);
        this.student = null;
    }

    @Override
    public String toString(){
        String string = "书名: " + name + "\n" + "书号: " + id + "\n" + "借书学生: ";
        string += student == null ? "无" : student.getName();
        return  string;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值