实现的基本功能:
登录时,需要输入姓名,然后选择作为管理者还是普通用户。选择成功后选择想要实现的功能。管理者的目录下方有有五个功能,而普通用户有4个功能,如下图
首先我们要建立Book这个类,里面包含书本的详细信息
public class Book { private String name; private String author; private int price; private String type; private boolean isLend; public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; }//并不需要将是否借出作为参数,因为每一本书在增添的时候都默认没有借出 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isLend() { return isLend; } public void setLend(boolean lend) { isLend = lend; } public String toString() { return "book{" + "书名 " + name + " " + "作者 " + author + " " + "价格 " + price + "类型 " + type + " " + "是否借出 " + (isLend?"已借出":"未借出") + '}'; } }
要设置一个书架,里面建立一个数组来放书:
private Book[] books=new Book[10]; private int usedSize; public BookList() { books[0]=new Book("三国演义","罗贯中",10,"小说"); books[1]=new Book("西游记","吴承恩",12,"小说"); books[2]=new Book("红楼梦","曹雪芹",11,"小说"); this.usedSize =3; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } public Book[] getBooks() { return books; } public void setBooks(Book[] books) { this.books = books; } public Book getBook(int pos){ return books[pos]; } public void setBook(Book book,int pos){ books[pos]=book; } public boolean isFull(){ if(this.usedSize==books.length){ return true; }else { return false; } } }
设置主函数:
public class Main { public static User login(){ System.out.println("请输入你的名字"); Scanner scanner=new Scanner(System.in); String name=scanner.nextLine(); System.out.println("请输入你的身份:1,管理员 2,普通用户"); int choice= scanner.nextInt(); if (choice==1){ return new AdminUser(name); }else { return new NormalUser(name); } } public static void main(String[] args) { BookList bookList=new BookList(); User user=login(); while(true) { int choice = user.menu(); user.doOperation(choice, bookList); } }
面对的有两类人,一类为普通用户,另一类为管理者,建立两个类,他们均继承User这个类,因为这个User这个类不用实现对象,所以为一个抽象类
public abstract class User { private String name; IOperation [] iOperations=new IOperation[]{}; public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, BookList bookList){ this.iOperations[choice].work(bookList); } }
public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations=new IOperation[]{new ExitSystem(), new FindBook(),new AddBook(),new DelBook(),new ShowBook()}; } public int menu(){ System.out.println("********管理员菜单**********"); System.out.println("1,查找图书"); System.out.println("2,新增图书"); System.out.println("3,删除图书"); System.out.println("4,显示图书"); System.out.println("0,退出系统"); System.out.println("**************************"); System.out.println("请输入你操作"); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; } }
public class NormalUser extends User { public NormalUser(String name) { super(name); this.iOperations=new IOperation[]{new ExitSystem(), new FindBook(),new LendBook(),new ReturnBook()}; } public int menu(){ System.out.println("********普通用户菜单*********"); System.out.println("1,查找图书"); System.out.println("2,借阅图书"); System.out.println("3,归还图书"); System.out.println("0,退出系统"); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; } }
实现各个功能:
public interface IOperation { void work(BookList bookList); }
新增图书
public class AddBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("新增图书"); if (bookList.isFull()==true){ System.out.println("满了不能放"); return; } System.out.println("请输入新增图书的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); System.out.println("请输入新增书的作者"); String author=scanner.nextLine(); System.out.println("请输入新增书的价格"); int price=scanner.nextInt(); scanner.nextLine(); System.out.println("请输入新增书的类型"); String type=scanner.nextLine(); Book book=new Book(bookName,author,price,type); int current=bookList.getUsedSize(); bookList.setBook(book,current); bookList.setUsedSize(current+1); System.out.println("新增图书成功"); } }
删除图书
public class DelBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("删除图书"); System.out.println("请输入要删除的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize= bookList.getUsedSize(); int pos=-1; int i = 0; for (; i <currentSize ; i++) { Book book=bookList.getBook(i); if(book.getName().equals(bookName)){ System.out.println("找到这本书,开始删除"); pos=i; break; } } if(i>=currentSize){ System.out.println("没有找到这本书"); return; } for (int j = pos; j <currentSize-1 ; j++) { Book book=bookList.getBook(j+1); bookList.setBook(book,j); } bookList.setUsedSize(currentSize-1); bookList.setBook(null,currentSize-1); System.out.println("删除成功"); } }
退出系统
public class ExitSystem implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系统"); for (int i = 0; i < bookList.getUsedSize(); i++) { bookList.setBook(null,i); } System.exit(0); } }
查找图书
public class FindBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("查找图书"); System.out.println("请输入要查找的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize= bookList.getUsedSize(); for (int i = 0; i <currentSize ; i++) { Book book=bookList.getBook(i); if(book.getName().equals(bookName)){ System.out.println("找到了这本书"); System.out.println(book); return; } } System.out.println("没有找到这本书"); } }
借书
public class LendBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("借书"); System.out.println("请输入要借阅的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize= bookList.getUsedSize(); for (int i = 0; i <currentSize ; i++) { Book book=bookList.getBook(i); if(book.getName().equals(bookName)){ book.setLend(true); System.out.println("借阅成功"); return; } } System.out.println("借阅失败"); } } 归还图书 public class ReturnBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("ReturnBook"); System.out.println("请输入要归还的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize= bookList.getUsedSize(); for (int i = 0; i <currentSize ; i++) { Book book=bookList.getBook(i); if(book.getName().equals(bookName)){ book.setLend(false); System.out.println("归还成功"); return; } } System.out.println("归还失败"); } }
显示图书
public class ShowBook implements IOperation{ @Override public void work(BookList bookList) { System.out.println("显示图书"); int currentSize= bookList.getUsedSize(); for (int i = 0; i <currentSize ; i++) { Book book=bookList.getBook(i); System.out.println(book); } } }