图书管理系统(交互式)

创建图书的书籍类

  • 图书的属性
private String title;//书名
private String author;//作者
private double price;//价格
  • 图书的有参构造(用于录入书籍使用)
public BookValue(String title, String author, double price) {
    this.title = title;
    this.author = author;
    this.price = price;
}
  • 图书属性的修改(set方法)
public void setTitle(String title) {
    this.title = title;
}

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

public void setPrice(double price) {
    this.price = price;
}
  • 图书书籍属性的显示(用于查询显示)
@Override
public String toString() {
	return "BookValue{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}

书籍管理系统

  • 创建书籍 的存储数据结构(链表)
  • 无需new; 因为会接着用上一次录入的图书数据
private static List<BookValue> BOOK_VALUE_LIST;
  • 书籍的管理指令
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("=====================================");

        switch (sc.nextInt()) {
            case 1:
                insert(sc);
                break;
            case 2:
                Book_List();
                break;
            case 3:
                BookDelete(sc);
                break;
            case 4:
                BookModify(sc);
                break;
            case 5:
                System.out.println("正在保存图书信息...");
                BookSave();
                System.out.println("感谢您的使用!!!");
                sc.close();
                return;
        }
    }
}
  • 1. 录入书籍
public static void insert(Scanner sc) {
    sc.nextLine();//用于吸收switch括号里sc中的换行符号(回车符)
    System.out.print("输入图书的名字: ");
    String title = sc.nextLine();
    System.out.print("输入图书的作者: ");
    String author = sc.nextLine();
    System.out.print("输入图书的价格: ");
    double price = sc.nextDouble();
    sc.nextLine();//用于吸收上一行sc换行符号(回车)

    BookValue book = new BookValue(title, author, price);
    //创建书籍的实体
    BOOK_VALUE_LIST.add(book);
    //将一本书的类(实体)加入书籍的链表
    System.out.println("图书信息添加成功 " + book);
    //book会显示toString方法的值
}
  • 2. 所有书籍的查询显示
public static void Book_List() {
    for (int i = 0; i < BOOK_VALUE_LIST.size(); i++) {
        System.out.println(i + ". " + BOOK_VALUE_LIST.get(i));
    }
}
  • 3. 书籍的删除
public static void BookDelete(Scanner sc) {
    sc.nextLine();//用于吸收switch括号里sc中的换行符号(回车符)
    System.out.print("输入图书索引号: ");
    int index = sc.nextInt();
    sc.nextLine();//用于吸收上一行sc换行符号(回车)
    while (index > BOOK_VALUE_LIST.size() || index < 0) {
        System.out.print("没有对应索引号, 请重新输入: ");
        index = sc.nextInt();
        sc.nextLine();//用于吸收上一行sc换行符号(回车)
    }
    BOOK_VALUE_LIST.remove(index);
    System.out.println("图书删除成功");
}
  • 4. 书籍的保存
  • try()可以不用写文件的close(语法糖)
