在这一个系统中我们会用到多态,继承,封装,接口等知识点,来综合性的设计与实现出一个简易的图书管理系统。
一、设计思路
1.首先图书管理系统缺不了的就是书类了,所以我们首先要创造一个书类
2.然后我们需要一个书架来管理图书
3.我们需要管理员和普通的用户来对书架里的图书进行相对应的操作
二、代码实现
2.1 Book类的实现
这个应该都会吧(狗头),书的应该有名字、作者、价格、还有类型、还有它是否被借出这个状态
然后用idea的生成一下就都出来了(狗头)
package library.Book; public class Book { private String name; private String author; private int price; private String type; private boolean isBorrowed; 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 isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + (isBorrowed == true ? " 已被借出 " : " 未被借出 " )+ '}'; } }
2.2 BookList类的实现(书架)
书架有书,当然我们就需要一个存储书的数组了,然后还需要一个书架上书的数量
package library.Book; public class BookList { private Book[] books = new Book[10]; private int BookSize;//书架上书的数量 public BookList() { books[0] = new Book("三国演义","XXX",89,"小说"); books[1] = new Book("西游记","宋XX",100,"小说"); books[2] = new Book("红楼梦","无名氏",200,"小说"); this.BookSize = 3; }//这里用无参构造,来初始化书架 public Book getBook(int pos) { return this.books[pos]; }//给一个索引,来获得书架上的某一本书 public void setBook(Book book){ this.books[BookSize] = book; }//利用这个方法来新增图书 public void setBook(int pos,Book book){ this.books[pos] = book; }//这个方法是在给的索引位置,放置传入的book这本书 public int getBookSize() { return BookSize; } public void setBookSize(int bookSize) { BookSize = bookSize; } }
2.3 用户类的实现
用户类中,这个用户类是父类,我们不需要实现他,就定义了为抽象类,里面有抽象的menu方法,因为管理员和普通用户的菜单都是不一样的,运用dowork方法进行调用方法。IOPeration这个接口数组中存放着实现了IOPeration接口的类,配合dowork方法完成图书的管理
package library.User; import library.Book.BookList; import library.opera.IOPeration; public abstract class User { protected String name;//姓名 protected IOPeration[] ioPerations;//向上转型,配合下面的方法,避免使用switch public User(String name) { this.name = name; } public abstract int menu(); public void doWork(int choice, BookList bookList){ this.ioPerations[choice].work(bookList); }//运用这个进行方法的调用,比如你传入的choice为1 调用的就是数组下的1方法 }
2.4 操作接口的实现
我们需要执行的操作有增加,删除,显示,查找等,他们都需要遵循一定的规范,而这个规范就由接口来实现,在增删查改等操作中,他们的主要方式就是工作,即work(),故接口中提供对应的work()方法供接口的实现类,即各种操作进行重写
package library.opera; import library.Book.BookList; public interface IOPeration { void work(BookList bookList); }
2.5 操作类的具体实现
2.5.1 AddOperation(添加)
package library.opera; import library.Book.Book; import library.Book.BookList; import java.util.Scanner; public class AddOperation implements IOPeration{ @Override public void work(BookList bookList) { Scanner sc = new Scanner(System.in); System.out.println("新增图书"); int size = bookList.getBookSize(); System.out.println("请输入书名"); String name = sc.nextLine(); System.out.println("输入作者"); String author = sc.nextLine(); System.out.println("输入书的类型"); String type = sc.nextLine(); System.out.println("输入价格"); int price = sc.nextInt(); Book book = new Book(name,author,price,type); for (int i = 0; i < size; i++) { Book book1 = bookList.getBook(i); if(book1.getName().equals(name)){ System.out.println("添加的书已存在"); return; } } bookList.setBook(book); bookList.setBookSize(size+1); System.out.println("新增成功"); } }
2.5.2 FindOperation(查找)
package library.opera; import library.Book.Book; import library.Book.BookList; import java.util.Scanner; public class FindOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("找到图书"); System.out.println("请输入书名"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int size = bookList.getBookSize(); for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ System.out.println("找到此书"); System.out.println(book); return; } } System.out.println("没找到此书"); } }
2.5.3 DelOperation(删除)
package library.opera; import library.Book.Book; import library.Book.BookList; import java.util.Scanner; public class DelOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("删除图书"); System.out.println("请输入删除图书的名字"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int size = bookList.getBookSize(); int index = -1; for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name)){ index = i;//找到要删除图书的索引 break; } } if(index != -1){//如果不等于-1说明找到要删除的书籍 for (int i = index; i < size - 1; i++) { Book book = bookList.getBook(i + 1);// 可以理解取出索引为i+1的元素 bookList.setBook(i,book);// 理解为a[i] = a[i+1] } bookList.setBookSize(size-1); //因为删除的是对象,所以 吧最后一个赋值为null bookList.setBook(size-1,null); System.out.println("删除成功"); }else { System.out.println("未找到"); } } }
2.5.4 ShowOperation(显示)
package library.opera; import library.Book.BookList; public class ShowOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("找到图书"); int size = bookList.getBookSize(); for (int i = 0; i < size; i++) { System.out.println(bookList.getBook(i)); } } }
2.5.5 BorrowOperation(借书)
package library.opera; import library.Book.Book; import library.Book.BookList; import java.util.Scanner; public class BorrowOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("借阅图书"); System.out.println("输入你要借阅的图书"); Scanner sc = new Scanner(System.in); int size = bookList.getBookSize(); String name = sc.nextLine(); for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name) && book.isBorrowed() == false){//要存在书籍 且 书籍的状态为false book.setBorrowed(true);//借出后要将书籍的状态弄为true,表示已被借出 System.out.println("借阅成功"); return; } } System.out.println("借阅失败"); } }
2.5.6 RutrunOperation(还书)
package library.opera; import library.Book.Book; import library.Book.BookList; import java.util.Scanner; public class RutrunOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("归还图书"); System.out.println("输入你要归还的图书"); Scanner sc = new Scanner(System.in); int size = bookList.getBookSize(); String name = sc.nextLine(); for (int i = 0; i < size; i++) { Book book = bookList.getBook(i); if(book.getName().equals(name) && book.isBorrowed()){ book.setBorrowed(false);//归还后要将book的状态还原成false System.out.println("归还成功"); return; } } System.out.println("归还失败"); } }
2.5.7 ExitOperation(退出)
package library.opera; import library.Book.BookList; public class ExitOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("退出系统"); System.exit(0); } }
2.6 管理员类的实现
首先,管理员类需要继承于用户类,同时实现用户类中的抽象方法menu(),同时继承了work方法,这里利用接口的多态性来进行实现的。
package library.User; import library.opera.*; import java.util.Scanner; 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()}; }//这里就是运用了接口的多态 //调用0方法就是退出系统 调用1方法就是找图书的方法依次类推,避免使用switch public int menu(){ System.out.println("-----------------"); System.out.println("hello"+name+"欢迎来到图书"); 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 sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; } }
2.7 读者类的实现
同理
package library.User; import library.opera.*; import java.util.Scanner; public class NormalUser extends User{ public NormalUser(String name) { super(name); this.ioPerations = new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),new RutrunOperation()}; } public int menu(){ System.out.println("-----------------"); System.out.println("hello"+name+"欢迎来到图书"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("-----------------"); System.out.println("请输入您的操作:"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; } }
2.8 Test运行
package library; import library.Book.BookList; import library.User.AdminUser; import library.User.NormalUser; import library.User.User; import java.util.Scanner; public class Test { public static User login(){ System.out.println("请输入您的姓名"); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.println("请输入您的身份:1->管理员 0->普通用户"); int choice = sc.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();//相当于User user = new AdminUser(name)或者User user = new NormalUser(name) while (true) { int choice = user.menu(); user.doWork(choice,bookList); } } }