Java完成一个基于IO的图书管理系统,

提示:可以使用对象流,登录成功直接将所有图书信息读取到集合中,增删改查直接操作集合,在退出时将最终集合数据放入文件。

主要代码:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("-----小蔡图书管理系统-----");
            System.out.println("1  注册");
            System.out.println("2  登录");
            System.out.println("请输入你的选择");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    //注册
                    register();
                    break;
                case 2:
                    //登录
                    login();
                    break;
            }
        }


    }

    //登录功能
    public static void login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("------欢迎登录---------");
        System.out.println("请输入用户名(不能有“=”):");
        String username = sc.next();
        System.out.println("请输入密码(不少于6位):");
        String password = sc.next();
        BufferedReader br = null;
        try {
            File userFile = new File("test/user.txt");
            br = new BufferedReader(new FileReader(userFile));
            boolean flag = false;
            String str;
            while ((str = br.readLine()) != null) {
                if (str.equals(username + "=" + password)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                System.out.println("登录成功。");
                libraryMenu();
            } else {
                System.out.println("用户名或密码错误,登录失败,重新登陆。");
                login();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关流
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //注册功能
    public static void register() {
        Scanner sc = new Scanner(System.in);
        String username;
        String password;
        while (true) {
            System.out.println("请输入用户名(不能有“=”):");
            username = sc.next();
            //判断用户名中有没有“=”
            int i = username.indexOf("=");
            if (i != -1) {
                System.out.println("用户名包含“=”,请重新输入。");
            } else {
                break;
            }
        }
        while (true) {
            System.out.println("请输入密码(不少于6位):");
            password = sc.next();
            if (password.length() < 6) {
                System.out.println("你的密码少于6位,请重新输入。");
            } else {
                break;
            }
        }
        BufferedWriter bw = null;
        try {
            File userFile = new File("test/user.txt");
            //创建文件
            userFile.createNewFile();
            bw = new BufferedWriter(new FileWriter(userFile, true));
            bw.write(username + "=" + password);
            bw.newLine();
            System.out.println("注册成功。");
            System.out.println("--------------------------------");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //图书馆菜单
    public static void libraryMenu() {
        ArrayList<Book> list = new ArrayList<>();

        Book book1 = new Book(1, "西游记", 19.9, "吴承恩");
        Book book2 = new Book(2, "红楼梦", 19.9, "曹雪芹");
        list.add(book1);
        list.add(book2);
        addListFile(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("0 退出");
            System.out.println("请输入你的选择:");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();

            switch (choose) {
                case 1:
                    System.out.println("查看图书");
                    showBook(list);
                    break;
                case 2:
                    System.out.println("新增图书");
                    addBook(list);
                    break;
                case 3:
                    System.out.println("修改图书");
                    updateBook(list);
                    break;
                case 4:
                    System.out.println("删除图书");
                    deleteBook(list);
                    break;
                case 0:
                    System.exit(0);
                default:
                    System.out.println("没有这个选项");
            }
        }

    }


    //查看图书
    public static void showBook(ArrayList<Book> list) {
        System.out.println("图书编号\t\t图书名称\t\t图书价格\t\t作者");
        BufferedReader br = null;
        boolean flag = false;
        for (Book book : list) {
            System.out.println(book);
        }

    }

    //添加图书
    public static void addBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("----------添加图书----------");
        int bookId;
        while (true) {
            System.out.println("请输入图书的编号:");
            bookId = sc.nextInt();
            if(!isId(list,bookId)){
                break;
            }else {
                System.out.println("要添加的图书编号已存在,请重新输入。");
            }
        }
        System.out.println("请输入图书的名称:");
        String bookName = sc.next();
        System.out.println("请输入图书的价格:");
        double price = sc.nextDouble();
        System.out.println("请输入图书的作者:");
        String author = sc.next();
        list.add(new Book(bookId, bookName, price, author));
        addListFile(list);

    }

    //更新图书
    public static void updateBook(ArrayList<Book> list) {

        Scanner sc = new Scanner(System.in);
        int bookId;
        while (true) {
            System.out.println("请输入要修改的图书编号:");
            bookId = sc.nextInt();
            if(isId(list,bookId)){
                break;
            }else {
                System.out.println("要修改的图书编号不存在,请重新输入。");
            }
        }

        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (book.getBookId() == bookId) {
                System.out.println("请输入新书名:");
                String newName = sc.next();
                book.setBookName(newName);
                System.out.println("请输入图书新价格");
                double price = sc.nextDouble();
                book.setPrice(price);
                System.out.println("请输入图书的作者:");
                String author = sc.next();
                book.setAuthor(author);
            }
        }
        addListFile(list);
        System.out.println("修改成功");
    }

    //删除图书
    public static void deleteBook(ArrayList<Book> list) {
        System.out.println("请输入要删除的图书编号:");
        Scanner sc = new Scanner(System.in);
        int bookId = sc.nextInt();
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (book.getBookId() == bookId) {
                flag = true;
                list.remove(book);
                addListFile(list);
            }
        }
        if(flag){
            System.out.println("删除成功。");
        }else {
            System.out.println("要删除的图书编号不存在。");
        }

    }


    //查询图书id是否存在
    public static boolean isId(ArrayList<Book> list, int id) {
        for (Book book : list) {
            if( book.getBookId() == id){
                return true;
            }
        }
        return false;
    }

    //将list保存到文件中,使用对象流
   public static void addListFile(ArrayList<Book> list){
        ObjectOutputStream oobj = null;
       try {
           oobj = new ObjectOutputStream(new FileOutputStream("test/book.txt"));
           oobj.writeObject(list);
       } catch (IOException e) {
           e.printStackTrace();
       }finally {
           try {
               if(oobj != null){
                   oobj.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }

}

Book实体类

//Book类,实现Serializable接口,序列化
public class Book implements Serializable {
    private int bookId;
    private String bookName;
    private double price;
    private String author;

    public Book() {
    }

    public Book(int bookId, String bookName, double price, String author) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.price = price;
        this.author = author;
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getPrice() {
        return price;
    }

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

    public String getAuthor() {
        return author;
    }

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

    public String toString() {

        return bookId + "\t\t\t" + bookName + "\t\t" + price + "元\t\t" + author;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值