图书管理系统(使用IO流实现数据的读取和写入)--version4.0

 目录

一、项目要求:

二、项目环境

三、项目使用的知识点

四、项目代码

五、项目运行结果

六、项目难点分析


图书管理系统--versions1.0:

图书管理系统--versions1.0-CSDN博客文章浏览阅读981次,点赞29次,收藏17次。本文使用了变量,数据类型,顺序,选择,循环,数组实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135583634?spm=1001.2014.3001.5501icon-default.png?t=N7T8https://blog.csdn.net/qq_53483101/article/details/135583634?spm=1001.2014.3001.5501

图书管理系统--versions2.0:

图书管理系统--versions2.0-CSDN博客文章浏览阅读1k次,点赞35次,收藏19次。本文使用了变量,数据类型,顺序,选择,循环,数组,对象及属性的封装实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135728923?spm=1001.2014.3001.5501icon-default.png?t=N7T8https://blog.csdn.net/qq_53483101/article/details/135728923?spm=1001.2014.3001.5501

图书管理系统--versions3.0:

图书管理系统(ArrayList和LinkedList)--versions3.0-CSDN博客本文使用了变量,数据类型,顺序,选择,循环,对象及属性的封装,使用ArrayList和LinkedList集合,实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135939196?spm=1001.2014.3001.5501icon-default.png?t=N7T8https://blog.csdn.net/qq_53483101/article/details/135939196?spm=1001.2014.3001.5501

 图书管理系统--versions4.0:图书管理系统(使用IO流实现数据的读取和写入)--version4.0-CSDN博客文章浏览阅读342次,点赞8次,收藏9次。使用io实现图书管理系统中管理员用户、普通用户、书籍信息的读取和写入。https://blog.csdn.net/qq_53483101/article/details/136306379?spm=1001.2014.3001.5501


一、项目要求:

1)通过账号控制图书管理系统,账号分为管理员账号和普通用户账号
    1. 管理员账号和普通用户账号都可以使用手机号码+密码、身份证号码+密码的形式登录,登录方式二选一

     2.管理员账号和普通账号除了手机号码、身份中号码、密码三个数据之外,还有姓名、性别、专业、住址信息

2)启动系统后,显示登录账号、注册账号、退出登录三个模块
        a)登录账号:
                1、显示管理员登录
                2、用户登录两个模块

        b)注册账号:
                1、显示注册管理员
                2、注册用户两个模块
        c)退出登录:

        结束程序运行

3)登录账号成功后,根据账号的性质来显示不同的模块:
        登录管理员账号成功后,显示如下模块:

                1、查看所有图书,图书显示哪些信息,这里设计的信息有书名,书的价格,书的数量

                2、添加图书

                3、修改图书 

                4、删除图书

                5、查看所有普通用户信息

                6、查看管理员账号信息

                7、修改管理员账号信息

                8、退出系统

        登录普通账号成功后,显示如下模块:

                1、查看所有图书,图书显示哪些信息,你自行设计

                2、借阅图书

                3、归还图书

                4、显示用户信息(只能查看自己的用户信息)

                5、修改用户信息(只能修改自己的用户信息)

                6、退出系统

4)使用对象对不同类及其各自属性进行封装与功能实现

5)使用IO流实现数据的读取和写入

二、项目环境

(1)开发工具:IDEA、JDK17
(2)开发语言:Java

三、项目使用的知识点

(1)理解程序基本概念——程序、变量、数据类型
(2)顺序、选择、循环、跳转语句

(3)使用对象的思想对图书管理系统--versions1.0进行改造

        这里使用了对象封装的思想,将其分为管理员用户类,普通用户,书类,工具类,系统主页面类,将各自的类的属性功能封装,在系统页面类里面调用这些属性方法实现各个功能的开发。

   (4)使用io流实现了数据的读取和写入。

四、项目代码

        这里说明一下,为了快速写出项目,前面的用户管理的八个属性均采用String类型存储,方便自己后续代码的编写及相关功能的实现,有不足或者想要自己实现的功能部分读者可以自行修改代码实现。

AdminUser类:
package com.iosystem;

import java.io.*;

import java.util.ArrayList;
import java.util.Scanner;

public class AdminUser {
    private String adminUser;  //0
    private String adminPhoneNumber;  // 1
    private String adminIdentityNumber;  //2
    private String adminPassword;  //3
    private String adminName;
    private String adminSex;
    private String adminCareer;
    private String adminAddress;


    public AdminUser() {
    }

    public AdminUser(String adminUser, String adminPhoneNumber, String adminIdentityNumber, String adminPassword, String adminName, String adminSex, String adminCareer, String adminAddress) {
        this.adminUser = adminUser;
        this.adminPhoneNumber = adminPhoneNumber;
        this.adminIdentityNumber = adminIdentityNumber;
        this.adminPassword = adminPassword;
        this.adminName = adminName;
        this.adminSex = adminSex;
        this.adminCareer = adminCareer;
        this.adminAddress = adminAddress;
    }

    public String getAdminUser() {
        return adminUser;
    }

    public void setAdminUser(String adminUser) {
        this.adminUser = adminUser;
    }

    public String getAdminPhoneNumber() {
        return adminPhoneNumber;
    }

