目录
一、项目要求:
二、项目环境
三、项目使用的知识点
四、项目代码
五、项目运行结果
六、项目难点分析
图书管理系统--versions1.0:
图书管理系统--versions1.0-CSDN博客文章浏览阅读981次,点赞29次,收藏17次。本文使用了变量,数据类型,顺序,选择,循环,数组实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135583634?spm=1001.2014.3001.5501
图书管理系统--versions2.0:
图书管理系统--versions2.0-CSDN博客文章浏览阅读1k次,点赞35次,收藏19次。本文使用了变量,数据类型,顺序,选择,循环,数组,对象及属性的封装实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135728923?spm=1001.2014.3001.5501
图书管理系统--versions3.0:
图书管理系统(ArrayList和LinkedList)--versions3.0-CSDN博客本文使用了变量,数据类型,顺序,选择,循环,对象及属性的封装,使用ArrayList和LinkedList集合,实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135939196?spm=1001.2014.3001.5501
一、项目要求:
1)通过账号控制图书管理系统,账号分为管理员账号和普通用户账号
1. 管理员账号和普通用户账号都可以使用手机号码+密码、身份证号码+密码的形式登录,登录方式二选一2.管理员账号和普通账号除了手机号码、身份中号码、密码三个数据之外,还有姓名、性别、专业、住址信息
2)启动系统后,显示登录账号、注册账号、退出登录三个模块
a)登录账号:
1、显示管理员登录
2、用户登录两个模块b)注册账号:
1、显示注册管理员
2、注册用户两个模块
c)退出登录:结束程序运行
3)登录账号成功后,根据账号的性质来显示不同的模块:
登录管理员账号成功后,显示如下模块:1、查看所有图书,图书显示哪些信息,这里设计的信息有书名,书的价格,书的数量
2、添加图书
3、修改图书
4、删除图书
5、查看所有普通用户信息
6、查看管理员账号信息
7、修改管理员账号信息
8、退出系统
登录普通账号成功后,显示如下模块:
1、查看所有图书,图书显示哪些信息,你自行设计
2、借阅图书
3、归还图书
4、显示用户信息(只能查看自己的用户信息)
5、修改用户信息(只能修改自己的用户信息)
6、退出系统
4)使用对象对不同类及其各自属性进行封装与功能实现
二、项目环境
(1)开发工具:IDEA、JDK17
(2)开发语言:Java
三、项目使用的知识点
(1)理解程序基本概念——程序、变量、数据类型
(2)顺序、选择、循环、跳转语句(3)使用对象的思想对图书管理系统--versions1.0进行改造
这里使用了对象封装的思想,将其分为管理员用户类,普通用户,书类,工具类,系统主页面类,将各自的类的属性功能封装,在系统页面类里面调用这些属性方法实现各个功能的开发。
(4) 加入集合的操作,取代数组对数据的存储。
(5) 数据的转型(多态)
四、项目代码
这里说明一下,为了快速写出项目,前面的用户管理的八个属性均采用String类型存储,方便自己后续代码的编写及相关功能的实现,有不足或者想要自己实现的功能部分读者可以自行修改代码实现。
AdminUser类:
package com.threecode;
public class AdminUser {
private String adminUser;
private String adminPhoneNumber;
private String adminIdentityNumber;
private String adminPassword;
private String adminName;
private String adminSex;
private String adminCareer;
private String adminAddress;
public AdminUser() {
}
public AdminUser(String adminUser, String adminPhoneNumber, String adminIdentityNumber, String adminPassword, String adminName, String adminSex, String adminCareer, String adminAddress) {
this.adminUser = adminUser;
this.adminPhoneNumber = adminPhoneNumber;
this.adminIdentityNumber = adminIdentityNumber;
this.adminPassword = adminPassword;
this.adminName = adminName;
this.adminSex = adminSex;
this.adminCareer = adminCareer;
this.adminAddress = adminAddress;
}
public String getAdminUser() {
return adminUser;
}
public void setAdminUser(String adminUser) {
this.adminUser = adminUser;
}
public String getAdminPhoneNumber() {
return adminPhoneNumber;
}
public void setAdminPhoneNumber(String adminPhoneNumber) {
this.adminPhoneNumber = adminPhoneNumber;
}
public String getAdminIdentityNumber() {
return adminIdentityNumber;
}
public void setAdminIdentityNumber(String adminIdentityNumber) {
this.adminIdentityNumber = adminIdentityNumber;
}
public String getAdminPassword() {
return adminPassword;
}
public void setAdminPassword(String adminPassword) {
this.adminPassword = adminPassword;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminSex() {
return adminSex;
}
public void setAdminSex(String adminSex) {
this.adminSex = adminSex;
}
public String getAdminCareer() {
return adminCareer;
}
public void setAdminCareer(String adminCareer) {
this.adminCareer = adminCareer;
}
public String getAdminAddress() {
return adminAddress;
}
public void setAdminAddress(String adminAddress) {
this.adminAddress = adminAddress;
}
@Override
public String toString() {
return
adminUser + "\t\t\t" +
adminPhoneNumber + "\t\t\t" +
adminIdentityNumber + "\t\t\t" +
adminPassword + "\t\t\t" +
adminName + "\t\t\t" +
adminSex + "\t\t\t" +
adminCareer + "\t\t\t" +
adminAddress + "\t\t\t" ;
}
}
AdminUserList类:
package com.threecode;
import java.util.ArrayList;
public class AdminUserList {
private ArrayList<AdminUser> adminUsers = new ArrayList<AdminUser>();
public AdminUserList() {
adminUsers.add( new AdminUser("root","1111","2222","123456","张三","男","计算机","安徽"));
}
public ArrayList<AdminUser> getAdminUsers() {
return adminUsers;
}
public void setAdminUsers(ArrayList<AdminUser> adminUsers) {
this.adminUsers = adminUsers;
}
}
AdminUserAction类:
package com.threecode;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/1/28 14:04
* @Version 1.0
*/
public class AdminUserAction {
Scanner scan = new Scanner(System.in);
//管理员用户登录验证
public int loginCheck(AdminUserList adminUser){
ArrayList<AdminUser> adminUsers = adminUser.getAdminUsers();
System.out.print("请输入你要登录的形式(1.手机号码+密码 2.身份证号码+密码):");
int verifyNum = scan.nextInt(); //verifyNum 验证方式数字
//只能选择一种登录方式
while (true) {
if (verifyNum == 1 || verifyNum == 2) {
break;
} else {
System.out.print("\n输入错误,请输入要选择要操作的序号(只能输入1,2):");
verifyNum = scan.nextInt();
}
}
// 1.手机号码+密码 2.身份证号码+密码
System.out.print("请输入你要登录的管理员序号(0,1,2):");
int adminId = scan.nextInt(); // 登录管理员账号序号
while(true){
if(adminId<=adminUsers.size()-1){
break;
}else {
System.out.print("你输入的管理员账户不存在,请重新输入你要登录的管理员序号(0,1,2):");
adminId = scan.nextInt(); // 登录管理员账号序号
}
}
if (verifyNum == 1) { // 管理员用户
// 管理员登录验证 手机号码+密码
System.out.println("\n请输入你的验证信息:");
while(true){
if(adminUsers.get(adminId)==null){
System.out.print("\n你输入的管理员用户不存在,请重新输入,");
adminId = scan.nextInt();
}else{
break;
}
}
System.out.print("\n请输入管理员账号(默认root):");
String adminUser1 = scan.next();
System.out.print("\n请输入手机号码");
String adminPhoneNumber1 = scan.next();
System.out.print("\n请输入密码:");
String adminPassword1 = scan.next();
AdminUser a1= adminUsers.get(adminId);
while (true) {
if (adminUser1.equals(a1.getAdminUser()) && adminPhoneNumber1.equals(a1.getAdminPhoneNumber()) && adminPassword1.equals(a1.getAdminPassword())) {
break;
} else {
System.out.println("输入信息错误,请重新输入信息!");
System.out.print("请输入管理员账号序号(默认root):");
adminUser1 = scan.next();
System.out.print("\n请输入手机号码:");
adminPhoneNumber1 = scan.next();
System.out.print("\n请输入密码:");
adminPassword1 = scan.next();
}
}
} else if (verifyNum == 2) { // 管理员登录验证 身份证号码+密码
System.out.print("请输入账号(默认root):");
String adminUser2 = scan.next();
System.out.print("\n请输入身份证号码:");
String adminIdentityNumber2 = scan.next();
System.out.print("\n请输入密码:");
String adminPassword2 = scan.next();
AdminUser a1= adminUsers.get(adminId);
while (true) {
if (adminUser2.equals(a1.getAdminUser()) && adminIdentityNumber2.equals(a1.getAdminIdentityNumber()) && adminPassword2.equals(a1.getAdminPassword())) {
break;
} else {
System.out.println("输入信息错误,请重新输入信息!");
System.out.print("请输入账号(默认root):");
adminUser2 = scan.next();
System.out.print("\n请输入身份证号码:");
adminIdentityNumber2 = scan.next();
System.out.print("\n请输入密码:");
adminPassword2 = scan.next();
}
}
}
return adminId;
}
// 注册管理员用户
public void addAdminUser(AdminUserList adminUser){
ArrayList<AdminUser> adminUsers = adminUser.getAdminUsers();
System.out.print("\n请输入新的管理员账户名:");
String user = scan.next();
System.out.print("\n请输入新的手机号码:");
String phonenumber = scan.next();
System.out.print("\n请输入新的身份证号码:");
String identitynumber = scan.next();
System.out.print("\n请输入新的密码:");
String password = scan.next();
System.out.print("\n请输入新的姓名:");
String name = scan.next();
System.out.print("\n请输入新的性别:");
String sex= scan.next();
System.out.print("\n请输入新的专业:");
String career= scan.next();
System.out.print("\n请输入新的住址信息:");
String address = scan.next();
AdminUser au =new AdminUser(user,phonenumber,identitynumber,password ,name,sex,career,address);
boolean add = adminUsers.add(au);
if (add) {
System.out.println("注册管理员用户成功!");
}else{
System.out.println("注册失败!");
}
}
//查看管理员账号信息
public void adminInfo(AdminUserList adminUser){
System.out.println("序号\t\t\t"+"账户名\t\t\t"+"手机号\t\t\t"+"身份证号\t\t\t"+"密码\t\t\t"+"姓名\t\t\t"+"性别\t\t\t"+"专业\t\t\t"+"住址\t\t\t");
ArrayList<AdminUser> adminUsers = adminUser.getAdminUsers();
for(int i=0;i<adminUsers.size();i++){
AdminUser a1= adminUsers.get(i);
System.out.println(i+"\t\t\t"+a1.toString());
}
}
//修改管理员账号信息
// num 修改信息序号 id 登录的是第几个管理员
public void adminAlter(int num,int id,AdminUserList adminUser) {
ArrayList<AdminUser> adminUsers = adminUser.getAdminUsers();
AdminUser a1 = adminUsers.get(id);
if (num == 1) { // 管理员账号名
System.out.print("\n请输入新的管理员账户名:");
String user = scan.next();
a1.setAdminUser(user);
} else if (num == 2) { // 手机号码
System.out.print("\n请输入新的手机号码:");
String phonenumber = scan.next();
a1.setAdminPhoneNumber(phonenumber);
} else if (num == 3) { // 身份证号码
System.out.print("\n请输入新的身份证号码:");
String identitynumber = scan.next();
a1.setAdminIdentityNumber(identitynumber);
} else if (num == 4) { // 密码
System.out.print("\n请输入新的密码:");
String password = scan.next();
a1.setAdminPassword(password);
} else if (num == 5) { // 姓名
System.out.print("\n请输入新的姓名:");
String name = scan.next();
a1.setAdminName(name);
} else if (num == 6) { // 性别
System.out.print("\n请输入新的性别:");
String sex = scan.next();
a1.setAdminSex(sex);
} else if (num == 7) { // 专业
System.out.print("\n请输入新的专业:");
String career = scan.next();
a1.setAdminCareer(career);
} else if (num == 8) { // 住址信息
System.out.print("\n请输入新的住址信息:");
String address = scan.next();
a1.setAdminAddress(address);
}
}
}
NormalUser类:
package com.threecode;
import java.util.Scanner;
public class NormalUser {
private String normalUser;
private String normalPhoneNumber;
private String normalIdentityNumber;
private String normalPassword;
private String normalName;
private String normalSex;
private String normalCareer;
private String normalAddress;
Scanner scan = new Scanner(System.in);
public NormalUser() {
}
public NormalUser(String normalUser, String normalPhoneNumber, String normalIdentityNumber, String normalPassword, String normalName, String normalSex, String normalCareer, String normalAddress) {
this.normalUser = normalUser;
this.normalPhoneNumber = normalPhoneNumber;
this.normalIdentityNumber = normalIdentityNumber;
this.normalPassword = normalPassword;
this.normalName = normalName;
this.normalSex = normalSex;
this.normalCareer = normalCareer;
this.normalAddress = normalAddress;
}
public String getNormalUser() {
return normalUser;
}
public void setNormalUser(String normalUser) {
this.normalUser = normalUser;
}
public String getNormalPhoneNumber() {
return normalPhoneNumber;
}
public void setNormalPhoneNumber(String normalPhoneNumber) {
this.normalPhoneNumber = normalPhoneNumber;
}
public String getNormalIdentityNumber() {
return normalIdentityNumber;
}
public void setNormalIdentityNumber(String normalIdentityNumber) {
this.normalIdentityNumber = normalIdentityNumber;
}
public String getNormalPassword() {
return normalPassword;
}
public void setNormalPassword(String normalPassword) {
this.normalPassword = normalPassword;
}
public String getNormalName() {
return normalName;
}
public void setNormalName(String normalName) {
this.normalName = normalName;
}
public String getNormalSex() {
return normalSex;
}
public void setNormalSex(String normalSex) {
this.normalSex = normalSex;
}
public String getNormalCareer() {
return normalCareer;
}
public void setNormalCareer(String normalCareer) {
this.normalCareer = normalCareer;
}
public String getNormalAddress() {
return normalAddress;
}
public void setNormalAddress(String normalAddress) {
this.normalAddress = normalAddress;
}
@Override
public String toString() {
return normalUser + "\t\t\t" +
normalPhoneNumber + "\t\t\t" +
normalIdentityNumber + "\t\t\t" +
normalPassword + "\t\t\t" +
normalName + "\t\t\t" +
normalSex + "\t\t\t" +
normalCareer + "\t\t\t" +
normalAddress + "\t\t\t" ;
}
}
NormalUserList类:
package com.threecode;
import java.util.ArrayList;
public class NormalUserList {
private ArrayList<NormalUser> normalUsers = new ArrayList<NormalUser>();
public NormalUserList() {
normalUsers.add(new NormalUser("normal", "9999", "123321", "123456", "李四", "男", "艺术", "北京"));
}
public ArrayList<NormalUser> getNormalUsers() {
return normalUsers;
}
public void setNormalUsers(ArrayList<NormalUser> normalUsers) {
this.normalUsers = normalUsers;
}
}
NormalUserAction类:
package com.threecode;
import java.util.ArrayList;
import java.util.Scanner;
public class NormalUserAction {
Scanner scan = new Scanner(System.in);
// 验证普通用户登录
public int loginCheck(NormalUserList normalUser) {
ArrayList<NormalUser> normalUsers = normalUser.getNormalUsers();
// 普通用户登录验证
System.out.print("请输入你要登录的形式(1.手机号码+密码 2.身份证号码+密码):");
int verifyNum = scan.nextInt(); //verifyNum 验证方式数字
//只能选择一种登录方式
while (true) {
if (verifyNum == 1 || verifyNum == 2) {
break;
} else {
System.out.print("\n输入错误,请重新输入要选择要操作的序号(只能输入1,2):");
verifyNum = scan.nextInt();
}
}
System.out.print("请输入你要登录的普通用户序号(0,1,2,3...):");
int normalID = scan.nextInt(); // 登录普通用户账号序号
// 1.手机号码+密码 2.身份证号码+密码
while(true){
if(normalID<=normalUsers.size()-1){
break;
}else {
System.out.print("你输入的管理员账户不存在,请重新输入你要登录的普通用户序号(0,1,2,3...):");
normalID = scan.nextInt(); // 登录管理员账号序号
}
}
if (verifyNum == 1) { // 普通用户
// 普通用户登录验证 手机号码+密码
System.out.print("\n请输入管理员账号(默认normal):");
String normalUser1 = scan.next();
System.out.print("\n请输入手机号码");
String normalPhoneNumber1 = scan.next();
System.out.print("\n请输入密码:");
String normalPassword1 = scan.next();
NormalUser n1=normalUsers.get(normalID);
while (true) {
if (normalUser1.equals(n1.getNormalUser()) && normalPhoneNumber1.equals(n1.getNormalPhoneNumber()) && normalPassword1.equals(n1.getNormalPassword())) {
break;
} else {
System.out.println("输入信息错误,请重新输入!");
System.out.print("\n请输入管理员账号序号(默认normal):");
normalUser1 = scan.next();
System.out.print("\n请输入手机号码:");
normalPhoneNumber1 = scan.next();
System.out.print("\n请输入密码:");
normalPassword1 = scan.next();
}
}
} else if (verifyNum == 2) { // 普通用户登录验证 身份证号码+密码
System.out.print("\n请输入账号(默认normal):");
String normalUser2 = scan.next();
System.out.print("\n请输入身份证号码:");
String normalIdentityNumber2 = scan.next();
System.out.print("\n请输入密码:");
String normalPassword2 = scan.next();
NormalUser n1=normalUsers.get(normalID);
while (true) {
if (normalUser2.equals(n1.getNormalUser()) && normalIdentityNumber2.equals(n1.getNormalIdentityNumber()) && normalPassword2.equals(n1.getNormalPassword())) {
break;
} else {
System.out.println("输入信息错误,请重新输入!");
System.out.print("\n请输入账号:");
normalUser2 = scan.next();
System.out.print("\n请输入身份证号码:");
normalIdentityNumber2 = scan.next();
System.out.print("\n请输入密码:");
normalPassword2 = scan.next();
}
}
}
return normalID;
}
// 注册普通用户
public void addNormalUser(NormalUserList normalUser){
ArrayList<NormalUser> normalUsers = normalUser.getNormalUsers();
System.out.print("\n请输入新的用户账户名:");
String user = scan.next();
System.out.print("\n请输入新的手机号码:");
String phonenumber = scan.next();
System.out.print("\n请输入新的身份证号码:");
String identitynumber = scan.next();
System.out.print("\n请输入新的密码:");
String password = scan.next();
System.out.print("\n请输入新的姓名:");
String name = scan.next();
System.out.print("\n请输入新的性别:");
String sex= scan.next();
System.out.print("\n请输入新的专业:");
String career= scan.next();
System.out.print("\n请输入新的住址信息:");
String address = scan.next();
NormalUser n = new NormalUser(user,phonenumber,
identitynumber,password,name,
sex,career,address);
boolean add = normalUsers.add(n);
if (add) {
System.out.println("注册普通用户成功!");
}else{
System.out.println("注册失败!");
}
}
// 查看所有普通用户信息
public void allNormal(NormalUserList normalUser){
ArrayList<NormalUser> normalUsers = normalUser.getNormalUsers();
System.out.println("所有普通用户信息如下:");
System.out.println("序号\t\t\t"+"账户名\t\t\t"+"手机号\t\t\t"+"身份证号\t\t\t"+"密码\t\t\t"+"姓名\t\t\t"+"性别\t\t\t"+"专业\t\t\t"+"住址\t\t\t");
for(int i=0;i<normalUsers.size();i++){
NormalUser n1=normalUsers.get(i);
System.out.println(i+"\t\t\t"+n1.toString());
}
}
// 修改用户信息
// num 修改信息序号 id 登录的是第几个管理员
public void normalAlter(int num,int normalid,NormalUserList normalUser){
ArrayList<NormalUser> normalUsers = normalUser.getNormalUsers();
NormalUser n1=normalUsers.get(normalid);
if(num ==1){ // 管理员账号名
System.out.print("\n请输入新的管理员账户名:");
String user = scan.next();
n1.setNormalUser(user);
} else if (num ==2) { // 手机号码
System.out.print("\n请输入新的手机号码:");
String phonenumber = scan.next();
n1.setNormalPhoneNumber(phonenumber);
} else if (num ==3) { // 身份证号码
System.out.print("\n请输入新的身份证号码:");
String identitynumber = scan.next();
n1.setNormalIdentityNumber(identitynumber);
} else if (num ==4) { // 密码
System.out.print("\n请输入新的密码:");
String password = scan.next();
n1.setNormalPassword(password); ;
} else if (num ==5) { // 姓名
System.out.print("\n请输入新的姓名:");
String name = scan.next();
n1.setNormalName(name);
} else if (num ==6) { // 性别
System.out.print("\n请输入新的性别:");
String sex= scan.next();
n1.setNormalSex(sex);
} else if (num ==7) { // 专业
System.out.print("\n请输入新的专业:");
String career= scan.next();
n1.setNormalCareer(career);
} else if (num ==8) { // 住址信息
System.out.print("\n请输入新的住址信息:");
String address = scan.next();
n1.setNormalAddress(address);
}
}
// 显示用户信息(只能查看自己的用户信息)
public void myInformation(int normalid,NormalUserList normalUser){
ArrayList<NormalUser> normalUsers = normalUser.getNormalUsers();
NormalUser n1=normalUsers.get(normalid);
System.out.println("个人信息如下:");
System.out.println("账户名\t\t\t"+"手机号\t\t\t"+"身份证号\t\t\t"+"密码\t\t\t"+"姓名\t\t\t"+"性别\t\t\t"+"专业\t\t\t"+"住址\t\t\t");
System.out.println(n1.toString());
}
}
Book类:
package com.threecode;
import java.util.Scanner;
public class Book {
private String bookName;
private double bookPrice;
private int bookNum;
Scanner scan = new Scanner(System.in);
public Book() {
}
public Book(String bookName, double bookPrice, int bookNum) {
this.bookName = bookName;
this.bookPrice = bookPrice;
this.bookNum = bookNum;
}
public String getBookName() {
return bookName;
}
public double getBookPrice() {
return bookPrice;
}
public int getBookNum() {
return bookNum;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
@Override
public String toString() {
return bookName + "\t\t\t" + bookPrice+"\t\t\t" + bookNum ;
}
}
BookList类:
package com.threecode;
import java.util.LinkedList;
public class BookList {
private LinkedList<Book> books = new LinkedList<Book>();
public BookList() {
books.add(new Book("C语言", 50.0, 40));
books.add(new Book("Python", 40.0, 20));
books.add(new Book("Java", 20.0, 35));
books.add(new Book("Go", 45.0, 44));
books.add(new Book("C++", 38.0, 47));
}
public LinkedList<Book> getBooks() {
return books;
}
public void setBooks(LinkedList<Book> books) {
this.books = books;
}
}
BookAction类:
package com.threecode;
import java.util.LinkedList;
import java.util.Scanner;
public class BookAction {
Scanner scan = new Scanner(System.in);
// 查看所有图书
public void viewAllBook(BookList bookList){
System.out.println("序号\t\t\t书名\t\t\t价格\t\t\t数量");
LinkedList<Book> books = bookList.getBooks();
int i=0;
for(Book book : books){
System.out.println(i+"\t\t\t"+book);
i++;
}
}
// 添加图书
public void addBook(BookList bookList){
System.out.print("\n请输入图书名:");
String name = scan.next();
System.out.print("\n请输入图书价格:");
double price = scan.nextDouble();
System.out.print("\n请输入图书数量:");
int num = scan.nextInt();
Book book1 = new Book(name,price,num);
LinkedList<Book> book= bookList.getBooks();
boolean add = book.add(book1);
if (add) {
System.out.println("添加图书信息如下:");
System.out.println("序号\t\t\t书名\t\t\t价格\t\t\t数量");
System.out.println(book.size()-1+"\t\t\t"+book.getLast().toString());
System.out.println("添加成功!");
}else{
System.out.println("添加图书失败!");
}
}
// 修改图书
public void alterBook(BookList bookList){
LinkedList<Book> book= bookList.getBooks();
System.out.print("请输入修改图书序号:");
int bookID=scan.nextInt(); //修改图书序号
while(true){
if(bookID>=0&&bookID<book.size()){
break;
}else {
System.out.println("输入错误,请输入(0~"+(book.size()-1)+")内数字:");
bookID=scan.nextInt();
}
}
Book b1 = book.get(bookID);
System.out.print("\n请输入图书名:");
String name = scan.next();
b1.setBookName(name);
System.out.print("\n请输入图书价格:");
double price = scan.nextDouble();
b1.setBookPrice(price);
System.out.print("\n请输入图书数量:");
int num = scan.nextInt();
b1.setBookNum(num);
}
// delete 删除图书
public void deleteBook(BookList bookList) {
LinkedList<Book> book= bookList.getBooks();
System.out.println("请输入删除图书序号:");
int bookID=scan.nextInt(); //修改图书序号
while(true){
if (bookID >= 0 &&bookID < book.size()) {
book.remove(bookID);
System.out.println("删除成功!");
break;
}else {
System.out.println("输入错误,请重新输入删除图书序号:");
bookID=scan.nextInt(); //修改图书序号
}
}
}
// 借阅图书
public void borrowBook(BookList bookList){
LinkedList<Book> book= bookList.getBooks();
System.out.print("\n请输入你要借阅图书序号:");
int numbook = scan.nextInt();
Book b1 = book.get(numbook);
System.out.print("\n该图书还有"+b1.getBookNum()+"本");
System.out.print("\n请输入你要借的图书数量:");
int nums = scan.nextInt(); // 借阅数量
if((b1.getBookNum()-nums)>=0){
b1.setBookNum(b1.getBookNum()-nums);
int num = b1.getBookNum();
System.out.print("\n恭喜你借阅成功,该图书还剩余"+num+"本\n");
}else{
System.out.print("\n很遗憾,该图书已经全被借阅!");
}
}
// 归还图书
public void giveBackBook(BookList bookList){
LinkedList<Book> book= bookList.getBooks();
System.out.print("\n请输入你要归还图书序号:");
int bk = scan.nextInt();
Book b1 = book.get(bk);
System.out.print("\n该图书目前剩余"+b1.getBookNum()+"本");
System.out.print("\n请输入归还书本数目:");
int givenum = scan.nextInt();
b1.setBookNum(b1.getBookNum()+givenum);
int num =b1.getBookNum();
System.out.print("\n恭喜你归还成功,现剩余该图书"+num+"本");
}
}
Tool类:
package com.threecode;
public class Tool {
public void page1(){
System.out.println("----------------请选择登录方式----------------");
System.out.println(" 1、登录账号");
System.out.println(" 2、注册账号");
System.out.println(" 3、退出登录");
}
public void page2(){
System.out.println("********欢迎进入登录账号页面********");
System.out.println("1.管理员登录:");
System.out.println("2.普通用户登录:");
}
public void page3(){
System.out.println("\n-----------欢迎进入管理员账号页面-----------");
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("8、退出系统");
}
public void page4(){
System.out.println("\n-----------普通用户登录成功-----------");
System.out.println("1.查看所有图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("4.显示用户信息(只能查看自己的用户信息)");
System.out.println("5.修改用户信息(只能修改自己的用户信息)");
System.out.println("6.退出系统");
}
public void page5(){
System.out.println("********欢迎进入注册账号页面********");
System.out.println("1.管理员注册:");
System.out.println("2.普通用户注册:");
}
}
BooksManagementSystemThree类:
package com.threecode;
import java.util.Scanner;
public class BooksManagementSystemThree {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Tool tool = new Tool();
AdminUserList adminUserList = new AdminUserList();
AdminUserAction adminUserAction = new AdminUserAction();
NormalUserList normalUserList = new NormalUserList();
NormalUserAction normalUserAction = new NormalUserAction();
BookList bookList = new BookList();
BookAction bookAction = new BookAction();
while(true){
while(true) {
tool.page1();
System.out.print("请输入要选择要操作的序号(只能输入1,2,3):");
int loginNum = scan.nextInt();
// 校验输入的序号,只能是1到3
while (true) {
if (loginNum == 1 || loginNum == 2 || loginNum == 3) {
break;
} else {
System.out.print("请输入要选择要操作的序号(只能输入1,2,3):");
loginNum = scan.nextInt();
}
}
switch(loginNum){
case 1:
tool.page2();
System.out.print("请输入要选择要操作的序号(只能输入1,2):");
int enterNum =scan.nextInt(); //enterNum登录数字 1.管理员 2.用户
while(true){
if(enterNum == 1||enterNum == 2 ){
break;
}else{
System.out.print("输入错误,请从从新输入要选择要操作的序号(只能输入1,2):");
enterNum =scan.nextInt();
}
}
if(enterNum==1){
int adminID = adminUserAction.loginCheck(adminUserList);
while(true) {
tool.page3();
System.out.print("请输入你选择的功能:");
int adminnum = scan.nextInt(); // adminnum 管理员账号页面功能查看
while(true){
if(adminnum>=1&&adminnum<=8){
break;
}else{
System.out.print("输入错误,请重新输入你选择的功能:");
adminnum = scan.nextInt();
}
}
if(adminnum ==1){ //1、查看所有图书
bookAction.viewAllBook(bookList);
continue;
} else if (adminnum==2) { //2、添加图书
bookAction.addBook(bookList);
continue;
} else if (adminnum==3) { //3、修改图书 //这里设计的是符合图书序号的所有图书信息都可以修改
bookAction.alterBook(bookList);
continue;
} else if (adminnum==4) { //4、删除图书
bookAction.deleteBook(bookList);
continue;
} else if (adminnum==5) { //5、查看所有普通用户信息
normalUserAction.allNormal(normalUserList);
continue;
} else if (adminnum==6) { //6、查看管理员账号信息
adminUserAction.adminInfo(adminUserList);
continue;
} else if (adminnum==7) { //7、修改管理员账号信息
System.out.println("请输入要修改管理员账号的的信息(1管理员账号名2手机号码3" +
"身份证号码4密码5姓名6性别7专业8住址信息):");
int adID =scan.nextInt(); // adID 修改序号
adminUserAction.adminAlter(adID, adminID,adminUserList);
continue;
}else if (adminnum==8){ //8、退出系统
System.out.println("谢谢使用,欢迎下次光临!");
System.exit(0);;
}
}
} else if (enterNum == 2) {
int normalID = normalUserAction.loginCheck(normalUserList);
while(true){
tool.page4();
System.out.print("请输入你需要操作功能的数字:");
int onetwo1 = scan.nextInt(); // 选择功能数字
if(onetwo1==1){ // 1.查看所有图书
bookAction.viewAllBook(bookList);
continue;
} else if (onetwo1==2) { // 2.借阅图书
bookAction.borrowBook(bookList);
continue;
} else if (onetwo1==3) { // 3.归还图书
bookAction.giveBackBook(bookList);
continue;
} else if (onetwo1==4) { // 4.显示用户信息(只能查看自己的用户信息)
normalUserAction.myInformation(normalID,normalUserList);
continue;
} else if (onetwo1==5) { // 5.修改用户信息(只能修改自己的用户信息)
System.out.print("\n请输入你要修改的信息的选项(1普通用户账号名2手机号码" +
"3身份证号码4密码5姓名6性别7专业8住址信息):");
int num=scan.nextInt(); // normalchange 修改序号
normalUserAction.normalAlter( num, normalID,normalUserList);
continue;
} else if (onetwo1==6) { // 6.退出系统
System.out.print("\n谢谢使用,欢迎下次光临!");
System.exit(0);;
}
}
}
case 2:
tool.page5();
System.out.print("请输入要选择要操作的序号(只能输入1,2):");
int registernum = scan.nextInt(); //注册管理员或者普通用户
while(true){
if(registernum>0||registernum<3){
break;
}else {
System.out.println("输入序号错误,请重新输入要操作的序号(只能输入1,2):");
registernum = scan.nextInt();
}
}
if(registernum==1){
adminUserAction.addAdminUser(adminUserList);
continue;
} else if (registernum==2) {
normalUserAction.addNormalUser(normalUserList);
continue;
}
case 3:
System.out.println("谢谢使用,欢迎下次光临!");
System.exit(0);;
}
}
}
}
}
五、项目运行结果
说明一下:这里并没有校验输入数据限制,是没写,太麻烦了,读者想要校验输入格式可以自行实现。
这里输入的数字均是int类型,输入其他类型数据可能直接报错退出程序。
说明:功能没有截图完全,其余功能实现,读者可以自行运行实现查看。
六、项目难点分析
1、校验功能数字是自己规定的数字
使用while(true){}循环实现,在循环体中设置只有输入规定数字才能跳出循环,执行后面的代码。
System.out.print("请输入要选择要操作的序号(只能输入1,2):"); int enterNum =scan.nextInt(); //enterNum登录数字 1.管理员 2.用户 while(true){ if(enterNum == 1||enterNum == 2 ){ break; }else{ System.out.print("输入错误,请从从新输入要选择要操作的序号(只能输入1,2):"); enterNum =scan.nextInt(); } }
2、明确变量的定义域
要清楚自己定义的变量作用的范围:
在Java中,变量的作用域决定了变量在程序中可见的范围。如果一个变量被定义在一个代码块内部,那么它的作用域就被限制在该代码块内部。如果一个变量被定义在方法内部,那么它的作用域就被限制在该方法内部。如果一个变量被定义在类内部但方法之外,那么它的作用域就是整个类。
这里要注意的是,作用范围大的变量,例如adminUser、bookName...等等,要定义在整个循环体的外面,若是定义在while(){}、for(;;){}、do{}while()三种循环和if(){}else{}、switch(){}选择语句中变量,作用域只能在循环体或者选择语句中,只用在循环体或者选择语句中使用,此范围之外,不能使用,或者可能出现操作该变量失效的现象。
3、将其划分为不同的类,在不同类里面进行不同类单独的属性方法的开发与封装。
4、加入集合的操作,取代数组对数据的存储。
根据集合的特点使用:
a)ArrayList 底层是数组实现的,方便查询操作
这里管理员用户和普通用户类,基本很少涉及增删,所以使用ArrayList 集合
b)LinkedList 底层是双向链表实现的,方便增加和删除
这里的Book类可能涉及多次的增删,所以使用LinkedList集合
5、数据的转型(多态)
例如, 这里的
ArrayList<NormalUser> normalUsers = normalUser.getNormalUsers();
在NormalUserAction类中,想要使用NormalUser类中的属性,必须要从集合中取出目标操作类,将其转换为NormalUser,才可以使用其定义的特有方法。
NormalUser n1=normalUsers.get(normalid);