目录
前言
基于类和对象、继承和多态、抽象类和接口相关内容,完成此小项目
实现逻辑
面向对象的原则:
1.找到对象
2.创建对象
3.使用对象
书
创建名为book的包,存放与书有关的类。
Book
在Book包中创建Book的类。
编写书的相关属性,书名 (name) ,作者 (author) ,价格 (price) ,类型 (type) 以及是否被借出(isBorrowed)。
将属性前的访问修饰符设置为private权限(用private修饰书的属性,体现书的密封性),提供相应的get方法和set方法以及构造方法(已将是否被借出的类型设置为boolean,默认为false),通过快捷键进行toString方法的重写。
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 getBorrowed() {
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) ? " 已经借出" : " 未被借出") +
/*", isBorrowed=" + isBorrowed +*/
'}';
}
}
BookList
在Book包中创建BookList的类
注意:不要使用编译器生成的Book数组books的get and set (Book)方法,因为其针对的是books数组本身,而不是数组中存放的书,设置名为pos的数组下标参数,在getBook方法中返回books的数组下标pos,在setBooks方法中,将 books数组的pos下标设置为一本书。这样就可以访问该数组的书成员
public class BookList {
private Book[] books ;
private int usedSize;//记录当前书架上实际存放的书的数量
private static final int DEFAULT_CAPACITY = 10;//方便修改书架容量
public BookList() {
this.books = new Book[DEFAULT_CAPACITY];
//放置书!!
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",9,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
// public Book[] getBooks() {
// return books;
// }
}
用户
User(抽象类)
在普通用户和管理员用户中存在许多共性,因此设置一个抽象类被继承使用
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;//操作接口
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
ioPerations[choice].work(bookList);
}
}
AdminUser
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("**********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
NormalUser
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
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("**********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
用户对书进行的一系列操作
IOPeration
提供一个接口供对书架实现不同操作
public interface IOPeration {
void work(BookList bookList);
}
BorrowOperation
1.要借阅哪本书?
2.借阅的书有没有?
输入想要借阅的图书的名字,遍历书架上的书,如果有,借出,如果没有,无法借阅
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)) {
book.setBorrowed(true);
System.out.println("借阅成功!");
System.out.println(book);
return;
}
}
System.out.println("你借阅的图书 不存在!! ");
}
}
ReturnOperation
- 与BorrowOperation思路一致
public class ReturnOperation implements IOPeration{
@Override
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)) {
book.setBorrowed(false);
System.out.println("归还成功!");
System.out.println(book);
return;
}
}
System.out.println("你归还的图书 不存在!! ");
}
}
AddOperation
public class AddOperation implements IOPeration{
@Override
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,price,type);
//检查数组当中有没有这本书(遍历)
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)) {
System.out.println("有这本书,不进行存放了!");
return;
}
}
if(currentSize == bookList.getBooks().length) {
System.out.println("书架满了!");
}else {
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
}
}
DelOperation
public class DelOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要删除的图书:");
String name = scanner.nextLine();
int pos = -1;
int currentSize = bookList.getUsedSize();
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("没有你要删除的图书!");
return;
}
//开始删除
int j = pos;
for (; j < currentSize-1; j++) {
//[j] = [j+1]
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(j,null);
bookList.setUsedSize(currentSize-1);
}
}
ShowOperation
public class ShowOperation 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);
}
}
}
FindOperation
public class FindOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
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("没有找到这本书!");
}
}
ExitOperation
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);//退出
}
}
完成测试
指引用户完成对系统的使用(Main)
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();
System.out.println("choice :"+ choice);
//根据choice 的选择来决定调用的是哪个方法
user.doOperation(choice,bookList);
}
}
}