user通过operation操作book,大致思想如下
在对于图书的操作中,一共出现了三类对象 分别是:用户 操作 书 ,其中 用户分为管理员和普通用户,操作是例如借阅归还等具体动作,书分为 书本身 以及 书架
主函数如上,
第一步,new一个BookList 类型的bookList ,对图书的所有操作基于bookList
第二部,login() 函数回根据你选择的不同,new不同的用户身份,用父类user接收,此处发生向上转型
第三步,根据用户所选的choice,和bookList当成参数完成具体操作
具体代码
user包
User
package user;
import Operation.IOperation;
import book.BookList;
//普通用户和管理员都将继承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){
// this.ioPerations[choice].work(bookList);
IOperation ioPeration = this.IOperations[choice];
ioPeration.work(bookList);
}
}
AdminUser
package user;
import Operation.*;
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("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("**************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
NormalUser
package user;
import Operation.*;
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 ReturnOperatuon(),
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("**************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
Operation包
IOperation
package Operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
AddOpeation
package Operation;
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();
System.out.println("请输入你要新增图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入你要新增图书的类型:");
String type = scanner.nextLine();
System.out.println("请输入你要新增图书的价格:");
int price = scanner.nextInt();
//构造book对象并存放
Book book = new Book(name,author,type,price);
int currentSize = bookList.getUsedSize();
for (int i = 0;i<currentSize;i++){
Book tmp = bookList.getBooks(i);
if (tmp.getName().equals(name)){//book.getName是什么 name是什么
System.out.println("存在这本书,信息如下");
System.out.println(book);
return;
}
}
//没有重复的书,开始新增
bookList.setBooks(book,currentSize);
System.out.println("新增成功! 新增书的信息如下:");
System.out.println(book);
bookList.setUsedSize(currentSize+1);
}
}
BorrowOperation
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation 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.getBooks(i);
if(book.getName().equals(name)){
//存在这本书且未被借出
if(!book.isBorrowed()){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
}
System.out.println("没有此书或者该书已经借出:"+name);
}
}
DelOperation
import java.util.Scanner;
public class DelOperation implements IOperation {
public void work(BookList bookList){
System.out.println("删除图书!");
Scanner scanner = new Scanner(System.in) ;
System.out.println("请输入你要删除的图书名:");
int index = -1;
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i=0;i<currentSize;i++){
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
index=i;//找到书,得到该书的下标
System.out.println("删除成功! 删除书目为:");
System.out.println(book);
break;
}
}
if(index==-1){
System.out.println("没有你要删除的图书!");
return;
}
//可以删除了
//移动数据进行覆盖,size--
for (int j=index;j<currentSize-1;j++){
// bookList[j]=bookList[j+1];
//前移 覆盖
Book book = bookList.getBooks(j+1);
bookList.setBooks(book,j);
//图书的size--
bookList.setUsedSize(currentSize-1);
//回收被删除的空间
bookList.setBooks(null,currentSize-1);
}
}
}
ExitOperation
package Operation;
import book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
//应该还需要对booklist进行资源回收
System.exit(0);
}
}
FindOperation
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
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<currentsize;i++){
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("存在此书,信息如下:");
System.out.println(book);
return;
}
}
//代码还没return出去,说明没有找到
System.out.println("没有找到书名为:"+name+"的书");
}
}
ReturnOperation
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperatuon 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.getBooks(i);
if(book.getName().equals(name)){
//这本书被借出
if(book.isBorrowed()){
//归还
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
}
System.out.println("没有此书或者该书已经被归还:"+name);
}
}
ShowOperation
package Operation;
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.getBooks(i);
System.out.println(book);
System.out.println();
}
}
}
book包
Book
package book;
public class Book {
private String name;//书名
private String author;//作者
private String type;//类型
private int price;//价格
private boolean isBorrowed;//是否被借出 默认false
public Book(String name,String author,String type,int price) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public String getAuthor(){
return this.author;
}
public void setAuthor(String author){
this.author=author;
}
public String getType(){
return this.type;
}
public void setType(String type){
this.type=type;
}
public int getPrice(){
return this.price;
}
public void setPrice(int price){
this.price=price;
}
public boolean isBorrowed(){
return this.isBorrowed;
}
public void setBorrowed(boolean borrowed){
this.isBorrowed=borrowed;
}
@Override
public String toString() {
return "BookList{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
((isBorrowed == true) ? " 已借出" : " 未借出")+
'}';
}
}
BookList
package book;
public class BookList {
private Book[] books;
private int usedSize;//记录当前书架放了几本书
public BookList(){
this.books = new Book[10];
this.books[0] = new Book("三国演义","罗贯中","小说",10);
this.books[1] = new Book("水浒传","施耐庵","小说",11);
this.books[2] = new Book("红楼梦","曹雪芹","小说",12);
this.books[3] = new Book("西游记","吴承恩","小说",13);
this.usedSize = 4;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBooks(int pos){
return books[pos];
}
public void setBooks(Book book,int pos){
books[pos] = book;
}
public void delBooks(String name){
}
}
Main
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
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();
user.doOperation(choice,bookList);
//根据菜单返回的choice来执行对应的操作
}
}
}