package com.youjiuye.bms;
import com.youjiuye.service.Tool;
public class Bms {
public static void main(String[] args) {
Tool.Wellcome();
Tool.systemOperation();
}
}
package com.youjiuye.service;
import com.youjiuye.bean.Book;
public class Service {
public Book[] getBooksByType(String type, Book[] books) {
Book[] books1 = new Book[20];
int index = 0;
for (int i = 0; i < books.length; i++) {
if (books[i] != null && books[i].getType() == type) {
books1[index++] = books[i];
}
}
return books1;
}
public boolean showBooks(Book[] books) {
boolean bo = false;
for (int i = 0; i < books.length; i++) {
if (books[i] != null) {
System.out.println(books[i]);
bo = true;
}
}
return bo;
}
public void borrowBook(Book book, Book[] lbooks, Book[] userBooks) {
if (book != null) {
for (int i = 0; i < lbooks.length; i++) {
if (lbooks[i] != null) {
String id1 = lbooks[i].getId();
String id2 = book.getId();
if (id1.equals(id2)) {
lbooks[i] = null;
System.out.println("借阅成功!");
break;
}
}
}
for (int i = 0; i < userBooks.length; i++) {
if (userBooks[i] == null) {
userBooks[i] = book;
break;
}
}
}
}
public void returnBook(Book book, Book[] Lbooks, Book[] userBooks) {
if (book != null) {
for (int i = 0; i < Lbooks.length; i++) {
if (Lbooks[i] == null) {
Lbooks[i] = book;
break;
}
}
for (int i = 0; i < userBooks.length; i++) {
if (userBooks[i] != null && userBooks[i].getId().equals(book.getId())) {
userBooks[i] = null;
}
}
}
}
public boolean changeBookType(Book book, String type, Book[] books) {
boolean bo = false;
if (book != null) {
for (int i = 0; i < books.length; i++) {
if (books[i] != null) {
if (books[i].getId().equals(book.getId())) {
books[i].setType(type);
break;
}
}
}
}
return bo;
}
public void addBook(Book[] books, Book book) {
if (book != null) {
for (int i = 0; i < books.length; i++) {
if (books[i] == null) {
books[i] = book;
System.out.println("添加成功!");
break;
}
}
}
}
public Book getBook(String id, Book[] books) {
Book bo = null;
for (int i = 0; i < books.length; i++) {
if (books[i] != null && books[i].getId().equals(id)) {
bo = books[i];
}
}
return bo;
}
public boolean judegIsEmpty(Book[] b) {
boolean bo = false;
for (int i = 0; i < b.length; i++) {
if (b[i] != null) {
bo = true;
}
}
return bo;
}
}
package com.youjiuye.service;
import java.util.Scanner;
import com.youjiuye.bean.Book;
import com.youjiuye.bean.Library;
import com.youjiuye.bean.Users;
public class Tool {
public static void Wellcome() {
System.out.println("***********************************");
System.out.println("\t欢迎来到何老板图书馆 ");
System.out.println("***********************************");
}
public static Users inputIdentityInfo(Users u) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = sc.next();
System.out.println("请选择您的身份(用户/管理员):");
String identity = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
if (identityCheck(identity, password)) {
System.out.println("登录成功!");
u = new Users(name, identity);
System.out.println("欢迎 " + identity + " " + name + " 来到系统");
hint(identity);
} else {
System.out.println("您输入的信息有误,请重新输入!");
u = inputIdentityInfo(u);
}
return u;
}
public static boolean identityCheck(String name, String password) {
boolean bo = false;
if (name.equals("用户")) {
if (password.equals("123")) {
bo = true;
}
} else if (name.equals("管理员")) {
if (password.equals("111")) {
bo = true;
}
}
return bo;
}
public static void hint(String identity) {
System.out.println("请选择您想要的服务");
if (identity.equals("用户")) {
System.out.println("1、借书\t2、查看已借书籍\t3、还书");
} else if (identity.equals("管理员")) {
System.out.println("1、添加书籍\t2、修改书籍信息");
}
}
public static int judgeChoseOperation() {
Scanner sc = new Scanner(System.in);
int chose = sc.nextInt();
return chose;
}
public static boolean borrow(Service s, Library l, Users u) {
boolean bo = true;
while (bo) {
System.out.println("当前图书馆中藏书:");
s.showBooks(l.getBooks());
System.out.println("请输入要借阅的图书编号:");
Scanner sc = new Scanner(System.in);
String bookid = sc.next();
Book boo = s.getBook(bookid, l.getBooks());
s.borrowBook(boo, l.getBooks(), u.getBos());
System.out.println("当前已成功借阅书籍:");
s.showBooks(u.getBos());
System.out.println("是否继续借阅书籍(y/n):");
boolean b = false;
bo = judgeOperationCon(b);
}
return bo;
}
public static void systemOperation() {
Users u0 = null;
Users u = inputIdentityInfo(u0);
if (u.getIdentity().equals("用户")) {
int chose = judgeChoseOperation();
userOperationDetail(u, chose);
} else {
int chose = judgeChoseOperation();
adminOperationDetail(u, chose);
}
}
public static void adminOperationDetail(Users u, int chose) {
Service s = new Service();
Library l = new Library("和老板图书馆");
switch (chose) {
case 1:
addBook(s, l);
System.out.println("是否进行其他操作(y/n):");
boolean boo = false;
if (judgeOperationCon(boo)) {
hint(u.getIdentity());
adminOperationDetail(u, judgeChoseOperation());
}
break;
case 2:
modifyBook(s, l);
System.out.println("是否进行其他操作(y/n):");
boolean booo = false;
if (judgeOperationCon(booo)) {
hint(u.getIdentity());
adminOperationDetail(u, judgeChoseOperation());
}
break;
default:
System.out.println("进行正确 的输入!");
System.out.println("1、添加书籍\t2、修改书籍信息");
int rchose = judgeChoseOperation();
adminOperationDetail(u, rchose);
break;
}
}
public static void addBook(Service s, Library l) {
boolean b = true;
while (b) {
System.out.println("当前图书馆中藏书:");
s.showBooks(l.getBooks());
System.out.println("请输入要还书的编号:");
Scanner sc = new Scanner(System.in);
String bookid = sc.next();
System.out.println("请输入要还书的名字:");
String bookName = sc.next();
System.out.println("请输入要还书的作者:");
String bookAuthor = sc.next();
System.out.println("请输入要还书的类型:");
String bookType = sc.next();
Book book = new Book(bookid, bookName, bookType, bookAuthor);
s.addBook(l.getBooks(), book);
System.out.println("当前图书馆中的图书:");
s.showBooks(l.getBooks());
System.out.println("是否继续添加书籍(y/n):");
boolean bo = false;
b = judgeOperationCon(bo);
}
}
public static void modifyBook(Service s, Library l) {
boolean b = true;
while (b) {
System.out.println("当前图书馆中藏书:");
s.showBooks(l.getBooks());
System.out.println("请输入要修改的图书的编号:");
Scanner sc = new Scanner(System.in);
String bookid = sc.next();
Book book = s.getBook(bookid, l.getBooks());
if (book != null) {
s.addBook(l.getBooks(), book);
System.out.println("当前图书馆中的图书:");
s.showBooks(l.getBooks());
System.out.println("是否继续添加书籍(y/n):");
boolean bo = false;
b = judgeOperationCon(bo);
} else {
System.out.println("对不起,当前书库中没有该图书");
System.out.println("是否继续修改(y/n):");
boolean bo = false;
b = judgeOperationCon(bo);
}
}
}
public static void userOperationDetail(Users u, int chose) {
Service s = new Service();
Library l = new Library("和老板图书馆");
switch (chose) {
case 1:
if (!borrow(s, l, u)) {
System.out.println("是否进行其他操作(y/n):");
boolean bo = false;
if (judgeOperationCon(bo)) {
hint(u.getIdentity());
userOperationDetail(u, judgeChoseOperation());
}
}
break;
case 2:
if (!s.showBooks(u.getBos())) {
System.out.println("您当前还没有已借阅的书籍,请选择其他操作");
hint(u.getIdentity());
userOperationDetail(u, judgeChoseOperation());
} else {
System.out.println("是否进行其他操作(y/n):");
boolean bo = false;
if (judgeOperationCon(bo)) {
hint(u.getIdentity());
userOperationDetail(u, judgeChoseOperation());
}
}
break;
case 3:
returnBook(u, s, l);
break;
default:
System.out.println("进行正确 的输入!");
System.out.println("1、借书\t2、查看已借书籍\t3、还书");
int rchose = judgeChoseOperation();
userOperationDetail(u, rchose);
break;
}
}
private static void returnBook(Users u, Service s, Library l) {
if (s.judegIsEmpty(u.getBos())) {
System.out.println("已借阅的书籍:");
s.showBooks(u.getBos());
System.out.println("请输入要还书的编号:");
Scanner sc = new Scanner(System.in);
String bookid = sc.next();
Book book = s.getBook(bookid, u.getBos());
s.returnBook(book, l.getBooks(), u.getBos());
System.out.println("还书成功");
if (s.judegIsEmpty(u.getBos())) {
System.out.println("是否继续还书(y/n):");
boolean b = false;
if (judgeOperationCon(b)) {
returnBook(u, s, l);
} else {
System.out.println("是否进行其他操作(y/n):");
boolean bo = false;
if (judgeOperationCon(bo)) {
hint(u.getIdentity());
userOperationDetail(u, judgeChoseOperation());
}
}
} else {
System.out.println("您已没有已借阅的图书了,是否选择其他操作(y/n):");
boolean bo = false;
if (judgeOperationCon(bo)) {
hint(u.getIdentity());
userOperationDetail(u, judgeChoseOperation());
}
}
} else {
System.out.println("您目前还没有已借阅的书籍,请选择其他操作!");
hint(u.getIdentity());
userOperationDetail(u, judgeChoseOperation());
}
}
public static boolean judgeOperationCon(boolean b) {
String yn = "";
Scanner sc = new Scanner(System.in);
yn = sc.next();
if (yn.equals("y")) {
b = true;
} else if (yn.equals("n")) {
b = false;
} else {
System.out.println("请进行正确的输入!");
judgeOperationCon(b);
}
return b;
}
}
package com.youjiuye.bean;
public class Book {
private String id;
private String bookName;
private String author;
private String type;
public Book(String id) {
this.id = id;
}
public Book(String id, String bookName, String type, String author) {
this.id = id;
this.bookName = bookName;
this.type = type;
this.author = author;
}
@Override
public String toString() {
return "[ 图书编号 =" + getId() + "\t 书名=" + getBookName() + "\t 作者=" + getAuthor() + "\t 类型=" + getType() + " ]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package com.youjiuye.bean;
public class Library {
private String name;
private static Book[] books = new Book[20];
static {
books[0] = new Book("001", "java", "编程类", "和老板");
books[1] = new Book("002", "html", "编程类", "江南");
books[2] = new Book("003", "流浪地球", "科幻类", "爪哇");
books[3] = new Book("004", "天龙八部", "武侠类", "金庸");
books[4] = new Book("005", "一千零一夜", "儿童类", "安徒生");
books[5] = new Book("006", "霸道总裁", "言情类", "萌妹");
books[6] = new Book("007", "射雕英雄传", "武侠类", "金庸");
}
public Library(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book[] getBooks() {
return books;
}
}
package com.youjiuye.bean;
public class Users {
private String name;
private String identity;
private String password;
private Book[] bos = new Book[10];
public Users() {
}
public Users(String name, String identity) {
this.name = name;
this.identity = identity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Book[] getBos() {
return bos;
}
public void setBos(Book[] bos) {
this.bos = bos;
}
@Override
public String toString() {
return "Users [name=" + getName() + "identity=" + getIdentity() + ", password=" + getPassword() + "]";
}
}