较为完善的图书管理系统
四个类
第一个类boos类
package com.day01;public class Boss {private String name="喜羊羊";private String password="123456";public Boss(String name, String password) {this.name = name;this.password = password;}public Boss(){}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
第二个类Users类
package com.day01;public class Users {private String name;private String password;private Books books;public static int setlogin=1;public Users(String name, String password) {this.name = name;this.password = password;}public Users(){}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Books getBooks() {return books;}public void setBooks(Books books) {this.books = books;}}
第三个类books类
package com.day01;public class Books {private String name;private String author;private String id;public Books(String name, String author, String id) {this.name = name;this.author = author;this.id = id;}public Books(){}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getId() {return id;}public void setId(String id) {this.id = id;}
}
最后一个测试类
package com.day01;import java.util.ArrayList;
import java.util.Scanner;public class Booksystem {static ArrayList<Books> libraries = new ArrayList<Books>();//储存图书馆图书的集合static ArrayList<Books> Usersbooks = new ArrayList<Books>();//储存被借出去的书的集合static ArrayList<Users> UsersDatas=new ArrayList<Users>();//储存用户信息的集合public static void main(String[] args) {//开始界面 判断行为方式Circulate: while (true) {System.out.println("——————————欢迎来到懒洋洋的图书管理系统——————————");System.out.println("请选择:1.登录/注册");System.out.println("请选择:2.退出");Scanner scanner = new Scanner(System.in);String choice = scanner.nextLine();switch (choice) {case "1":Login();//进入登录break;case "2":break Circulate;//立即推出default:System.out.println("输入错误,请在输入一次");}}}public static void Login()//登录/注册方式{System.out.println("请选择1.管理员登录");System.out.println("请选择2.用户登录");System.out.println("请选择3.用户注册");Scanner scanner = new Scanner(System.in);String choice = scanner.nextLine();switch (choice) {case "1"-> BossLogin();//管理员登陆case "2"->UsersLogin(UsersDatas);//用户登录case"3"->Enroll();//注册}}public static void BossLogin() {//管理员登陆Boss boss = new Boss();while (true) {System.out.println("请输入管理员的用户名");Scanner scanner = new Scanner(System.in);String Bossname = scanner.nextLine();System.out.println("请输入管理员的密码");String password = scanner.nextLine();if (Bossname.equals(boss.getName()) && password.equals(boss.getPassword())) {System.out.println("欢迎您!亲爱的管理员");BossChoice();break;} else {System.out.println("您的用户名或密码输入错误,请在输入一次");}}}public static void UsersLogin(ArrayList<Users> UsersDatas)//用户登录{Users user = new Users();if(Users.setlogin==0){System.out.println("您已经被禁止登录 即可返回");Login();}while (true) {int flag=1;System.out.println("请输入用户的用户名");Scanner scanner = new Scanner(System.in);String Usersname = scanner.nextLine();System.out.println("请输入用户的密码");String password = scanner.nextLine();for (int i = 0; i < UsersDatas.size(); i++) {if (Usersname.equals(UsersDatas.get(i).getName()) && password.equals(UsersDatas.get(i).getPassword())) {System.out.println("恭喜您!登录成功");flag=0;user=UsersDatas.get(i);UserChoice(user);break;}}if (flag==1) {System.out.println("不好意思账号或密码错误,请在输入一次");}}}public static void Enroll() {//用户注册String answer="";do {System.out.println("欢迎来到用户注册页面");System.out.println("请输入您想要的用户名");Scanner scanner = new Scanner(System.in);String Username = scanner.nextLine();System.out.println("恭喜您!输入成功");System.out.println("请输入你想要的密码");String Password = scanner.nextLine();System.out.println("恭喜您输入成功!");System.out.println("您的账号是" + Username + ", " + "您的密码是" + Password);Users user = new Users();user.setName(Username);user.setPassword(Password);UsersDatas.add(user);System.out.println("您是否还需要注册y/n");answer = scanner.nextLine();}while (answer.equals("y"));Login();}public static void UserChoice(Users user)//用户的操作选项{System.out.println("亲爱的用户,请选择你的操作");System.out.println("1.查看图书馆里所有书");System.out.println("2.我要借书了!");System.out.println("3.我要还书了");System.out.println("4.我要对书进行评价");System.out.println("5.退出");Scanner scanner = new Scanner(System.in);String choice = scanner.nextLine();switch (choice) {case "1"->UserBookSawAll(libraries,user);//用户查看图书馆里所有的书case"2"->BorrowBook(libraries, Usersbooks,user);//用户借书case"3"->Returnbook(libraries, Usersbooks,user);//用户还书case"4"->Appraisebook();//用户评价书case"5"->Login();//用户退出}}public static void UserBookSawAll(ArrayList<Books> libraries, Users user)//用户查看图书馆里的所有书{for (int i = 0; i <libraries.size() ; i++) {System.out.println(libraries.get(i).getId()+" "+libraries.get(i).getName()+" "+libraries.get(i).getAuthor());}System.out.println("查看完毕 返回主页面");UserChoice(user);}public static void BorrowBook(ArrayList<Books> libraries,ArrayList<Books> Usersbooks, Users user )//用户借书{System.out.println("请输入你要所要借书的id");Scanner scanner = new Scanner(System.in);String Bookid = scanner.nextLine();for (int i = 0; i < libraries.size() ; i++) {if (libraries.get(i).getId().equals(Bookid)) {Usersbooks.add(libraries.get(i));user.setBooks(libraries.get(i));libraries.remove(i);}}System.out.println("恭喜你,借书完毕,即将返回主页面");UserChoice(user);}public static void Returnbook(ArrayList<Books> libraries,ArrayList<Books> Usersbooks, Users user )//用户还书{System.out.println("请输入你要所要还书的id");Scanner scanner = new Scanner(System.in);String id = scanner.nextLine();for (int i = 0; i < Usersbooks.size() ; i++) {if (Usersbooks.get(i).getId().equals(id)) {libraries.add(Usersbooks.get(i));user.setBooks(null);Usersbooks.remove(i);}}System.out.println("恭喜你,还书完毕,即将返回主页面");UserChoice(user);}public static void Appraisebook()//用户评价书{Scanner scanner = new Scanner(System.in);System.out.println("请输入要评价书的id");int id = scanner.nextInt();System.out.println("请输入对书的评论");String appraise=scanner.nextLine();System.out.println("我对"+libraries.get(id)+"的评价是"+appraise);}public static void BossChoice()//管理员的操作面板{System.out.println("亲爱的管理员,请选择你的操作");System.out.println("1.录入书籍");System.out.println("2.移除书籍");System.out.println("3.查看所有书籍");System.out.println("4.查看所有用户借的书");System.out.println("5.查看所有用户");System.out.println("6.禁止用户登录");System.out.println("7.退出登录并返回主界面");Scanner scanner = new Scanner(System.in);String choice = scanner.nextLine();switch (choice) {case "1"->BookInput();//录入书籍case"2"-> BookRemove(libraries);//移除书籍case "3"->BookSeeAll(libraries);//查看所有的书籍case"4"-> BossSawUser(Usersbooks);//查看用户借的所有的书case"5"->BossSeeAllUsersdate(UsersDatas);//查看用户的信息 如果借了书 显示用户名和借了的书case"6"->Stopusers();//禁止用户登录case"7"->Login();//退出}}public static void BookInput()//管理员插入书籍{String choose="";do {Books book = new Books();System.out.println("请输入这本书的id");Scanner scanner = new Scanner(System.in);String Bookid = scanner.nextLine();book.setId(Bookid);System.out.println("请输入这本书的名字");String Bookname = scanner.nextLine();book.setName(Bookname);System.out.println("请输入这本书的作者");String Bookauthor = scanner.nextLine();book.setAuthor(Bookauthor);libraries.add(book);System.out.println("添加成功");System.out.println("您是否还想添加书?");System.out.println("请输入y/n");choose = scanner.nextLine();}while (choose.equals("y"));BossChoice();}public static void BookRemove(ArrayList<Books> libraries) {//管理员移除书籍int flage=1;System.out.println("请输入你要删除书的id");Scanner scanner = new Scanner(System.in);String Bookid = scanner.nextLine();for (int i = 0; i < libraries.size(); i++) {if (libraries.get(i).getId().equals(Bookid)) {libraries.remove(i);System.out.println("恭喜你,删除成功");flage=0;}}if(flage==1){System.out.println("您要删除的书不存在");}BossChoice();}public static void BookSeeAll(ArrayList<Books> libraries){//管理员查看所有的书for (int i = 0; i <libraries.size() ; i++) {System.out.println(libraries.get(i).getId()+" "+libraries.get(i).getName()+" "+libraries.get(i).getAuthor());}BossChoice();}public static void BossSawUser(ArrayList<Books> Usersbooks)//管理员查看所有被借了的书{for (int i = 0; i < Usersbooks.size() ; i++) {System.out.println(Usersbooks.get(i).getId()+" "+Usersbooks.get(i).getName()+" "+Usersbooks.get(i).getAuthor());}BossChoice();}public static void BossSeeAllUsersdate(ArrayList<Users> UsersDatas)//管理员查看用户的信息 如果借了书 显示被借的书{for (int i = 0; i< UsersDatas.size() ; i++) {System.out.println(UsersDatas.get(i).getName());if(UsersDatas.get(i).getBooks()!=null){System.out.println("该用户借了"+UsersDatas.get(i).getBooks().getName());}}BossChoice();}public static void Stopusers()//禁止用户登录{System.out.println("您已经开启禁止用户登陆权限");Users.setlogin=0;}
}