From 36fc52c7f1db87194d729720302499b41b5e9187 Mon Sep 17 00:00:00 2001 From: "@luoshitian" <908631923@qq.com> Date: Wed, 28 Apr 2021 10:01:10 +0800 Subject: [PATCH] first commit --- .../BookManager.java" | 699 ++++++++++++++++++ 1 file changed, 699 insertions(+) create mode 100644 "Java\351\241\271\347\233\256_\347\274\226\350\257\221\347\211\210/\347\254\2547\345\260\217\347\273\204/\347\275\227\344\273\225\345\244\251/BookManager.java" diff --git "a/Java\351\241\271\347\233\256_\347\274\226\350\257\221\347\211\210/\347\254\2547\345\260\217\347\273\204/\347\275\227\344\273\225\345\244\251/BookManager.java" "b/Java\351\241\271\347\233\256_\347\274\226\350\257\221\347\211\210/\347\254\2547\345\260\217\347\273\204/\347\275\227\344\273\225\345\244\251/BookManager.java" new file mode 100644 index 0000000..c73b1d0 --- /dev/null +++ "b/Java\351\241\271\347\233\256_\347\274\226\350\257\221\347\211\210/\347\254\2547\345\260\217\347\273\204/\347\275\227\344\273\225\345\244\251/BookManager.java" @@ -0,0 +1,699 @@ + + +import java.util.Scanner; + +public class BookManager { + static Scanner sc = new Scanner(System.in); + static String[][] users = new String[20][4]; + static String[][] books = new String[20][5]; + static String[][] pubs = new String[20][3]; + static String loginUser = ""; + + public static void main(String[] args) { + init(); + index(); + } + + public static boolean isEmpty(String str) { + return str==null || str.trim().length()==0; + } + + private static void index() { + while (true) { + System.out.println("欢迎来到闽大图书管理系统!"); + System.out.println("1.登录 2.注册"); + int logOrReg = sc.nextInt(); + switch (logOrReg) { + case 1: + login(); + break; + case 2: + register(); + break; + default: + System.out.println("系统退出!"); + System.exit(0); + break; + } + } + } + + private static void register() { + System.out.println("请输入所属部门:"); + String deptName = sc.next(); + System.out.println("请输入用户名:"); + String userName = sc.next(); + System.out.println("请输入密码:"); + String passWord = sc.next(); + for (int i = 0; i < users.length; i++) { + if (users[i][1] == null) { + users[i][0] = deptName; + users[i][1] = userName; + users[i][2] = passWord; + users[i][3] = "普通用户"; + System.out.println("注册成功!"); + break; + } + } + } + + private static boolean login() { + boolean logFlag = false; + while (!logFlag) { + System.out.println("请输入用户名:"); + String userName = sc.next(); + System.out.println("请输入密码:"); + String passWord = sc.next(); + if (!isEmpty(userName) && !isEmpty(passWord)) { + for (int i = 0; i < users.length; i++) { + if (userName.equals(users[i][1]) && passWord.equals(users[i][2])) { + System.out.println("登录成功!"); + loginUser = userName; + homePage(); + } + } + System.out.println("输入错误,请重新输入!"); + } + } + + return false; + } + + private static void homePage() { + System.out.println(loginUser + ", 欢迎您使用闽大书籍管理系统!!!"); + while (true) { + System.out.println("请输入数字进行选择:1 图书管理 2 出版社管理 3 退出登录 4 退出系统"); + int key = sc.nextInt(); + switch (key) { + case 1: + bookManage(); + break; + case 2: + pubManage(); + break; + case 3: + loginUser = ""; + System.out.println("已退出登录!"); + index(); + break; + case 4: + System.out.println("系统退出成功!"); + System.exit(0); + break; + + default: + break; + } + + } + } + + private static void pubManage() { + System.out.println("请输入:1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单"); + int key = sc.nextInt(); + switch (key) { + case 1: + addPub(); + break; + case 2: + delPub(); + break; + case 3: + updatePub(); + break; + case 4: + searchPubByName(); + break; + case 5: + // 查询所有出版社信息 + searchAllPubCom(); + break; + case 6: + homePage(); + break; + default: + break; + } + } + + // 查询所有出版社信息 + static void searchAllPubCom() { + System.out.println("出版社名称\t地址 \t联系人\t"); + for (int i = 0; i < pubs.length; i++) { + if (pubs[i][0] != null) { + System.out.println(pubs[i][0] + "\t" + pubs[i][1] + "\t" + pubs[i][2] + "\t"); + } + } + } + + private static void searchPubByName() { + System.out.println("请输入出版社名称:"); + String pcName = sc.next(); + int index = getPubIndex(pcName); + if (index != -1) { + System.out.println("出版社名称:" + pubs[index][0]); + System.out.println("出版社地址:" + pubs[index][1]); + System.out.println("出版社联系人:" + pubs[index][2]); + } else { + System.out.println("该出版社不存在!!!"); + } + + } + + private static void updatePub() { + // 更新出版社信息 + System.out.println("请输入要更新的出版社名称:"); + String pubName = sc.next(); + int index = getPubIndex(pubName); + // 根据下标打印 + printPubCom(index); + // 接受用户输入新的信息(因为没有设置id,用name当主键,所以出版社名称不能改) + System.out.println("请输入要更新的地址:"); + String pcAddress = sc.next(); + System.out.println("请输入要更新的联系人姓名:"); + String pcPerson = sc.next(); + // 调用更新函数 + boolean b = updatePubCom(index, pcAddress, pcPerson); + if (b) { + System.out.println("更新成功"); + // 更新成功后打印最新数据 + printPubCom(index); + } else { + System.out.println("更新失败!!!"); + } + + } + + // 修改某下标的出版社信息(不含出版社名称) + static boolean updatePubCom(int index, String pcAddress, String pcPerson) { + boolean b = false; + + pubs[index][1] = pcAddress; + pubs[index][2] = pcPerson; + b = true; + + return b; + } + + private static void delPub() { + System.out.println("请输入要删除的出版社名称:"); + String pubName = sc.next(); + + boolean boo = delByPCName(pubName); + + if (boo) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + + } + + private static void addPub() { + System.out.println("请输入出版社名称:"); + String pcName = sc.next(); + System.out.println("请输入出版社地址:"); + String pcAddress = sc.next(); + System.out.println("请输入出版社联系人:"); + String pcPerson = sc.next(); + // b为false表示已经存在,所以添加失败 + boolean b = insertPC(pcName, pcAddress, pcPerson); + if (b) { + System.out.println("出版社添加成功"); + } else { + System.out.println("出版社添加失败"); + } + } + + // 根据下标打印出版社信息 + static void printPubCom(int index) { + System.out.println("出版社名称\t地址 \t联系人\t"); + System.out.print(pubs[index][0] + "\t"); + System.out.print(pubs[index][1] + "\t"); + System.out.println(pubs[index][2] + "\t"); + } + + // 出版社添加 + static boolean insertPC(String pcName, String pcAddress, String pcPerson) { + boolean b = false; + // 如果该出版社不存在 + if (getPubIndex(pcName) == -1) { + // 查找第一个null的下标 + int index = getPCIndexOfFirstNull(); + // 在null下标上完成添加操作 + pubs[index][0] = pcName; + pubs[index][1] = pcAddress; + pubs[index][2] = pcPerson; + b = true; + } + + return b; + } + + // 查找出版社数组中第一个null的下标 + static int getPCIndexOfFirstNull() { + int index = -1; + + for (int i = 0; i < pubs.length; i++) { + if (pubs[i][0] == null) { + index = i; + break; + } + } + + return index; + } + + private static void bookManage() { + while (true) { + System.out.println("请输入:1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单"); + int key = sc.nextInt(); + switch (key) { + case 1: + addBook(); + break; + case 2: + delBook(); + break; + case 3: + updateBook(); + break; + case 4: + searchBook(); + break; + case 5: + homePage(); + break; + + default: + break; + } + + } + } + + // 根据isbn查询图书 + static void searchBookByISBN(String isbn) { + System.out.println("ISBN\t书名 \t价格\t出版社 \t作者\t"); + for (int i = 0; i < books.length; i++) { + if (isbn.equals(books[i][0])) { + System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + + books[i][4] + "\t"); + // 因为isbn是唯一的,所以退出循环 + break; + } + } + } + + private static void searchBook() { + boolean y = true; + while (y) { + System.out.println("请输入查询种类:1.isbn 2.书名(模糊) 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级 "); + int h = sc.nextInt(); + if (h == 1) { + System.out.println("请输入ISBN号:"); + String isbn = sc.next(); + searchBookByISBN(isbn); + } else if (h == 2) { + System.out.println("请输入书名:"); + String bookName = sc.next(); + searchByBookName(bookName); + } else if (h == 3) { + System.out.print("请输入出版社前的数字进行选择:"); + // 打印所有的出版社 + printPC(); + int d = sc.nextInt(); + + if (d >= 1 && d <= getNumOfPC()) { // 用户输入的数字有效 + // 根据输入的整数要找到对应的出版社名称 + String pcName = pubs[d - 1][0]; + // 调用函数根据出版社名称查询 + searchBookByPC(pcName); + } else { // 用户输入的数字超出范围 + System.out.println("请选择正确的数字!!!"); + } + } else if (h == 4) { + System.out.println("请输入作者姓名:"); + String author = sc.next(); + searchBookByAuthor(author); + } else if (h == 5) { + System.out.println("请输入最低价格"); + double min = sc.nextDouble(); + System.out.println("请输入最高价格"); + double max = sc.nextDouble(); + searchBookByPrice(min, max); + } else if (h == 6) { + // 调用查询所有函数 + searchAllBook(); + } else if (h == 7) { + y = false; + } else { + System.out.println("请输入正确的数字选项!!!"); + } + } + } + + // 查询所有图书 + static void searchAllBook() { + System.out.println("ISBN\t书名 \t价格\t出版社 \t作者\t"); + for (int i = 0; i < books.length; i++) { + if (books[i][0] != null) { + System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + + books[i][4] + "\t"); + } + } + } + + // 根据价格范围查询图书 + static void searchBookByPrice(double min, double max) { + System.out.println("ISBN\t书名 \t价格\t出版社 \t作者\t"); + for (int i = 0; i < books.length; i++) { + + if (books[i][0] != null) { + double price = Double.parseDouble(books[i][2]); + if (price >= min && price <= max) { + System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + + books[i][4] + "\t"); + } + } + + } + } + + // 根据作者查询图书 + static void searchBookByAuthor(String author) { + System.out.println("\tISBN\t书名 \t价格\t出版社 \t作者\t"); + for (int i = 0; i < books.length; i++) { + if (author.equals(books[i][4])) { + System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + + "\t" + books[i][4] + "\t"); + } + } + } + + // 根据出版社查询图书 + static void searchBookByPC(String pcName) { + System.out.println("\tISBN\t书名 \t价格\t出版社 \t作者\t"); + for (int i = 0; i < books.length; i++) { + if (pcName.equals(books[i][3])) { + System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + + "\t" + books[i][4] + "\t"); + } + } + } + + // 得到出版社的个数 + static int getNumOfPC() { + int count = 0; + + for (int i = 0; i < pubs.length; i++) { + if (pubs[i][0] != null) { + count++; + } + } + + return count; + } + + // 在用户选择出版社的时候动态输出所有出版社的信息 + static void printPC() { + for (int i = 0; i < pubs.length; i++) { + if (pubs[i][0] != null) { + System.out.print(i + 1 + ". " + pubs[i][0] + " "); + } + } + System.out.println(); + } + + // 根据书名模糊查询图书 + static void searchByBookName(String bookName) { + System.out.println("\tISBN\t书名 \t价格\t出版社 \t作者\t"); + for (int i = 0; i < books.length; i++) { + if (books[i][0] != null && books[i][1].indexOf(bookName) != -1) { + System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + + "\t" + books[i][4] + "\t"); + } + } + } + + private static void updateBook() { + System.out.println("请输入ISBN号:"); + String isbn = sc.next(); + int index = getIndexByISBN(isbn); + if (index != -1) { + 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 = updateBook(index, bookName, bookPrice, pubCom, author); + if (b) { + System.out.println("更新成功!!!"); + } else { + System.out.println("更新失败"); + } + } else { + System.out.println("该ISBN号不存在!!!"); + } + } + + // 根据下标书籍更新(此处isbn号不能更新) + static boolean updateBook(int index, String bookName, String bookPrice, String pubCom, String author) { + boolean b = false; + + books[index][1] = bookName; + books[index][2] = bookPrice; + books[index][3] = pubCom; + books[index][4] = author; + + b = true; + + return b; + } + + private static void delBook() { + System.out.println("请输入要删除的书本名称:"); + String book = sc.next(); + + boolean b = delBookByName(book); + + if (b) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + } + + private static boolean delBookByName(String bookName) { + boolean flag = false; + int index = getBookIndexByName(bookName); + if (index==-1) { + System.out.println("没有找到该书!"); + }else { + books[index][0] = null; + books[index][1] = null; + books[index][2] = null; + books[index][3] = null; + books[index][4] = null; + flag = true; + } + return flag; + } + + private static int getBookIndexByName(String bookName) { + int index = -1; + for (int i = 0; i < books.length; i++) { + if(bookName.equals(books[i][1])) { + index = i; + break; + } + } + + return index; + } + + // 根据出版社名称精确查询 + static int getPubIndex(String pcName) { + int index = -1; + + for (int i = 0; i < pubs.length; i++) { + if (pcName.equals(pubs[i][0])) { + index = i; + break; + } + } + + return index; + } + + // 判断出版社是否在图书表中有关联信息 + static boolean isExistInBook(String pcName) { + boolean bool = false; // 默认不存在 + + for (int i = 0; i < books.length; i++) { + if (pcName.equals(books[i][3])) { + bool = true; + break; + } + } + + return bool; + } + + // 出版社删除 + static boolean delByPCName(String pcName) { + boolean b = false; + int index = getPubIndex(pcName); + // 如果出版社存在,并且没有关联的图书数据就删除 + if (index != -1) { + if (!isExistInBook(pcName)) { + for (int i = index; i < pubs.length - 1; i++) { + pubs[i] = pubs[i + 1];//出版社往前移 + } + b = true; + } else { + System.out.println("该出版社有相关图书信息存在!不可以删除!如果要删除请先删除所有相关图书信息!!!"); + } + + } + + return b; + } + + private static void addBook() { + 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("添加失败!!!该书已经存在!!!"); + } + + } + + // 书籍增加 + 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 = getBookIndexOfFirstNull(); + // 赋值 + books[index][0] = isbn; + books[index][1] = bookName; + books[index][2] = bookPrice; + books[index][3] = pubCom; + books[index][4] = author; + b = true; + } + return b; + } + + // 根据isbn号查询下标,默认不存在为-1 + static int getIndexByISBN(String isbn) { + int index = -1; + for (int i = 0; i < books.length; i++) { + if (isbn.equals(books[i][0])) { + // 因为isbn是唯一的,所以退出循环 + index = i; + break; + } + } + return index; + } + + // 查找书籍数组中第一个null的下标 + static int getBookIndexOfFirstNull() { + int index = -1; + + for (int i = 0; i < books.length; i++) { + if (books[i][0] == null) { + index = i; + break; + } + } + + return index; + } + + private static void init() { + // 用户信息(部门、用户名、密码、用户角色) + users[0][0] = "行政部"; + users[0][1] = "admin"; + users[0][2] = "123"; + users[0][3] = "管理员"; + + users[1][0] = "市场部"; + users[1][1] = "user01"; + users[1][2] = "123"; + users[1][3] = "普通用户"; + + users[2][0] = "软件部"; + users[2][1] = "user02"; + users[2][2] = "123"; + users[2][3] = "普通用户"; + + users[3][0] = "硬件部"; + users[3][1] = "user02"; + users[3][2] = "123"; + users[3][3] = "普通用户"; + + // 书籍信息(编码(ISBN)、书籍名称、价格、出版社、作者) + books[0][0] = "97871010"; + books[0][1] = "史记"; + books[0][2] = "125.0"; + books[0][3] = "中华书局"; + books[0][4] = "司马迁"; + + books[1][0] = "97875063"; + books[1][1] = "活着"; + books[1][2] = "20.0"; + books[1][3] = "作家出版社"; + books[1][4] = "余华"; + + books[2][0] = "97872290"; + books[2][1] = "三体全集"; + books[2][2] = "168.0"; + books[2][3] = "重庆出版社"; + books[2][4] = "刘慈欣"; + + books[3][0] = "97875442"; + books[3][1] = "白夜行"; + books[3][2] = "39.5"; + books[3][3] = "南海出版公司"; + books[3][4] = "[日]东野圭吾"; + + // 出版社信息(出版社名称、地址、联系人) + pubs[0][0] = "中华书局"; + pubs[0][1] = "北京市王府井大街36号"; + pubs[0][2] = "张三"; + + pubs[1][0] = "作家出版社"; + pubs[1][1] = "香港九龙荷李活商业中心8楼"; + pubs[1][2] = "李四"; + + pubs[2][0] = "重庆出版社"; + pubs[2][1] = "茶园新区开拓路6号"; + pubs[2][2] = "王五"; + + pubs[3][0] = "南海出版公司"; + pubs[3][1] = "海秀中路51-1号星华大厦5楼"; + pubs[3][2] = "周毅"; + + } +} -- Gitee