该程序有两个用户,为管理员和普通用户,管理员模式下,有增删查,打印功能,普通用户模式下,有查借还功能,具体实现代码如下
book package:
Book:
package book;
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.Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
BookList:
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList(){
books[0] = new Book("三国演义","罗贯中",59,"名著");
books[1] = new Book("西游记","吴承恩",49,"名著");
books[2] = new Book("红楼梦","曹雪芹",69,"名著");
usedSize = 3;
}
public Book getBook(int pos){
return books[pos];
}
public void setBooks(int pos,Book book){
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
operation package:
AddOperation:
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("Add book!");
Scanner scanner = new Scanner(System.in);
System.out.println("please input the name of the book");
String name = scanner.nextLine();
System.out.println("please input the author of the book");
String author = scanner.nextLine();
System.out.println("please input the price of the book");
int price = scanner.nextInt();
System.out.println("please input the type of the book");
String type = scanner.next();
Book book = new Book(name,author,price,type);
bookList.setBooks(bookList.getUsedSize(),book);
bookList.setUsedSize(bookList.getUsedSize() + 1);
System.out.println("success add book!");
}
}
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("BorrowBook!");
Scanner scanner = new Scanner(System.in);
System.out.println("Please input the name of the book");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("you get it");
return;
}
}
System.out.println("Can't find this book");
}
}
DelOperationn
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("Delete Book!");
Scanner scanner = new Scanner(System.in);
System.out.println("Please input the name of the book");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
bookList.setBooks(i,null);
return;
}
}
System.out.println("Can't find this book");
}
}
DisplayOperation:
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("Display book!");
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
ExitOperation:
package operation;
import book.BookList;
public class EixtOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("Exit the system!");
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("FindBook!");
Scanner scanner = new Scanner(System.in);
System.out.println("Please input the name of the book");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("find it!");
System.out.println(book);
return;
}
}
System.out.println("Can't find this book");
}
}
IOperation:
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
ReturnOperation:
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("BorrowBook!");
Scanner scanner = new Scanner(System.in);
System.out.println("Please input the name of the book");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("you return it");
return;
}
}
System.out.println("Can't find this book");
}
}
user package:
AdminUser:
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String userName) {
super(userName);
this.iOperations = new IOperation[]{
new EixtOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu(){
System.out.println("hello " + this.userName + "Welcome to the Library!");
System.out.println("#########################");
System.out.println("##### 1.FindBook ####");
System.out.println("##### 2.AddBook ####");
System.out.println("##### 3.DeleteBook ####");
System.out.println("##### 4.DisplayBook####");
System.out.println("##### 0.Exit ####");
System.out.println("#########################");
System.out.println("Please input your choose");
Scanner sca = new Scanner(System.in);
int choice = sca.nextInt();
return choice;
}
public void doOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
NormalUser:
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String userName) {
super(userName);
this.iOperations = new IOperation[]{
new EixtOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu(){
System.out.println("hello " + this.userName + "Welcome to the Library!");
System.out.println("#########################");
System.out.println("##### 1.FindBook ####");
System.out.println("##### 2.BorrowBook ####");
System.out.println("##### 3.ReturnBook ####");
System.out.println("##### 0.Exit ####");
System.out.println("#########################");
System.out.println("Please input your choose");
Scanner sca = new Scanner(System.in);
int choice = sca.nextInt();
return choice;
}
public void doOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
User:
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String userName;
protected IOperation[] iOperations;
public User(String userName) {
this.userName = userName;
}
public abstract int menu();
public abstract void doOperation(int choice, BookList bookList);
}
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("Please input your name");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("Please input your identify");
System.out.println("1:->AdminUser 2:->NormalUser");
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);
}
}
}