上篇
面向对象编程之继承、多态、封装、抽象类、接口、包-上
使用面向对象思想,运用继承、多态、封装、抽象、接口、包等思想、技术、方法等来实现图书管理系统。
图书管理系统首先要有书,书有属性,有名字、价格、作者、书的类型、是否被借阅等,所以可以设计为一个Book 类来存放这些属性。将属性设置为私有,提供对应get和set方法,这就是封装。
图书馆不止一本书,有书架来存放许多书,所以在设计一个BookList类,来存放多本书。书可以被借阅、查询、管理员可以增加书籍等,是不是类似与顺序表或链表的增删查改?可以使用顺序表或链表结构来设计这个BookList类。
可以有两类用户,一个管理员和普通用户,这两类用户都有对书共同的操作,如查阅等,还有自己独有的操作,管理员可以增加或删除书籍,普通用户可以借阅书籍。是不是想到了上篇说的向上转型?所以可以想到,设计一个抽象父类User,有name属性,一系列操作等,然后管理员类和普通用户类继承这个User类,重写父类声明的方法然后来重写这些方法,分别实现自己所独有的操作,也体现了多态。
还有就是两类用户所对应的操作了。他们有相同的操作和不同的操作,可以设计一个接口Operation,它有一个work方法,然后让不同操作去实现它的work方法,这就用到了接口。
最后可以将对有关书籍的类放在同一个包中,将有关操作放在一个包中,将用户类放在一个包中。
核心也就是来实现对应的增删查改操作,本质上就是对顺序表的基本操作。这里我使用了简单的顺序表。
简单的登录
管理端de
查阅书籍
增加书籍
删除书籍
显示书籍列表
退出
普通用户端
查询书籍
借阅书籍
归还书籍
显示书籍列表
退出
提供构造方法,加入对应set/get方法
/**
* user:ypc;
* date:2021-04-22;
* time: 15:40;
*/
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed = false;
public Book(String name, String author, int price, String type, boolean isBorrowed) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
this.isBorrowed = isBorrowed;
}
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=" + isBorrowed +
'}';
}
}
结构类似于顺序表,有一个Book类的数组,还有usesize并且使用构造方法初始化
package book;
import java.util.Arrays;
/**
* user:ypc;
* date:2021-04-22;
* time: 15:41;
*/
public class BookList {
@Override
public String toString() {
return "BookList{" +
"books=" + Arrays.toString(books) +
", useSize=" + useSize +
'}';
}
private Book [] books = new Book[10];
private int useSize = 3;
public BookList() {
books[0] = new Book("三国演义", "罗贯中", 100, "小说",false);
books[1] = new Book("水浒传", "施耐庵", 100, "小说",false);
books[2] = new Book("西游记", "吴承恩", 100, "小说",false);
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos, Book book) {
books[pos] = book;
}
public int getSize() {
return useSize;
}
public void setSize(int size) {
this.useSize = size;
}
}
package operation;
import book.*;
/**
* user:ypc;
* date:2021-04-22;
* time: 15:49;
*/
public interface IOperation {
void work(BookList booklist);
}
增加图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* user:ypc;
* date:2021-04-22;
* time: 15:49;
*/
public class AddOperation implements IOperation {
@Override
public void work(BookList booklist) {
Scanner s = new Scanner(System.in);
Book book;
boolean isBorrowed = false;
System.out.println("请输入书名");
String name = s.nextLine();
System.out.println("请输入作者");
String author = s.nextLine();
System.out.println("请输入类型");
String type = s.nextLine();
System.out.println("请输入价格");
int price = s.nextInt();
book = new Book(name,author,price,type,isBorrowed);
booklist.setBook(booklist.getSize(),book);
booklist.setSize(booklist.getSize()+1);
System.out.println("增加书籍成功");
}
}
增加图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* user:ypc;
* date:2021-04-22;
* time: 16:44;
*/
public class BorrowOperation implements IOperation {
@Override
public void work(BookList booklist) {
System.out.println("借阅图书:");
Scanner scanner =new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
for (int i = 0; i < booklist.getSize(); i++) {
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有这本书!");
}
}
增加图书
package operation;
import book.BookList;
/**
* user:ypc;
* date:2021-04-22;
* time: 15:50;
*/
public class DisplayOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("显示图书!!!");
for (int i = 0; i <booklist.getSize(); i++) {
System.out.println(booklist.getBook(i));
}
}
}
其它操作类似,就是顺序表的基本操作
package User;
import operation.IOperation;
import book.*;
/**
* user:ypc;
* date:2021-04-22;
* time: 16:33;
*/
public abstract class User {
protected String name;
protected IOperation[] operations;
public User(String name) {
this.name = name;
}
// 显示菜单
abstract public int menu();
// 根据用户选项执行操作
public void doOperation(int choice, BookList bookList) {
operations[choice].work(bookList);
}
}
package User;
import operation.*;
import java.util.Scanner;
/**
* user:ypc;
* date:2021-04-22;
* time: 16:41;
*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.operations= new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("==========="+ "hello " + this.name +" 欢迎!==============");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("=====================================================");
int choice = scanner.nextInt();
return choice;
}
}
与普通用户类类似
源码太多了,可以去github地址下载
欢迎关注评论点赞啊,大家一起学习啊