图书管理系统IO

public class MainClass {
    public static void main(String[] args) {

        // 图书类Book
        //   书名
        //   作者
        //   ISBN
        // 用户类User
        //   用户名
        //   密码
        //
        // 图书管理BookSystem
        //   维护一个装有Book类型的List对象
        //   维护一个装有User用户的List对象
        //
        //   增
        //   删
        //   改
        //   查
        //   登录
        //   注册
        //   退出系统
        //   显示列表
        //


        BookSystem bs = new BookSystem();
        bs.run();


    }
}

import java.io.Serializable;
// Serializable用于IO读写
public class Book implements Serializable {

    private String name;
    private String author;
    private String ISBN;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

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

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }
}

import java.io.Serializable;

public class User implements Serializable {

    private String name;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

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

public class BookSystem {

    private List<Book> bookList = new ArrayList<>();
    private List<User> userList = new ArrayList<>();
    // 用于最外层循环的控制变量
    private boolean flag = true;
//IO文件路径
    private final static String BOOK_PATH = "./booklist";
    private final static String USER_PATH = "./userlist";


    public void run(){
        // 运行的流程
        Scanner input = new Scanner(System.in);

        loadData(BOOK_PATH);
        loadData(USER_PATH);

        while (flag){

            System.out.println("请输入要进行的操作:");
            System.out.println("1. 注册 2.登录");
            try {
                int i = input.nextInt();
                if (i == 1){
                    register();
                } else if (i == 2){

                    if (login()){
                        // 正常运行登录成功之后的方法
                        manageBooks();
                    }
                } else {
                    System.out.println("输入有误, 请重新输入");
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("输入有误, 请重新输入");
            }
        }


    }

    // 登录之后做图书管理
    public void manageBooks(){

        Scanner input = 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("6.退出系统");
            int i = input.nextInt();

            if (i == 1){
                add();
            } else if (i == 2){
                remove();
            } else if (i == 3){
                update();
            } else if (i == 4){
                search();
            } else if (i == 5){
                listAllBooks();
            } else {
                System.out.println("程序正在退出...");
                flag = false;
                break;
            }
        }
    }



    public void add(){

        Scanner input = new Scanner(System.in);

        System.out.println("请输入书名:");
        String name = input.next();
        System.out.println("请输入作者:");
        String author = input.next();
        System.out.println("请输入ISBN:");
        String ISBN = input.next();

        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setISBN(ISBN);

        bookList.add(book);

        System.out.println("添加成功!");
        saveData(BOOK_PATH);
    }

    public void remove(){

        List<Book> searchResult = search();

        if (searchResult.size() != 0){
            Scanner input = new Scanner(System.in);
            System.out.println("请输入要删除的序号:");

            int i = input.nextInt();
            if (i>=0 && i<searchResult.size()){

                Book book = searchResult.get(i);

                // 删除对象
                bookList.remove(book);
                searchResult.remove(book);
                System.out.println("删除成功");
                saveData(BOOK_PATH);
            } else {
                System.out.println("输入无效!");
            }
        }
    }

    public void update(){
        List<Book> searchResult = search();

        if (searchResult.size() != 0){
            Scanner input = new Scanner(System.in);
            System.out.println("请输入要删除的序号:");

            int i = input.nextInt();
            if (i>=0 && i<searchResult.size()){

                Book book = searchResult.get(i);

                System.out.println("请输入要修改的属性序号:");
                System.out.println("1.书名");
                System.out.println("2.作者");
                System.out.println("3.ISBN");
                int prop = input.nextInt();
                System.out.println("请输入新的内容:");
                String ctn = input.next();

                if (prop == 1){
                    book.setName(ctn);
                } else if (prop == 2){
                    book.setAuthor(ctn);
                } else if (prop == 3) {
                    book.setISBN(ctn);
                } else {
                    System.out.println("序号输入错误,没有该属性");
                    return;
                }

                System.out.println("修改成功!");
                saveData(BOOK_PATH);

            } else {
                System.out.println("输入无效!");
            }
        }

    }

    public List<Book> search(){

        // 根据书名查找图书
        Scanner input = new Scanner(System.in);

        System.out.println("请输入要查找的书名:");
        String name = input.next();

        List<Book> searchResult = new ArrayList<>();

        for (Book book : bookList) {
            if (name.equals(book.getName())){
                searchResult.add(book);
            }
        }

        if (searchResult.size() == 0){
            System.out.println("查无此书!");
        } else {
            System.out.println("查到的书如下:");

            for (int i = 0; i < searchResult.size(); i++) {
                Book book = searchResult.get(i);
                System.out.println(i+"."+book.getName()
                        +" 作者:"+book.getAuthor());
            }
        }

        return searchResult;
    }

    public void listAllBooks(){

        for (int i = 0; i < bookList.size(); i++) {
            Book book = bookList.get(i);
            System.out.println(i+"."+book.getName()
                    +" 作者:"+book.getAuthor());
        }

    }

    public boolean login(){

        // 登录
        Scanner input = new Scanner(System.in);

        System.out.println("请输入用户名:");
        String username = input.next();
        System.out.println("请输入密码:");
        String password = input.next();

        // 用于判断是否登录成功
        boolean b = false;
        for (User user : userList) {
            if (username.equals(user.getName()) &&
            password.equals(user.getPassword())){
                System.out.println("登录成功");
                b = true;
                break;
            }
        }
        return b;
    }

    public void register(){

        // 注册
        Scanner input = new Scanner(System.in);

        System.out.println("请输入用户名:");
        String username = input.next();
        System.out.println("请输入密码:");
        String password = input.next();

        // 判断list中是否存在该用户名
        for (User user : userList) {
            if (username.equals(user.getName())){
                System.out.println("该用户名已经被占用.");
                return;
            }
        }

        // 只要执行到这个位置,
        // 一定是遍历的时候没有任何username和输入的内容匹配

        User user = new User();
        user.setName(username);
        user.setPassword(password);

        userList.add(user);

        saveData(USER_PATH);
        System.out.println("注册成功!");
    }

    // 从硬盘文件中读取数据
    public void loadData(String path){

        File file = new File(path);

        if (!file.exists()){
            System.out.println("欢迎新用户使用.");
            return;
        }

        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));

            if (BOOK_PATH.equals(path)){
                bookList = (List<Book>) ois.readObject();
            } else if (USER_PATH.equals(path)){
                userList = (List<User>) ois.readObject();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    // 把内存数据保存到硬盘上
    public void saveData(String path){

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));

            if (BOOK_PATH.equals(path)){
                oos.writeObject(bookList);
            } else if (USER_PATH.equals(path)){
                oos.writeObject(userList);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }




}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值