package tushuguanli;
import java.util.Scanner;
/*一, 登录
提前初始化好用户名和密码,要求用户输入用户名和密码登录,错误次数不超过3次
登录成功后,进入主界面
二,主界面
欢迎来到国信安java203图书管理系统
String[][] books = {{“w”,”我的”},{“z”,”中国xx”}};
选择以下操作: 1,显示图书列表(要求排序)
2, 添加图书(不允许有重复的书名) (输入书的名字和首字母)成功之后立即显示图书列表
3, 删除图书 , 成功之后立即显示图书列表1,2,3 要求询问,是否回到主界面
4, 退出系统
*/
public class TuShuGuanLi {
public static void main(String[] args) {
String[][] userArr = { { "1", "1" }, { "wang", "huan" }, { "admin", "123" }, { "root", "root" } };
Scanner sc = new Scanner(System.in);
System.out.print("请输入用户名 :");
String inputUsername = sc.next();
System.out.print("请输入密码: ");
String inputPassword = sc.next();
String[] inputArr = { inputUsername, inputPassword };
int count = dengLu(userArr, inputArr); //登录判断
if (count == -1) { //登录成功进入该语句
String[][] books = { { "l", "鲁滨逊漂流记" }, { "A", "安徒生童话" }, { "S", "圣经的故事 " }, { "h", "环绕世界八十天" },
{ "j", "简·爱" }, { "y", "伊索寓言" } }; //初始化二维数组
booksPaiXu(books); //先利用冒泡排序把数组按字母顺序排序好,省的后面case语句里面进行多次排序操作
while (count == -1) { //登录成功进入while循环
printShouYe();//显示初始化界面
Object[] obj = show(count, books);//显示选择操作,并且继续选择,传入books和count,返回被改动的books和count回来,实现每一次操作books都能更新和利用返回过来的count值来判断每一次操作是否需要继续
books = (String[][]) obj[1];
count = (int) obj[0];
}
}
}
public static Object[] show(int count, String[][] books) {
Scanner sc1 = new Scanner(System.in);
Object[] obj = { count, books }; //初始化obj传入count和books的初始值,方便后面case操作改变ojb里面的值
int selectNum = sc1.nextInt();
switch (selectNum) { //利用switch case语句进行判断操作
case 1:
count = caseOne(count, books); //进行查询操作
obj[0] = count; //更新count值
break;
case 2:
obj = caseTwo(count, books);//进行添加操作,并且更新添加后的books值和count值
break;
case 3:
obj = caseThree(count, books);//进行删除操作,并且更新删除后的books值和count值
break;
case 4:
System.out.println("退出成功");
System.out.println("再见");
count = 0;
obj[0] = count;//更新count值
break;
default: //如果输入的不是1-4则输出输入错误
System.out.println("输入有误,已重新返回主页面:");
break; //退出switch语句,重新进入while循环
}
return obj; //返回最终的obj值
}
public static int dengLu(String[][] userArr, String[] inputArr) {
int count = 3;//定义count来计数
while (count > 0) {//外层while循环
if (count <= 2) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入用户名: ");
inputArr[0] = sc.next();
System.out.print("请输入密码: ");
inputArr[1] = sc.next();
}
for (int i = 0; i < userArr.length; i++) {
if (userArr[i][0].equals(inputArr[0])) {
if (userArr[i][1].equals(inputArr[1])) {
System.out.println("登录成功");
count = -1;
break;
} else {
count--;
if (count == 0) { //三次机会用完count降为0,自动退出循环
System.out.println("密码错误");
System.out.println("你的机会已用完,登录失败");
break;
} else {
System.out.print("密码错误,你还有" + count + "次机会");
System.out.println();
break;
}
}
} else {
if (i == userArr.length - 1) {
count--;
if (count == 0) { //三次机会用完count降为0,自动退出循环
System.out.println("用户名错误");
System.out.println("你的机会已用完,登录失败");
break;
} else {
System.out.print("用户名错误,你还有" + count + "次机会");
System.out.println();
break;
}
}
}
}
}
return count;
}
public static void printShouYe() {
System.out.println("-------欢迎来到图书管理系统-------");
System.out.println(
"请选择以下操作: " + "\t" + "|");
System.out.println(
"1, 显示图书列表 " + "\t" + "|");
System.out.println(
"2, 添加图书 " + "\t" + "|");
System.out.println(
"3, 删除图书 " + "\t" + "|");
System.out.println(
"4, 退出系统 " + "\t" + "|");
System.out.println("----------------------------------------");
}
public static void booksPaiXu(String[][] books) {
for (int i = 0; i < books.length - 1; i++) {
for (int i1 = 0; i1 < books.length - 1 - i; i1++) {
if (books[i1][0].toLowerCase().charAt(0) > books[i1 + 1][0].toLowerCase().charAt(0)) {
//toLowerCase忽略字母大小写进行比较
String[] temp = books[i1];
books[i1] = books[i1 + 1];
books[i1 + 1] = temp;
}
}
}
}
public static void booksBianLi(String[][] books) {
for (int i = 0; i < books.length; i++) {
for (int j = 0; j < books[j].length; j++) {
System.out.print(books[i][j] + " ");
}
System.out.println();
}
}
public static int yesOrNoQuit() {
int count = -1;
Scanner sc1 = new Scanner(System.in);
System.out.print("是否返回主页面y/n:");
String s = sc1.next();
if (s.equalsIgnoreCase("y")) {
System.out.println("返回成功");
} else if (s.equalsIgnoreCase("n")) {
System.out.println("由于你无需再进行操作,已自动退出系统");
count = 0;//count为0就不能再次进行while循环
} else {
System.out.println("输入有误,已重新返回主界面");
}
return count;
}
public static String[] tianJiaPanDuan(String[][] books) {
Scanner sc1 = new Scanner(System.in);
System.out.print("请输入首字母: ");
String str = sc1.next();
System.out.print("请输入你要添加的书籍名字: ");
String str1 = sc1.next();
while (true) {//外循环,重复就一直循环输入语句,不重复才添加成功
int count1 = 0; //定义一个count1计数,放在while循环里面,保证每一次while循环count1都可以重新开始计数
for (int m = 0; m < books.length; m++) {//内循环
if (str1.equals(books[m][1])) {//相同则重复,注意要用equals比较
System.out.print("书名重复,请重新输入: ");
str1 = sc1.next();
break;//一次添加不成功之后,m需要重新初始化为0,这样才能保证接下来重新输入的str1,从第一个开始比较
} else {
count1++;
//不相同则++,
}
}
if (count1 == books.length) {//循环的每一次都进行了count1++,说明跟每一个书名都不相同
System.out.println("添加成功");
break;//跳出while循环
}
}
String[] strArr = { str, str1 };
//赋给一个一维数组
return strArr;
}
public static String shangChuPanDuan(String[][] books) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入首写字母: ");
String str1 = sc.next();
boolean falg = false;
while (true) {
int num = 0;
for (int i = 0; i < books.length; i++) {
if (str1.equals(books[i][0])) {
System.out.println("已找到相对于的首字母");
falg = true;
break;
} else {
num++;
}
}
if (num == books.length) {
System.out.println("未找到对应的首字母,请重新输入");
str1 = sc.next();
} else {
break;
}
}
System.out.print("请输入你要删除的书名: ");
String str2 = sc.next();
while (falg) {
int count2 = 0; //用来计数,好判断,要写在while循环里面,每循环一次要初始化为0
for (int m = 0; m < books.length; m++) {
if (str2.equals(books[m][1]) == false) {//如果书名不相同则进入循环
count2++;
}
}
if (count2 == books.length) {//都不相同则进入语句
System.out.print("没有你要删除的这本书,请重新输入: ");
str2 = sc.next();
} else {
System.out.println("删除成功");
break;//有相同的则跳出while循环
}
}
return str2;
}
public static int caseOne(int count, String[][] books) {
System.out.println("查询成功");
booksBianLi(books);//遍历数组
count = yesOrNoQuit();//判断是否返回,更新count值
return count;//返回count值
}
public static Object[] caseTwo(int count, String[][] books) {
String[] strArr = tianJiaPanDuan(books); //能够添加才把需要添加的值赋给strArr
String[][] newBooks = new String[books.length + 1][];//每循环一次重新开辟一个数组大小为上一个的大小加1
String[] temp = null;
for (int i = 0; i < books.length; i++) {
if (strArr[0].toLowerCase().charAt(0) < books[i][0].toLowerCase().charAt(0)) {
temp = books[i];
books[i] = strArr;
strArr = temp;
}
} //一边插入,一般排序,最大的剩出来,值为temp
for (int n = 0; n < newBooks.length; n++) {
if (n == newBooks.length - 1) {
newBooks[n] = temp;
} else {
newBooks[n] = books[n];
}
} //把添加进来的数据和本来的数据组成一个新的数组
books = newBooks;//这样添加数据出去之后再重新查询就能刷新出添加进去的数据了
booksBianLi(books); //遍历数组
count = yesOrNoQuit();//判断是否返回
Object[] obj = { count, books }; //把更新好了的count和booos放到obj
return obj;//返回Obj
}
public static Object[] caseThree(int count, String[][] books) {
String str2 = shangChuPanDuan(books);//判断是否有相同的,有再进赋值给str2,才能进行后面的删除操作
String newTwoBooks[][] = new String[books.length - 1][]; //开辟一个新的数组接收删除数据之后组成的新数组
int j = 0;
for (int i = 0; i < books.length; i++) {
if (str2.equals(books[i][1])) {
} else {
newTwoBooks[j] = books[i];
j++;
}
}
books = newTwoBooks;//把新的数组又赋给books,这样退出循环之后图书里面的书本数据才会更新
booksBianLi(books);//遍历数组
count = yesOrNoQuit();//判断是否返回
Object[] obj = { count, books }; //把更新了的count和books放到Obj里面
return obj;//返回obj
}
}