图书管理系统
一、项目整体框图
二、Java类
1.Book类定义了书的属性(bookname,type,isBorrow, author, price,username)
并且提供空参构造器、有参构造器、get和set以及toString方法。
2.Account类定义了普通用户属性(name,password)提供空参、无参构造器和get、
set方法。
3.Manager类定义了管理员属性,设计采用单例模式,因为只有一位管理员提供get、set
方法
4.Account_Service类负责提供账号注册、账号登录、锁定账号的方法,对输入账号
或密码错误次数达到三次的用户进行锁定。
5.Book_Service类主要负责为普通用户提供列出书籍信息,查找书籍、借阅书籍、
归还书籍的功能。
6.Manager_Service类主要负责为管理员提供、查找图书、收录图书、删除图书、
修改图书、解锁用户、锁定用户的功能
7.Utility类主要负责为从键盘录入数据提供一些静态方法、以及读写文件的方法。
8.MianProgram是主程序、负责调用其他类已经写好的方法。
项目源码下载接:
三、Service类的代码展示
1. Account_Service
package Book_mana;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
* @ClassName Acount_Service
* @Author Xinxin
* @Date 2021/8/31 18:11
* @Version 1.0
* @Description TODO
*/
public class Acount_Service {
private static String islock = "-0";
public static final String ACCOUNTPATH = "src\\Book_mana\\account.txt";
private static List<String> user_data = Utility.read_txt(ACCOUNTPATH);
private static List<String> new_user_data = user_data;
//设置是否需要锁定
public static void setIslock(String name,String oldlock,String newlock) {
int num = 0;
Iterator<String> iterator = user_data.iterator();
while (iterator.hasNext()){
String str = iterator.next();
if(str.contains(name)){
String str1 = str.replace(oldlock,newlock);
new_user_data.remove(str);
new_user_data.add(str1);
Utility.Update_txt(ACCOUNTPATH,new_user_data);
break;
}
else if(num == Utility.read_txt(ACCOUNTPATH).size() - 1 && str.contains(name)){
System.out.println("修改失败,需要修改的用户不存在");
}
num++;
}
}
//账号登录
public static void Login_Account(){
boolean flag = false;
int count = 0;
int num = 0;
String username;
System.out.println("温馨提示,密码输入错误达到三次,账号会被自动锁定");
while (true){
System.out.println("请输入你的账号名");
username = Utility.read_str(10);
System.out.println("请输入你的密码");
String password1 = Utility.read_str(16);
String str = username + "," + password1;
if(num == 2 && !flag){
System.out.println("密码输入错误已达三次,账号已被锁定,解锁请联系管理员Jack");
Acount_Service.setIslock(username,"-0","-1");
break;
}
num++;
for(String str1 : user_data){
if(str1.contains(str)){
if(str1.endsWith(islock)){
flag = true;
System.out.println("账号" + username +"登陆成功");
while (flag){
MainProgram.list_choice();
System.out.println("\t\t\t请输入你的选择:");
int select = Utility.read_int();
flag = Book_Service.select_choices(select,username);
System.out.println();
if(flag){
continue;
}else {
return;
}
}
break;
}
else if(str1.endsWith("-1")){
System.out.println("账号已被锁定,需要解锁请联系管理员Jack");
return;
}
}
else if(count == user_data.size() - 1){
System.out.println("账号或密码输入错误,请重新输入");
flag = false;
break;
}
count++;
}
}
}
//注册账号
public static void Creat_Account(){
FileWriter fw = null;
try {
fw = new FileWriter(new File(ACCOUNTPATH),true);
String data;
System.out.println("请设置账号的id:");
String name = Utility.read_str(10);
System.out.println("请设置账号的密码:");
String password = Utility.read_str(16);
data = name + "," + password + "," + islock + "\n";
if(Utility.isExists("Book_mana\\account.txt")){
for(String str : Utility.read_txt(ACCOUNTPATH)){
if(str.contains(name)){
System.out.println("创建账号失败,账号名已存在");
break;
}
else {
fw.write(data);
System.out.println("创建账号成功!");
}
}
}
else {
fw.write(data);
System.out.println("创建账号成功!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.Book_Service
package Book_mana;
import java.util.Iterator;
import java.util.List;
/**
* @ClassName Book_Service
* @Author Xinxin
* @Date 2021/8/31 18:10
* @Version 1.0
* @Description TODO
*/
/*
首先java的foreach循环其实就是根据list对象创建一个Iterator迭代对象,用这个迭代对象来遍历list,
相当于list对象中元素的遍历托管给了Iterator,你如果要对list进行增删操作,都必须经过Iterator,
否则Iterator遍历时会乱,所以直接对list进行删除时,Iterator会抛出ConcurrentModificationException异常
*/
public class Book_Service {
private static List<String> book_data = Utility.read_txt(Book.BOOK_PATH);
private static List<String> New_book_data = book_data;
private static String bookname = null;
private static List<String> list = New_book_data;
//列出普通用户登录后的选项
public static boolean select_choices(int select,String username){
boolean flag = true;
int num = 0;
switch (select){
case 1:
Search_Books();
break;
case 2:
Borrow_Books(username);
break;
case 3:
Return_Books(username);
break;
case 4:
List_All_Books();
break;
case 5:
flag = false;
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
return flag;
}
//查找书籍
public static void Search_Books(){
int num = 0;
System.out.println("请输入想要查找的书籍");
bookname = Utility.read_str(20);
for(String str : New_book_data){
if(str.contains(bookname)){
System.out.println(bookname + "查找成功");
System.out.println(bookname + "的信息如下");
System.out.println(str);
break;
}
else if(!str.contains(bookname) && num == New_book_data.size() - 1){
System.out.println("输入有误,找不到该书籍");
}
num++;
}
}
//借阅书籍
public static void Borrow_Books(String username){
int num = 0;
System.out.println("请输入需要借阅的书籍");
bookname = Utility.read_str(15);
for(String str : New_book_data){
if(!str.contains(bookname) && num == New_book_data.size()- 1){
System.out.println("借阅失败,找不到该书籍");
num++;
break;
}
else if(str.contains(bookname) && str.contains("isBorrow=true")){
System.out.println("借阅失败,该书籍以被其他用户借走");
break;
}
else {
String oldUser = "username=null";
String newUser = "username=" + username;
String oldStatus = "isBorrow=false";
String newStatus = "isBorrow=true";
update_List(oldUser,newUser,oldStatus,newStatus);
Utility.Update_txt(Book.BOOK_PATH,New_book_data);
System.out.println(bookname + "借阅成功");
break;
}
}
}
//归还书籍
public static void Return_Books(String username){
int num = 0;
System.out.println("请输入需要归还的书籍");
bookname = Utility.read_str(15);
for(String str : New_book_data){
if(!str.contains(bookname) && num == New_book_data.size()- 1){
System.out.println("归还失败,找不到该书籍");
break;
}
else if(str.contains(bookname) && str.contains("username=null")){
System.out.println("归还失败,该书籍未被用户借走");
break;
}
else if(str.contains(bookname) && !str.contains(username)){
System.out.println("归还失败,您不是借走" + bookname + "的用户");
break;
}
else {
String newUser = "username=null";
String oldUser = "username=" + username;
String newStatus = "isBorrow=false";
String oldStatus = "isBorrow=true";
update_List(oldUser,newUser,oldStatus,newStatus);
Utility.Update_txt(Book.BOOK_PATH,New_book_data);
System.out.println(bookname + "归还成功");
break;
}
}
}
//查看书籍信息
public static void List_All_Books(){
System.out.println("所有书籍信息如下");
for (String str : New_book_data){
System.out.println(str);
}
}
//修改图书列表信息
public static void update_List(String oldUser,String newUser,String oldStatus,String newStatus){
Iterator<String> iterator = book_data.iterator();
while (iterator.hasNext()) {
String str = iterator.next();
if (str.contains(bookname)) {
String str1 = str.replace(oldUser, newUser);
String str2 = str1.replace(oldStatus, newStatus);
New_book_data.remove(str);
New_book_data.add(str2);
break;
}
}
}
}
3.Manager_Service
package Book_mana;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
/**
* @ClassName Manager_Service
* @Author Xinxin
* @Date 2021/9/1 9:55
* @Version 1.0
* @Description TODO
*/
public class Manager_Service {
private static Manager manager = Manager.getInstance();
private static boolean loop = true;
//列举选择
public static void List_Choice(){
String Choice[] = new String[]{
"\t\t\t1.查找图书",
"\t\t\t2.收录图书",
"\t\t\t3.删除图书",
"\t\t\t4.修改图书",
"\t\t\t5.解锁用户",
"\t\t\t6.锁定用户",
"\t\t\t7.退出当前界面\n"
};
for(String str : Choice){
System.out.println(str);
}
}
//管理员选项
public static void Select_Choice(){
loop = Manager_Service.Manager_Login();
String name;
while (loop){
Manager_Service.List_Choice();
System.out.println("\t\t\t请输入你的选择:");
int choice = Utility.read_int(0);
switch (choice){
case 1:
Book_Service.Search_Books();
System.out.println();
break;
case 2:
Manager_Service.Add_Books();
System.out.println();
break;
case 3:
Manager_Service.delete_books();
System.out.println();
break;
case 4:
Manager_Service.update_book();
System.out.println();
break;
case 5:
System.out.println("请输入需要解锁的用户名字");
name = Utility.read_str(20);
Acount_Service.setIslock(name,"-1","-0");
System.out.println(name + "解锁成功");
break;
case 6:
System.out.println("请输入需要锁定的用户名字");
name = Utility.read_str(20);
Acount_Service.setIslock(name,"-0","-1");
System.out.println(name + "锁定成功");
break;
case 7:
loop = false;
System.out.println("退出成功");
break;
default:
System.out.println("输入有误,请重新输入");
System.out.println();
continue;
}
}
}
//列举修改选项
public static void list_update_choice(){
String Choice[] = new String[]{
"\t\t\t1.书名",
"\t\t\t2.书本类型",
"\t\t\t3.书本作者",
"\t\t\t4.书本价格",
"\t\t\t5.书本借阅情况",
"\t\t\t6.书本借阅人",
"\t\t\t7.退出当前界面\n"
};
for(String str : Choice){
System.out.println(str);
}
}
//管理员登陆
public static boolean Manager_Login(){
boolean flag = true;
String manager_num;
int count = 0;
String password;
System.out.println("输入错误账号或密码达到三次将自动退出登录");
while (flag){
System.out.println("请输入管理员的账号:");
manager_num = Utility.read_str(20);
System.out.println("请输入管理员的密码:");
password = Utility.read_str(20);
//这里不能用==进行比较因为,==比较的是地址值而equals比较的是两者的内容
if(manager_num.equals(manager.getAcct_num()) && password.equals(manager.getPassword())) {
System.out.println("===============欢迎管理员" + manager.getName() + "登录管理界面===============");
return flag;
}else {
if(count == 3){
flag = false;
break;
}
count++;
System.out.println("账号或密码错误,请重新输入");
}
}
return flag;
}
//收录图书
public static void Add_Books(){
int count = 0;
System.out.println("请录入书名");
String bookname = Utility.read_str(20);
System.out.println("请录入书本类型");
String type = Utility.read_str(10);
System.out.println("请录入作者名字");
String author = Utility.read_str(15);
System.out.println("请录书本价格");
String isBorrow;
int price;
while (true){
price = Utility.read_int();
if(price > 0){
break;
}else {
System.out.println("录入的书本价格不得小于0");
}
}
System.out.println("请录入书本借阅状态");
while (true){
isBorrow = Utility.read_str(6).toLowerCase();
if(isBorrow.equals("ture") || isBorrow.equals("false")){
break;
}else {
System.out.println("借阅状态录入有误,请重新输入");
}
}
System.out.println("请录入借书人的名字");
String username = Utility.read_str(15);
Book book = new Book(bookname,type,isBorrow,author,price,username);
String str1 = book.toString();
String str1_bookname = str1.substring(str1.indexOf("=") + 1 ,str1.indexOf(","));
String str_bookname;
List<String> list = Utility.read_txt(Book.BOOK_PATH);
for(String str : list){
str_bookname = str.substring(str.indexOf("=") + 1 ,str.indexOf(","));
if(count == list.size() - 1 && !str_bookname.equals(str1_bookname)){
add_txt(Book.BOOK_PATH,str1);
System.out.println(bookname + "收录成功");
break;
}else if(str_bookname.equals(str1_bookname)){
System.out.println("该书本已被图书馆收录,不可再次收录");
break;
}
count++;
}
}
//删除图书
public static void delete_books(){
String str;
boolean flag = false;
int count = 0;
System.out.println("请输入需要删除的书名");
String bookname = Utility.read_str(20);
List<String> book_list = Utility.read_txt(Book.BOOK_PATH);
Iterator<String> iterator = book_list.iterator();
while (iterator.hasNext()){
str = iterator.next();
if(str.substring(str.indexOf("=") + 1,str.indexOf(",")).equals(bookname)){
book_list.remove(str);
flag = true;
System.out.println(bookname + "删除成功");
Utility.Update_txt(Book.BOOK_PATH,book_list);
break;
}else if(count == book_list.size() - 1 && !flag){
System.out.println("删除失败,需要删除的书籍不存在");
}
}
}
//修改图书
public static void update_book(){
while (true){
System.out.println("请输入需要修改的书名");
String bookname = Utility.read_str(20);
list_update_choice();
System.out.println("请选择需要修改的字段");
int choice = Utility.read_int();
switch (choice){
case 1:
update_format("名字",bookname);
return;
case 2:
update_format("类型",bookname);
return;
case 3:
update_format("作者",bookname);
return;
case 4:
update_format("价格",bookname);
return;
case 5:
update_format("借阅情况",bookname);
return;
case 6:
update_format("借阅人",bookname);
return;
case 7:
return;
default:
System.out.println("输入有误请重新输入");
}
}
}
//修改的固定格式
public static void update_format(String format,String bookname){
System.out.println("请输入修改前的书本" + format);
String oldstr = Utility.read_str(20);
System.out.println("请输入修改后的书本" + format);
String newstr = Utility.read_str(20);
update_data(bookname,oldstr,newstr);
}
//修改指定内容
public static void update_data(String bookname,String oldstr,String newstr){
String update_str;
int count = 0;
List<String> list = Utility.read_txt(Book.BOOK_PATH);
Iterator<String> iterator = list.iterator();
String str;
while (iterator.hasNext()){
str = iterator.next();
if(str.startsWith("bookname=" + bookname)){
list.remove(str);
update_str = str.replace(oldstr,newstr);
list.add(update_str);
Utility.Update_txt(Book.BOOK_PATH,list);
System.out.println(bookname + "信息修改成功");
break;
}else if(count == list.size() - 1 && !str.startsWith("str1" + bookname)){
System.out.println("修改失败,需要修改的书本不存在");
}
count++;
}
}
//在文件末尾追加内容
public static void add_txt(String filepath,String content){
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath,true)));
bw.write(content);
bw.newLine();
} catch (IOException e) {
//将编译异常转为运行异常
throw new RuntimeException(e);
} finally {
try {
if(bw != null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、部分结果展示
五、注意事项
JDK版本推荐使用 JDK8或JDK11
IDE推荐使用:idea(推荐小白使用)****、Vscode。
代码如有写得不好、不对的地方欢迎大佬在评论区指正