    public void setAdminPhoneNumber(String adminPhoneNumber) {
        this.adminPhoneNumber = adminPhoneNumber;
    }

    public String getAdminIdentityNumber() {
        return adminIdentityNumber;
    }

    public void setAdminIdentityNumber(String adminIdentityNumber) {
        this.adminIdentityNumber = adminIdentityNumber;
    }

    public String getAdminPassword() {
        return adminPassword;
    }

    public void setAdminPassword(String adminPassword) {
        this.adminPassword = adminPassword;
    }

    public String getAdminName() {
        return adminName;
    }

    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }

    public String getAdminSex() {
        return adminSex;
    }

    public void setAdminSex(String adminSex) {
        this.adminSex = adminSex;
    }

    public String getAdminCareer() {
        return adminCareer;
    }

    public void setAdminCareer(String adminCareer) {
        this.adminCareer = adminCareer;
    }

    public String getAdminAddress() {
        return adminAddress;
    }

    public void setAdminAddress(String adminAddress) {
        this.adminAddress = adminAddress;
    }



    Scanner scan = new Scanner(System.in);
    //管理员用户登录验证
    public int loginCheck(File file) throws IOException {
        BufferedReader brd = new BufferedReader(new FileReader(file));

        System.out.print("请输入你要登录的形式(1.手机号码+密码 2.身份证号码+密码):");
        int verifyNum = scan.nextInt();  //verifyNum  验证方式数字
        //只能选择一种登录方式
        while (true) {
            if (verifyNum == 1 || verifyNum == 2) {
                break;
            } else {
                System.out.print("\n输入错误,请输入要选择要操作的序号(只能输入1,2):");
                verifyNum = scan.nextInt();
            }
        }
        //   1.手机号码+密码   2.身份证号码+密码
        System.out.print("请输入你要登录的管理员序号(0,1,2):");
        int adminId = scan.nextInt();  //  登录管理员账号序号
        ArrayList<String> lines = new ArrayList<>();

        String line;
        while((line=brd.readLine())!=null){
            lines.add(line);
        }

        int x = lines.size();
        while(true){
            if(adminId<=x-1){
                break;
            }else {
                System.out.print("你输入的管理员账户不存在,请重新输入你要登录的管理员序号(0,1,2):");
                adminId = scan.nextInt();  //  登录管理员账号序号
            }

        }

        String[] strings = lines.get(adminId).split(",");
        if (verifyNum == 1) {    // 管理员用户
            //  管理员登录验证     手机号码+密码
            System.out.println("\n---请输入你的验证信息---");
            System.out.print("\n请输入管理员账号(默认root):");
            String adminUser1 = scan.next();
            System.out.print("\n请输入手机号码");
            String adminPhoneNumber1 = scan.next();
            System.out.print("\n请输入密码:");
            String adminPassword1 = scan.next();

            while (true) {
                if (adminUser1.equals(strings[0]) && adminPhoneNumber1.equals(strings[1]) && adminPassword1.equals(strings[3])) {
                    break;
                } else {
                    System.out.println("输入信息错误,请重新输入信息!");
                    System.out.print("请输入管理员账号序号(默认root):");
                    adminUser1 = scan.next();
                    System.out.print("\n请输入手机号码:");
                    adminPhoneNumber1 = scan.next();
                    System.out.print("\n请输入密码:");
                    adminPassword1 = scan.next();
                }
            }
        } else if (verifyNum == 2) {   //  管理员登录验证     身份证号码+密码
            System.out.print("请输入账号(默认root):");
            String adminUser2 = scan.next();
            System.out.print("\n请输入身份证号码:");
            String adminIdentityNumber2 = scan.next();
            System.out.print("\n请输入密码:");
            String adminPassword2 = scan.next();
            while (true) {
                if (adminUser2.equals(strings[0]) && adminIdentityNumber2.equals(strings[2]) && adminPassword2.equals(strings[3])) {
                    break;
                } else {
                    System.out.println("输入信息错误,请重新输入信息!");
                    System.out.print("请输入账号(默认root):");
                    adminUser2 = scan.next();
                    System.out.print("\n请输入身份证号码:");
                    adminIdentityNumber2 = scan.next();
                    System.out.print("\n请输入密码:");
                    adminPassword2 = scan.next();
                }
            }
        }
        System.out.println("登录成功!");
        brd.close();
        return adminId;
    }
    // 注册管理员用户
    public void addAdminUser(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        ArrayList<String> lines = new ArrayList<String>();
        String line=null;
        while((line = bufferedReader.readLine())!=null){
            lines.add(line);
        }
        System.out.print("\n请输入新的管理员账户名:");
        String user = scan.next();
        System.out.print("\n请输入新的手机号码:");
        String phonenumber = scan.next();
        System.out.print("\n请输入新的身份证号码:");
        String identitynumber = scan.next();
        System.out.print("\n请输入新的密码:");
        String password = scan.next();
        System.out.print("\n请输入新的姓名:");
        String name = scan.next();
        System.out.print("\n请输入新的性别:");
        String sex= scan.next();
        System.out.print("\n请输入新的专业:");
        String career= scan.next();
        System.out.print("\n请输入新的住址信息:");
        String address = scan.next();
        line =user  +","+ phonenumber +","+ identitynumber+","
                    +password+"," +name+"," + sex+","
                    +career+"," + address;

        lines.add(line);
        BufferedWriter bf = new BufferedWriter(new FileWriter(file));

        for (String content :lines){
            bf.write(content);
            bf.newLine();
        }
        System.out.println("注册管理员用户成功!");
        bf.flush();
        bf.close();
        bufferedReader.close();
    }

    //查看管理员账号信息
    public void adminInfo(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));

        System.out.println("序号\t\t\t"+"账户名\t\t\t"+"手机号\t\t\t"+"身份证号\t\t\t"+"密码\t\t\t"+"姓名\t\t\t"+"性别\t\t\t"+"专业\t\t\t"+"住址\t\t\t");
        String line;
        int count =0;
        while ((line=br.readLine())!=null){
            String[] strings = line.split(",");
            System.out.print(count++ +"\t\t\t");
            for(int i=0;i<strings.length;i++){
                System.out.print(strings[i]+"\t\t\t");
            }
            System.out.print("\n");
        }
        br.close();
    }
    //修改管理员账号信息
    // num 修改信息序号  id 登录的是第几个管理员
    public void adminAlter(int num, int id, File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<String>();
        String line =null;
        int count=0;
        while((line=bufferedReader.readLine())!=null){
            if(count==id){
                String[] strings = line.split(",");
                if (num == 1) {                  // 管理员账号名
                    System.out.print("\n请输入新的管理员账户名:");
                    String user = scan.next();
                    strings[0] = user;
                    line = String.join(",",strings);
                    strb.add(line);

                } else if (num == 2) {        // 手机号码
                    System.out.print("\n请输入新的手机号码:");
                    String phonenumber = scan.next();
                    strings[1] = phonenumber;
                    line = String.join(",",strings);
                    strb.add(line);
                } else if (num == 3) {        // 身份证号码
                    System.out.print("\n请输入新的身份证号码:");
                    String identitynumber = scan.next();
                    strings[2] = identitynumber;
                    line = String.join(",",strings);
                    strb.add(line);
                } else if (num == 4) {        // 密码
                    System.out.print("\n请输入新的密码:");
                    String password = scan.next();
                    strings[3] = password;
                    line = String.join(",",strings);
                    strb.add(line);
                } else if (num == 5) {        // 姓名
                    System.out.print("\n请输入新的姓名:");
                    String name = scan.next();
                    strings[4] = name;
                    line = String.join(",",strings);
                    strb.add(line);
                } else if (num == 6) {        // 性别
                    System.out.print("\n请输入新的性别:");
                    String sex = scan.next();
                    strings[5] = sex;
                    line = String.join(",",strings);
                    strb.add(line);
                } else if (num == 7) {        // 专业
                    System.out.print("\n请输入新的专业:");
                    String career = scan.next();
                    strings[6] = career;
                    line = String.join(",",strings);
                    strb.add(line);
                } else if (num == 8) {        // 住址信息
                    System.out.print("\n请输入新的住址信息:");
                    String address = scan.next();
                    strings[7] = address;
                    line = String.join(",",strings);
                    strb.add(line);
                }
                System.out.println("修改成功!");
            }else {
                strb.add(line);
            }
            count++;
        }

        BufferedWriter bf = new BufferedWriter(new FileWriter(file));
        for(String info:strb){
            bf.write(info);
            bf.newLine();
        }
        bf.flush();
        bf.close();
        bufferedReader.close();
    }
}
NormalUser类:
package com.iosystem;

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

