Java图书管理系统

功能

实现简单的增删改查

Main类

java 复制代码
package com.test;

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

public class Main {

    private static List<Book> LIST;

    public static void main(String[] args) {
        System.out.println("正在初始化图书管理系统...");
        load();
        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. 修改书籍信息");
            System.out.println("5. 退出系统");
            System.out.println("=======================================");
            switch(scanner.nextInt()){
                case 1:
                    insert(scanner);
                    break;
                case 2:
                    list();
                    break;
                case 3:
                    delete(scanner);
                    break;
                case 4:
                    modify(scanner);
                    break;
                case 5:
                    System.out.println("正在保存图书数据...");
                    save();
                    System.out.println("Bye bye ~");
                    return;
            }
        }
    }

    //导入数据
    @SuppressWarnings("unchecked")
    private static void load(){
        File file = new File("data");
        if(file.exists()){
            try (ObjectInputStream stream = new ObjectInputStream(new FileInputStream("data"))){
                LIST = (List<Book>) stream.readObject();
            } catch (IOException | ClassNotFoundException e){
                e.printStackTrace();
            }
        }else{
            LIST = new LinkedList<>();
        }
    }

    //保存信息
    private static void save(){
        try (ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("data"))){
            stream.writeObject(LIST);
            stream.flush();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    //插入书籍
    private static void insert(Scanner scanner){
        //吸收回车
        scanner.nextLine();
        System.out.print("请输入书籍的标题:");
        String title = scanner.nextLine();
        System.out.print("请输入书籍的作者:");
        String author = scanner.nextLine();
        System.out.print("请输入书籍的价格:");
        int price = scanner.nextInt();
        scanner.nextLine();
        Book book = new Book(title, author, price);
        LIST.add(book);
        System.out.println("书籍信息添加成功:" + book);

//        //建造者模式
//        Book book = Book.builder()
//                .title(scanner.nextLine())
//                .author(scanner.nextLine())
//                .price(scanner.nextInt())
//                .build();
    }

    //查询书籍
    private static void list(){
        for(int i = 0; i < LIST.size(); i++){
            System.out.println(i + "." + LIST.get(i));
        }
    }

    //删除书籍
    private static void delete(Scanner scanner){
        scanner.nextLine();
        System.out.print("请输入你要删除的书籍ID编号:");
        int index = scanner.nextInt();
        scanner.nextLine();
        while(index >= LIST.size() || index < 0){
            System.out.print("没有对应的书籍,请重新输入:");
            index = scanner.nextInt();
            scanner.nextLine();
        }
        LIST.remove(index);
        System.out.println("删除书籍信息成功!");
    }

    //修改书籍
    private static void modify(Scanner scanner){
        scanner.nextLine();
        System.out.print("请输入你要修改的书籍ID编号:");
        int index = scanner.nextInt();
        scanner.nextLine();
        while(index >= LIST.size() || index < 0){
            System.out.println("没有对应的书籍,请重新输入:");
            index = scanner.nextInt();
            scanner.nextLine();
        }
        Book book = LIST.get(index);
        System.out.print("请输入书籍的标题:");
        book.setTitle(scanner.nextLine());
        System.out.print("请输入书籍的作者:");
        book.setAuthor(scanner.nextLine());
        System.out.print("请输入书籍的价格:");
        book.setPrice(scanner.nextInt());
        scanner.nextLine();
        System.out.println("书籍信息修改成功!");
    }
}

Book类

java 复制代码
package com.test;

import java.io.Serializable;

public class Book implements Serializable {
    private String title;
    private String author;
    private int price;

    public Book(String title, String author, int price){
        this.title = title;
        this.author = author;
        this.price = price;
    }

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

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

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

    //    public static BookBuilder builder(){
//        return new BookBuilder();
//    }

    @Override
    public String toString() {
        return "《" + title + "》作者:" + author + " (" + price + "元)";
    }

//    public static class BookBuilder{
//
//        private String title;
//        private String author;
//        private int price;
//        private BookBuilder(){}
//
//        public BookBuilder title(String title){
//            this.title = title;
//            return this;
//        }
//
//        public BookBuilder author(String author){
//            this.author = author;
//            return this;
//        }
//
//        public BookBuilder price(int price){
//            this.price = price;
//            return this;
//        }
//
//        public Book build(){
//            return new Book(title, author, price);
//        }
//    }
}
相关推荐
一个不正经的林Sir2 分钟前
C#WPF基础介绍/第一个WPF程序
开发语言·c#·wpf
愤怒的代码6 分钟前
Spring Boot对访问密钥加解密——HMAC-SHA256
java·spring boot·后端
带多刺的玫瑰6 分钟前
Leecode刷题C语言之切蛋糕的最小总开销①
java·数据结构·算法
API快乐传递者6 分钟前
Python爬虫获取淘宝详情接口详细解析
开发语言·爬虫·python
公众号Codewar原创作者8 分钟前
R数据分析:工具变量回归的做法和解释,实例解析
开发语言·人工智能·python
赵钰老师11 分钟前
基于R语言APSIM模型应用及批量模拟(精细农业、水肥管理、气候变化、粮食安全、土壤碳周转、环境影响、农业可持续性、农业生态等)
开发语言·数据分析·r语言
栗豆包22 分钟前
w118共享汽车管理系统
java·spring boot·后端·spring·tomcat·maven
lly20240628 分钟前
Highcharts 饼图:数据可视化利器
开发语言
夜半被帅醒28 分钟前
MySQL 数据库优化详解【Java数据库调优】
java·数据库·mysql
lw向北.34 分钟前
Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
android·开发语言·qt