之前的作业 后面整完了也没注意来这边发 今天终于想起来 代码如下
import java.util.Scanner;
public class Demo {
static String[][] user = new String[10][4];
static String[][] book = new String[20][5];
static String[][] pubCom = new String[5][3];
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
init();
System.out.println("**********欢迎使用书籍管理系统**********");
boolean loginSuc = true;
//条件为true进入系统,条件为false退出系统
while(loginSuc) {
//接受用户名和密码
System.out.println("请输入用户名");
String userName = sc.next();
System.out.println("请输入密码");
String userPwd = sc.next();
//判断是否正确,正确进入系统,错误无法进入
if (login(userName,userPwd)) {
boolean p = true;
while(p) {
System.out.println("请进行功能选择");
System.out.println("1.图书管理 2.出版社管理 3.退出系统");
int choose = sc.nextInt();
if (choose == 1) {
boolean a = true;
while(a) {
System.out.println("请进行功能选择:1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
int c = sc.nextInt();
if (c == 1) {
//书籍添加
BookAdd();
}
else if (c == 2) {
System.out.println("请输入要删除的书籍ISBN码");
String isbn = sc.next();
//进行删除并判断是否删除成功
searchAllBook();
boolean b=deleteBook(isbn);
if (b) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败,输入的isbn码有误!");
}
searchAllBook();
}
else if (c == 3) {
//书籍更新
updateBook();
}
else if (c == 4) {
//书籍查询
boolean find=true;
while(find) {
System.out.println("请选择查询方式:1.根据ISBN查询 2.根据书名查询(模糊) 3.根据出版社查询"
+ "4.根据作者查询 5.根据价格范围查询 6.查询所有书籍信息 7.返回上一级菜单");
int cs = sc.nextInt();
if (cs == 1) {
//根据ISBN查询
findISBN();
}
else if (cs == 2) {
//根据书名查询(模糊)
findBookName();
}
else if (cs == 3) {
//根据出版社查询
findPubCom();
}
else if (cs == 4) {
//根据作者查询
findAuthor();
}
else if (cs == 5) {
//根据价格范围查询
findPrice();
}
else if (cs == 6) {
//查询所有书籍信息
searchAllBook();
}
else if (cs == 7) {
//返回上一级菜单
find = false;
}
else {
System.out.println("输入数字有误,请重新输入!");
}
}
}
else if (c == 5) {
a=false;
}
else {
System.out.println("输入有误,请重新输入");
}
}
}
else if(choose == 2) {
boolean a=true;
while(a) {
System.out.println("请进行功能选择:");
System.out.println("1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
int c = sc.nextInt();
if (c == 1) {
//增加出版社
addPubCom();
}
else if (c == 2) {
//删除出版社
deletePubCom();
}
else if (c == 3) {
//更新出版社
updatePubCom();
}
else if (c == 4) {
//根据出版社名称查询
findPubName();
}
else if (c == 5) {
//查询所有出版社
searchAllPubCom();
}
else if (c == 6) {
//返回上一级菜单
a=false;
}
else {
System.out.println("输入有误,请重新输入");
}
}
}
else if(choose == 3) {
//退出系统
p = false;
loginSuc = false;
System.out.println("感谢使用本系统!");
}
else {
System.out.println("输入有误,请重新输入!");
}
}
}
else {
System.out.println("用户名不存在或密码错误,请重新输入!");
}
}
}
private static void searchAllPubCom() {
// 查询所有出版社
System.out.println("出版社名称 \t地址 \t联系人");
for (int i = 0; i < pubCom.length; i++) {
if (pubCom[i][0] != null) {
for (int j = 0; j < pubCom[i].length; j++) {
System.out.print(pubCom[i][j]+"\t");
}
System.out.println();
}
}
}
private static void findPubName() {
// 根据出版社名称查询
System.out.println("请输入要查询的出版社名称:");
String PubName = sc.next();
System.out.println("出版社名称 \t地址 \t联系人");
for (int i = 0; i < pubCom.length; i++) {
if (PubName.equals(pubCom[i][0])) {
for (int j = 0; j < pubCom[i].length; j++) {
System.out.print(pubCom[i][j]+"\t");
}
System.out.println();
}
}
}
private static void updatePubCom() {
searchAllPubCom();
// 更新出版社
System.out.println("请输入要更新的出版社名称:");
String name = sc.next();
int index = searchByPCName(name);
System.out.println("输入更新的出版社地址");
pubCom[index][1]=sc.next();
System.out.println("输入更新的出版社联系人");
pubCom[index][2]=sc.next();
searchAllPubCom();
}
private static void deletePubCom() {
searchAllPubCom();
// 删除出版社
System.out.println("请输入要删除的出版社名称:");
String name = sc.next();
//判断是否删除成功
boolean b = delByPCName(name);
if (b) {
System.out.println("删除成功!");
}else {
System.out.println("输入错误或该出版社有相关图书信息,删除失败!");
}
searchAllPubCom();
}
private static boolean delByPCName(String name) {
// 进行删除操作
boolean b = false;
int index = searchByPCName(name);
if (index != -1) {
if (!isHaveBook(name)) {
for (int i = index; i < pubCom.length-1; i++) {
pubCom[i]=pubCom[i+1];
if (pubCom[i+1] == null) {
break;
}
}
b=true;
if (pubCom[pubCom.length-1][0] != null) {
pubCom[pubCom.length-1][0] = null;
pubCom[pubCom.length-1][1] = null;
pubCom[pubCom.length-1][2] = null;
}
}
}
return b;
}
private static boolean isHaveBook(String name) {
// 判断出版社是否存在图书信息
boolean b = false;
for (int i = 0; i < book.length; i++) {
if (name.equals(book[i][3])) {
b=true;
break;
}
}
return b;
}
private static void addPubCom() {
searchAllPubCom();
// 增加出版社
System.out.println("请输入出版社名称:");
String Name = sc.next();
System.out.println("请输入出版社地址:");
String Address = sc.next();
System.out.println("请输入出版社联系人");
String Tel = sc.next();
//判断是否添加成功
boolean b = insertPubCom(Name,Address,Tel);
if (b) {
System.out.println("添加成功!");
}
else {
System.out.println("添加失败,该出版社已存在!");
}
searchAllPubCom();
}
private static boolean insertPubCom(String name, String address, String tel) {
// 进行出版社添加操作
boolean b = false;
int pcName = searchByPCName(name);
if (pcName == -1) {
int index = getPubComFirstNull();
if (index == -1) {
System.out.println("数组位置已满,没位置添加!");
}
pubCom[index][0] = name;
pubCom[index][1] = address;
pubCom[index][2] = tel;
}
return b;
}
private static int searchByPCName(String name) {
// 查询出版社名称下标
int b = -1;
for (int i = 0; i < pubCom.length; i++) {
if (name.equals(pubCom[i][0])) {
b=i;
}
}
return b;
}
private static int getPubComFirstNull() {
// 获得第一个空值下标
int index = -1;
for (int i = 0; i < pubCom.length; i++) {
if (pubCom[i][0] == null) {
index = i;
}
}
return index;
}
private static void findPrice() {
// 根据价格查询
System.out.println("请输入最低价格:");
int min = sc.nextInt();
System.out.println("请输入最高价格:");
int max = sc.nextInt();
for (int i = 0; i < book.length; i++) {
if (book[i][0] != null) {
int price = Integer.parseInt(book[i][2]);
if (price >= min && price <= max) {
printIndexBook(i);
}
}
else {
break;
}
}
}
private static void findAuthor() {
// 根据作者查询
System.out.println("请输入要查询的作者姓名:");
String author = sc.next();
System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
for (int i = 0; i < book.length; i++) {
if (author.equals(book[i][4])) {
for (int j = 0; j < book[i].length; j++) {
System.out.print(book[i][j]+"\t");
}
System.out.println();
}
}
}
private static void findPubCom() {
// 根据出版社查询
System.out.println("请输入要查询的出版社名称:");
String PubCom = sc.next();
System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
for (int i = 0; i < book.length; i++) {
if (PubCom.equals(book[i][3])) {
for (int j = 0; j < book[i].length; j++) {
System.out.print(book[i][j]+"\t");
}
System.out.println();
}
}
}
private static void findBookName() {
// 根据书名查询
System.out.println("请输入要查询的书名:");
String BookName = sc.next();
System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
for (int i = 0; i < book.length; i++) {
if (book[i][1]!=null && book[i][1].indexOf(BookName)!=-1) {
for (int j = 0; j < book[i].length; j++) {
System.out.print(book[i][j]+"\t");
}
System.out.println();
}
}
}
private static void findISBN() {
// 根据isbn码查询
System.out.println("请输入要查询的ISBN码:");
String isbn = sc.next();
int index = getindexByISBN(isbn);
System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
printIndexBook(index);
}
private static void printIndexBook(int index) {
// 打印输出第index行的书籍
for (int i = 0; i < book[index].length; i++) {
System.out.print(book[index][i]+"\t");
}
}
private static void updateBook() {
searchAllBook();
// 书籍更新
System.out.println("请输入要更新的书籍的isbn码");
String isbn = sc.next();
int index = getindexByISBN(isbn);
if (index != -1) {
System.out.println("请输入更新的书名:");
book[index][1]=sc.next();
System.out.println("请输入更新的价格:");
book[index][2]=sc.next();
System.out.println("请输入更新的出版社:");
book[index][3]=sc.next();
System.out.println("请输入更新的作者:");
book[index][4]=sc.next();
//显示更新后的书籍信息
searchAllBook();
}else {
System.out.println("输入的isbn码有误!");
}
}
private static void searchAllBook() {
// 显示所有的书籍信息
System.out.println("编码"+"\t"+"书名"+"\t"+"价格"+"\t"+"出版社"+"\t"+"作者");
for (int i = 0; i < book.length; i++) {
if (book[i][0] != null) {
for (int j = 0; j < book[i].length; j++) {
System.out.print(book[i][j]+"\t");
}
System.out.println();
}
}
}
private static void BookAdd() {
searchAllBook();
// 书籍添加
System.out.println("请输入图书ISBN:");
String isbn = sc.next();
System.out.println("请输入图书名:");
String bookName = sc.next();
System.out.println("请输入价格");
String bookPrice = sc.next();
System.out.println("请输入出版社");
String pubCom = sc.next();
System.out.println("请输入作者");
String author = sc.next();
//判断是否添加成功
boolean b = insertBook(isbn,bookName,bookPrice,pubCom,author);
if (b) {
System.out.println("添加成功!");
}
else {
System.out.println("添加失败,该书已存在!");
}
searchAllBook();
}
private static boolean deleteBook(String isbn) {
// 进行书籍删除操作,判断删除是否成功
boolean b = false;
int index=getindexByISBN(isbn);
if (index!=-1) {
for (int i = 0; i < book.length-1; i++) {
book[i]=book[i+1];
if (book[index+1] == null) {
break;
}
}
if (book[book.length-1][0]!=null) {
book[book.length-1][0] = null;
book[book.length-1][1] = null;
book[book.length-1][2] = null;
book[book.length-1][3] = null;
book[book.length-1][4] = null;
}
b=true;
}
else {
b=false;
}
return b;
}
private static boolean insertBook(String isbn, String bookName, String bookPrice, String pubCom, String author) {
// 进行书籍添加操作,判断添加书籍是否存在
boolean b = false;
int i=getindexByISBN(isbn);
if (i==-1) {
//找到空位置下标
int index = getBookIndexFirstNull();
if (index == -1) {
System.out.println("书籍数组已满,没有位置插入");
}
//赋值
book[index][0]=isbn;
book[index][1]=bookName;
book[index][2]=bookPrice;
book[index][3]=pubCom;
book[index][4]=author;
b=true;
}
return b;
}
private static int getBookIndexFirstNull() {
// 找到首个空位置下标
int index=-1;
for (int i = 0; i < book.length; i++) {
if (book[i][0] == null) {
index = i;
break;
}
}
return index;
}
private static int getindexByISBN(String isbn) {
//查询ISBN号下标,默认不存在为-1
int index = -1;
for (int i = 0; i < book.length; i++) {
if (isbn.equals(book[i][0])) {
index=i;
break;
}
}
return index;
}
private static boolean login(String userName, String userPwd) {
// 查找是否存在该用户名和密码是否正确
boolean a=false;
for (int i = 0; i < user.length; i++) {
if (userName.equals(user[i][1]) && userPwd.equals(user[i][2])) {
a = true;
break;
}
}
return a;
}
//数据初始化
private static void init() {
// 初始化
user[0][0] = "HP001";
user[0][1] = "ken";
user[0][2] = "123";
user[0][3] = "admin";
user[1][0] = "HP002";
user[1][1] = "ryu";
user[1][2] = "123";
user[1][3] = "user";
user[2][0] = "HP003";
user[2][1] = "guile";
user[2][2] = "123";
user[2][3] = "user";
book[0][0] = "001";
book[0][1] = "JAVA高级编程";
book[0][2] = "100";
book[0][3] = "铁道部出版社";
book[0][4] = "张三";
book[1][0] = "002";
book[1][1] = "SQLSERVER高级编程";
book[1][2] = "150";
book[1][3] = "清华出版社";
book[1][4] = "李四";
book[2][0] = "003";
book[2][1] = "JAVA基础入门";
book[2][2] = "50";
book[2][3] = "铁道部出版社";
book[2][4] = "张三";
book[3][0] = "004";
book[3][1] = "ORACLE高级编程";
book[3][2] = "120";
book[3][3] = "清华出版社";
book[3][4] = "王五";
pubCom[0][0] = "清华出版社";
pubCom[0][1] = "武汉市XXXX";
pubCom[0][2] = "aaa";
pubCom[1][0] = "铁道出版社";
pubCom[1][1] = "北京市XXXX";
pubCom[1][2] = "bbb";
pubCom[2][0] = "邮电出版社";
pubCom[2][1] = "南京市XXXX";
pubCom[2][2] = "ccc";
}
}
大概就这样完结了,之后学习C#了,以后再继续深入研究java