public static void BookSave() {
    try (ObjectOutputStream BookStream = new ObjectOutputStream(new FileOutputStream("BookData"))) {
        BookStream.writeObject(BOOK_VALUE_LIST);
        //对象的输出流: ObjectOutputStream
        //对象的输入流:  ObjectInputStream
        BookStream.flush();//迫使所有缓冲的输出数据被写出到底层输出流中
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 5. 书籍的修改(针对书籍链表的索引号)
public static void BookModify(Scanner sc) {
    sc.nextLine();
    System.out.print("输入需要修改图书索引号: ");
    int index = sc.nextInt();
    sc.nextLine();//用于吸收上一行sc换行符号(回车)
    while (index > BOOK_VALUE_LIST.size() - 1 || index < 0) {
        System.out.print("没有对应索引号, 请重新输入: ");
        index = sc.nextInt();
        sc.nextLine();//用于吸收上一行sc换行符号(回车)
    }
    BookValue book = BOOK_VALUE_LIST.get(index);
    System.out.print("输入图书的名字: ");
    book.setTitle(sc.nextLine());

    System.out.print("输入图书的作者: ");
    book.setAuthor(sc.nextLine());

    System.out.print("输入图书的价格: ");
    book.setPrice(sc.nextDouble());

    System.out.println("图书修改成功");
}
  • 6. 当前使用书籍管理系统且需要加载上次录入的数据进当前系统的使用
@SuppressWarnings("unchecked")//跳过检查错误
public static void BookLoad() {
    File file = new File("BookData");//文件的开启
    if (file.exists()) {
        try (ObjectInputStream BookStream = new ObjectInputStream(new FileInputStream("BookData"))) {
            BOOK_VALUE_LIST = (List<BookValue>) BookStream.readObject();
            //将BookData中文件数据读入当前的书籍链表(需要强制转换为List<BookValue>)
            //对象的输出流: ObjectOutputStream
            //对象的输入流:  ObjectInputStream
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        BOOK_VALUE_LIST = new LinkedList<>();
    }
}

书籍类代码

package BookManger;

import lombok.Getter;

import java.io.Serializable;
@Getter
public class BookValue implements Serializable {//添加序列化,就是保存
    // 序列化的过程,就是一个“freeze”的过程,
    // 它将一个对象freeze(冷冻)住,然后进行存储,
    // 等到再次需要的时候,再将这个对象de-freeze就可以立即使用。
    private String title;
    private String author;
    private double price;

    public BookValue(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public void setTitle(String title) {
        this.title = title;
    }

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

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

    @Override
    public String toString() {
        return "BookValue{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}

书籍管理代码

package BookManger;

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


public class MainSysteam {
    private static List<BookValue> BOOK_VALUE_LIST;

    public static void main(String[] args) {
        System.out.println("正在加载图书管理系统...");
        BookLoad();//上次书籍数据的载入
        Scanner sc = 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. 修改图书信息");
            System.out.println("5. 退出图书系统");
            System.out.println("=====================================");

            switch (sc.nextInt()) {
                case 1:
                    insert(sc);
                    break;
                case 2:
                    Book_List();
                    break;
                case 3:
                    BookDelete(sc);
                    break;
                case 4:
                    BookModify(sc);
                    break;
                case 5:
                    System.out.println("正在保存图书信息...");
                    BookSave();
                    System.out.println("感谢您的使用!!!");
					sc.close();
                    return;
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static void BookLoad() {
        File file = new File("BookData");
        if (file.exists()) {
            try (ObjectInputStream BookStream = new ObjectInputStream(new FileInputStream("BookData"))) {
                BOOK_VALUE_LIST = (List<BookValue>) BookStream.readObject();
                //对象的输出流: ObjectOutputStream
                //对象的输入流:  ObjectInputStream
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            BOOK_VALUE_LIST = new LinkedList<>();
        }
    }

    public static void BookModify(Scanner sc) {
        sc.nextLine();//用于吸收switch中的sc中的换行符号(回车)
        System.out.print("输入需要修改图书索引号: ");
        int index = sc.nextInt();
        sc.nextLine();//用于吸收上一行sc中的换行符号(回车)
        while (index > BOOK_VALUE_LIST.size() - 1 || index < 0) {
            System.out.print("没有对应索引号, 请重新输入: ");
            index = sc.nextInt();
            sc.nextLine();//用于吸收上一行sc中的换行符号(回车)
        }
        BookValue book = BOOK_VALUE_LIST.get(index);
        System.out.print("输入图书的名字: ");
        book.setTitle(sc.nextLine());

        System.out.print("输入图书的作者: ");
        book.setAuthor(sc.nextLine());

        System.out.print("输入图书的价格: ");
        book.setPrice(sc.nextDouble());

        System.out.println("图书修改成功");
    }

    public static void BookDelete(Scanner sc) {
        sc.nextLine();//用于吸收switch中的sc中的换行符号(回车)
        System.out.print("输入图书索引号: ");
        int index = sc.nextInt();
        sc.nextLine();//用于吸收上一行sc中的换行符号(回车)
        while (index > BOOK_VALUE_LIST.size() || index < 0) {
            System.out.print("没有对应索引号, 请重新输入: ");
            index = sc.nextInt();
            sc.nextLine();//用于吸收上一行sc中的换行符号(回车)
        }
        BOOK_VALUE_LIST.remove(index);
        System.out.println("图书删除成功");
    }

    public static void BookSave() {
        try (ObjectOutputStream BookStream = new ObjectOutputStream(new FileOutputStream("BookData"))) {
            BookStream.writeObject(BOOK_VALUE_LIST);
            //对象的输出流: ObjectOutputStream
            //对象的输入流:  ObjectInputStream
            BookStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void insert(Scanner sc) {
        sc.nextLine();//用于吸收switch中的sc中的换行符号(回车)
        System.out.print("输入图书的名字: ");
        String title = sc.nextLine();
        System.out.print("输入图书的作者: ");
        String author = sc.nextLine();
        System.out.print("输入图书的价格: ");
        double price = sc.nextDouble();
        sc.nextLine();//用于吸收上一行sc中的换行符号(回车)

        BookValue book = new BookValue(title, author, price);
        BOOK_VALUE_LIST.add(book);
        System.out.println("图书信息添加成功 " + book);
    }

    public static void Book_List() {
        for (int i = 0; i < BOOK_VALUE_LIST.size(); i++) {
            System.out.println(i + ". " + BOOK_VALUE_LIST.get(i));
        }
    }
}

注意:

  • public class BookValue implements Serializable;Serializable可以让类拥有存储冻结对象的功能
  • sc.nextLine(); 用于吸收上一行sc换行符号(回车);避免输入的错误