Java面向对象 ----- 图书管理系统(利用Java实现简单的图书管理系统)

简介: Java面向对象 ----- 图书管理系统(利用Java实现简单的图书管理系统)

Java图书管理系统
类图如下:
在这里插入图片描述
具体用户操作再Test测试类中实现

以下为具体代码:
Person类:

package com.test2;

public abstract class Person {

    private String name;
    private int age;
    private String sex;
    
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if (age < 0) {
            System.out.println("年龄设置错误,默认为20岁!");
            this.age = 20;
        } else {
            this.age = age;
        }
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        if (sex.equals("1")) {
            this.sex = "男";
        } else {
            this.sex = "女";
        }
    }
}

User类

 package com.test2;

import java.util.Scanner;

/**
 * 用户类
 *
 */
public class User extends Person{

    OrderForm order;
    Scanner sca = new Scanner(System.in);
    
    
    
    /**
     * 修改信息
     */
    public void modifiy() {
        if (this.getName() != null) {
            System.out.println("请输入您的昵称:");
            String oldName = sca.next();
            System.out.println("请输入您的年龄:");
            int age = sca.nextInt();
            System.out.println("请输入您的性别:");
            String sex = sca.next();
            if (oldName.equals(this.getName()) && age == this.getAge()
                    && sex.equals(this.getSex())) {
                System.out.println("匹配成功,请输入您修改后的昵称:");
                this.setName(sca.next());
                System.out.println("请输入年龄:");
                this.setAge(sca.nextInt());
                System.out.println("请选择性别:1.男\t2.女");
                this.setSex(sca.next());
                System.out.println("修改成功~");
            } else {
                System.out.println("您输入的与旧用户信息不匹配,修改失败!");
            }
        } else {
            System.out.println("您没有任何信息!");
        }
    }
    
    
    
    
    /**
     * 结算订单
     * 将借书日期和归还日期计算出相差多少天,就是借了多少天,天数*一天的价格=总价
     */
    public void balance() {
        if (order.books[0] != null) {
            int count = 0;
            System.out.println("订单序号\t书籍名称\t借出价格/天\t书籍原价\t借出天数\t总价");
            for (int i = 0; i < order.books.length; i++) {
                if (order.books[i] != null) {
                    if (order.states[i] == 1) {
                        count++;
                        System.out.print((i + 1) + "\t");
                        order.books[i].check();
                        System.out.println("\t" + order.day[i] + "\t" + order.sumMoney[i]);
                    }
                }
            }
            if (count != 0) {
                System.out.println("请输入要结算的订单序号:");
                int index = sca.nextInt();
                System.out.println("请支付:" + order.sumMoney[index - 1] + "元");
                double pay = 0.00;
                do {
                    pay = sca.nextDouble();
                    if (pay > order.sumMoney[index - 1]) {
                        System.out.println("支付成功,找您:" + (pay - order.sumMoney[index - 1]) + "元");
                    } else if (pay < order.sumMoney[index - 1]) {
                        System.out.println("您支付的金额小于总金额!请您重新支付:");
                    } else {
                        System.out.println("支付成功~");
                    }
                } while((pay < order.sumMoney[index - 1]));
                order.isBuy[index - 1] = 1;
            } else {
                System.out.println("您无可结算的订单!");
            }
        } else {
            System.out.println("您无订单信息!");
        }
    }
    
    
    /**
     * 计算借书日期至归还日期的天数
     * @param outYear    借书年份
     * @param outMonth    借书月份
     * @param outDate    借书日期
     * @param backYear    归还年份
     * @param backMonth    归还月份
     * @param backDate    归还日期
     * @return    天数
     */
    public int calcDate(int outYear, int outMonth, int outDate,
            int backYear, int backMonth, int backDate) {
        //计算开始年份至结束年份的天数
        int outYearDays = 0;
        for (int i = outYear; i < backYear; i++) {
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
                outYearDays += 366;
            } else {
                outYearDays += 365;
            }
        }
        
