目录
一、项目要求:
二、项目环境
三、项目使用的知识点
四、项目代码
五、项目运行结果
六、项目难点分析
图书管理系统--versions1.0-CSDN博客文章浏览阅读981次,点赞29次,收藏17次。本文使用了变量,数据类型,顺序,选择,循环,数组实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135583634?spm=1001.2014.3001.5501
图书管理系统--versions2.0-CSDN博客文章浏览阅读1k次,点赞35次,收藏19次。本文使用了变量,数据类型,顺序,选择,循环,数组,对象及属性的封装实现了一个简单的小项目--图书管理系统,其中包括用户管理,图书管理,不同权限管理的内容不同。https://blog.csdn.net/qq_53483101/article/details/135728923?spm=1001.2014.3001.5501
图书管理系统(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)启动系统后,显示登录账号、注册账号、退出登录三个模块
登录账号:
显示管理员登录
用户登录两个模块注册账号:
显示注册管理员
注册用户两个模块
退出登录:结束程序运行
3)登录账号成功后,根据账号的性质来显示不同的模块:
登录管理员账号成功后,显示如下模块:
查看所有图书,图书显示哪些信息,这里设计的信息有书名,书的价格,书的数量
添加图书
修改图书
删除图书
查看所有普通用户信息
查看管理员账号信息
修改管理员账号信息
退出系统
登录普通账号成功后,显示如下模块:
查看所有图书,图书显示哪些信息,你自行设计
借阅图书
归还图书显示用户信息(只能查看自己的用户信息)
修改用户信息(只能修改自己的用户信息)
退出系统4)使用对象对不同类及其各自属性进行封装与功能实现
(1)开发工具:IDEA、JDK17
(2)开发语言:Java
(1)理解程序基本概念——程序、变量、数据类型
(2)顺序、选择、循环、跳转语句
(3)数组(4)使用对象的思想对图书管理系统--versions1.0进行改造
这里使用了对象封装的思想,将其分为管理员用户类,普通用户,书类,工具类,系统主页面类,将各自的类的属性功能封装,在系统页面类里面调用这些属性方法实现各个功能的开发。
这里说明一下,为了快速写出项目,前面的用户管理的八个属性均采用String类型存储,方便自己后续代码的编写及相关功能的实现,有不足或者想要自己实现的功能部分读者可以自行修改代码实现。
package com.newcode;
import java.util.Scanner;
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;
private static int adminNum; // 管理员数量
private static String[] adminUsers = new String[3];
private static String[] adminPhoneNumbers =new String[3];
private static String[] adminIdentityNumbers = new String[3];
private static String[] adminPasswords =new String[3];
private static String[] adminNames = new String[3];
private static String[] adminSexs = new String[3];
private static String[] adminCareers =new String[3];
private static String[] adminAddresss =new String[3];
Scanner scan = new Scanner(System.in);
public AdminUser() {
adminUsers[0] = "root";
adminPhoneNumbers[0] ="1111";
adminIdentityNumbers[0] ="2222";
adminPasswords[0] ="123456";
adminNames[0] = "张三";
adminSexs[0] = "男";
adminCareers[0] ="计算机";
adminAddresss[0] ="安徽";
adminNum=1;
}
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;
}
//管理员用户登录验证
public int loginCheck(){
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(); // 登录管理员账号序号
if (verifyNum == 1) { // 管理员用户
// 管理员登录验证 手机号码+密码
System.out.println("\n请输入你的验证信息:");
while(true){
if(adminUsers[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();
while (true) {
if (adminUser1.equals(adminUsers[adminId]) && adminPhoneNumber1.equals(adminPhoneNumbers[adminId]) && adminPassword1.equals(adminPasswords[adminId])) {
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();
while (true) {
if (adminUser2.equals(adminUsers[adminId]) && adminIdentityNumber2.equals(adminIdentityNumbers[adminId]) && adminPassword2.equals(adminPasswords[adminId])) {
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(AdminUser admin){
adminNum++;
adminUsers[adminNum-1] = admin.getAdminUser();
adminPhoneNumbers[adminNum-1] = admin.getAdminPhoneNumber();
adminIdentityNumbers[adminNum-1] =admin.getAdminIdentityNumber() ;
adminPasswords[adminNum-1] =admin.getAdminPassword() ;
adminNames[adminNum-1] =admin.getAdminName() ;
adminSexs[adminNum-1] =admin.getAdminSex() ;
adminCareers[adminNum-1] =admin.getAdminCareer() ;
adminAddresss[adminNum-1] =admin.getAdminAddress() ;
System.out.println("添加信息如下:");
System.out.println(
adminUsers[adminNum-1]+"\t\t\t"+adminPhoneNumbers[adminNum-1]+"\t\t\t"+
adminIdentityNumbers[adminNum-1]+"\t\t\t"+adminPasswords[adminNum-1]+"\t\t\t"+
adminNames[adminNum-1]+"\t\t\t"+adminSexs[adminNum-1]+"\t\t\t"+
adminCareers[adminNum-1]+"\t\t\t"+adminAddresss[adminNum-1]);
System.out.println("添加管理员账户成功!");
}
//查看管理员账号信息
public void adminInfo(){
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");
for(int i=0;i
package com.newcode;
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;
private static int normalNum; // 普通用户数量
private static String[] normalUsers =new String[8];
private static String[] normalPhoneNumbers =new String[8];
private static String[] normalIdentityNumbers = new String[8];
private static String[] normalPasswords =new String[8];
private static String[] normalNames = new String[8];
private static String[] normalSexs = new String[8];
private static String[] normalCareers =new String[8];
private static String[] normalAddresss =new String[3];
public NormalUser() {
normalUsers[0] = "normal";
normalPhoneNumbers[0] ="1234";
normalIdentityNumbers[0] ="5678";
normalPasswords[0] ="654321";
normalNames[0] ="梦洛";
normalSexs[0] = "女";
normalCareers[0] ="艺术";
normalAddresss[0]="北京";
normalNum=1;
}
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;
}
Scanner scan = new Scanner(System.in);
// 验证普通用户登录
public int loginCheck() {
// 普通用户登录验证
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.身份证号码+密码
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();
while (true) {
if (normalUser1.equals(normalUsers[normalID]) && normalPhoneNumber1.equals(normalPhoneNumbers[normalID]) && normalPassword1.equals(normalPasswords[normalID])) {
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();
while (true) {
if (normalUser2.equals(normalUsers[normalID]) && normalIdentityNumber2.equals(normalIdentityNumbers[normalID]) && normalPassword2.equals(normalPasswords[normalID])) {
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(NormalUser normal){
normalNum++;
normalUsers[normalNum-1] =normal.getNormalUser();
normalPhoneNumbers[normalNum-1] =normal.getNormalPhoneNumber();
normalIdentityNumbers[normalNum-1] =normal.getNormalIdentityNumber();
normalPasswords[normalNum-1] =normal.getNormalPassword();
normalNames[normalNum-1] =normal.getNormalName() ;
normalSexs[normalNum-1] =normal.getNormalSex() ;
normalCareers[normalNum-1] =normal.getNormalCareer() ;
normalAddresss[normalNum-1] =normal.getNormalAddress() ;
System.out.println("添加信息如下:");
System.out.println(
normalUsers[normalNum-1]+"\t\t\t"+normalPhoneNumbers[normalNum-1]+"\t\t\t"+
normalIdentityNumbers[normalNum-1]+"\t\t\t"+normalPasswords[normalNum-1]+"\t\t\t"+
normalNames[normalNum-1]+"\t\t\t"+normalSexs[normalNum-1]+"\t\t\t"+
normalCareers[normalNum-1]+"\t\t\t"+normalAddresss[normalNum-1]);
System.out.println("添加用户账户成功");
}
// 查看所有普通用户信息
public void allNormal(){
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");
for(int i=0;i
package com.newcode;
import java.util.Scanner;
public class Book {
private String bookName;
private double bookPrice;
private int bookNum;
// 书本种类
private static int bookTypeNum;
private static String[] bookNames =new String[20];
private static double[] bookPrices =new double[20];
private static int[] bookNums =new int[20];
Scanner scan = new Scanner(System.in);
public Book() {
bookNames[0] = "JAVA";
bookPrices[0] =30.0;
bookNums[0] =20;
bookNames[1] = "Python";
bookPrices[1] =25.0;
bookNums[1] =20;
bookNames[2] = "C语言程序设计";
bookPrices[2] =35.0;
bookNums[2] =20;
bookTypeNum = 3;
}
public Book(String bookName, double bookPric, int bookNum) {
this.bookName = bookName;
this.bookPrice = bookPric;
this.bookNum = bookNum;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public int getBookNum() {
return bookNum;
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
// 查看所有图书
public void viewAllBook(){
System.out.println("序号\t\t\t书名\t\t\t价格\t\t\t数量");
for(int i=0;i=0&&bookId= 0 && bookId < bookTypeNum) {
for (int i = bookId; i < bookTypeNum; i++) {
bookNames[i] = bookNames[i + 1];
bookPrices[i] = bookPrices[i + 1];
bookNums[i] = bookNums[i + 1];
}
bookTypeNum--;
}else if (bookId == bookTypeNum-1) {
bookNames[bookId] = null; // 防止数据残留导致出现的问题
bookPrices[bookId]=0;
bookNums[bookId]=0;
bookTypeNum--;
}
System.out.println("删除成功!");
}
// 借阅图书
public void borrowBook(){
System.out.print("\n请输入你要借阅图书序号:");
int numbook = scan.nextInt();
System.out.print("\n该图书还有"+bookNums[numbook]+"本");
System.out.print("\n请输入你要借的图书数量:");
int nums = scan.nextInt(); // 借阅数量
if((bookNums[numbook]-nums)>=0){
bookNums[numbook] -=nums ;
System.out.print("\n恭喜你借阅成功,该图书还剩余"+bookNums[numbook]+"本\n");
}else{
System.out.print("\n很遗憾,该图书已经全被借阅!");
}
}
// 归还图书
public void giveBackBook(){
System.out.print("\n请输入你要归还图书序号:");
int givenumbook = scan.nextInt();
System.out.print("\n该图书目前剩余"+bookNums[givenumbook]+"本");
System.out.print("\n请输入归还书本数目:");
int givenum = scan.nextInt();
System.out.print("\n恭喜你归还成功,现剩余该图书"+(bookNums[givenumbook]+=givenum)+"本");
}
}
package com.newcode;
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.普通用户注册:");
}
}
package com.newcode;
import java.util.Scanner;
public class BooksManagementSystemNew {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Tool tool = new Tool();
AdminUser adminUser= new AdminUser();
Book book = new Book();
NormalUser normalUser = new NormalUser();
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 = adminUser.loginCheck();
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、查看所有图书
book.viewAllBook();
continue;
} else if (adminnum==2) { //2、添加图书
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);
book.addBook(book1);
continue;
} else if (adminnum==3) { //3、修改图书 //这里设计的是符合图书序号的所有图书信息都可以修改
System.out.print("请输入修改图书序号:");
int bookID=scan.nextInt(); //修改图书序号
book.alterBook(bookID);
continue;
} else if (adminnum==4) { //4、删除图书
System.out.println("请输入删除图书序号:");
int bookID=scan.nextInt(); //修改图书序号
book.deleteBook(bookID);
continue;
} else if (adminnum==5) { //5、查看所有普通用户信息
normalUser.allNormal();
continue;
} else if (adminnum==6) { //6、查看管理员账号信息
adminUser.adminInfo();
continue;
} else if (adminnum==7) { //7、修改管理员账号信息
System.out.println("请输入要修改管理员账号的的信息(1管理员账号名2手机号码3" +
"身份证号码4密码5姓名6性别7专业8住址信息):");
int adID =scan.nextInt(); // adID 修改序号
adminUser.adminAlter(adID, adminID);
continue;
}else if (adminnum==8){ //8、退出系统
System.out.println("谢谢使用,欢迎下次光临!");
System.exit(0);;
}
}
} else if (enterNum == 2) {
int normalID = normalUser.loginCheck();
while(true){
tool.page4();
System.out.print("请输入你需要操作功能的数字:");
int onetwo1 = scan.nextInt(); // 选择功能数字
if(onetwo1==1){ // 1.查看所有图书
book.viewAllBook();
continue;
} else if (onetwo1==2) { // 2.借阅图书
book.borrowBook();
continue;
} else if (onetwo1==3) { // 3.归还图书
book.giveBackBook();
continue;
} else if (onetwo1==4) { // 4.显示用户信息(只能查看自己的用户信息)
normalUser.myInformation(normalID);
continue;
} else if (onetwo1==5) { // 5.修改用户信息(只能修改自己的用户信息)
System.out.print("\n请输入你要修改的信息的选项(1普通用户账号名2手机号码" +
"3身份证号码4密码5姓名6性别7专业8住址信息):");
int num=scan.nextInt(); // normalchange 修改序号
normalUser.normalAlter( num, normalID);
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){
System.out.print("------管理员注册页面------");
System.out.print("\n请输入新的管理员账户名:");
String adminuser = scan.next();
System.out.print("\n请输入新的手机号码:");
String adminphonenumber = scan.next();
System.out.print("\n请输入新的身份证号码:");
String adminidentitynumber = scan.next();
System.out.print("\n请输入新的密码:");
String adminpassword = scan.next();
System.out.print("\n请输入新的姓名:");
String adminname = scan.next();
System.out.print("\n请输入新的性别:");
String adminsex= scan.next();
System.out.print("\n请输入新的专业:");
String admincareer= scan.next();
System.out.print("\n请输入新的住址信息:");
String adminaddress = scan.next();
AdminUser au =new AdminUser(adminuser,adminphonenumber,adminidentitynumber,adminpassword ,adminname,adminsex,admincareer,adminaddress);
adminUser.addAdminUser(au);
continue;
} else if (registernum==2) {
System.out.print("------普通用户注册页面------");
System.out.print("\n请输入新的用户账户名:");
String normaluser = scan.next();
System.out.print("\n请输入新的手机号码:");
String normalphonenumber = scan.next();
System.out.print("\n请输入新的身份证号码:");
String normalidentitynumber = scan.next();
System.out.print("\n请输入新的密码:");
String normalpassword = scan.next();
System.out.print("\n请输入新的姓名:");
String normalname = scan.next();
System.out.print("\n请输入新的性别:");
String normalsex= scan.next();
System.out.print("\n请输入新的专业:");
String normalcareer= scan.next();
System.out.print("\n请输入新的住址信息:");
String normaladdress = scan.next();
NormalUser n = new NormalUser(normaluser,normalphonenumber,
normalidentitynumber,normalpassword,normalname,
normalsex,normalcareer,normaladdress);
normalUser.addNormalUser(n);
continue;
}
case 3:
System.out.println("谢谢使用,欢迎下次光临!");
System.exit(0);;
}
}
}
}
}
说明一下:这里并没有校验输入数据限制,是没写,太麻烦了,读者想要校验输入格式可以自行实现。
这里输入的数字均是int类型,输入其他类型数据可能直接报错退出程序。
说明:功能没有截图完全,其余功能实现,读者可以自行运行实现查看。
1、 使用数组对象保存图书和用户信息1) 根据项目需求分析可知,管理员用户和普通用户都有八个属性,分别是:账号,手机号码,身份证号码,密码,姓名,性别,专业,住址信息。可以使用数组来保存多条相同类型的信息。
2)定义8个数组分别保存用户信息。
3)各个数组中下标表示相同的元素表示一个用户数据。
4)在无参构造方法中对管理员用户,普通用户,书进行了初始化赋值
public AdminUser() { adminUsers[0] = "root"; adminPhoneNumbers[0] ="1111"; adminIdentityNumbers[0] ="2222"; adminPasswords[0] ="123456"; adminNames[0] = "张三"; adminSexs[0] = "男"; adminCareers[0] ="计算机"; adminAddresss[0] ="安徽"; adminNum=1; }
public NormalUser() { normalUsers[0] = "normal"; normalPhoneNumbers[0] ="1234"; normalIdentityNumbers[0] ="5678"; normalPasswords[0] ="654321"; normalNames[0] ="梦洛"; normalSexs[0] = "女"; normalCareers[0] ="艺术"; normalAddresss[0]="北京"; normalNum=1; }
public Book() { bookNames[0] = "JAVA"; bookPrices[0] =30.0; bookNums[0] =20; bookNames[1] = "Python"; bookPrices[1] =25.0; bookNums[1] =20; bookNames[2] = "C语言程序设计"; bookPrices[2] =35.0; bookNums[2] =20; bookTypeNum = 3; }
2、校验功能数字是自己规定的数字
使用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();
}
}
3、明确变量的定义域要清楚自己定义的变量作用的范围:
在Java中,变量的作用域决定了变量在程序中可见的范围。如果一个变量被定义在一个代码块内部,那么它的作用域就被限制在该代码块内部。如果一个变量被定义在方法内部,那么它的作用域就被限制在该方法内部。如果一个变量被定义在类内部但方法之外,那么它的作用域就是整个类。
这里要注意的是,作用范围大的变量,例如adminUser、bookName...等等,要定义在整个循环体的外面,若是定义在while(){}、for(;;){}、do{}while()三种循环和if(){}else{}、switch(){}选择语句中变量,作用域只能在循环体或者选择语句中,只用在循环体或者选择语句中使用,此范围之外,不能使用,或者可能出现操作该变量失效的现象。
4、将其划分为不同的类,在不同类里面进行不同类单独的属性方法的开发与封装。