图书管理系统(数据保存在本地)

图书管理系统(数据保存在本地)

/*图书管理系统:
        1.录入书籍       书名 作者 简介(增)
        2.下架书籍       根据书名下架(删)
        3.更新书籍       根据书名修改书籍信息(改)
        4.查询书籍       根据书名查询书籍(查)
        5.查询所有未借出书籍信息(显示)
        6.借出书籍       根据书名先查询该书是否被借出    是否借出:y/n
        7.查询已借出所有的书籍   (最好按照借出时间进行排列显示)
        8.归还书籍      根据书名查询是否该书是之前借出的。是:则还入---》根据借出的时间 1s1块
        9.退出 持久化*/
public class Program {
    public static void main(String[] args) {
        Menu menu = new Menu();
        menu.menu();
    }
}
import java.util.Scanner;

public class Menu {
    public void menu(){
        Choose choose = new Choose();
        //导入文件中的数据
        choose.out();
        //让程序一直执行,直到程序退出
        while (true) {
            System.out.println("欢迎来到电子图书馆");
            System.out.println(
                    "        1.录入书籍       书名 作者 简介(增)\n" +
                    "        2.下架书籍       根据书名下架(删)\n" +
                    "        3.更新书籍       根据书名修改书籍信息(改)\n" +
                    "        4.查询书籍       根据书名查询书籍(查)\n" +
                    "        5.查询所有未借出书籍信息(显示)\n" +
                    "        6.借出书籍       根据书名先查询该书是否被借出    是否借出:y/n\n" +
                    "        7.查询已借出所有的书籍   (最好按照借出时间进行排列显示)\n" +
                    "        8.归还书籍      根据书名查询是否该书是之前借出的。是:则还入---》根据借出的时间 1s1块\n" +
                    "        9.退出");
            System.out.println("请你选择要完成的功能");
            Scanner sc = new Scanner(System.in);
            int key = sc.nextInt();
            switch (key) {
                case 1:
                    choose.add();
                    break;
                case 2:
                    choose.delete();
                    break;
                case 3:
                    choose.update();
                    break;
                case 4:
                    choose.show_Not_Send();
                    break;
                case 5:
                    choose.find();
                    break;
                case 6:
                    choose.show_Send();
                    break;
                case 7:
                    choose.show_Byname();
                    break;
                case 8:
                    choose.return_book();
                    break;
                case 9:
                    choose.write();
                    choose.exit();
                    break;
            }
        }
    }
}

import java.io.Serializable;

public class Book implements Serializable {
      private String book_name;
      private String author;
      private String introduce_book;

    public Book(String book_name, String author, String introduce_book) {
        this.book_name = book_name;
        this.author = author;
        this.introduce_book = introduce_book;
    }

    @Override
    public String toString() {
        return "Book{" +
                "book_name='" + book_name + '\'' +
                ", author='" + author + '\'' +
                ", introduce_book='" + introduce_book + '\'' +
                '}';
    }

    public String getBook_name() {
        return book_name;
    }

    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }

    public String getAuthor() {
        return author;
    }

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

    public String getIntroduce_book() {
        return introduce_book;
    }

    public void setIntroduce_book(String introduce_book) {
        this.introduce_book = introduce_book;
    }
}
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

