分三个包,分别存放了使用者的信息,书籍信息,以及进行的操作方法
一、book
先来实现每本书的信息(Book类)
package book;
public class Book {
private String name;//图书名
private String author;//作者
private int price;//价格
private String type;//类型
private boolean isLend;//是否被借出
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 isLend() {
return isLend;
}
public void setLend(boolean lend) {
isLend = lend;
}
@Override
public String toString() {
return "book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isLend=" + isLend +
'}';
}
}
再来创建一个书架,存放书籍和记录书的个数
package book;
public class BookList {
private Book[] books = new Book[10];
private int useSize;//书架存放书的个数
public BookList(){
books[0]=new Book("三国演义","罗贯中",10,"小说");
books[1]=new Book("水浒传","施耐庵",10,"小说");
books[2]=new Book("红楼梦","曹雪芹",10,"小说");
this.useSize = 3;
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
public Book getBook(int pos){
return books[pos];
}//得到某本书
public boolean isFull(){
if(this.useSize == books.length){
return true;
}
return false;
}//判断是否满了
public void setBook(int pos,Book book){
books[pos] = book;
}
}
二、user
之后再来实现使用者User类,存放了使用者的姓名和操作选择
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] operations;
public User(String name){
this.name = name;
}
public abstract int menu();//抽象类,在子类中实现对应的选择菜单
public void doIoperation(int choice,BookList bookList){
this.operations[choice].work(bookList);
}//对书架进行相应的操作
}
使用者分两类分别是管理员还有普通用户,两者都是使用者,所以需要继承User类
管理员
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations = 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;
}
}
普通用户
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.operations = new IOperation[]{new ExitOperation(),new FindOperation(),new BorrowedOperation(),
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("*********************************");
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);
}
查找图书
package operation;
import book.BookList;
import java.util.Scanner;
import book.Book;
public class FindOperation implements IOperation {
public void work(BookList bookList){
System.out.println("查找图书.......");
System.out.println("请输入你要查找的图书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUseSize();
for(int i = 0;i<currentSize;i++){
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有找到你要找的书..........");
}
}
新增图书
package operation;
import book.BookList;
import java.util.Scanner;
import book.Book;
public class AddOperation implements IOperation{
public void work (BookList bookList){
System.out.println("新增图书......");
if(bookList.isFull()){
System.out.println("书架满了 不能新增了");
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要新增的图书的书名:");
String bookName = scanner.nextLine();
System.out.println("请输入你要新增的图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入你要新增的图书的价格:");
int price = scanner.nextInt();
System.out.println("请输入你要新增的图书的类型:");
String blank = scanner.nextLine();//拿取因输入price的'\n'来进行下一步的输入类型操作
String type = scanner.nextLine();
Book book = new Book(bookName,author,price,type);
int currentSize =bookList.getUseSize();
bookList.setBook(currentSize,book);
bookList.setUseSize(currentSize+1);
System.out.println("新增图书成功");
}
}
删除图书
package operation;
import book.BookList;
import java.util.Scanner;
import book.Book;
public class DelOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("删除图书..........");
System.out.println("请输入你要删除的图书的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize =bookList.getUseSize();
int pos = -1;
int i =0;
for(i=0;i<currentSize;i++){
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)){
//找到记录下标
pos = i;
break;
}
}
if(i>=currentSize){
System.out.println("没有找到你要删除的书");
return;
}
//开始删除
int j = 0;
for(j=pos;j<currentSize-1 ; j++){
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setUseSize(currentSize-1);
bookList.setBook(currentSize-1,null);
System.out.println("删除成功");
}
}
显示图书
package operation;
import book.BookList;
import book.Book;
public class ShowOperation implements IOperation{
public void work(BookList bookList){
System.out.println("显示图书......");
int currentSize = bookList.getUseSize();
for(int i = 0;i<currentSize;i++){
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
借阅图书
package operation;
import book.BookList;
import book.Book;
import java.util.Scanner;
public class BorrowedOperation implements IOperation {
public void work(BookList bookList) {
System.out.println("借阅图书.......");
System.out.println("请输入你要借阅的图书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUseSize();
int i = 0;
for (i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(bookName)) {
if (book.isLend()) {
System.out.println("图书已经被借出 借阅失败");
return;
}
book.setLend(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有找到你要借阅的书");
}
}
归还图书
package operation;
import book.BookList;
import book.Book;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
public void work(BookList bookList){
System.out.println("归还图书.........");
System.out.println("请输入你要归还的图书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUseSize();
for(int i=0;i<currentSize;i++){
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)){
book.setLend(false);
System.out.println("归还成功");
return;
}
}
System.out.println("归还失败");
}
}
退出系统
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
public void work(BookList bookList){
System.out.println("退出系统..........");
int currentSize =bookList.getUseSize();
for(int i=0;i<currentSize;i++){
bookList.setBook(i,null);
}//将书架置为空
System.exit(0);
}
}
四、Library
登录
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Library {
//AdminUser和NormalUser都继承User,所以返回值是User
public static User Login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scanner.nextLine();
System.out.println("请输入你的身份:1、管理员 2、普通用户");
int choice = scanner.nextInt();
if(choice == 1){
AdminUser adminUser = new AdminUser(name);
return adminUser;
}else{
NormalUser normalUser = new NormalUser(name);
return normalUser;
}
}
public static void main(String[] args) {
BookList bookList = new BookList();//创建书架
User user = Login();//根据用户选择看是管理员还是普通用户
while(true) {
int choice = user.menu();//可能是管理员或普通用户的功能,需要区分组织
user.doIoperation(choice, bookList);
}
}
}