目录
一:创建对象
1.学生类对象Student :
2.图书类对象book :
3.管理员类对象OP:
二.创建主要交互界面MainMenu,并实现主界面功能
1.创建交互界面:
2.实现主界面功能:
三:创建OP管理界面:
1.创建主要交互界面及实现"管理学生"功能OPmenu:
(1).交互界面:
(2):实现"管理学生"功能:
2.实现"管理图书"功能OPbookManage:
(1).交互界面:
(2)功能实现:
四.创建学生功能界面:
1.创建交互界面
2.实现功能:
Ciallo~(∠・ω< )⌒★我是夏娜!
今天给大家带来一个图书馆管理系统.
通过本管理系统,可以实现管理员(OP)和学生(Student)两大类功能
管理员:学生以及图书的增删改查
学生:借书系统以及密码重置
增加账号安全系统:学生输错密码过多账号锁定,可通过管理员解封,管理员输错密码无法解封
完整的交互系统
一:创建对象
1.学生类对象Student :
public class Student {
private String name;
private String sex;
private int id;
private String bookid;
private String phonenum;
private boolean ifbook;//是否借书
private boolean safe;//true代表账号正常,fause代表账号被封
private String password;//我是啥币,我居然忘记整密码了;
public Student(){}
public Student(String name,String sex,int id,String bookid,String phonenum,boolean safe,String password,boolean ifbook){
this.name=name;
this.id=id;
this.sex=sex;
this.bookid=bookid;
this.phonenum=phonenum;
this.safe=safe;
this.password=password;
this.ifbook=ifbook;
}
public String getName(){
return name;
}
public void setName(String name){ this.name=name; }
public String getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getBookid(){
return bookid;
}
public void setBookid(String bookid){
this.bookid=bookid;
}
public String getPhonenum(){return phonenum;}
public void setPhonenum(String phonenum) { this.phonenum = phonenum; }
public boolean getSafe() { return safe; }
public void setSafe(boolean safe) { this.safe = safe; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public boolean getIfbook() { return ifbook; }
public void setIfbook(boolean ifbook) { this.ifbook = ifbook; }
2.图书类对象book :
public class book {
private String bookid;
private String bookname;
private boolean jie; //true表示借走,false表示没借走
public book() { }
public book(String bookid,String bookname,boolean jie){
this.bookid=bookid;
this.bookname=bookname;
this.jie=jie;
}
public String getBookid(){
return bookid;
}
public void setBookid(String bookid){
this.bookid=bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public boolean getJie() {
return jie;
}
public void setJie(boolean jie) {
this.jie = jie;
}
}
3.管理员类对象OP:
public class OP { //我超,( )( )!
private int opid=114514;
private int oppassword=1919810;
private boolean safe;
public boolean getSafe() {
return safe;
}
public void setSafe(boolean safe) {
this.safe = safe;
}
}
(OP账号不可更改)
二.创建主要交互界面MainMenu,并实现主界面功能
在此使用ArrayList容器保存学生对象(list2)
1.创建交互界面:
import java.util.Scanner;
import java.util.ArrayList;
public class MainMenu {
static ArrayList<Student> list2 = new ArrayList<>();
public static void main(String[] args) {
while (true) {
System.out.println("Ciallo~(∠・ω< )⌒★ 欢迎来到图书管理系统!");
System.out.println("请选择您需要的操作:");
System.out.println("1.普通账号登录");
System.out.println("2.管理员账号登录(默认管理员账号:114514 密码:1919810)");
System.out.println("3.注册普通法账号");
System.out.println("4.忘记密码");
System.out.println("5.退出");
System.out.println("请选择:");
Scanner in = new Scanner(System.in);
String a = in.next();
switch (a) {
case "1":
login(list2);
break;
case "2":
OPlogin();
break;
case "3":
zhuce(list2);
break;
case "4":
wangji(list2);
break;
case "5":
System.out.println("拜拜拜拜拜拜拜拜拜拜拜拜拜拜拜拜拜拜");
System.exit(0);
}
}
}
2.实现主界面功能:
注册账号要求:
id唯一
手机号必须11位
找回密码要求:
输入正确姓名和手机号
//功能方法****************************************************************
//登录
private static void login(ArrayList<Student> list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入账号(id):");
int a = in.nextInt();
if (ifOnly(list, a)) {
System.out.println("账号不存在!");
return;
}
for (int i = 0; i < 3; i++) {
System.out.println("请输入密码:");
String password = in.next();
int index = findindex(list, a);
if (!list.get(index).getSafe()) {
System.out.println("(笑脸手指)该账号已锁定,请联系管理员(OP)解封!");
return;
}
if (list.get(index).getPassword().equals(password)) {
STUmenu x = new STUmenu();
System.out.println("亻尔 女子");
x.STUmenu114(list,index);//进入STUmenu
return;
}
if (i == 2) {
list.get(index).setSafe(false);
System.out.println("(笑脸手指)输入错误次数过多!该账号已锁定!");
}
System.out.println("密码错误!还有" + (2 - i) + "次机会!");
}
}
//OP上号
private static void OPlogin() {
OP OP6 = new OP();
Scanner in = new Scanner(System.in);
int OP1, OP2;
while (true) {
System.out.println("请输入您的OP账号");
try {
OP1 = in.nextInt();
if (OP1 != 114514)
System.out.println("OP账号不存在!");
else
break;
} catch (Exception e) {
System.out.println("输入非法!\n\n");
return;
}
}
if (OP6.getSafe()) {
System.out.println("(流汗黄豆)您的OP账号已锁定!");
return;
}
System.out.println("请输入您的OP密码:");
for (int i = 0; i < 3; i++) {
OP2 = in.nextInt();
OPmenu OP666 = new OPmenu();
if (OP2 == 1919810) {
OP666.OPmenu();
break;
}
if (i == 2) {
OP6.setSafe(false);
System.out.println("OP账户已锁定!vivo50告诉你密码(笑)");
System.exit(0);
}
System.out.print("密码错误!请重试!");
System.out.println("(您还有" + (2 - i) + "次机会!)");
}
}
//注册
public static void zhuce(ArrayList<Student> list) {
Scanner in = new Scanner(System.in);
System.out.print("请输入新学生的id:");
int id;
while (true) {
id = in.nextInt();
if (ifOnly(list, id)) {
System.out.println("id注册成功^_^");
break;
}
System.out.println("id已经被占用,请重新输入");
}
String p1;
while (true) {
System.out.println("请输入新密码:");
p1 = in.next();
System.out.println("请再次输入密码");
String p2 = in.next();
if (p1.equals(p2))
break;
System.out.println("两次密码不相同!请重试!");
}
System.out.print("请输入姓名:");
String name = in.next();
System.out.println("请输入您的性别\n支持112种性别:");
String sex = in.next();
System.out.println("请输入11位电话号码:");
String phonenum;
while (true) {
phonenum = in.next();
if (phonenum.length() == 11) {
System.out.println("输入成功!已完成注册!");
break;
}
System.out.println("电话号码格式非法!请重新输入!");
}
Student s = new Student(name, sex, id, null, phonenum, true, p1,false);
list.add(s);
}
//忘记密码
private static void wangji(ArrayList<Student> list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入您的原id:");
int id = in.nextInt();
if (ifOnly(list, id)) {
System.out.println("未查询到id(" + id + ")!");
return;
}
int index = findindex(list, id);
System.out.println("查询到id(" + id + "),进入验证环节");
System.out.println("请输入您的手机号码:");
String num = in.next();
System.out.println("请输入您的姓名:");
String name = in.next();
Student stu = list.get(index);
if (!(stu.getPhonenum().equals(num) && stu.getName().equals(name))) {
System.out.println("手机号或姓名输入错误!(笑脸手指)");
return;
}
System.out.println("验证成功!开始修改");
String p1;
while (true) {
System.out.println("请输入新密码:");
p1 = in.next();
System.out.println("请再次输入密码");
String p2 = in.next();
if (p1.equals(p2))
break;
System.out.println("两次密码不相同!请重试!");
}
stu.setPassword(p1);
System.out.println("密码重置成功!");
}
//辅助方法**********************************************************************
//查找id的序列
public static int findindex(ArrayList<Student> list, int x) {
int i = 0;
for (; i < list.size(); i++) {
if (list.get(i).getId() == x)
break;
}
return i;
}
//id查重
public static boolean ifOnly(ArrayList<Student> list, int x) {
for (Student z : list) {
if (x == z.getId()) {
return false;
}
}
return true;
}
}
三:创建OP管理界面:
这里我分成了"管理学生"和"管理图书"的两个大功能,
为了方便管理,我将主要交互界面和"管理学生"功能放在了一个类中
1.创建主要交互界面及实现"管理学生"功能OPmenu:
(1).交互界面:
import java.util.ArrayList;
import java.util.Scanner;
public class OPmenu {
public void OPmenu(){
while (true) {
System.out.println(" 欢迎您,尊贵的管理员!");
System.out.println("请选择您的操作:");
System.out.println("1.学生管理");
System.out.println("2.图书管理");
System.out.println("3.返回上一级");
System.out.println("请选择:");
Scanner in = new Scanner(System.in);
String a = in.next();
switch (a) {
case "1":
StuManage();
break;
case "2":
OPbookManage.OPbookManage666();
break;
case "3":
return;
default:
System.out.println("输入非法!请重试!");
}
}
}
public static void StuManage(){
ArrayList<Student> list2=MainMenu.list2;
while(true){
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.返回上一级");
System.out.println("请选择:");
Scanner in = new Scanner(System.in);
String a=in.next();
switch (a){
case "1":
findStu(list2);
break;
case "2":
MainMenu.zhuce(list2);
break;
case "3":
DelStu(list2);
break;
case "4":
OutputAllStu(list2);
break;
case "5":
changeStu(list2);
break;
case "6":
jiefeng(list2);
case "7":
break;
default:
System.out.println("输入非法!请重试!");
}
}
}
(2):实现"管理学生"功能:
//************************************************************************************
public static void findStu(ArrayList<Student>list2){
System.out.println("请输入您想查询的学生id:");
Scanner in=new Scanner(System.in);
int id = in.nextInt();
// MainMsnu中的IfOnly方法来查找id是否存在
if(MainMenu.ifOnly(list2,id)){
System.out.println("该id不存在!");
return;
}
int index=MainMenu.findindex(list2,id);
System.out.println("查询到学生"+list2.get(index).getName()+".");
System.out.println("学号\t\t\t姓名\t\t\t性别\t\t\t电话号码\t\t\t密码\t\t\t");
outStu(list2,index);
}
private static void DelStu(ArrayList<Student>list2){
System.out.println("请输入您想删除的学生id:");
Scanner in=new Scanner(System.in);
int id = in.nextInt();
if(MainMenu.ifOnly(list2,id)){
System.out.println("该id不存在!");
return;
}
int index=MainMenu.findindex(list2,id);
list2.remove(index);
System.out.println("删除成功!");
}
private static void OutputAllStu(ArrayList<Student>list2){
System.out.println("学号\t\t\t姓名\t\t\t性别\t\t\t电话号码\t\t\t密码\t\t\t");
for (int i = 0; i < list2.size(); i++) {
outStu(list2,i);
}
}
private static void changeStu(ArrayList<Student>list2){
System.out.println("请输入想修改学生的id:");
Scanner in = new Scanner(System.in);
int id=in.nextInt();
if(MainMenu.ifOnly(list2,id)){
System.out.println("该学生不存在!请检查输入是否有误!");
}
int index=MainMenu.findindex(list2,id);
System.out.println("请输入想修改的东西:\n1.姓名\n2.性别\n3.电话号码\n4.密码\n5.退出");
String a=in.next();
switch (a){
case "1":
System.out.println("请输入新的姓名:");
list2.get(index).setName(in.next());
System.out.println("修改完成");
break;
case "2":
System.out.println("请输入新的性别:");
list2.get(index).setSex(in.next());
System.out.println("修改完成");
break;
case "3":
System.out.println("请输入新的信息:");
list2.get(index).setPhonenum(in.next());
System.out.println("修改完成");
break;
case "4":
System.out.println("请输入新密码:");
list2.get(index).setPassword(in.next());
System.out.println("修改完成");
break;
case "5":
return;
default:
System.out.println("输入非法!请重试!");
}
}
private static void jiefeng(ArrayList<Student> list2){
System.out.println("请输入想要解封学生id:");
Scanner in = new Scanner(System.in);
int id = in.nextInt();
if(MainMenu.ifOnly(list2,id)){
System.out.println("未查询到id");
return;
}
int index=MainMenu.findindex(list2,id);
if(list2.get(index).getSafe())
{
System.out.println("该学生无需解封!");
return;
}
System.out.println("查询到学生"+list2.get(index).getName()+"是否解封?\n1.是\n2.否");
String a = in.next();
switch (a){
case "1":
list2.get(index).setSafe(true);
System.out.println("已解封");
return;
case "2":
System.out.println("已取消!");
return;
default:
System.out.println("输入非法!请重试!");
}
}
//***************************************************************************
//输出某学生信息
private static void outStu(ArrayList<Student>list2 ,int index){
System.out.println(list2.get(index).getId()+"\t\t\t"+list2.get(index).getName()+"\t\t\t"+list2.get(index).getSex()+"\t\t\t"+list2.get(index).getPhonenum()+"\t\t\t"+list2.get(index).getPassword());
}
}
2.实现"管理图书"功能OPbookManage:
在此使用ArrayList容器保存图书对象(list1)
(1).交互界面:
import java.util.Scanner;
import java.util.ArrayList;
public class OPbookManage {
public static ArrayList<book> list1 = new ArrayList<>();
public static void OPbookManage666() {
ArrayList<book> list1 = STUmenu.list1;
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("图书管理:");
System.out.println("1.增加图书");
System.out.println("2.删除图书");
System.out.println("3.查找图书信息");
System.out.println("4.图书一览");
System.out.println("5.返回上一级");
String a = in.next();
switch (a) {
case "1":
addBook(list1);
break;
case "2":
delBook(list1);
break;
case "3":
findBook(list1);
break;
case "4":
allBook(list1);
break;
case"5":
return;
default:
System.out.println("输入非法!请重新输入!");
}
}
}
(2)功能实现:
//********************************************************
private static void addBook(ArrayList<book> list1) {
System.out.println("请输入图书id");
Scanner in = new Scanner(System.in);
String bookid = in.next();
if (bookChaChong(list1, bookid)) {
System.out.println("id已被占用!");
return;
}
System.out.println("请输入图书名:");
String bookName = in.next();
book NEWbook = new book();
NEWbook.setBookid(bookid);
NEWbook.setBookname(bookName);
NEWbook.setJie(false);
list1.add(NEWbook);
System.out.println("图书添加成功!");
}
private static void delBook(ArrayList<book> list1) {
System.out.println("请输入想要删除的图书id:");
Scanner in = new Scanner(System.in);
String bookid = in.next();
if (!bookChaChong(list1, bookid)) {
System.out.println("未查找到id:" + bookid + "的图书!");
return;
}
System.out.println("查找到图书!确认要删除吗");
while (true) {
System.out.println("1.确认");
System.out.println("2.当然确认");
System.out.println("3.终止且返回");
String a = in.next();
switch (a) {
case "1":
case "2":
int index = STUmenu.findindex(list1, bookid);//借用STUmenu的方法
list1.remove(index);
System.out.println("删除成功!");
return;
case "3":
System.out.println("已终止");
return;
default:
System.out.println("输入非法!请重新输入!");
}
}
}
private static void findBook(ArrayList<book> list1) {
System.out.println("请输入想要查找的图书id:");
Scanner in = new Scanner(System.in);
String bookid = in.next();
if (!bookChaChong(list1, bookid)) {
System.out.println("未找到图书!");
return;
}
int index = STUmenu.findindex(list1, bookid);//借用STUmenu的方法
book b = list1.get(index);
System.out.println("id:\t" + b.getBookid());
System.out.println("图书名称:\t" + b.getBookname());
if (b.getJie()) {
int Stuindex=findSTUindex(MainMenu.list2,bookid);
System.out.print("借阅状态:已被"+MainMenu.list2.get(Stuindex).getName()+"借走");
} else {
System.out.println("借阅状态:未借阅");
}
}
public static void allBook(ArrayList<book> list1){
System.out.println("图书一览表");
System.out.println("id\t\t书名\t\t借阅状态");
for (int i = 0; i < list1.size(); i++) {
System.out.print(list1.get(i).getBookid()+"\t\t"+list1.get(i).getBookname()+"\t\t");
if(list1.get(i).getJie())
System.out.println("已借");
else
System.out.println("未借");
}
}
//*****************************************************************************************
//图书id查重
private static boolean bookChaChong(ArrayList<book> list1, String bookid) {
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getBookid().equals(bookid))
return true;
}
return false;
}
//通过图书id查找学生索引
private static int findSTUindex(ArrayList<Student> list2,String bookid){
for (int i = 0; i < list2.size(); i++) {
if(list2.get(i).getBookid().equals(bookid))
return i;
}
return -1;
}
四.创建学生功能界面:
1.创建交互界面
import java.util.Scanner;
import java.util.ArrayList;
public class STUmenu {
public static ArrayList<book>list1=OPbookManage.list1;//导入OPbookManage的list1
public void STUmenu114(ArrayList<Student> list2, int index) {
while(true) {
System.out.println("^_^欢迎来到图书馆借书系统!");
System.out.println("请选择:");
System.out.println("1.借书");
System.out.println("2.还书");
System.out.println("3.图书一览");
System.out.println("4.返回上一级");
System.out.print("请键入你的选择:");
Scanner in = new Scanner(System.in);
String a = in.next();
switch (a) {
case "1":
jie(list1, list2,index);
break;
case "2":
huan(list1,list2,index);
break;
case "3":
OPbookManage.allBook(list1);
break;
case "4":
return;
default:
System.out.println("输入非法!请重新选择!");
}
}
}
2.实现功能:
private void jie(ArrayList<book> list1,ArrayList<Student> list2,int i) {
Scanner in = new Scanner(System.in);
if(list2.get(i).getIfbook())
{
System.out.println("您已借书!请先还书!");
return;
}
System.out.println("请输入图书名称:");
String name=in.next();
if(!chunzai(list1,name))
{
System.out.println("图书 "+name+" 不存在!");
return;
}
int index=findindex(list1,name);
if(list1.get(index).getJie())
{
System.out.println("书被借走辣(悲)");
return;
}
System.out.println("借书成功!");
list1.get(index).setJie(true);
list2.get(i).setBookid(list1.get(index).getBookid());//在学生类存储book的id
list2.get(i).setIfbook(true);
}
private static void huan(ArrayList<book>list1,ArrayList<Student>list2,int index){
Scanner in = new Scanner(System.in);
if(!list2.get(index).getIfbook())
{
System.out.println("您未借书,无需还书!");
return;
}
System.out.println("2222");
System.out.println("您当前借的书为:"+findname(list1,list2,index)+",您确认要还么?");
System.out.println("1.还还还!");
System.out.println("2.当然要还辣!");
System.out.println("3.不还");
System.out.print("请选择:");
while(true){
String a=in.next();
switch (a){
case "1":
case "2":
changehuan(list1,list2,index);
return;
case"3":
System.out.println("好好好~");
return;
default:
System.out.println("输入非法!请重新选择!");
break;
}
}
}
//**************************************************************************************
//查找图书是否存在
private static boolean chunzai(ArrayList<book> list1,String x){
for (int i = 0; i < list1.size(); i++) {
if(list1.get(i).getBookname().equals(x))
return true;
}
return false;
}
//用图书名字查找图书索引;
public static int findindex(ArrayList<book> list1,String name){
int i;
for (i = 0; i < list1.size(); i++) {
if(list1.get(i).getBookname().equals(name))
return i;
}
return -1;
}
//通过学生类保存的图书id来查找图书名
private static String findname(ArrayList<book>list1,ArrayList<Student>list2,int index) {
for (int i = 0; i < list2.size(); i++) {
if (list2.get(index).getBookid().equals(list1.get(i).getBookid()))
return list1.get(i).getBookname();
}
return null;
}
//改变图书状态为未借阅,学生状态为未借书
private static void changehuan(ArrayList<book>list1,ArrayList<Student>list2,int index){
list2.get(index).setIfbook(false);
list2.get(index).setBookid(null);
String bookid=list2.get(index).getBookid();
int bookIndex=findindex(list1,bookid);
list1.get(bookIndex).setJie(false);
System.out.println("更改完成!");
}
}
大功告成!完成了整个图书管理系统功能!
本人第一次发博客,如果觉得我的文章有帮助的话,可以点个赞哦^_^文章来源:https://www.toymoban.com/news/detail-523036.html
文章来源地址https://www.toymoban.com/news/detail-523036.html
到了这里,关于Java 实现图书馆管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!