//此类实现图书管理系统的各部分功能
public class Choose {
    //该集合用于存放刚放到图书馆没有进行任何操作的图书
    ArrayList<Book> list1 = new ArrayList<>();
    //存放已经被借出去的图书的信息
    ArrayList<Book> list2 = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
     //1.录入书籍       书名 作者 简介(增)
    public void add(){
        System.out.println("请你输入要增加图书的书名");
        String name = sc.next();
        System.out.println("请你输入要增加图书的作者");
        String author = sc.next();
        System.out.println("请你输入要增加图书的简介");
        String intrduce_book = sc.next();
        Book book = new Book(name, author, intrduce_book);
        list1.add(book);
        System.out.println("图书增加成功");
    }
    //2.下架书籍       根据书名下架(删)
    public void delete(){
        System.out.println("请你输入你要删除的书的书名");
        String name = sc.next();
        //在集合中遍历是否存在该图书
        for (int i = 0;i<list1.size();i++){
            String book_name = list1.get(i).getBook_name();
            if (book_name.equals(name)) {//说明已经找到该书
                //删除图书
                list1.remove(list1.get(i));
                System.out.println("图书删除成功");
            }else {
                System.out.println("图书馆没有该书");
            }
        }
    }
    //3.更新书籍       根据书名修改书籍信息(改)
    public void update(){
        //先循环找出该书
        System.out.println("请你输入要修改的书的名字");
        String name = sc.next();
        for (int i = 0;i<list1.size();i++){
            String book_name = list1.get(i).getBook_name();
            if (name.equals(book_name)){
                //此时说明已经找到该书
                System.out.println("请你选择要修改的图书的信息");
                System.out.println("1、修改书名 2、修改作者 3、修改简介");
                int key = sc.nextInt();
                switch (key){
                    case 1:
                        System.out.println("请你输入要修改的书的的书名");
                        String name1 = sc.next();
                        list1.get(i).setBook_name(name1);
                        System.out.println("信息修改成功");
                        break;
                    case 2:
                        System.out.println("请你输入要修改的书的的作者");
                        String author = sc.next();
                        list1.get(i).setAuthor(author);
                        System.out.println("信息修改成功");
                        break;
                    case 3:
                        System.out.println("请你输入要修改的书的简介");
                        String intrduce_book = sc.next();
                        list1.get(i).setIntroduce_book(intrduce_book);
                        System.out.println("信息修改成功");
                        break;
                }
            }else {
                System.out.println("图书馆没有该图书");
            }
        }
    }
    //4.查询书籍       根据书名查询书籍(查)
    public void show_Not_Send(){
        //先找到该书
        System.out.println("请你输入要查询的书的名字");
        String name = sc.next();
        for (int i = 0;i<list1.size();i++){
            Book book = list1.get(i);
            if (book.getBook_name().equals(name)){
                //说明找到该书
                System.out.println(book);
            }else {
                System.out.println("图书馆没有该书!!!");
            }
        }
    }
    //5.查询所有未借出书籍信息(显示)
    public void find(){
        //循环集合中所有图书
        for (int i =0;i<list1.size();i++){
            System.out.println(list1.get(i));
        }
    }
    //6.借出书籍       根据书名先查询该书是否被借出    是否借出:y/n
    public void show_Send(){
        System.out.println("请输入要查询的书的名字");
        String name = sc.next();
        //遍历集合1中是否有该书
        for (int i = 0;i<list1.size();i++){
            Book book = list1.get(i);
            if (book.getBook_name().equals(name)){
                //说明找到该书
                System.out.println("该书没有被借出");
                System.out.println("你是否想借出书籍");
                System.out.println("请输入小写的y/n");
                String next = sc.next();
                if (next.equals("y")){
                    //把该书增加到集合2中,注意一定要先加后删
                    list2.add(book);
                    //把该书在集合1中删除
                    list1.remove(book);
                    System.out.println("借书成功");
                }else if (next.equals("n")){
                        System.out.println("请告诉我不借书的原因");
                    }
                }else {
                System.out.println("图书馆现在还没有该书");
            }
        }
    }
    //7.查询已借出所有的书籍   (最好按照借出时间进行排列显示)
    public void show_Byname(){
        //遍历集合2
        for (int j = 0;j<list2.size();j++){
            //打印已经被借出的图书的信息
            System.out.println(list2.get(j));
        }
    }
    //8.归还书籍      根据书名查询是否该书是之前借出的。是:则还入---》根据借出的时间 1s1块
    public void return_book(){
        System.out.println("请输入归还的图书的书名");
        String name = sc.next();
        for (int i = 0;i<list2.size();i++){
            Book book = list2.get(i);
            //判断集合2中是否有该书,如果有说明是之前借出去的图书
            if (book.getBook_name().equals(name)){
                list1.add(book);
                //删除集合2中的归还的图书信息
                list2.remove(book);
            }else {
                System.out.println("该书不是之前借出去的图书");
            }
        }
    }
    //9.退出
    public void exit(){
        System.exit(0);
    }
    //10、把数据写到文件中
    public void write(){
        //第一个集合
        try (ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\fuwei\\Desktop\\Test\\pp\\aa.txt"))){
            oos.writeObject(list1);
            oos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //第二个集合
        try (ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\fuwei\\Desktop\\Test\\pp\\bb.txt"))){
            oos.writeObject(list2);
            oos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //11、把文件中的数据写到集合中
    public void out(){
        //第一个集合
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream=null;
        try {
            fileInputStream=new FileInputStream("C:\\Users\\fuwei\\Desktop\\Test\\pp\\aa.txt");
            objectInputStream = new ObjectInputStream(fileInputStream);
            list1 = (ArrayList<Book>)objectInputStream.readObject();
            objectInputStream.close();
        } catch (FileNotFoundException e) {
            File file = new File("C:\\Users\\fuwei\\Desktop\\Test\\pp\\aa.txt");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
       //第二个集合
        ObjectInputStream objectInputStream1 = null;
        FileInputStream fileInputStream1 = null;
        try {
            fileInputStream1 =new FileInputStream("C:\\Users\\fuwei\\Desktop\\Test\\pp\\bb.txt");
            objectInputStream1 = new ObjectInputStream(fileInputStream1);
            list2 = (ArrayList<Book>)objectInputStream1.readObject();
            objectInputStream1.close();
        } catch (FileNotFoundException e) {
            File file = new File("C:\\Users\\fuwei\\Desktop\\Test\\pp\\bb.txt");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值