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,望各位大佬在评论区留言,????
今天又是学习代码的一天呀,加油,共勉????????