        //计算开始年份的月份天数
        int outMonthDays = 0;
        for (int i = 1; i < outMonth; i++) {
            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
                outMonthDays += 31;
            } else if (i == 4 || i == 6 || i == 9 || i == 11) {
                outMonthDays += 30;
            } else {
                if ((outYear % 4 == 0 && outYear % 100 != 0) || outYear % 400 == 0) {
                    outMonthDays += 29;
                } else {
                    outMonthDays += 28;
                }
            }
        }
        
        //计算结束年份的月份的天数
        int backMonthDays = 0;
        for (int i = 1; i < backMonth; i++) {
            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
                backMonthDays += 31;
            } else if (i == 4 || i == 6 || i == 9 || i == 11) {
                backMonthDays += 30;
            } else {
                if ((backYear % 4 == 0 && backYear % 100 != 0) || backYear % 400 == 0) {
                    backMonthDays += 29;
                } else {
                    backMonthDays += 28;
                }
            }
        }
        
        //计算天数差
        int result = outYearDays - (outMonthDays + outDate) + 
                (backMonthDays + backDate);
        return result;
    }
    
    /**
     * 删除订单
     */
    public void delete() {
        get();
        if (order.books[0] != null) {
            System.out.println("请输入要删除的订单序号:");
            int index = sca.nextInt();
            if (index < 0 || index > order.books.length) {
                System.out.println("订单不存在!");
            } else {
                if (order.states[index - 1] == 1 && order.isBuy[index - 1] == 1) {
                    order.books[index - 1] = null;
                    for (int j = index - 1; j < order.books.length - 1; j++) {
                        order.books[j] = order.books[j + 1];
                        order.outYear[j] = order.outYear[j + 1];
                        order.outMonth[j] = order.outMonth[j + 1];
                        order.outDate[j] = order.outDate[j + 1];
                        order.backYear[j] = order.backYear[j + 1];
                        order.backMonth[j] = order.backMonth[j + 1];
                        order.backDate[j] = order.backDate[j + 1];
                        order.isBuy[j] = order.isBuy[j + 1];
                        order.states[j] = order.states[j + 1];
                    }
                    order.books[order.books.length - 1] = null;
                    System.out.println("删除成功~");
                } else {
                    System.out.println("订单未签收或未结算!");
                }
            }
        }
    }
    
    
    /**
     * 还书
     */
    public void returnBook() {
        get();
        if (order.books[0] != null) {
            System.out.println("请输入要签收的订单序号:");
            int index = sca.nextInt();
            if (index < 0 || index > order.books.length) {
                System.out.println("订单不存在!");
            } else {
                if (order.states[index - 1] == 0) {
                    order.states[index - 1] = 1;
                    System.out.println("请输入今天的日期:");
                    for (int i = 0; i < order.backYear.length; i++) {
                        if (order.backYear[i] == 0) {
                            System.out.println("请输入年份:");
                            order.backYear[i] = sca.nextInt();
                            System.out.println("请输入月份:");
                            order.backMonth[i] = sca.nextInt();
                            System.out.println("请输入日期:");
                            order.backDate[i] = sca.nextInt();
                            order.day[i] = calcDate(order.outYear[index - 1], order.outMonth[index - 1], order.outDate[index - 1],
                                    order.backYear[index - 1], order.backMonth[index - 1], order.backDate[index - 1]);
                            order.sumMoney[i] = order.day[i] * Book.booksDayPrice[i];
                            System.out.println("日期相差" + order.day[i] + "天,一天借出价格:" + Book.booksDayPrice[i] + ",总价:" + order.sumMoney[i]);
                            break;
                        }
                    }
                    System.out.println("签收成功~");
                } else {
                    System.out.println("订单已签收,无需重复签收!");
                }
            }
        }
    }
    
    
    /**
     * 查看订单
     */
    public void get() {
        int count = 0;
        if (order.books[0] != null) {
            System.out.println("姓名:" + this.getName()
                        + "\n年龄:" + this.getAge()
                        + "\n性别:" + this.getSex());
            System.out.println("书籍序号\t书籍名称\t借出价格\t书籍原价\t借出书本的日期\t当前状态\t是否结算\t归还书本的日期");
        }
        for (int i = 0; i < order.books.length; i++) {
            if (order.books[i] != null) {
                System.out.print("  " + (i + 1) + "\t");
                order.books[i].check();
                String state = order.states[i] == 0 ? "未归还" : "已归还";
                String isBuy = order.isBuy[i] == 0 ? "未结算" : "已结算";
                System.out.print("\t" + order.outYear[i]
                        + "/" + order.outMonth[i]
                        + "/" + order.outDate[i] + "\t " + state
                        + "\t " + isBuy);
                if (state.equals("已归还")) {
                    System.out.println("\t" + order.backYear[i]
                            + "/" + order.backMonth[i]
                            + "/" + order.backDate[i]);
                } else {
                    System.out.println();
                }
                count++;
            }
        }
        if (count == 0) {
            System.out.println("您没有任何订单记录!");
        }
    }
    
    
    /**
     * 借书
     */
    public void giveBook(OrderForm order) {
        if (this.getName() == null) {
            System.out.println("请输入您的姓名:");
            this.setName(sca.next());
        }
        if (this.getAge() == 0) {
            System.out.println("请输入您的年龄:");
            this.setAge(sca.nextInt());
        }
        if (this.getSex() == null) {
            System.out.println("请输入您的性别:\n"
                    + "1.男\t2.女");
            this.setSex(sca.next());
        }
        if (order.books[order.books.length - 1] != null) {
            System.out.println("存储空间已满,无法存储!");
        } else {
            Book book = new Book();
            book.show();
            System.out.println("请选择要借的书籍序号:");
            book.add(sca.nextInt());
            System.out.println("请输入今天的日期:");
            for (int i = 0; i < order.outYear.length; i++) {
                if (order.outYear[i] == 0) {
                    System.out.println("请输入年份:");
                    order.outYear[i] = sca.nextInt();
                    System.out.println("请输入月份:");
                    order.outMonth[i] = sca.nextInt();
                    System.out.println("请输入日期:");
                    order.outDate[i] = sca.nextInt();
                    break;
                }
            }
            for (int i = 0; i < order.books.length; i++) {
                if (order.books[i] == null) {
                    order.books[i] = book;
                    System.out.println("借书成功~");
                    break;
                }
            }
        }
    }
    
}

