整体的文件结构如下:
实现一下功能:可以登录两个用户,并且封装每个用户不同的功能
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=" + isBorrowed +*/
(isBorrowed == true ? " ,已借出":" ,未借出")+
'}';
}
}
实现对私有变量name author price type isBorrowed进行封装
1.首先得为这几个私有变量提供set和get方法。
2.重写构造方法,方便对象进行初始化变量
3.重写ToString方法,打印引用变量返回值为一个数组,数组参数为Book类的成员成员变量。
public class BookList {
private static final int DEFAULT_SIZE = 10;
private Book[] books = new Book[DEFAULT_SIZE];
private int usedSize; //记录当前Book数组中有多少本书?
public BookList() {
books[0] = new Book("三国演义","罗贯中",89,"小说");
books[1] = new Book("西游记","吴承恩",78,"小说");
books[2] = new Book("红楼梦","曹雪芹",49,"小说");
this.usedSize = 3;
}
public void setBook(int pos, Book book){
// 用于指定位置放入书籍
this.books[pos] = book;
}
public Book getBook(int pos){
return this.books[pos];
}
public void setBook(Book book){
this.books[usedSize] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
1.设置最大的存书范围DEFAULT_SIZE,以及记录当前Book数组中有多少本书usedSize
2.实例化Book类,用数组变量进行接收,每本书籍的信息。
3.创建初始书架
4.为usedSize提供set和get方法,用于修改当前书籍位置的已使用数量
5.创建一个setBook()方法实现指定位置存放一本书用于归还书籍功能的使用
6.为Book对象创建get和set方法。get方法用于获取具体位置的书籍的信息,set方法用于增加书籍功能的使用
import book.BookList;
import opera.IOperation;
public abstract class User {
protected String name;
public User(String name) {
this.name = name;
}
public abstract int menu();
public IOperation[] iOperations;
public void doWork(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
1.创建成员变量:name ;成员方法:菜单menu() dowork();
2.创建iOperations接口变量,用于子类指向该接口实现类的对象(操作功能)
import 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 BrowOperation(),
new ReturnOperation(),
};
}
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 scanner = new Scanner(System.in);
return scanner.nextInt();
}
}
1.帮助父类重写构造方法,包含继承父类的成员变量name,以及父类接口变量指向实现该接口功能的类对象。
2.普通用户实现iOperations接口功能的类对象有
new ExitOperation(), ----退出系统
new FindOperation(), ----查找图书
new BrowOperation(), ----借阅图书
new ReturnOperation() ----归还图书
3.重写父类的菜单方法,实现调用普通用户自己的菜单。
import 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(),
};
}
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("******************************");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
}
与普通用户类相似,做同样的工作,只是接口变量指向的类对象不同以及菜单的显示不同。
import book.Book;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
实现work()方法传入书架引用变量对书架进行操作
import book.Book;
import book.BookList;
import java.util.Scanner;
import java.util.SortedMap;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
System.out.println("请输入要查找的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
// 记录当前书架存放书籍的数量
int currentSize = bookList.getUsedSize();
// 遍历书架,寻找与输入名字相同的书籍,并返回该书籍的所有信息。如果没有找到,返回未找到这本书。
for (int i = 0; i
实现接口功能,重写接口IOperation中work方法,达到查找书籍的功能
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
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();
int currentSize = bookList.getUsedSize();
for (int i = 0; i
输入书名,遍历书架查找是否有相同名字的书籍,如果有,直接return不再输入新增信息。没有的话,创建书籍变量接收输入书籍信息,使用书架类设置书籍的方法存储书籍信息,并且修改当前已存放书籍的数量。
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
System.out.println("请输入您要删除的图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i
遍历书架进行寻找相同名字的书籍进行删除,找到之后退出循环,使用index记录该书籍的位置,找到这本书,将后面的数组内容往前覆盖,j从index开始到currentSize-1结束,因为删除的是对象,最后一位与最后两位存放的一样的地址,所以要把该数组位置为null,才算覆盖完毕。
package opera;
import book.Book;
import book.BookList;
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);
}
}
}
遍历书架显示书籍信息
package opera;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
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) && !book.isBorrowed()){
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
}
}
遍历书架,将要借阅的书籍信息改为已借出book.getName().equals(name) && !book.isBorrowed(),要保证名字相同,且该书籍没有借出.实例化该书籍,设置是否借出方法将其状态改为ture,book.setBorrowed(true)
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
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) && book.isBorrowed()){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有您要归还的书");
}
}
与借阅功能相似,找到该书籍,确实名字相同以及确定借出,调用方法,设置是否借出方法将其状态改为false,book.setBorrowed(false),名字不同或者没有借出,则显示没有归还的书籍。
该项目代码地址:https://gitee.com/yao-fa/javase/tree/master/TestBook