public class NormalUser {
    private String normalUser;
    private String normalPhoneNumber;
    private String normalIdentityNumber;
    private String normalPassword;
    private String normalName;
    private String normalSex;
    private String normalCareer;
    private String normalAddress;

    Scanner scan = new Scanner(System.in);


    public NormalUser() {

    }

    public NormalUser(String normalUser, String normalPhoneNumber, String normalIdentityNumber, String normalPassword, String normalName, String normalSex, String normalCareer, String normalAddress) {
        this.normalUser = normalUser;
        this.normalPhoneNumber = normalPhoneNumber;
        this.normalIdentityNumber = normalIdentityNumber;
        this.normalPassword = normalPassword;
        this.normalName = normalName;
        this.normalSex = normalSex;
        this.normalCareer = normalCareer;
        this.normalAddress = normalAddress;
    }

    public String getNormalUser() {
        return normalUser;
    }

    public void setNormalUser(String normalUser) {
        this.normalUser = normalUser;
    }

    public String getNormalPhoneNumber() {
        return normalPhoneNumber;
    }

    public void setNormalPhoneNumber(String normalPhoneNumber) {
        this.normalPhoneNumber = normalPhoneNumber;
    }

    public String getNormalIdentityNumber() {
        return normalIdentityNumber;
    }

    public void setNormalIdentityNumber(String normalIdentityNumber) {
        this.normalIdentityNumber = normalIdentityNumber;
    }

    public String getNormalPassword() {
        return normalPassword;
    }

    public void setNormalPassword(String normalPassword) {
        this.normalPassword = normalPassword;
    }

    public String getNormalName() {
        return normalName;
    }

    public void setNormalName(String normalName) {
        this.normalName = normalName;
    }