Book类

package com.test2;

/**
 * 书本类
 *
 */
public class Book {

    String[] booksName = {"物理", "化学", "数学", null, null};
    static double[] booksDayPrice = {1.3, 1.2, 1.5, 0.0, 0.0};
    double[] booksPrice = {30.0, 40.0, 35.0, 0.0, 0.0};
    
    private String[] choiceName = new String[booksName.length];
    private double[] choiceDayPrice = new double[booksDayPrice.length];
    private double[] choicePrice = new double[booksPrice.length];
    
    
    /**
     * 输出书籍信息
     */
    public void show() {
        System.out.println("书籍序号\t书籍名称\t借出价格/天\t书籍原价");
        for (int i = 0; i < booksName.length; i++) {
            if (booksName[i] != null) {
                System.out.println((i + 1) + "\t" + booksName[i]
                        + "\t" + booksDayPrice[i] + "\t" + booksPrice[i]);
            }
        }
    }
    
    
    public void add(int index) {
        for (int i = 0; i < choiceName.length; i++) {
            if (choiceName[i] == null) {
                choiceName[i] = booksName[index - 1];
                choiceDayPrice[i] = booksDayPrice[index - 1];
                choicePrice[i] = booksPrice[index - 1];
                break;
            }
        }
    }
    
    public void check() {
        for (int i = 0; i < choiceName.length; i++) {
            if (choiceName[i] != null) {
                System.out.print("《" + choiceName[i] + "》\t¥"
                        + choiceDayPrice[i] + "\t¥" + choicePrice[i]);
            }
        }
    }
}

OrderForm类

package com.test2;

/**
 * 订单类
 *
 */
public class OrderForm {

    
    //借书日期
    int[] outYear = new int[3];
    int[] outMonth = new int[3];
    int[] outDate = new int[3];

    //归还日期 
    int[] backYear = new int[3];
    int[] backMonth = new int[3];
    int[] backDate = new int[3];
    
    Book[] books = new Book[3];//书籍最大存储
    
    int[] states = new int[3];//当前书籍状态
    
    int[] isBuy = new int[3];//当前书籍是否结算
    
    int[] day = new int[3];//相差的天数
    
    double[] sumMoney = new double[3];//总价
    
}

Test测试类

package com.test2;

import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * 测试类
 *
 */
public class Test {

    public static void main(String[] args) {
        //初始化工具
        Scanner sca = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("####0.00");
        System.out.println("欢迎来到图书科技管理系统");
        User user = new User();
        user.order = new OrderForm();
        boolean isExit = false;
        do {
            System.out.println("*****************\n"
                    + "1.我要借书\n"
                    + "2.查看订单\n"
                    + "3.归还书籍\n"
                    + "4.删除订单\n"
                    + "5.结算订单\n"
                    + "6.修改信息\n"
                    + "7.退出系统\n"
                    + "*****************\n"
                    + "请根据编号进行输入:");
            switch (sca.next()) {
                case "1":
                    System.out.println("***我要借书***");
                    user.giveBook(user.order);
                    break;
                case "2":
                    System.out.println("***查看订单***");
                    user.get();
                    break;
                case "3":
                    System.out.println("***归还书籍***");
                    user.returnBook();
                    break;
                case "4":
                    System.out.println("***删除订单***");
                    user.delete();
                    break;
                case "5":
                    System.out.println("***结算订单***");
                    user.balance();
                    break;
                case "6":
                    System.out.println("***修改信息***");
                    user.modifiy();
                    break;
                case "7":
                    System.out.println("确定退出吗?1:退出  0:继续");
                    if (sca.next().equals("1")) {
                        isExit = true;
                    }
                    break;
                default :
                    System.out.println("您的输入有误,请您重新输入:");
            }
        } while(!isExit);
        if (isExit) {
            System.out.println("感谢使用,再见!");
        }
        sca.close();
    }
}

