欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Java创建一个简单的图书管理系统

发布时间:2024/3/13 65 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Java创建一个简单的图书管理系统 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

由于暂时没有学过Java,写的乱七八糟
甚至连内部类和外部类都是写到一半才知道
不少地方还不清楚是为什么

package 图书管理系统; import java.util.Scanner; //引入Scanner public class Library {final int book_kinds_number = 5;final int students_number = 5;final int password_length = 4;final int max_borrow_number = 2;public static void main(String[] args) {Book_Manage_System SWJTU_Library = new Library().new Book_Manage_System(); //外部类要优先于内部类出现Scanner input = new Scanner(System.in);int sl_1,sl_2, log = -1;boolean judge = true;boolean judge_1 = true;boolean judge_2 = true;do {judge = true;judge_1 = true;judge_2 = true;do {System.out.println("欢迎使用图书借阅系统"); //登录部分System.out.println("登录...1\n离开...0");sl_1 = input.nextInt();switch(sl_1) {case 1: log = SWJTU_Library.Login();if(log == -1) {System.out.println("密码输入错误\n");judge_1 = true;}else if(log == -2) {System.out.println("用户名输入错误\n");}else {judge_1 = false;}break;case 0:System.exit(0);default:System.out.print("输入有误,请重新输入");break;}}while(judge_1);do {System.out.println("功能选择项"); //功能部分System.out.println("查询馆内图书...1\n借阅图书...2\n归还图书...3\n退出登录...0");sl_2 = input.nextInt();switch(sl_2) {case 0:judge_2 = false;judge = true; break;case 1:SWJTU_Library.Show_All_Books();break;case 2:if(log != -1) {SWJTU_Library.Borrow(SWJTU_Library.students[log]);}break;case 3:SWJTU_Library.Show_Student_Books(SWJTU_Library.students[log]);SWJTU_Library.Return(SWJTU_Library.students[log]);break;default:judge_2 = false;break;}}while(judge_2);}while(judge);}public class Book {String name;int num;}public class Student{String name;int borrow_number;String password; //密码为字符串型Book book[] = new Book[max_borrow_number];public void show_books(){int i;for(i=0;i<max_borrow_number;i++) {if(book[i].num>0) {System.out.print(book[i].name);}if(i!=max_borrow_number - 1) {System.out.print("、");}}}Student(){ //学生的初始化函数int i;for(i=0;i<max_borrow_number;i++) {book[i] = new Book();book[i].num = 0;book[i].name = "";}}}public class Book_Manage_System{Book books[] = new Book[5];Student students[] = new Student[5];public int Login() //登录函数实现部分{int i, flag = -2;Scanner input = new Scanner(System.in);System.out.print("请输入用户名: ");String nm = input.nextLine();System.out.print("请输入密码");String key = input.nextLine();for(i=0;i<students_number;i++) {if(students[i].name.equals(nm)) {flag = -1;if(students[i].password.equals(key)) {System.out.println("登录成功\n欢迎"+students[i].name+"同学");flag = i;break;}}}return flag;}public void Show_All_Books(){int i;for(i=0;i<book_kinds_number;i++) {System.out.print(books[i].name+" "+books[i].num+"本\n");}}public void Borrow(Student stu) //借阅功能实现部分{int i,j;for(i=0;i<book_kinds_number;i++) {System.out.println(books[i].name+" 剩余"+books[i].num);}System.out.println("请问要借哪本书?");Scanner input = new Scanner(System.in);String bk = input.next();for(i=0;i<book_kinds_number;i++) {if(stu.borrow_number == max_borrow_number) {System.out.println("您的借书数量已达上限");break;}if(books[i].name.equals(bk)) { //equals:字符串比较函数,若两字符串相同则返回trueif(books[i].num==0) {System.out.println("此书已被借完");}else {System.out.println(stu.name+"借出一本《"+books[i].name+"》");for(j=0;j<max_borrow_number;j++) {if(stu.book[j].num == 0) {stu.book[j].name = bk;stu.book[j].num += 1;break;}}books[i].num-=1;stu.borrow_number += 1;}break;}if(i==book_kinds_number-1) {System.out.println("图书馆里没有《"+bk+"》这本书");}}}public void Show_Student_Books(Student stu){int i;for(i=0;i<max_borrow_number;i++) {if(stu.book[i].num>0) {System.out.println(stu.book[i].name+" "+stu.book[i].num);}}}public void Return(Student stu){int i,j,flag = 0;System.out.println("请问要还哪一本书?");Scanner input = new Scanner(System.in);String rt = input.nextLine();for(i=0;i<max_borrow_number;i++) {if(stu.book[i].name.equals(rt)) { //equals:字符串比较函数,若两字符串相同则返回truestu.book[i].name="";stu.book[i].num -= 1;for(j=0;j<book_kinds_number;j++) {if(books[j].name.equals(rt)) {System.out.println("归还成功");books[j].num+=1;break;}}flag = 1;}}if(flag == 0) {System.out.println("您没有这本书");}}Book_Manage_System() //初始内容:学生的姓名、密码储存的地方{books[0] = new Book(); //这里如果不进行动态分配就会报错books[1] = new Book();books[2] = new Book();books[3] = new Book();books[4] = new Book();students[0] = new Student();students[1] = new Student();students[2] = new Student();students[3] = new Student();students[4] = new Student();books[0].name = "高等数学";books[0].num = 2;books[1].name = "Java编程入门";books[1].num = 2;books[2].name = "漫游者日记";books[2].num = 2;books[3].name = "百武装战记";books[3].num = 2;books[4].name = "刀剑神域";books[4].num = 2;students[0].name = "无上辐光";students[0].password = "2337";students[1].name = "失败冠军";students[1].password = "2330";students[2].name = "空洞骑士";students[2].password = "1111";students[3].name = "艾米璐";students[3].password = "2222";students[4].name = "桐人";students[4].password = "3333";}} }

总结

以上是生活随笔为你收集整理的Java创建一个简单的图书管理系统的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。