一、分析有主要对象
二、整理思路
三、框架的搭建
四、操作内部的具体实现
我们做的图书管理系统的目的,是可以根据不同的用户,所能执行的操作不一样,主要有增删查改图书等操作,选择这些不同的操作会给我们反馈不一样的结果,而我们的主要对象就有书、书架、用户、操作这四个对象。
书里面可以放书名、作者、价格等变量,书架是用来放书的,因为书有多本,所以我们可以用数组管理起来,也方便后面的调用,我们可以把书和书架放在同一包;用户分为普通用户和管理员用户,也可以把普通用户和管理员用户放在同一个包上,在建立一个用户类,利用多态,抽象类方便将他们统一;不同的用户操作也会不同,也可以用一个把许多操作放在一起,再利用接口,通过接口把这些操作串起来。
三个包和一个主方法
包下面创建书类和书架类
在书类我们创建相对的变量(名字,作者,类型,价格,bollen类型判断是否被借出,),在创建设置和得到这些变量的方法,以及构造方法初始化这些变量、重写toString方法,改写成我们想要打印显示出来的效果
代码如下
public class Book { private String name; private String author; private String type; private int price; private boolean isBorrow;//默认false public Book(String name, String author, String type, int price) { this.name = name; this.author = author; this.type = type; this.price = price; } 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 String getType() { return type; } public void setType(String type) { this.type = type; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", type='" + type + '\'' + ", price=" + price + ((this.isBorrow == true) ? "已经被借出" :"未被借出") + '}'; } }
创建书架类
里面放书,并且默认初始化三本书,创建书类的数组,记录书的数量,设置和得到书的方法和书的数量,还有一个设置和得到书的数组的方法
代码如下
public class BookList{ private Book[] books;//管理书的数组 private int usedSize;//记录书的数量 private static final int DEFAULT_CAPACITY = 10; public BookList(){ //默认初始化三本书 this.books = new Book[DEFAULT_CAPACITY]; books[0] = new Book("三国演义" ,"罗贯中" ,"小说", 10); books[1] = new Book("西游记", "吴承恩","小说",9); books[2] = new Book("红楼梦", "曹雪芹", "小说", 19); this.usedSize = 3; } public Book getBook(int pos) { return books[pos]; } public Book setBook(int pos, Book book) { return books[pos] = book; } public Book[] getBooks() { return books; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } }
创建操作的包里面的类,
用一个接口,统一管理起来这些操作类,如下
使用work方法调用这些类,
其他类都实现这个IOperation接口
如下
其他的类也一样,就不展示了
用户包
上面两个用户类继承User类,并且把User定义成抽象类
User类内部具体代码如下:
public abstract class User { protected String name; public User(String name) { this.name = name; } protected IOperation[] iOperations; public abstract int menu(); public void doOperation(int choice, BookList bookList) { iOperations[choice].work(bookList); } }
AdminUser类和NormalUser类public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations = new IOperation[] { new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new ShowOperation() }; } 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 ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } @Override public int menu() { System.out.println("*****普通用户******"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("*****************"); System.out.println("请输入您的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
写Main主方法
首先,我们要实例化一个书架,然后根据提示输入选择,展示出一个菜单供我们选择,我们要写一个登录的方法,而选择不同,返回的类型也不同,这里运用到向上转型,代码如下
public class Main { public static User login() { Scanner scanner = new Scanner(System.in); System.out.println("请输入您的姓名"); 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(); int choice =user.menu(); user.doOperation(choice, bookList); } }
用user和接口把他们全部串起来
通过我们不同的选择,就会调用不同的操作
public class AddOperation implements IOperation{ public void work(BookList bookList) { System.out.println("新增图书"); Scanner scanner = new Scanner(System.in); System.out.println("请输入书名"); String name = scanner.nextLine(); System.out.println("请输入作者"); String author = scanner.nextLine(); System.out.println("请输入类型"); String type = scanner.nextLine(); System.out.println("请输入价格"); int price = scanner.nextInt(); Book book = new Book(name, author, type, price); int currentSize = bookList.getUsedSize(); int i = 0; for (; i < currentSize; i++) { Book book1 = bookList.getBook(i); if(book1.getName().equals(name)) { System.out.println("有这本书了,不进行存放"); } } if(currentSize == bookList.getBooks().length) { System.out.println("书架放满了"); return; } else { bookList.setBook(currentSize,book); bookList.setUsedSize(currentSize + 1); } } }
public class BorrowOperation implements IOperation{ public void work(BookList bookList) { System.out.println("借阅图书"); Scanner scanner = new Scanner(System.in); System.out.println("请输入您要借阅的图书"); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = new BookList().getBook(i); if(book.getName().equals(name)) { book.setBorrow(true); System.out.println("借阅成功"); System.out.println(book); return; } } System.out.println("您要查阅的图书没有"); } }
public class DelOperation implements IOperation{ public void work(BookList bookList) { System.out.println("删除图书"); Scanner scanner = new Scanner(System.in); System.out.println("请输入您要删除的图书"); String name = 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(name)) { pos = i;//标记要删除书的下标 break; } } if(i == currentSize) { System.out.println("没有您要删除的图书"); } int j = pos; //下标[j+1]放在[j]上,往前推 for (; j < currentSize - 1; j++) { Book book = bookList.getBook(j + 1); bookList.setBook(j, book); } bookList.setBook(j, null); bookList.setUsedSize(currentSize - 1); } }
public class ExitOperation implements IOperation{ public void work(BookList bookList) { System.out.println("退出系统"); System.exit(0); } }
public class FindOperation implements IOperation{ public void work(BookList bookList) { System.out.println("查找图书"); Scanner scanner = new Scanner(System.in); System.out.println("请输入您要查找的图书"); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)) { System.out.println("找到了这本书,信息如下"); System.out.println(book); return; } } System.out.println("没找到这本书"); } }
public class ReturnOperation implements IOperation{ public void work(BookList bookList) { System.out.println("归还图书"); Scanner scanner = new Scanner(System.in); System.out.println("请输入您要归还的图书"); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)) { System.out.println("归还成功,归还的图书如下:"); System.out.println(book); return; } } System.out.println("您要归还的图书不存在"); } }
public class ShowOperation implements IOperation{ 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); } } }
都看到这了,观众老爷方便的话点个赞呗