直接复制就能运行~~~

如果我未发现的bug,望各位大佬在评论区留言,😜

今天又是学习代码的一天呀,加油,共勉🤞💕

相关文章
|
2月前
|
Java
java中面向过程和面向对象区别?
java中面向过程和面向对象区别?
36 1
|
3月前
|
JavaScript 前端开发 Java
还不明白面向对象? 本文带你彻底搞懂面向对象的三大特征(2024年11月Java版)
欢迎来到我的博客,我是瑞雨溪,一名热爱JavaScript和Vue的大一学生。如果你从我的文章中受益,欢迎关注我,我将持续更新更多优质内容。你的支持是我前进的动力!🎉🎉🎉
36 0
还不明白面向对象? 本文带你彻底搞懂面向对象的三大特征(2024年11月Java版)
|
3月前
|
Java 关系型数据库 数据库
面向对象设计原则在Java中的实现与案例分析
【10月更文挑战第25天】本文通过Java语言的具体实现和案例分析,详细介绍了面向对象设计的五大核心原则:单一职责原则、开闭原则、里氏替换原则、接口隔离原则和依赖倒置原则。这些原则帮助开发者构建更加灵活、可维护和可扩展的系统,不仅适用于Java,也适用于其他面向对象编程语言。
61 2
|
5月前
|
存储 Java
Java——图书管理系统
该文档详细介绍了一个图书管理系统的设计与实现。系统包含普通用户和管理员两种角色,通过书架操作图书,如添加、查找、借阅、归还及删除图书等功能。文档展示了各个功能的具体代码实现,并使用继承和接口等方式优化了系统结构。通过多态技术实现了不同用户角色调用相应功能。整体设计清晰,逻辑严谨,便于理解和实现。
231 17
Java——图书管理系统
|
5月前
|
Java 编译器
封装,继承,多态【Java面向对象知识回顾①】
本文回顾了Java面向对象编程的三大特性:封装、继承和多态。封装通过将数据和方法结合在类中并隐藏实现细节来保护对象状态,继承允许新类扩展现有类的功能,而多态则允许对象在不同情况下表现出不同的行为,这些特性共同提高了代码的复用性、扩展性和灵活性。
封装,继承,多态【Java面向对象知识回顾①】
|
5月前
|
Java
java中面向过程和面向对象区别?
java中面向过程和面向对象区别?
51 4
|
5月前
|
Java
接口和抽象类【Java面向对象知识回顾②】
本文讨论了Java中抽象类和接口的概念与区别。抽象类是不能被实例化的类,可以包含抽象和非抽象方法,常用作其他类的基类。接口是一种纯抽象类型,只包含抽象方法和常量,不能被实例化,且实现接口的类必须实现接口中定义的所有方法。文章还比较了抽象类和接口在实现方式、方法类型、成员变量、构造方法和访问修饰符等方面的不同,并探讨了它们的使用场景。
接口和抽象类【Java面向对象知识回顾②】
|
4月前
|
存储 Java 程序员
Java基础-面向对象
Java基础-面向对象
39 0
|
6月前
|
Java 数据处理 开发者
【Java基础面试十二】、说一说你对面向对象的理解
这篇文章阐述了面向对象是一种以类和对象为基础,通过封装、继承和多态等概念来模拟现实世界中的事物及其相互关系的程序设计方法,它强调以事物为中心进行思考和系统构造,与结构化程序设计相比,更符合人类的自然思维方式。
【Java基础面试十二】、说一说你对面向对象的理解
|
6月前
|
Java
【Java基础面试十三】、面向对象的三大特征是什么?
这篇文章介绍了面向对象程序设计的三大基本特征:封装、继承和多态,其中封装隐藏对象实现细节,继承实现软件复用,多态允许子类对象表现出不同的行为特征。
【Java基础面试十三】、面向对象的三大特征是什么?