前言该我的项目外围需要:
实现简略登录
管理员端实现下列性能①查找书籍②减少书籍③删除书籍④展现全副书籍⑤退出零碎
通用户实现下列性能①查问书籍②借阅书籍③偿还书籍④退出零碎
我的项目类的设计展现
图书相干的类Book:定义书籍的信息 BookList:示意书库,外面寄存书籍
package book;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:54 */public class Book { //定义成员变量 private String name; private String author; private int price; private String type; private boolean isBorrowed = false; //示意书的借阅状态 public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type;// this.isBorrowed = isBorrowed; } public String getName() { return name; } public String getAuthor() { return author; } public int getPrice() { return price; } public String getType() { return type; } public boolean isBorrowed() { return isBorrowed; } public void setName(String name) { this.name = name; } public void setAuthor(String author) { this.author = author; } public void setPrice(int price) { this.price = price; } public void setType(String type) { this.type = type; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; }// @Override// public String toString() {// return "Book{" +// "name='" + name + '\'' +// ", author='" + author + '\'' +// ", price=" + price +// ", type='" + type + '\'' +// ", isBorrowed=" + isBorrowed +// '}';// } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ((isBorrowed == true) ? " 借阅状态: 已借出" : " 借阅状态: 未借出") + '}'; }}package book;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:54 */public class BookList { public int usedSize = 3; //书架 public Book[] books = new Book[10]; //书的类型为Book,用顺数组book去存储 public BookList() { books[0] = new Book("三国演义","罗贯中", 100, "小说"); books[1] = new Book("水浒传", "施耐庵", 100, "小说"); books[2] = new Book("西游记", "吴承恩", 100, "小说"); } //给指定地位放书 public void setBooks(int pos,Book book) { this.books[pos] = book; } //拿到指定地位的书 public Book getBooks(int pos){ return this.books[pos]; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; }}对书库(程序表)操作的类//新增public class AddOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("增加书籍"); Scanner sc = new Scanner(System.in); System.out.println("请输出书名"); String name = sc.nextLine(); System.out.println("请输出书的作者"); String author = sc.nextLine(); System.out.println("请输出书的价格"); int price = sc.nextInt(); System.out.println("请输出书的类型"); String type = sc.next(); Book newBook = new Book(name,author,price,type); //构建新书(对象) int size = booklist.getUsedSize(); //通过bookList援用拜访以后程序表长度 booklist.setBooks(size,newBook); //将新书放在程序表最初面 booklist.setUsedSize(size+1); //程序表放了新书之后,长度加1 }}//借阅public class BorrowOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("借阅书籍"); Scanner sc = new Scanner(System.in); System.out.println("请输出书名"); String name = sc.nextLine(); //name为所要借阅书名 for (int i = 0; i < booklist.getUsedSize(); i++) { //通过booklist下标遍历每一本书 Book book = booklist.getBooks(i); if(book.getName().equals(name)){ //如果为true,阐明要借阅的书存在,咱们须要做的是批改书的借阅状态 book.setBorrowed(true); //为true示意书曾经结出 return ; } } System.out.println("十分道歉,本馆没有您要借阅的书!"); // }}//删除public class DelOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("删除书籍"); Scanner sc = new Scanner(System.in); System.out.println("请输出书名"); String name = sc.nextLine(); //name为所要删除的书名 int i = 0; for (; i < booklist.getUsedSize(); i++) { Book book = booklist.getBooks(i); if(book.getName().equals(name)){ break; } } if(i >= booklist.getUsedSize()){ System.out.println("没有要删除的这本书!"); return ; } //此时i为所要删除书的下标 for (int j = i; j < booklist.getUsedSize()-1; j++) { Book book = booklist.getBooks(j+1); //取得j+1地位的书 booklist.setBooks(j,book); //将j+1地位的书给j地位 } int size = booklist.getUsedSize(); //取得程序表长度 booklist.setUsedSize(size-1); //删除书后,长度减去1 System.out.println("书已被删除!"); }}//展现public class DisplayOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("展现书籍"); for (int i = 0; i < booklist.getUsedSize(); i++) { Book book = booklist.getBooks(i); System.out.println(book); } }}//查找public class FindOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("查找书籍"); Scanner sc = new Scanner(System.in); System.out.println("请输出书名"); String name = sc.nextLine(); //name为所要查找书名 for (int i = 0; i < booklist.getUsedSize(); i++) { //通过booklist下标遍历每一本书 Book book = booklist.getBooks(i); if(book.getName().equals(name)){ System.out.println("该书存在!"); System.out.println(book); //间接打印书的信息,toString办法已被重写 return ; } } System.out.println("没有这本书!"); }}//偿还public class ReturnOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("偿还书籍"); Scanner sc = new Scanner(System.in); System.out.println("请输出书名"); String name = sc.nextLine(); //name为所要偿还的书名 for (int i = 0; i < booklist.getUsedSize(); i++) { Book book = booklist.getBooks(i); if(book.getName().equals(name)){ book.setBorrowed(false); System.out.println(book); //间接打印书的信息,toString办法已被重写 return ; } } System.out.println("没有你要偿还的这本书!"); }}//退出public class ExitOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("退出零碎"); System.exit(1); //示意退出零碎 }}//总接口public interface IOperation { void work(BookList booklist);}用户相干类<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">用户类</mark>
...