为了更进一步理解多态这一思想,这里我们写一个简易版图书管理系统来加以巩固。
1)实现思路
java实现图书管理系统,要求两种运行方式1.管理员 2.使用者
管理员功能:查找图书,新增图书,删除图书,显示图书,退出系统
使用者功能:查找图书,借阅图书,归还图书,退出系统
因为图书管理系统功能比较多,因此我们会建立三个包来封装不同的功能以便于后面的使用。
1、book包
(1)首先我们要向实现这个图书管理系统,必不可少的就是图书,因此我们需要建立一个Book类来定义一本图书。
(2)接下来就是存储书本的东西,因此我们继续得实现一个BookList类,用来当做书架存储书籍。
2、user包
这个包是用来定义用户功能的,不同的用户有不同的功能
(1)首先我们需要构建一个父类User类,这个父类是一个抽象方法,因为不同的用户拥有不同的功能,然后实现一个操作方法。
(2)接下了我们就可以建立管理员以及普通用户的类了,首先他两是User的子类,然后因为每个人的功能不一样,因此每个人的菜单选项也不一样,实现的功能也不一样,因此我们需要在他们各自的类里面在构建父类User的抽象方法,操作方法父类早已建好因此就不需要我们构建了。
3、operation包
这个包用来用户各种功能的实现
(1)首先我们可以建立一个接口IOperation,在这个接口里面实现一个方法,然后后面的方法直接重载方法就可以。
(2)实现不同的功能,每一种功能都具有接口IOperation,这样每一种方法也就是IOperation类。
最后利用Test方法来测试。
2)具体实现
1)book包
1.Book类
package 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 getCurState(){
return this.isBorrowed;
}
public void setCurState(boolean state){
this.isBorrowed=state;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
(this.isBorrowed ? "已借出" : "未借出")+
'}';
}
}
2.BookList类
package book;
public class BookList {
private static final int maxSize=10;
private Book[] books=new Book[maxSize];
private int usedSize;
public BookList(){
books[0] = new Book("三国演义","罗贯中",86,"小说");
books[1] = new Book("西游记","吴承恩",78,"小说");
books[2] = new Book("红楼梦","曹雪芹",49,"小说");
this.usedSize=3;
}
public Book getBook(int pos){
return this.books[pos];
}
public void setBook(Book book){
this.books[usedSize]=book;
}
public void setBook(Book book,int pos){
this.books[pos]=book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
2)user包
1.AdminUser类
package user;
import opera.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
super.ioperations = new Ioperation[]{
new Exit(),
new FindBook(),
new AddBook(),
new DeleteBook(),
new ShowBookList()
};
}
@Override
public int menu() {
System.out.println("*********************");
System.out.println("Welcome "+name+" to library task");
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("Please input your choice~");
Scanner scan=new Scanner(System.in);
int choice=scan.nextInt();
return choice;
}
}
2.NomalUser类
package user;
import opera.*;
import java.util.Scanner;
public class NomalUser extends User{
public NomalUser(String name){
super(name);
super.ioperations=new Ioperation[]{
new Exit(),
new FindBook(),
new BorrowBook(),
new ReturnBook()
};
}
@Override
public int menu() {
System.out.println("****************");
System.out.println("Welcome "+name+" to library task");
System.out.println("1.查找图书!");
System.out.println("2.借阅图书!");
System.out.println("3.归还图书!");
System.out.println("0.退出界面!");
System.out.println("****************");
System.out.println("Please input your choice~");
Scanner scan=new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
3.User类
package user;
import book.BookList;
import opera.Ioperation;
public abstract class User {
protected String name;
protected Ioperation[] ioperations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doWork(int choice, BookList bookList){
this.ioperations[choice].work(bookList); //多态
}
}
3)operation包
1.Ioperation接口
package opera;
import book.BookList;
public interface Ioperation {
void work(BookList bookList); //多态
}
2.AddBook类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddBook implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("添加图书");
Scanner scan=new Scanner(System.in);
System.out.println("请输入书名");
String name=scan.nextLine();
System.out.println("请输入作者");
String author=scan.nextLine();
System.out.println("请输入价格");
int price=scan.nextInt();
System.out.println("请输入类型");
String type=scan.nextLine();
Book book=new Book(name,author,price,type);
//把书放到书架
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
System.out.println("已经存在这本书了");
return;
}
}
bookList.setBook(book);
//修改usedsize
bookList.setUsedSize(bookList.getUsedSize()+1);
}
}
3.BorrowBook类
package opera;
import book.BookList;
import java.util.Scanner;
public class BorrowBook implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
System.out.println("输入你想借阅的书籍名");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
//借书,把相应书籍的状态由未借出改为借出就ok了
//借书前,会先查找这本书,所以要借的书肯定是未借状态
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name) &&
!bookList.getBook(i).getCurState()){
bookList.getBook(i).setCurState(true);
System.out.println("借阅成功");
return;
}
}
}
}
4.DeleteBook类
package opera;
import book.BookList;
import java.util.Scanner;
public class DeleteBook implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
System.out.println("输入书名");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
//删除图书,找到待删书的索引,然后循环,把后面的书依次往前挪,
//usedSize-1并把原usedSize位置处的书置空即可
//删书,肯定是删掉确实存在的书,故不用考虑书不存在的情况
int index=-1;
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
index=i;
break;
}
}
for (int i = index; i < bookList.getUsedSize()-1; i++) {
bookList.setBook(bookList.getBook(i+1),i);
}
bookList.setBook(null,bookList.getUsedSize());
bookList.setUsedSize(bookList.getUsedSize()-1);
System.out.println("删除成功");
}
}
5.Exit类
package opera;
import book.BookList;
public class Exit implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("退出程序");
System.exit(0); //0 正常终止Java程序
}
}
6.FindBook类
package opera;
import book.BookList;
import java.util.Scanner;
public class FindBook implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("查找书籍");
System.out.println("请输入书名");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
System.out.println("书存在");
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("书不存在");
}
}
7.ReturnBook类
package opera;
import book.BookList;
import java.util.Scanner;
public class ReturnBook implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("归还书籍");
System.out.println("输入书名");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
//还书,把书的状态由已借出改为未借出就ok了
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name) &&
bookList.getBook(i).getCurState()){
bookList.getBook(i).setCurState(false);
System.out.println("还书成功");
return;
}
}
}
}
8.ShwoBookList类
package opera;
import book.BookList;
public class ShowBookList implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("展示书籍");
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
uu们加油(ง •_•)ง