    public String getNormalSex() {
        return normalSex;
    }

    public void setNormalSex(String normalSex) {
        this.normalSex = normalSex;
    }

    public String getNormalCareer() {
        return normalCareer;
    }

    public void setNormalCareer(String normalCareer) {
        this.normalCareer = normalCareer;
    }

    public String getNormalAddress() {
        return normalAddress;
    }

    public void setNormalAddress(String normalAddress) {
        this.normalAddress = normalAddress;
    }





    // 验证普通用户登录
    public int loginCheck(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        //  普通用户登录验证
        System.out.print("请输入你要登录的形式(1.手机号码+密码 2.身份证号码+密码):");
        int verifyNum = scan.nextInt();  //verifyNum  验证方式数字
        //只能选择一种登录方式
        while (true) {
            if (verifyNum == 1 || verifyNum == 2) {
                break;
            } else {
                System.out.print("\n输入错误,请重新输入要选择要操作的序号(只能输入1,2):");
                verifyNum = scan.nextInt();
            }
        }
        System.out.print("请输入你要登录的普通用户序号(0,1,2,3...):");
        int normalID = scan.nextInt();  //  登录普通用户账号序号
        ArrayList<String> lines = new ArrayList();

        String line ;
        while((line=bufferedReader.readLine())!=null){
            lines.add(line);
        }
        int x = lines.size();
        //   1.手机号码+密码   2.身份证号码+密码
        while(true){
            if(normalID<=x-1){
                break;
            }else {
                System.out.print("你输入的管理员账户不存在,请重新输入你要登录的普通用户序号(0,1,2,3...):");
                normalID = scan.nextInt();  //  登录管理员账号序号
            }

        }
        String[] strings = lines.get(normalID).split(",");
        if (verifyNum == 1) {    // 普通用户
            //  普通用户登录验证     手机号码+密码
            System.out.print("\n请输入管理员账号(默认normal):");
            String normalUser1 = scan.next();
            System.out.print("\n请输入手机号码");
            String normalPhoneNumber1 = scan.next();
            System.out.print("\n请输入密码:");
            String normalPassword1 = scan.next();
            while (true) {
                if (normalUser1.equals(strings[0]) && normalPhoneNumber1.equals(strings[1]) && normalPassword1.equals(strings[3])) {
                    break;
                } else {
                    System.out.println("输入信息错误,请重新输入!");
                    System.out.print("\n请输入管理员账号序号(默认normal):");
                    normalUser1 = scan.next();
                    System.out.print("\n请输入手机号码:");
                    normalPhoneNumber1 = scan.next();
                    System.out.print("\n请输入密码:");
                    normalPassword1 = scan.next();
                }
            }
        } else if (verifyNum == 2) {   //  普通用户登录验证     身份证号码+密码
            System.out.print("\n请输入账号(默认normal):");
            String normalUser2 = scan.next();
            System.out.print("\n请输入身份证号码:");
            String normalIdentityNumber2 = scan.next();
            System.out.print("\n请输入密码:");
            String normalPassword2 = scan.next();
            while (true) {
                if (normalUser2.equals(strings[0]) && normalIdentityNumber2.equals(strings[2]) && normalPassword2.equals(strings[3])) {
                    break;
                } else {
                    System.out.println("输入信息错误,请重新输入!");
                    System.out.print("\n请输入账号:");
                    normalUser2 = scan.next();
                    System.out.print("\n请输入身份证号码:");
                    normalIdentityNumber2 = scan.next();
                    System.out.print("\n请输入密码:");
                    normalPassword2 = scan.next();
                }
            }
        }
        System.out.println("登录成功!");
        bufferedReader.close();

        return normalID;
    }

