首页 / JAVA / java模拟实现图书管理系统
java模拟实现图书管理系统
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java模拟实现图书管理系统,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含27874字,纯文字阅读大概需要40分钟。
内容图文
![java模拟实现图书管理系统](/upload/InfoBanner/zyjiaocheng/608/d8601ede30b44c1f8f8f75dfbeab927e.jpg)
文章目录
一、用户登录系统
package com.obj;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public interface Obj {
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
}
package com.login;
import com.obj.Obj;
public interface Login extends Obj {
//数据地址
public static final String DATA = "E:\\Project\\Book\\user.data";
//登录功能
public void login()throws Exception;
//注册功能
public void enroll() throws Exception;
//判断该用户是否已存在
public boolean exist(String name) throws Exception;
//检索用户编号
public int number()throws Exception;
//启动
public void start()throws Exception;
void method01()throws Exception;
}
实现用户的登录、数据存储及密码修改
package com.login;
import java.io.*;
public class Login1 implements Login {
private int existingUsersNum; //现登录用户编号
//启动
public void start() throws Exception {
System.out.println("请输入你需要的服务编号:" + "\n 1. 登录 2.创建用户 3.退出");
int a = im(1, 3);
service(a);
}
public void method01() throws Exception {
System.out.println("您是否需要修改密码,请选择: 1. 是 2.暂不修改");
int a = im(1, 2);
if (a == 1) {
coodChange();
}
}
//输入编号
public int im(int min, int max) throws Exception {
int aa;
while (true) {
String str = bu.readLine();
try {
int a = Integer.valueOf(str);
if (a >= min && a <= max) {
aa = a;
break;
} else {
System.out.println("请输入合理的服务标号");
}
} catch (Exception e) {
System.out.println("格式错误,请重写输入");
}
}
return aa;
}
//服务
public void service(int a) throws Exception {
switch (a) {
case 1:
login(); //登录 ,这里应该获取标号
break;
case 2:
enroll(); //创建
break;
}
}
//登录
public void login() throws Exception {
int cc = -1;
File ff = new File(DATA);
if (ff.exists()) {
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA)));
String str = "用户名";
String name = storage(str, 2, 18); //输入用户名
login01(oi, name, cc);
oi.close();
}
}
public void login01(ObjectInputStream oi, String name, int cc) throws Exception {
while (true) { //没有-1所以需要用EOFException异常来中断程序
try {
Object obj = oi.readObject();
if (obj instanceof User) {
User us = (User) obj;
if (name.equals(us.getName())) { //是否有这个用户
if (ww(us)) { //返回为false则登录成功;
start();
} else {
existingUsersNum = us.getNum();
break;
}
}
}
} catch (EOFException e) {
System.out.println("您输入的账户不存在,请确认后重新输入");
start();
break;
}
}
}
//当有该账号时,比较密码
public boolean ww(User us) throws Exception {
String str1 = "密码";
String cood = storage(str1, 10, 18);
if (cood.equals(us.getCood())) {
System.out.println("登录成功");
return false;
} else {
System.out.println("密码错误,请确认后,重新登录");
return true;
}
}
//注册,将用户对象写入文件中
public void enroll() throws Exception {
int cc = -1;
String name;
String cood;
while (true) {
String str = "";
str = "用户名";
name = storage(str, 2, 18);
if (exist(name)) { //不重名再进行下面的判断
System.out.println("用户名重复,请重新输入");
} else {
String str1 = "密码";
cood = storage(str1, 10, 18);
break;
}
}
int num = number();
User us = new User();
us.setName(name);
us.setCood(cood);
us.setNum(num + 1);
acquire(us);
System.out.println("恭喜您创建用户成功,您的编号为:" + us.getNum() + " ,您的账户为:" + us.getName() + " ,您的密码为:" + us.getCood());
this.existingUsersNum = us.getNum();
}
//输入信息并作合理性判定
public String storage(String str, int min, int max) throws Exception {
String str1;
while (true) {
System.out.println("请输入您的" + str + " ,要求:" + min + "位以上," + max + "位以下");
str1 = bu.readLine(); //整行录入
if ((str1 != null) && (str1.trim().length() >= min && str1.length() <= max)) {
break;
} else {
System.out.println("输入不合理,请重写输入");
}
}
return str1;
}
//有一个文件作为数据的存储位置,先将他全部读出,写入可变长数组中,再加上要添加的数据,然后再从数组中将数据读取出来写入文件中
public void acquire(User us) throws Exception {
//写入
File ff = new File(DATA);
ByteArrayOutputStream bao = new ByteArrayOutputStream(); //需要单独定义
ObjectOutputStream ou = new ObjectOutputStream(new BufferedOutputStream(bao)); //写入数组中
//再读取文件的时候,需要做存在性判定,写出不需要
if (ff.exists()) {
ObjectInputStream oi = new ObjectInputStream(new FileInputStream(ff)); //读取文件
while (true) {
try {
Object obj = oi.readObject();
ou.writeObject(obj);
} catch (EOFException e) {
break;
}
}
oi.close();
}
ou.writeObject(us);
ou.close();
//从数组中读取并写入文件
ObjectInputStream oi1 = new ObjectInputStream(new
BufferedInputStream(new ByteArrayInputStream(bao.toByteArray()))); //从数组中读取数据
ObjectOutputStream ou1 = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(DATA)));//写出
while (true) {
try {
Object obj = oi1.readObject();
ou1.writeObject(obj);
} catch (EOFException e) {
break;
}
}
oi1.close();
ou1.close();
}
//判定输入的用户是否存在
public boolean exist(String name) throws Exception {
boolean aa = false;
//读取文件
File ff = new File(DATA);
if (ff.exists()) {
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA)));
while (true) { //没有-1所以需要用EOFException异常来中断程序
try {
Object obj = oi.readObject();
if (obj instanceof User) {
User us = (User) obj;
if (name.equals(us.getName())) {
aa = true;
break;
}
}
} catch (EOFException e) {
break;
}
}
oi.close();
}
return aa;
}
//检索用户编号
public int number() throws Exception {
int max = -1;
File ff = new File(DATA);
if (ff.exists()) {
//读取文件,搜索文件中编号最大的用户,则新注册的用户,最大编号加1即可
ObjectInputStream oi = new ObjectInputStream(new FileInputStream(DATA));
while (true) {
try {
Object obj = oi.readObject();
if (obj instanceof User) {
User us = (User) obj;
if (max < us.getNum()) {
max = us.getNum();
}
}
} catch (EOFException e) {
break;
}
}
oi.close();
}
return max;
}
//打印所有用户信息:
private void list() throws Exception {
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA)));
while (true) {
try {
Object obj = oi.readObject();
if (obj instanceof User) {
User us = (User) obj;
System.out.println(us);
}
} catch (EOFException e) {
break;
}
}
}
//获取当前用户信息
public void whoi() throws Exception {
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA)));
while (true) {
try {
Object obj = in.readObject();
User us = (User) obj;
if (us.getNum() == existingUsersNum) {
System.out.println(us);
break;
}
} catch (EOFException e) {
break;
}
}
in.close();
}
//修改密码
public void coodChange() throws Exception {
String str = storage("之前密码", 10, 18);
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA)));
while (true) {
try {
Object obj = in.readObject();
if (obj instanceof User) {
User us = (User) obj;
if (us.getNum() == existingUsersNum) { //寻找当前用户
if (str.equals(us.getCood())) { //判断输入的密码是否正确
String str1 = storage("新密码", 10, 18); //新密码;
//要把新密码写入文件中
coodChange01(str1);
System.out.println("密码修改成功,请保护好您的密码");
break;
} else {
System.out.println("您输入的当前密码不正确");
method01();
break;
}
}
}
} catch (EOFException e) {
break;
}
}
in.close();
}
//修改密码后重新写入,先写入缓存数组中,再从数组中读取,再写入文件
public void coodChange01(String str1) throws Exception {
File ff = new File(DATA);
if (ff.exists()) {
ByteArrayOutputStream by = new ByteArrayOutputStream();
ObjectOutputStream ou = new ObjectOutputStream(by);
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA)));
while (true) { //读一次读完
try {
Object obj = oi.readObject();
if (obj instanceof User) {
User us = (User) obj;
if (us.getNum() == existingUsersNum) {
us.setCood(str1); //设置新密码
ou.writeObject(us);
} else {
ou.writeObject(obj);
}
}
} catch (EOFException e) {
break;
}
}
ou.close();
oi.close();
ObjectInputStream oi1 = new ObjectInputStream(new ByteArrayInputStream(by.toByteArray()));
ObjectOutputStream ou1 = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(DATA)));
while (true) {
try {
Object obj = oi1.readObject();
ou1.writeObject(obj);
} catch (EOFException e) {
break;
}
}
oi1.close();
ou1.close();
}
}
//测试的主方法
public static void main(String[] args) throws Exception {
Login lo = new Login1();
lo.start();
}
}
用户类
package com.login;
import java.io.Serializable;
public class User implements Serializable {
private String name; //用户名
private String cood; //用户密码
private int num; //用户编号
private boolean status=false; //身份是否为管理员
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCood() {
return cood;
}
public void setCood(String cood) {
this.cood = cood;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public User() {
}
public User(String name, String cood, int num, boolean status) {
this.name = name;
this.cood = cood;
this.num = num;
this.status = status;
}
@Override
public String toString() {
return "用户编号:" + num +
",用户名:" + name +
"用户密码:" + cood +
", 是否为管理员" + status;
}
}
二、实现对书籍的管理
书籍类
package com.book;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 图书:有名字,状态(借出去没),日期,借出次数;
*/
public class Book {
private String name;
private String state = "在"; // 状态,图书在不在
private Date date; // 借出日期
private int frequency = 0; // 借出次数 ,每借出一次++
private long money = 0;
//设定天数取整,一个月内不收费,超出一个月一天一毛
private String str;
SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public long getMoney() {
return money;
}
public String getName() {
return name;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
if ("不在".equals(state) || "在".equals(state)) {
this.state = state;
} else {
System.out.println("输入错误,请重新输入");
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getFrequency() {
return frequency;
}
public void setFrequency() {
frequency++;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
//借书
public void getBook() {
frequency++;
state = "不在";
date = new Date();
str = si.format(date);
}
public void setBook() {
state = "在";
date = null;
str = null;
}
public void Money01() {
Date date01 = new Date();
long aa = date01.getTime() - date.getTime();
String str1 = si.format(date);
String str2 = si.format(date01);
long bb = aa / (1000 * 60 * 60 * 24); //天数
if (bb > 30) {
money = (bb - 30) * 1; //分
System.out.println("您归还的书籍借阅时间为:" + str1 + "归还时间为:" + str2);
System.out.println("您所借阅的书籍,产生的费用为:" + money / 10 + "元" + money % 10 + "分,请缴费");
} else {
System.out.println("您归还的书籍借阅时间为:" + str1 + "归还时间为:" + str2);
System.out.println("感谢您遵守约定,欢迎您的下次借阅");
}
}
@Override
public String toString() {
return "书名 :" + name + "; 状态 :" + state + " ;借出日期 :" + str + ";借出次数=" + frequency;
}
}
抽象书架类
package com.bookshelf;
import com.book.Book;
import com.obj.Obj;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.Scanner;
/**
* 书架:存放图书的数组,最多存放多少本书,目前有多少本书 ;书架可以新增图书
* 可以把借出的图书按次数从高到低排序,可以查看所有图书的信息。
* 有借出时间和归还时间,能管理书架的费用,可以初始化系统里的书,能借书,还书,增加图书,
* 删除图书,费用统计,
* 排行榜,增加删除借出归还统计图书等。
*/
public abstract class BookShelf implements Obj { //书架,将书籍分类
abstract void methood();
abstract void start() throws Exception;
public void method() throws Exception {
System.out.println("请选择您需要的服务:");
System.out.println("1.查巡相关 2.借出与归还 3.添加或删除 4.返回上一级");
int a = shuRu(1, 4);
if (a != 4) {
panDuan(a);
}
}
//输入服务编号
public int shuRu(int min, int max) throws Exception {
while (true) {
try {
String str = bu.readLine();
int a = Integer.valueOf(str);
if (a >= min && a <= max) {
return a;
} else {
System.out.println("请输入合理的服务编号");
}
} catch (Exception e) {
System.out.println("输入格式错误,请重新输入");
}
}
}
//选择服务
public void panDuan(int a) throws Exception {
switch (a) {
case 1: //查看相关
method01();
method();
break;
case 2: //2.借出与归还
method03();
method();
break;
case 3: //3.添加或删除
method04();
method();
break;
case 4:
break;
}
}
//2.借出与归还
public void method03() throws Exception {
System.out.println("请选择您需要的服务: 1.借书 2.还书 3.返回上一级");
int a = shuRu(1, 3);
String str;
switch (a) {
case 1: //借书
str = "借";
jieHuanShu(str);
method03();
break;
case 2: //还书
str = "还";
jieHuanShu(str);
method03();
break;
case 3:
break;
}
}
//添加与删除
public void method04() throws Exception {
System.out.println("请选择您需要的服务: 1.添加图书 2.删除图书 3.返回上一级");
int a = shuRu(1, 3);
String str;
switch (a) {
case 1: //添加图书
str = "添加";
String name = shuBook(str);
add(name);
break;
case 2: //删除图书
str = "删除";
String name1 = shuBook(str);
reMove(name1);
break;
case 3:
break;
}
}
//服务1 查看相关
public void method01() throws Exception {
System.out.println("请选择您需要的服务:");
System.out.println("1.书架存放上限 2.书架目前有多少书 3.获取书籍列表 4.按借出按次数排序 5.返回上一级");
int aa = shuRu(1, 5);
switch (aa) {
case 1: //1.书架存放上限
length();
method01();
break;
case 2: //2.书架目前有多少书
System.out.println("该书架现存书籍为:" + how() + "本");
method01();
break;
case 3: //3.获取书籍列表
method02();
method01();
break;
case 4: //4.按借出按次数排序
getname();
method01();
break;
case 5:
break;
}
}
//输入书籍名称
public String shuBook(String str) throws Exception {
System.out.println("请输入要" + str + "的书籍名称");
String str1;
while (true) {
str1 = bu.readLine();
if (str1.equals(null)) {
System.out.println("书名不能为空");
} else {
return str1;
}
}
}
public String shuBook01(String str) throws Exception {
String str1;
while (true) {
System.out.println("请输入您想" + str + "的书籍");
str1 = bu.readLine();
if (str1.equals(null)) {
System.out.println("书名不能为空");
} else {
return str1;
}
}
}
public abstract void length(); //获取书架可以装多少本书
public abstract int how(); //获取书架有多少本书
public abstract void method02(); //获取书籍列表
public abstract void jieHuanShu(String str) throws Exception; //借还书
public abstract void getname(); //把借出的图书按次数从高到低排序,可以查看所有图书的信息
public abstract void add(String name);
//删除
public abstract void reMove(String name);
}
科普类书籍
package com.bookshelf;
import com.book.Book;
import java.util.Objects;
public class Science extends BookShelf {
private Book[] bo1 = new Book[100]; //科普类
public void methood() { //给书架里添加基本默认书籍
add("自然选择");
add("昆虫记");
add("世界100自然奇观");
add("自中国未解之谜");
add("见微知著");
add("现实不似你所见");
add("漫步到宇宙尽头");
add("万物起源");
}
public void start()throws Exception {
methood();
method();
}
public void length() { //获取书架可以装多少本书
System.out.println("该书架存放书籍上限为:" + bo1.length + "本");
}
public int how() { //获取书架有多少本书
int a = 0;
for (; a < bo1.length; a++) {
if (Objects.equals(bo1[a], null)) {
break;
}
}
return a;
}
public void method02() { //获取书籍列表
for (int a = 0; a < how(); a++) {
System.out.println(bo1[a]);
}
}
public void jieHuanShu(String str) throws Exception { //借还书
String str1 = shuBook01(str);
for (int a = 0; a < how(); a++) {
if (bo1[a].getName().equals(str1)) {
if ("借".equals(str)) {
if (bo1[a].getState().equals("在")) {
bo1[a].getBook();
System.out.println("借书成功,您借的书籍为:" + bo1[a].getName() + ",借出日期为:" + bo1[a].getStr() + ",借出次数为:" + bo1[a].getFrequency());
break;
} else if (bo1[a].getState().equals("不在"))
System.out.println("很遗憾,您想借的书籍已借出,请确认后重新输入");
break;
} else if ("还".equals(str)) {
if (bo1[a].getState().equals("在")) {
System.out.println("您想要归还的书籍并未借出,请确认后再输入");
break;
} else if (bo1[a].getState().equals("不在")) {
bo1[a].Money01();
bo1[a].setBook();
System.out.println("还书成功,您归还的书籍为:" + bo1[a].getName());
break;
}
}
} else {
System.out.println("您想" + str + "的书籍不存在,请确认后重新输入");
break;
}
}
}
public void getname() { //把借出的图书按次数从高到低排序,可以查看所有图书的信息
int bb = how();
for (int a = 1; a < bb; a++) { //只对书架上现有的书籍进行排序
for (int b = 0; b < bb - a; b++) {
if (bo1[b].getFrequency() < bo1[b + 1].getFrequency()) {
Book dd = bo1[b];
bo1[b] = bo1[b + 1];
bo1[b + 1] = dd;
}
}
}
for (int a = 0; a < how(); a++) {
System.out.println(bo1[a]);
}
}
public void add(String name) {
Book book = new Book();
book.setName(name);
//添加新书籍,输入书籍的名字,其他状态均为默认
for (int a = 0; a < how(); a++) {
if (name.equals(bo1[a].getName())) {
System.out.println("您要添加的书籍已存在,请确认后重新输入");
break;
} else if (!name.equals(bo1[a].getName())) {
int aa = how();
bo1[aa] = book;
break;
}
}
if (how() == 0) {
bo1[0] = book;
}
}
//删除
public void reMove(String name) {
for (int a = 0; a < how(); a++) {
if (name.equals(bo1[a].getName())) {
Book[] bo1 = new Book[100];
for (int aa = 0; aa < how(); aa++) {
if (aa < a) {
bo1[aa] = bo1[aa];
} else if (aa == a) {
continue;
} else if (aa > a) {
bo1[aa - 1] = bo1[aa];
}
}
bo1 = bo1;
System.out.println("删除成功");
break;
} else {
System.out.println("您要删除的书籍不存在,请确认后重新输入");
break;
}
}
}
}
教学类书籍
package com.bookshelf;
import com.book.Book;
import java.util.Objects;
public class Theaching extends BookShelf {
private Book[] bo = new Book[100]; //教学类
public void methood() { //给书架里添加基本默认书籍
add("高等数学");
add("大学英语");
add("计算机基础");
add("线性代数");
}
public void start() throws Exception{
methood();
method();
}
public void length() { //获取书架可以装多少本书
System.out.println("该书架存放书籍上限为:" + bo.length + "本");
}
public int how() { //获取书架有多少本书
int a = 0;
for (; a < bo.length; a++) {
if (Objects.equals(bo[a], null)) {
break;
}
}
return a;
}
public void method02() { //获取书籍列表
for (int a = 0; a < how(); a++) {
System.out.println(bo[a]);
}
}
public void jieHuanShu(String str) throws Exception { //借还书
String str1 = shuBook01(str);
for (int a = 0; a < how(); a++) {
if (bo[a].getName().equals(str1)) {
if ("借".equals(str)) {
if (bo[a].getState().equals("在")) {
bo[a].getBook();
System.out.println("借书成功,您借的书籍为:" + bo[a].getName() + ",借出日期为:" + bo[a].getStr() + ",借出次数为:" + bo[a].getFrequency());
break;
} else if (bo[a].getState().equals("不在"))
System.out.println("很遗憾,您想借的书籍已借出,请确认后重新输入");
break;
} else if ("还".equals(str)) {
if (bo[a].getState().equals("在")) {
System.out.println("您想要归还的书籍并未借出,请确认后再输入");
break;
} else if (bo[a].getState().equals("不在")) {
bo[a].Money01();
bo[a].setBook();
System.out.println("还书成功,您归还的书籍为:" + bo[a].getName());
break;
}
}
} else {
System.out.println("您想" + str + "的书籍不存在,请确认后重新输入");
break;
}
}
}
public void getname() { //把借出的图书按次数从高到低排序,可以查看所有图书的信息
int bb = how();
for (int a = 1; a < bb; a++) { //只对书架上现有的书籍进行排序
for (int b = 0; b < bb - a; b++) {
if (bo[b].getFrequency() < bo[b + 1].getFrequency()) {
Book dd = bo[b];
bo[b] = bo[b + 1];
bo[b + 1] = dd;
}
}
}
for (int a = 0; a < how(); a++) {
System.out.println(bo[a]);
}
}
public void add(String name) {
Book book = new Book();
book.setName(name);
//添加新书籍,输入书籍的名字,其他状态均为默认
for (int a = 0; a < how(); a++) {
if (name.equals(bo[a].getName())) {
System.out.println("您要添加的书籍已存在,请确认后重新输入");
break;
} else if (!name.equals(bo[a].getName())) {
int aa = how();
bo[aa] = book;
break;
}
}
if (how() == 0) {
bo[0] = book;
}
}
//删除
public void reMove(String name) {
for (int a = 0; a < how(); a++) {
if (name.equals(bo[a].getName())) {
Book[] bo1 = new Book[100];
for (int aa = 0; aa < how(); aa++) {
if (aa < a) {
bo1[aa] = bo[aa];
} else if (aa == a) {
continue;
} else if (aa > a) {
bo1[aa - 1] = bo[aa];
}
}
bo = bo1;
System.out.println("删除成功");
break;
} else {
System.out.println("您要删除的书籍不存在,请确认后重新输入");
break;
}
}
}
}
文学类书籍
package com.bookshelf;
import com.book.Book;
import java.util.Objects;
import java.util.Scanner;
public class Letter extends BookShelf { //文学类
private Book[] bo2 = new Book[100];
public void methood() {//给书架里添加基本默认书籍
add("钢铁是怎样练成的");
add("百年孤独");
add("论语");
add("傲慢与偏见");
add("红与黑");
add("平凡的世界");
add("简爱");
add("诗经");
add("生死场");
add("一千零一夜");
add("老人与海");
}
public void start() throws Exception {
methood();
method();
}
public void add(String name) {
Book book = new Book();
book.setName(name);
//添加新书籍,输入书籍的名字,其他状态均为默认
for (int a = 0; a < how(); a++) {
if (name.equals(bo2[a].getName())) {
System.out.println("您要添加的书籍已存在,请确认后重新输入");
break;
} else if (!name.equals(bo2[a].getName())) {
int aa = how();
bo2[aa] = book;
break;
}
}
if (how() == 0) {
bo2[0] = book;
}
}
public void length() { //获取书架可以装多少本书
System.out.println("该书架存放书籍上限为:" + bo2.length + "本");
}
public int how() { //获取书架有多少本书
int a = 0;
for (; a < bo2.length; a++) {
if (Objects.equals(bo2[a], null)) {
break;
}
}
return a;
}
public void method02() { //获取书籍列表
for (int a = 0; a < how(); a++) {
System.out.println(bo2[a]);
}
}
public void jieHuanShu(String str) throws Exception { //借还书
String str1 = shuBook01(str);
for (int a = 0; a < how(); a++) {
if (this.bo2[a].getName().equals(str1)) {
if ("借".equals(str)) {
if (bo2[a].getState().equals("在")) {
bo2[a].getBook();
System.out.println("借书成功,您借的书籍为:" + bo2[a].getName() + ",借出日期为:" + bo2[a].getStr() + ",借出次数为:" + bo2[a].getFrequency());
break;
} else if (bo2[a].getState().equals("不在"))
System.out.println("很遗憾,您想借的书籍已借出,请确认后重新输入");
break;
} else if ("还".equals(str)) {
if (bo2[a].getState().equals("在")) {
System.out.println("您想要归还的书籍并未借出,请确认后再输入");
break;
} else if (bo2[a].getState().equals("不在")) {
bo2[a].Money01();
bo2[a].setBook();
System.out.println("还书成功,您归还的书籍为:" + bo2[a].getName());
break;
}
}
} else {
System.out.println("您想" + str + "的书籍不存在,请确认后重新输入");
break;
}
}
}
public void getname() { //把借出的图书按次数从高到低排序,可以查看所有图书的信息
int bb = how();
for (int a = 1; a < bb; a++) { //只对书架上现有的书籍进行排序
for (int b = 0; b < bb - a; b++) {
if (bo2[b].getFrequency() < bo2[b + 1].getFrequency()) {
Book dd = bo2[b];
bo2[b] = bo2[b + 1];
bo2[b + 1] = dd;
}
}
}
for (int a = 0; a < how(); a++) {
System.out.println(bo2[a]);
}
}
//删除
public void reMove(String name) {
for (int a = 0; a < how(); a++) {
if (name.equals(bo2[a].getName())) {
Book[] bo1 = new Book[100];
for (int aa = 0; aa < how(); aa++) {
if (aa < a) {
bo1[aa] = bo2[aa];
} else if (aa == a) {
continue;
} else if (aa > a) {
bo1[aa - 1] = bo2[aa];
}
}
bo2 = bo1;
System.out.println("删除成功");
break;
} else {
System.out.println("您要删除的书籍不存在,请确认后重新输入");
break;
}
}
}
}
娱乐类书籍
package com.bookshelf;
import com.book.Book;
import java.util.Objects;
public class Recreation extends BookShelf {
private Book[] bo3 = new Book[100]; //y娱乐
public void methood() { //给书架里添加基本默认书籍
add("求魔");
add("将夜");
add("剑来");
add("庆余年");
}
public void start()throws Exception {
methood();
method();
}
public void length() { //获取书架可以装多少本书
System.out.println("该书架存放书籍上限为:" + bo3.length + "本");
}
public int how() { //获取书架有多少本书
int a = 0;
for (; a < bo3.length; a++) {
if (Objects.equals(bo3[a], null)) {
break;
}
}
return a;
}
public void method02() { //获取书籍列表
for (int a = 0; a < how(); a++) {
System.out.println(bo3[a]);
}
}
public void jieHuanShu(String str) throws Exception { //借还书
String str1 = shuBook01(str);
for (int a = 0; a < how(); a++) {
if (bo3[a].getName().equals(str1)) {
if ("借".equals(str)) {
if (bo3[a].getState().equals("在")) {
bo3[a].getBook();
System.out.println("借书成功,您借的书籍为:" + bo3[a].getName() + ",借出日期为:" + bo3[a].getStr() + ",借出次数为:" + bo3[a].getFrequency());
break;
} else if (bo3[a].getState().equals("不在"))
System.out.println("很遗憾,您想借的书籍已借出,请确认后重新输入");
break;
} else if ("还".equals(str)) {
if (bo3[a].getState().equals("在")) {
System.out.println("您想要归还的书籍并未借出,请确认后再输入");
break;
} else if (bo3[a].getState().equals("不在")) {
bo3[a].Money01();
bo3[a].setBook();
System.out.println("还书成功,您归还的书籍为:" + bo3[a].getName());
break;
}
}
} else {
System.out.println("您想" + str + "的书籍不存在,请确认后重新输入");
break;
}
}
}
public void getname() { //把借出的图书按次数从高到低排序,可以查看所有图书的信息
int bb = how();
for (int a = 1; a < bb; a++) { //只对书架上现有的书籍进行排序
for (int b = 0; b < bb - a; b++) {
if (bo3[b].getFrequency() < bo3[b + 1].getFrequency()) {
Book dd = bo3[b];
bo3[b] = bo3[b + 1];
bo3[b + 1] = dd;
}
}
}
for (int a = 0; a < how(); a++) {
System.out.println(bo3[a]);
}
}
public void add(String name) {
Book book = new Book();
book.setName(name);
//添加新书籍,输入书籍的名字,其他状态均为默认
for (int a = 0; a < how(); a++) {
if (name.equals(bo3[a].getName())) {
System.out.println("您要添加的书籍已存在,请确认后重新输入");
break;
} else if (!name.equals(bo3[a].getName())) {
int aa = how();
bo3[aa] = book;
break;
}
}
if (how() == 0) {
bo3[0] = book;
}
}
//删除
public void reMove(String name) {
for (int a = 0; a < how(); a++) {
if (name.equals(bo3[a].getName())) {
Book[] bo1 = new Book[100];
for (int aa = 0; aa < how(); aa++) {
if (aa < a) {
bo1[aa] = bo3[aa];
} else if (aa == a) {
continue;
} else if (aa > a) {
bo1[aa - 1] = bo3[aa];
}
}
bo3 = bo1;
System.out.println("删除成功");
break;
} else {
System.out.println("您要删除的书籍不存在,请确认后重新输入");
break;
}
}
}
}
书架启动类
package com.bookshelf;
import com.obj.Obj;
import java.io.IOException;
public class Administrators implements Obj {
BookShelf bo1 = new Theaching(); //教学类
BookShelf bo2 = new Science(); //科普
BookShelf bo3 = new Letter(); //文学类
BookShelf bo4 =new Recreation(); //娱乐
public void method01() throws Exception {
System.out.println("请选择您需要的服务 : 1.前往图书馆开启智慧人生 2.残忍拒绝,我要离开 ");
int b =method(1,2);
if(b==1){
methodoo();
}
}
public void methodoo()throws Exception{
System.out.println("请选择您想访问的书架编号: 1.教学类 2.科普类 3.文学类 4.娱乐类 5.退出");
int a = method(1,5);
method01(a);
}
public int method(int min ,int max ) throws IOException {
int a;
while (true) {
try {
String str = bu.readLine();
a = Integer.valueOf(str);
if (a >= min && a <= max) {
break;
} else {
System.out.println("请输入合理的服务编号");
}
} catch (Exception e) {
System.out.println("格式错误,请重新输入");
}
}
return a;
}
public void method01(int a) throws Exception {
switch (a) {
case 1:
bo1.start();
methodoo();
break;
case 2:
bo2.start();
methodoo();
break;
case 3:
bo3.start();
methodoo();
break;
case 4:
bo4.start();
methodoo();
break;
}
}
}
三、启动类
package com.start;
import com.bookshelf.*;
import com.login.Login;
import com.login.Login1;
public class Start {
public static void main(String[] args)throws Exception {
Administrators ad =new Administrators();
Login lo = new Login1();
lo.start();
lo.method01();
ad. method01();
}
}
内容总结
以上是互联网集市为您收集整理的java模拟实现图书管理系统全部内容,希望文章能够帮你解决java模拟实现图书管理系统所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。