import book.BookList; 2 import user.AdminUser; 3 import user.NormalUser; 4 import user.User; 5 6 import java.util.Scanner; 7 8 /** 9 * Created with IntelliJ IDEA. 10 * Description: 11 * User: WHY 12 * Date: 2022-06-24 13 * Time: 19:40 14 */ 15 public class Main { 16 public static User login(){ 17 System.out.println("请输入您的姓名"); 18 Scanner scanner=new Scanner(System.in); 19 String name= scanner.nextLine(); 20 21 System.out.println("请输入您的身份:1:-》管理员,0:—》普通用户"); 22 int choice=scanner.nextInt(); 23 if(choice==1){ 24 return new AdminUser(name); 25 } 26 else{ 27 return new NormalUser(name); 28 } 29 } 30 public static void main(String[] args) { 31 //开始整合 32 BookList booklist=new BookList();//准备图书 33 34 //登录 35 User user=login();//相当于向上转型 36 user.menu(); 37 int choice = user.menu();//动态绑定 38 user.doOperation(choice,booklist); 39 } 40 } +74 2022.6.24/2022.6.24/src/book/Book.java 0 → 100644 1 package book; 2 3 /** 4 * Created with IntelliJ IDEA. 5 * Description: 6 * User: WHY 7 * Date: 2022-06-24 8 * Time: 18:00 9 */ 10 public class Book { 11 private String name; 12 private String author; 13 private int price; 14 private String type; 15 private boolean isBorrowed; 16 17 public Book(String name, String author, int price, String type) { 18 this.name = name; 19 this.author = author; 20 this.price = price; 21 this.type = type; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public String getAuthor() { 33 return author; 34 } 35 36 public void setAuthor(String author) { 37 this.author = author; 38 } 39 40 public int getPrice() { 41 return price; 42 } 43 44 public void setPrice(int price) { 45 this.price = price; 46 } 47 48 public String getType() { 49 return type; 50 } 51 52 public void setType(String type) { 53 this.type = type; 54 } 55 56 public boolean isBorrowed() { 57 return isBorrowed; 58 } 59 60 public void setBorrowed(boolean borrowed) { 61 isBorrowed = borrowed; 62 } 63 64 @Override 65 public String toString() { 66 return "Book{" + 67 "name='" + name + '\'' + 68 ", author='" + author + '\'' + 69 ", price=" + price + 70 ", type='" + type + '\'' + 71 ", isBorrowed=" + isBorrowed + 72 '}'; 73 } 74 } +49 2022.6.24/2022.6.24/src/book/BookList.java 0 → 100644 1 package book; 2 3 /** 4 * Created with IntelliJ IDEA. 5 * Description: 6 * User: WHY 7 * Date: 2022-06-24 8 * Time: 18:01 9 */ 10 public class BookList { 11 12 private Book[] books=new Book[20]; 13 private int usedSize;//实时记录当前books这个数组当中有多少本书 14 15 16 public BookList(){ 17 books[0]=new Book("白夜行","东野圭吾",60,"悬疑推理"); 18 books[1]=new Book("嫌疑人X的献身","东野圭吾",80,"悬疑推理"); 19 books[2]=new Book("彷徨之刃","东野圭吾",120,"悬疑推理"); 20 usedSize=3; 21 22 } 23 //根据书的位置,拿到这本书 24 public Book getBook(int pos){ 25 return books[pos]; 26 } 27 /* 28 * 新增书,POS是要放的位置 29 * book是你要放的书 30 * */ 31 public void setBooks( int pos , Book book){ 32 books[pos]=book; 33 } 34 /* 35 * 36 * 实时获取当前的书的个数 37 * 38 * */ 39 public int getUsedSize(){ 40 return usedSize; 41 } 42 /* 43 * 实时修改当前书架上的书的个数 44 * 45 * */ 46 public void setUsedSize(int size){ 47 usedSize=size; 48 } 49 } +17 2022.6.24/2022.6.24/src/operation/AddOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 18:54 11 */ 12 public class AddOperation implements IOperation { 13 @Override 14 public void work(BookList booklist) { 15 System.out.println("新增图书"); 16 } 17 } +17 2022.6.24/2022.6.24/src/operation/BorrowOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 19:01 11 */ 12 public class BorrowOperation implements IOperation{ 13 @Override 14 public void work(BookList booklist) { 15 System.out.println("借阅图书"); 16 } 17 } +17 2022.6.24/2022.6.24/src/operation/DelOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 18:57 11 */ 12 public class DelOperation implements IOperation{ 13 @Override 14 public void work(BookList booklist) { 15 System.out.println("删除图书"); 16 } 17 } +17 2022.6.24/2022.6.24/src/operation/ExitOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 19:00 11 */ 12 public class ExitOperation implements IOperation{ 13 @Override 14 public void work(BookList booklist) { 15 System.out.println("退出系统"); 16 } 17 } +17 2022.6.24/2022.6.24/src/operation/FindOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 18:56 11 */ 12 public class FindOperation implements IOperation { 13 @Override 14 public void work(BookList booklist) { 15 System.out.println("查找图书"); 16 } 17 } +15 2022.6.24/2022.6.24/src/operation/IOperation.java 0 → 100644 1 package operation; 2 3 import book.Book; 4 import book.BookList; 5 6 /** 7 * Created with IntelliJ IDEA. 8 * Description: 9 * User: WHY 10 * Date: 2022-06-24 11 * Time: 18:48 12 */ 13 public interface IOperation { 14 void work(BookList booklist); 15 } +19 2022.6.24/2022.6.24/src/operation/ReturnOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 19:02 11 */ 12 public class ReturnOperation implements IOperation{ 13 ; 14 15 @Override 16 public void work(BookList booklist) { 17 System.out.println("归还图书"); 18 } 19 } +17 2022.6.24/2022.6.24/src/operation/ShowOperation.java 0 → 100644 1 package operation; 2 3 import book.BookList; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * Description: 8 * User: WHY 9 * Date: 2022-06-24 10 * Time: 18:59 11 */ 12 public class ShowOperation implements IOperation{ 13 @Override 14 public void work(BookList booklist) { 15 System.out.println("显示图书"); 16 } 17 } +43 2022.6.24/2022.6.24/src/user/AdminUser.java 0 → 100644 1 package user; 2 3 import operation.*; 4 5 import java.util.Scanner; 6 7 /** 8 * Created with IntelliJ IDEA. 9 * Description: 10 * User: WHY 11 * Date: 2022-06-24 12 * Time: 19:17 13 */ 14 public class AdminUser extends User{ 15 public AdminUser(String name) { 16 super(name); 17 this.iOperations = new IOperation[]{ 18 new ExitOperation(), 19 new FindOperation(), 20 new AddOperation(), 21 new DelOperation(), 22 new ShowOperation(), 23 24 25 26 }; 27 } 28 public int menu(){ 29 System.out.println("hello"+this.name+"欢迎您来到图书小练习!🎉"); 30 System.out.println("1.查找图书!"); 31 System.out.println("2.新增图书!"); 32 System.out.println("3.删除图书!"); 33 System.out.println("4.显示图书!"); 34 System.out.println("0.退出系统!"); 35 System.out.println("请输入您的操作😁"); 36 Scanner scanner=new Scanner(System.in); 37 int choice = scanner.nextInt(); 38 return choice; 39 40 41 } 42 43 } +40 2022.6.24/2022.6.24/src/user/NormalUser.java 0 → 100644 1 package user; 2 3 import operation.*; 4 5 import java.util.Scanner; 6 7 /** 8 * Created with IntelliJ IDEA. 9 * Description: 10 * User: WHY 11 * Date: 2022-06-24 12 * Time: 19:16 13 */ 14 public class NormalUser extends User { 15 public NormalUser(String name) { 16 super(name); 17 this.iOperations = new IOperation[]{ 18 new ExitOperation(), 19 new FindOperation(), 20 new BorrowOperation(), 21 new ReturnOperation(), 22 23 24 25 }; 26 } 27 public int menu(){ 28 System.out.println("hello"+this.name+"欢迎您来到图书小练习!🎉"); 29 System.out.println("1.查找图书!"); 30 System.out.println("2.借阅图书!"); 31 System.out.println("3.归还图书!"); 32 System.out.println("0.退出系统!"); 33 System.out.println("请输入您的操作😁"); 34 Scanner scanner=new Scanner(System.in); 35 int choice = scanner.nextInt(); 36 return choice; 37 38 39 } 40 } +27 2022.6.24/2022.6.24/src/user/User.java 0 → 100644 1 package user; 2 3 import book.BookList; 4 import operation.IOperation; 5 6 /** 7 * Created with IntelliJ IDEA. 8 * Description: 9 * User: WHY 10 * Date: 2022-06-24 11 * Time: 19:06 12 */ 13 abstract public class User { 14 protected String name;//用户名 15 16 public User(String name) { 17 this.name = name; 18 } 19 public abstract int menu(); 20 protected IOperation[] iOperations ;// 21 public void doOperation(int choice, BookList booklist){ 22 this.iOperations[choice].work(booklist); 23 } 24 25 26 27 } package operation; 2 2 3 import book.Book; 3 4 import book.BookList; 4 5 6 import java.util.Scanner; 7 5 8 /** 6 9 * Created with IntelliJ IDEA. 7 10 * Description: @@ -13,5 +16,30 @@ public class DelOperation implements IOperation{ 13 16 @Override 14 17 public void work(BookList booklist) { 15 18 System.out.println("删除图书"); 19 Scanner scanner=new Scanner(System.in); 20 System.out.println("请输入删除图书的名字:"); 21 String name= scanner.nextLine(); 22 int currentSize= booklist.getUsedSize(); 23 int index=-1; 24 int i =0; 25 for (; i <currentSize ; i++) { 26 Book book=booklist.getBook(i); 27 if(book.getName().equals(name)){ 28 index=i; 29 break; 30 } 31 } 32 33 if(i>=currentSize){ 34 System.out.println("没有找到这本书"); 35 return; 36 } 37 for(int j=index;j<currentSize ;j++){ 38 Book book=booklist.getBook(i+1); 39 booklist.setBooks(i,book); 40 } 41 booklist.setBooks( currentSize-1,null);//删除了书籍,进行了移动,然后将最后的置为空,不会有内存泄漏 42 booklist.setUsedSize(currentSize-1); 43 System.out.println("删除成功"); 16 44 } 17 45 }
以上是图书管理系统的实现,具体的思路我们下期再说奥