图书管理系统java后端代码实现

文章详细介绍了使用Java编写的图书管理系统,包括查阅、增加、删除图书功能以及登录和注册过程,涉及Java基础语法、面向对象编程和异常处理等内容。
摘要由CSDN通过智能技术生成

内容讲解:

这是一个用java写的一个图书管理系统,有查阅功能,增加图书功能,删除图书功能,登录功能,注册功能等

知识涵盖:

java基础语法,面向对象,IO流,异常处理等

效果展示:

程序核心代码:

import java.io.*;
import java.nio.channels.SelectableChannel;
import java.util.*;

public class LibrarySystem {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        programRun();
    }

    private static void programRun() throws IOException, ClassNotFoundException {
        System.out.println("-----------------------图书系统-----------------------");
        int log = 0;
        while (true){
            System.out.println("-----------------------homePage-----------------------");
            int select = homePage();
            switch (select){
                case 1:
                    Registration();
                    break;

                case 2:
                    log = LogIn();
                    break;

                case 3:
                    if(log == 1000){
                        bookColumn();
                    }else {
                        System.out.println("------------------------请先登录或注册-------------------------------");
                    }
                    break;

                case 4:
                    if(log == 1000){
                        addBooks();
                    }else {
                        System.out.println("------------------------请先登录或注册-------------------------------");
                    }
                    break;

                case 5:
                    if(log == 1000){
                        delectBook();
                    }else {
                        System.out.println("------------------------请先登录或注册-------------------------------");
                    }
                    break;

                case 6:
                    select = 1000;
                    break;
            }
            if (select == 1000){
                System.out.println("------------------------Good bye-------------------------------");
                break;
            }
        }
    }


    private static void delectBook() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\book.txt"));
        ArrayList<Book> list = (ArrayList<Book>) ois.readObject();
        ois.close();
        int a = 0;
        System.out.print("输入想删除书籍的书名>>>");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        Iterator<Book> it = list.iterator();
        while (it.hasNext()){
            if (it.next().getName().equals(name)){
                it.remove();
                a = 1000;
                System.out.println("删除成功");
            }
        }
        if (a != 1000){
            System.out.println("没有这本书");
        }else {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\book.txt"));
            oos.writeObject(list);
            oos.close();
        }

    }

    private static void bookColumn() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\book.txt"));

        ArrayList<Book> list = (ArrayList<Book>) ois.readObject();

        Book bo = new Book();
        for (int i = 0; i < list.size(); i++) {
            for (int j = i+1; j < list.size(); j++) {
                if (list.get(i).getNumber() > list.get(j).getNumber()){
                    bo = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j,bo);
                }
            }

        }

        String title = "书名" + "\t\t\t\t\t\t" + "id" + "\t\t" + "价格" + "\t\t\t" + "销量" ;
        System.out.println(title);
        int salesall = 0;
        int max = 0;
        String maxname = "";

        for (Book books : list) {
            //System.out.println(books);
            String name = books.getName();
            if (books.getName().length()<10){
                int n = 10 - books.getName().length();
                for (int i = 0; i < n; i++) {
                    name += " ";
                }
            }
            String Exhibit = name + "\t\t\t" + books.getNumber() + "\t\t" + books.getPrice() + "\t\t" + books.getSales();
            System.out.println(Exhibit);
            salesall += books.getSales();
            if (books.getSales() > max){
                max = books.getSales();
                maxname = books.getName();
            }
        }
        System.out.println("图书馆总销售额为>>>" +"\t"+salesall);
        System.out.println("图书销售冠军是>>>" + "\t"+ maxname);
        ois.close();
    }

    private static void addBooks() throws IOException, ClassNotFoundException {
        System.out.println("------------------------add book-------------------------------");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\book.txt"));
        ArrayList<Book> list = (ArrayList<Book>) ois.readObject();
        ois.close();

        Scanner sc = new Scanner(System.in);
        System.out.print("输入书名>>>");
        String name = sc.next();
        System.out.print("输入书籍ID>>>");
        int id = sc.nextInt();
        System.out.print("输入价格>>>");
        double price = sc.nextDouble();
        System.out.print("输入销量>>>");
        int sales = sc.nextInt();

        Book book = new Book(name, id, price, sales);
        list.add(book);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\book.txt"));
        oos.writeObject(list);
        oos.close();

        System.out.println("添加成功");
        System.out.print("再添加书籍输入0,退回操作平台输入1>>>");
        int selcet = sc.nextInt();
        if (selcet == 0){
            addBooks();
        }

    }

    private static int homePage() {

        System.out.print("1、注册\n2、登录\n3、阅览书栏\n4、增加图书\n5、删除图书\n6、退出\n输入您的选择>>>");
        Scanner sc = new Scanner(System.in);
        int select = 100;
        try {
            select = sc.nextInt();
        }catch (InputMismatchException e){
            System.out.println("输入非数字选项,重新输入");
            homePage();
        }
        if (select >= 1 && select<=6){
            System.out.println("--------------------------------------------------");
        }else {
            System.out.println("输入数字非选择类,请重新输入");
            homePage();
        }
        return select;
    }

    private static int LogIn() {
        String filePath = "D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\u_name.txt";

        Map<String, String>  UInfor = new HashMap<>();

        try {

            FileReader fileReader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] parts = line.split(" : ");
                if (parts.length == 2) {
                    String key = parts[0];
                    String value = parts[1];
                    UInfor.put(key, value);
                }
            }
            bufferedReader.close();

        } catch (IOException e) {
            System.out.println("读取文件时出现错误:" + e.getMessage());
        }

        int log = 0;
        int num = 1;
        while (num<=3){
            Scanner sc = new Scanner(System.in);
            System.out.print("输入您的用户名>>>");
            String Uname = sc.next();
            System.out.print("输入您的密码>>>");
            String Upassword = sc.next();
            boolean flag = UInfor.containsKey(Uname);
            if (flag && UInfor.get(Uname).equals(Upassword)){
                System.out.println("-----------------------登录成功-----------------------");
                log = 1000;
                break;
            }else {
                System.out.println("密码或用户名输入错误,请重新输入");
                if (num == 3){
                    System.out.println("输入错误三次,请找客服");
                }
                num++;
            }
        }
        return log;


    }

    private static void Registration(){
        String filePath = "D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\u_name.txt";

        Map<String, String>  UInfor = new HashMap<>();

        try {
            // 创建一个文件读取对象
            FileReader fileReader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] parts = line.split(" : ");
                if (parts.length == 2) {
                    String key = parts[0];
                    String value = parts[1];
                    UInfor.put(key, value);
                }
            }
            bufferedReader.close();

        } catch (IOException e) {
            System.out.println("读取文件时出现错误:" + e.getMessage());
        }

        Scanner sc = new Scanner(System.in);

        String uName,u_password;
        while (true){
            System.out.print("输入您的用户名>>>");
            uName = sc.next();
            System.out.print("输入您的密码>>>");
            u_password = sc.next();
            System.out.print("确认您的密码>>>");
            String u_password1 = sc.next();

            if (u_password1.equals(u_password)){
                break;
            }else {
                System.out.println("输入密码和确认密码不一致,重新注册");
            }
        }
        UInfor.put(uName,u_password);
        File file = new File("D:\\jdk\\code\\LearnJavaCoreTechnology\\src\\u_name.txt");

        try {

            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


            for (Map.Entry<String, String> entry : UInfor.entrySet()) {
                String line = entry.getKey() + " : " + entry.getValue();
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }

            bufferedWriter.close();
            System.out.println("HashMap内容已写入文件:" + file);
        } catch (IOException e) {
            System.out.println("写入文件时出现错误:" + e.getMessage());
        }

    }
}


类:

import java.io.Serializable;

public class Book implements Serializable {
    private String name;
    private int number;
    private double price;
    private int sales;

    public Book() {
    }

    public Book(String name, int number, double price, int sales) {
        this.name = name;
        this.number = number;
        this.price = price;
        this.sales = sales;
    }

    public String getName() {
        return name;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public double getPrice() {
        return price;
    }

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

    public int getSales() {
        return sales;
    }

    public void setSales(int sales) {
        this.sales = sales;
    }

    @Override
    public String toString() {
        return "BOOK{" +
                "name='" + name + '\'' +
                ", number=" + number +
                ", price=" + price +
                ", sales=" + sales +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1号全栈玩家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值