    // 注册普通用户
    public void addNormalUser(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<>();
        String line=null;
        while((line = bufferedReader.readLine())!=null){
            strb.add(line);
        }
        System.out.print("\n请输入新的用户账户名:");
        String user = scan.next();

        System.out.print("\n请输入新的手机号码:");
        String phonenumber = scan.next();

        System.out.print("\n请输入新的身份证号码:");
        String identitynumber = scan.next();

        System.out.print("\n请输入新的密码:");
        String password = scan.next();

        System.out.print("\n请输入新的姓名:");
        String name = scan.next();

        System.out.print("\n请输入新的性别:");
        String sex= scan.next();

        System.out.print("\n请输入新的专业:");
        String career= scan.next();

        System.out.print("\n请输入新的住址信息:");
        String address = scan.next();

        String addString =user+","+phonenumber+","+identitynumber+","
                    +password+","+name+","+sex+","
                    +","+career+","+address;
        strb.add(addString);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));

        for(String info:strb){
            bw.write(info);
            bw.newLine();
        }
        System.out.println("注册成功!");
        bw.flush();
        bw.close();
        bufferedReader.close();

    }

    // 查看所有普通用户信息

    public void allNormal(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        System.out.println("所有普通用户信息如下:");
        System.out.println("序号\t\t\t"+"账户名\t\t\t"+"手机号\t\t\t"+"身份证号\t\t\t"+"密码\t\t\t"+"姓名\t\t\t"+"性别\t\t\t"+"专业\t\t\t"+"住址\t\t\t");

        String line = null;
        String[] strings = null;  // 存储一条用户信息
       for(int i=0;(line=bufferedReader.readLine())!=null;i++){
            strings = line.split(",");
           if (line != "\n") {
               System.out.print(i +"\t\t\t");
           }
            for(int j=0;j<strings.length;j++){
                System.out.print(strings[j]+"\t\t\t");
            }
           System.out.print("\n");
        }
        bufferedReader.close();
    }
    // 修改用户信息
    // num 修改信息序号  id 登录的是第几个管理员
    public void normalAlter(int num,int normalid,File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        ArrayList<String> strb = new ArrayList<>();
        String line =null;
        int count=0;
        while((line=bufferedReader.readLine())!=null){
            if(count==normalid){
                String[] strings = line.split(",");
                if(num ==1){                  // 管理员账号名
                    System.out.print("\n请输入新的管理员账户名:");
                    String user = scan.next();
                    strings[0] = user;
                } else if (num ==2) {        // 手机号码
                    System.out.print("\n请输入新的手机号码:");
                    String phonenumber = scan.next();
                    strings[1]=phonenumber;
                } else if (num ==3) {        // 身份证号码
                    System.out.print("\n请输入新的身份证号码:");
                    String identitynumber = scan.next();
                    strings[2] = identitynumber;
                } else if (num ==4) {        // 密码
                    System.out.print("\n请输入新的密码:");
                    String password = scan.next();
                    strings[3] = password;
                } else if (num ==5) {        // 姓名
                    System.out.print("\n请输入新的姓名:");
                    String name = scan.next();
                    strings[4]=name;
                } else if (num ==6) {        // 性别
                    System.out.print("\n请输入新的性别:");
                    String sex= scan.next();
                    strings[5] = sex;
                } else if (num ==7) {        // 专业
                    System.out.print("\n请输入新的专业:");
                    String career= scan.next();
                    strings[6] = career;
                } else if (num ==8) {        // 住址信息
                    System.out.print("\n请输入新的住址信息:");
                    String address = scan.next();
                    strings[7] = address;
                }
                line = String.join(",", strings);
                strb.add(line);
                System.out.println("修改成功");
            }else {
                strb.add(line);
            }
            count++;
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for(String info: strb){
            bw.write(info);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        bufferedReader.close();
    }
    // 显示用户信息(只能查看自己的用户信息)
    public void myInformation(int normalId,File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line=null;
        System.out.println("个人信息如下:");
        System.out.println("账户名\t\t\t"+"手机号\t\t\t"+"身份证号\t\t\t"+"密码\t\t\t"+"姓名\t\t\t"+"性别\t\t\t"+"专业\t\t\t"+"住址\t\t\t");
        int count=0;
        while((line = bufferedReader.readLine())!=null){
            if(count==normalId){
                String[] strings = line.split(",");
                for(int j=0;j<strings.length;j++){
                    System.out.print(strings[j]+"\t\t\t");
                }
                System.out.print("\n");
            }
            count++;
        }
        System.out.println("查看结束");
        bufferedReader.close();
    }


}
Book类:
package com.iosystem;

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

public class Book {
    private String bookName;
    private String bookPrice;
    private String bookNum;


    Scanner scan = new Scanner(System.in);


    public Book() {

    }

    public Book(String bookName, String bookPrice, String bookNum) {
        this.bookName = bookName;
        this.bookPrice = bookPrice;
        this.bookNum = bookNum;
    }

    public String getBookName() {
        return bookName;
    }

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

    public String getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(String bookPrice) {
        this.bookPrice = bookPrice;
    }

    public String getBookNum() {
        return bookNum;
    }

    public void setBookNum(String bookNum) {
        this.bookNum = bookNum;
    }



    //  查看所有图书
    public void viewAllBook(File file) throws IOException {
        System.out.println("序号\t\t\t书名\t\t\t价格\t\t\t数量");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;


        for(int i=0;(line=bufferedReader.readLine())!=null;i++){
            String[] strings = line.split(",");
            System.out.print(i +"\t\t\t");
            for(int j=0;j<strings.length;j++){
                System.out.print(strings[j]+"\t\t\t");
            }
            System.out.print("\n");
        }
        bufferedReader.close();
    }
    // 添加图书
    public void addBook(File file) throws IOException {
        BufferedReader bufferedReader =  new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<>();
        System.out.print("\n请输入图书名:");
        String name = scan.next();
        System.out.print("\n请输入图书价格:");
        String price = scan.next();
        System.out.print("\n请输入图书数量:");
        String num = scan.next();

        String addBook =name+","+price+","+num;
        String line =null;
        while((line = bufferedReader.readLine())!=null){
            strb.add(line);
        }
        strb.add(addBook);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));;
        for(String info:strb){
            bw.write(info);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        bufferedReader.close();
        System.out.println("添加图书成功!");
    }
    // 修改图书
    public void alterBook(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<>();
        String line = null;
        int count=0;

        System.out.print("请输入修改图书序号:");
        int bookID=scan.nextInt();  //修改图书序号

        while((line = bufferedReader.readLine())!=null){
            if(count == bookID){
                String[] strings = line.split(",");
                System.out.print("\n请输入图书名:");
                String name = scan.next();
                strings[0] = name;

                System.out.print("\n请输入图书价格:");
                String price = scan.next();
                strings[1] = price;

                System.out.print("\n请输入图书数量:");
                String num = scan.next();
                strings[2] = num;

                line =String.join(",", strings);
                strb.add(line);
                System.out.println("修改图书成功!");
            }else {
                strb.add(line);
            }
            count++;
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));

        for(String info: strb){
            bw.write(info);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        bufferedReader.close();
    }
    // delete 删除图书
    public void deleteBook(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<>();
        System.out.println("请输入删除图书序号:");
        int bookID=scan.nextInt();  //修改图书序号
        String line = null;

        while((line = bufferedReader.readLine())!=null){
            strb.add(line);
        }
        int x = strb.size();
        while(true){
            if (bookID >= 0 &&bookID < x) {
                break;
            }else {
                System.out.println("输入错误,请重新输入删除图书序号:");
                bookID=scan.nextInt();  //修改图书序号
            }
        }
        // 集合中删除图书
        strb.remove(bookID);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for(String info :strb){
            bw.write(info);
            bw.newLine();
        }
        System.out.println("删除图书成功!");
        bw.flush();
        bw.close();
        bufferedReader.close();
    }
    // 借阅图书
    public void borrowBook(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<>();
        System.out.print("\n请输入你要借阅图书序号:");
        int numbook = scan.nextInt();
        String line =null;
        int count=0;
        while((line = bufferedReader.readLine())!=null){
            if(count ==numbook){
                String[] strings = line.split(",");
                System.out.print("\n该图书还有"+strings[2]+"本");
                System.out.print("\n请输入你要借的图书数量:");
                int nums = scan.nextInt();   //  借阅数量
                int bookNum =Integer.parseInt(strings[2]);
                if((bookNum-nums)>=0){
                    bookNum-=nums;
                    String strNum = String.valueOf(bookNum);
                    strings[2] = strNum;
                    line = String.join(",", strings);
                    strb.add(line);
                    System.out.print("\n恭喜你借阅成功,该图书还剩余"+bookNum+"本\n");
                }else{
                    System.out.print("\n很遗憾,该图书已经全被借阅!");
                }
            }else {
                strb.add(line);
            }
            count++;
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for(String info : strb){
            bw.write(info);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        bufferedReader.close();
    }
    // 归还图书
    public void giveBackBook(File file) throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        ArrayList<String> strb = new ArrayList<>();

        System.out.print("\n请输入你要归还图书序号:");
        int bk = scan.nextInt();

        String line = null;
        int count=0;
        while((line = bufferedReader.readLine())!=null){
            if(count == bk){
                String[] strings = line.split(",");
                System.out.print("\n该图书目前剩余"+strings[2]+"本");
                System.out.print("\n请输入归还书本数目:");
                int givenum = scan.nextInt();
                int sum=Integer.parseInt(strings[2])+givenum;
                strings[2] = String.valueOf(sum);
                line = String.join(",",strings);
                strb.add(line);
                System.out.print("\n恭喜你归还成功,现剩余该图书"+sum+"本");
            }else {
                strb.add(line);
            }
            count++;
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (String info: strb){
            bw.write(info);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        bufferedReader.close();
    }

}
Tool类:
package com.iosystem;

public class Tool {
    public void page1(){
        System.out.println("----------------请选择登录方式----------------");
        System.out.println("             1、登录账号");
        System.out.println("             2、注册账号");
        System.out.println("             3、退出登录");
    }
    public void page2(){
        System.out.println("********欢迎进入登录账号页面********");
        System.out.println("1.管理员登录:");
        System.out.println("2.普通用户登录:");
    }
    public void page3(){
        System.out.println("\n-----------欢迎进入管理员账号页面-----------");
        System.out.println("1、查看所有图书");
        System.out.println("2、添加图书");
        System.out.println("3、修改图书");
        System.out.println("4、删除图书");
        System.out.println("5、查看所有普通用户信息");
        System.out.println("6、查看管理员账号信息");
        System.out.println("7、修改管理员账号信息");
        System.out.println("8、退出系统");
    }
    public void page4(){
        System.out.println("\n-----------普通用户登录成功-----------");
        System.out.println("1.查看所有图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("4.显示用户信息(只能查看自己的用户信息)");
        System.out.println("5.修改用户信息(只能修改自己的用户信息)");
        System.out.println("6.退出系统");
    }
    public void page5(){
        System.out.println("********欢迎进入注册账号页面********");
        System.out.println("1.管理员注册:");
        System.out.println("2.普通用户注册:");
    }

}
 BooksManagementSystemThree类:
package com.iosystem;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class BooksManagementSystemNew {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Tool tool = new Tool();
        AdminUser adminUser= new AdminUser();
        Book book = new Book();
        NormalUser normalUser = new NormalUser();

        File adminUL = new File("E:\\Java Code base\\FoodOrderingSystem\\src\\com\\Admin.txt");
        File bookL = new File( "E:\\Java Code base\\FoodOrderingSystem\\src\\com\\Book.txt");
        File normalUL = new File("E:\\Java Code base\\FoodOrderingSystem\\src\\com\\Normal.txt");

        while(true){
            while(true) {
                tool.page1();
                System.out.print("请输入要选择要操作的序号(只能输入1,2,3):");
                int loginNum = scan.nextInt();
                //  校验输入的序号,只能是1到3
                while (true) {
                    if (loginNum == 1 || loginNum == 2 || loginNum == 3) {
                        break;
                    } else {
                        System.out.print("请输入要选择要操作的序号(只能输入1,2,3):");
                        loginNum = scan.nextInt();
                    }
                }
                switch(loginNum){
                    case 1:
                        tool.page2();
                        System.out.print("请输入要选择要操作的序号(只能输入1,2):");
                        int enterNum =scan.nextInt();  //enterNum登录数字  1.管理员  2.用户
                        while(true){
                            if(enterNum == 1||enterNum == 2 ){
                                break;
                            }else{
                                System.out.print("输入错误,请从从新输入要选择要操作的序号(只能输入1,2):");
                                enterNum =scan.nextInt();
                            }
                        }
                        if(enterNum==1){

                            int adminID = 0;
                            try {
                                adminID = adminUser.loginCheck(adminUL);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            while(true) {
                                tool.page3();
                                System.out.print("请输入你选择的功能:");
                                int adminnum = scan.nextInt();  // adminnum  管理员账号页面功能查看
                                while(true){
                                    if(adminnum>=1&&adminnum<=8){
                                        break;
                                    }else{
                                        System.out.print("输入错误,请重新输入你选择的功能:");
                                        adminnum = scan.nextInt();
                                    }
                                }

                                if(adminnum ==1){               //1、查看所有图书
                                    try {
                                        book.viewAllBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (adminnum==2) {      //2、添加图书
                                    try {
                                        book.addBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;

                                } else if (adminnum==3) {    //3、修改图书      //这里设计的是符合图书序号的所有图书信息都可以修改
                                    try {
                                        book.alterBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;

                                } else if (adminnum==4) {    //4、删除图书
                                    try {
                                        book.deleteBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (adminnum==5) {    //5、查看所有普通用户信息
                                    try {
                                        normalUser.allNormal(normalUL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;

                                } else if (adminnum==6) {    //6、查看管理员账号信息
                                    try {
                                        adminUser.adminInfo(adminUL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (adminnum==7) {    //7、修改管理员账号信息
                                    System.out.println("请输入要修改管理员账号的的信息(1管理员账号名2手机号码3" +
                                            "身份证号码4密码5姓名6性别7专业8住址信息):");
                                    int adID =scan.nextInt();   // adID  修改序号
                                    try {
                                        adminUser.adminAlter(adID, adminID,adminUL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                }else if (adminnum==8){      //8、退出系统
                                    System.out.println("谢谢使用,欢迎下次光临!");
                                    System.exit(0);;
                                }
                            }

                        } else if (enterNum == 2) {

                            int normalID = 0;
                            try {
                                normalID = normalUser.loginCheck(normalUL);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            while(true){
                                tool.page4();
                                System.out.print("请输入你需要操作功能的数字:");
                                int onetwo1 = scan.nextInt();   //  选择功能数字
                                if(onetwo1==1){                 //  1.查看所有图书
                                    try {
                                        book.viewAllBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (onetwo1==2) {        //  2.借阅图书
                                    try {
                                        book.borrowBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (onetwo1==3) {        //  3.归还图书
                                    try {
                                        book.giveBackBook(bookL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (onetwo1==4) {        //  4.显示用户信息(只能查看自己的用户信息)
                                    try {
                                        normalUser.myInformation(normalID,normalUL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (onetwo1==5) {        //  5.修改用户信息(只能修改自己的用户信息)
                                    System.out.print("\n请输入你要修改的信息的选项(1普通用户账号名2手机号码" +
                                            "3身份证号码4密码5姓名6性别7专业8住址信息):");
                                    int  num=scan.nextInt();   //  normalchange  修改序号
                                    try {
                                        normalUser.normalAlter( num, normalID,normalUL);
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;
                                } else if (onetwo1==6) {        //  6.退出系统
                                    System.out.print("\n谢谢使用,欢迎下次光临!");
                                    System.exit(0);;
                                }
                            }
                        }
                    case 2:
                        tool.page5();
                        System.out.print("请输入要选择要操作的序号(只能输入1,2):");
                        int registernum   = scan.nextInt();               //注册管理员或者普通用户
                        while(true){
                            if(registernum>0||registernum<3){
                                break;
                            }else {
                                System.out.println("输入序号错误,请重新输入要操作的序号(只能输入1,2):");
                                registernum   = scan.nextInt();
                            }
                        }
                        if(registernum==1){
                            try {
                                adminUser.addAdminUser(adminUL);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            continue;

                        } else if (registernum==2) {
                            try {
                                normalUser.addNormalUser(normalUL);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            continue;
                        }
                    case 3:
                        System.out.println("谢谢使用,欢迎下次光临!");
                        System.exit(0);;
                }

            }
        }
    }

}

初始化相关txt文件:

Admin.txt

root,1111,2222,123456,lisi,男,计算机,安徽

Normal.txt

normal,1234,5678,654321,梦洛,女,艺术,北京

Book.txt

C语言,50.0,58
Python,50.0,50

五、项目运行结果

        说明一下:这里并没有校验输入数据限制,是没写,太麻烦了,读者想要校验输入格式可以自行实现。

        说明:这里进行了完成的功能展示。

普通用户注册:

查看Normal.txt文件

​普通用户登录功能查看:

功能1:查看所有图书

功能2:借阅图书

验证图书数目是否减少响应数目:

功能3:归还图书

验证图书归还后数目是否正确:

功能4:显示用户信息

功能5:修改用户信息

查看用户信息是否修改成功:

功能6:退出系统:

管理员账户注册:

注册是否成功,查看Admin.txt

管理员用户功能查看:

1查看所有图书:

2添加图书

3修改图书

4删除图书

5查看所有普通用户信息

6查看管理员账号信息

7修改管理员账号信息

8退出登录

六、项目难点分析

1、校验功能数字是自己规定的数字

        使用while(true){}循环实现,在循环体中设置只有输入规定数字才能跳出循环,执行后面的代码。

      System.out.print("请输入要选择要操作的序号(只能输入1,2):");
      int enterNum =scan.nextInt();  //enterNum登录数字  1.管理员  2.用户
      while(true){
            if(enterNum == 1||enterNum == 2 ){
                break;
            }else{
                System.out.print("输入错误,请从从新输入要选择要操作的序号(只能输入1,2):");
                enterNum =scan.nextInt();
             }
      }
2、明确变量的定义域

要清楚自己定义的变量作用的范围:

        在Java中,变量的作用域决定了变量在程序中可见的范围。如果一个变量被定义在一个代码块内部,那么它的作用域就被限制在该代码块内部。如果一个变量被定义在方法内部,那么它的作用域就被限制在该方法内部。如果一个变量被定义在类内部但方法之外,那么它的作用域就是整个类。

        这里要注意的是,作用范围大的变量,例如adminUser、bookName...等等,要定义在整个循环体的外面,若是定义在while(){}、for(;;){}、do{}while()三种循环和if(){}else{}、switch(){}选择语句中变量,作用域只能在循环体或者选择语句中,只用在循环体或者选择语句中使用,此范围之外,不能使用,或者可能出现操作该变量失效的现象。

3、将其划分为不同的类,在不同类里面进行不同类单独的属性方法的开发与封装。
4、文件的读取和写入

        这里必须完成数据的读取才能创建写入操作,若FileRead和FileWriter:

FileRead fileRead = new FileRead(file);
FileWriter fileWriter = new FileWriter(File);

        以上述方式同时创建,这两个线程同时创建,FileWriter会打开一个空文件,会把原本文件中的数据全部清空,这样FileReader对象读取时会读取一个空文件,不会返回任何内容,造成Bug,所以必须在读取文件所有内容和对内容的操作完成之后创建FileWriter对象。

  • 61
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Scala实现Spark读取OSS数据写入另一个OSS的示例代码: ```scala import org.apache.spark.SparkConf import org.apache.spark.SparkContext import org.apache.spark.rdd.RDD import org.apache.spark.sql.SparkSession import org.apache.hadoop.fs.FileSystem import org.apache.hadoop.fs.Path import com.aliyun.oss.OSSClient import com.aliyun.oss.model.ObjectMetadata import com.aliyun.oss.model.PutObjectResult object OSSReadWriteExample { def main(args: Array[String]): Unit = { val accessKeyId = "your-access-key-id" val accessKeySecret = "your-access-key-secret" val endpoint = "your-oss-endpoint" val inputBucketName = "your-input-bucket-name" val outputBucketName = "your-output-bucket-name" val inputPath = "your-input-file-path" val outputPath = "your-output-file-path" // Create SparkSession val sparkConf = new SparkConf().setAppName("OSS Read Write Example") val spark = SparkSession.builder().config(sparkConf).getOrCreate() // Read data from OSS val inputData = spark.read.textFile(s"oss://${inputBucketName}.${endpoint}/${inputPath}") // Process data val processedData = inputData.map(line => line.toUpperCase()) // Write data to OSS val ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret) val outputObjectMetadata = new ObjectMetadata() outputObjectMetadata.setContentLength(processedData.toString().length) val outputObject = new PutObjectResult() val outputStream = processedData.map(line => line + "\n").map(_.getBytes("UTF-8")).collect().flatten ossClient.putObject(outputBucketName, outputPath, new java.io.ByteArrayInputStream(outputStream), outputObjectMetadata) // Close OSS client and SparkSession ossClient.shutdown() spark.close() } } ``` 在代码中,首先设置了OSS的访问凭证、OSS的Endpoint、输入和输出的Bucket名称、输入和输出的文件路径。然后创建了一个SparkSession,读取了输入文件中的数据,对数据进行处理,最后将处理后的结果写入到输出文件中。在写入数据到OSS时,使用了阿里云Java SDK提供的OSSClient类,创建了一个OSS客户端,通过putObject方法将数据写入到指定的OSS Bucket和文件路径中。最后关闭了OSS客户端和SparkSession。 需要注意的是,使用阿里云Java SDK时需要将对应的jar包添加到项目中,例如: ```xml <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.11.0</version> </dependency> ``` 同时,需要在pom.xml中添加OSS Maven仓库的配置: ```xml <repositories> <repository> <id>oss-aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值