src/Main.java
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);
}
}
}
src/book/Book.java
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);
}
}
}
src/book/BookList.java
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//记录当前有多少本书
public BookList(){
books[0] = new Book("三国演义","罗贯中",19,"小说" );
books[1] = new Book("西游记","吴承恩",15,"小说" );
books[2] = new Book("红楼梦","曹雪芹",25,"小说" );
usedSize = 3;
}
/**
* 得到书
* @param pos 此时pos位置一定要合法
* @return 这是一本书
*/
public Book getBook(int pos){
return books[pos];
}
/**
* 放入图书
* @param pos pos位置一定要合法
* @param book 要放入的书
*/
public void setBook(int pos,Book book){
books[pos] = book;
}
/**
* 实时获取当前书的个数
* @return
*/
public int getUsedSize(){
return usedSize;
}
/**
* 实时修改当前书架书的个数
* @param size
*/
public void setUsedSize(int size){
usedSize = size;
}
}
src/operation/AddOperation.java
package operation;
import book.BookList;
import java.util.Scanner;
import book.Book;
public class AddOperation implements IOperation{
@Override
public void word(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);
bookList.setBook(bookList.getUsedSize(),book);
bookList.setUsedSize(bookList.getUsedSize()+1);
System.out.println("新增图书成功!");
}
}
src/operation/BorrowOperation.java
package operation;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation extends FindBook implements IOperation{
@Override
public void word(BookList bookList){
System.out.println("借阅图书!");
System.out.println("请输入你要借阅的图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int ret = findBook(bookList, name);
if(ret != -1){
bookList.getBook(ret).setBorrowed(true);
System.out.println("借阅成功!");
}
else{
System.out.println("找不到此书!");
}
}
}
src/operation/DelOperation.java
package operation;
import book.BookList;
import java.util.Scanner;
public class DelOperation extends FindBook implements IOperation{
@Override
public void word(BookList bookList){
System.out.println("删除图书!");
System.out.println("请输入你要删除的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int ret = findBook(bookList, name);
if(ret != -1){
for(; ret < bookList.getUsedSize() - 1; ret++){
bookList.setBook(ret,bookList.getBook(ret + 1));
}
bookList.setBook(ret,null);
bookList.setUsedSize(bookList.getUsedSize() - 1);
System.out.println("删除成功!");
}
else{
System.out.println("未找到图书!");
}
}
}
src/operation/DisplayOperation.java
package operation;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void word(BookList bookList) {
System.out.println("显示图书!");
for(int i = 0; i < bookList.getUsedSize(); i++){
System.out.println(bookList.getBook(i));
}
}
}
src/operation/ExitOperation.java
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void word(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
src/operation/FindBook.java
package operation;
import book.BookList;
public class FindBook {
public int findBook(BookList bookList, String name){
int i = 0;
for(i = 0; i < bookList.getUsedSize(); i++){
if(name.equals(bookList.getBook(i).getName())){
return i;
}
}
return -1;
}
}
src/operation/FindOperation.java
package operation;
import java.util.Scanner;
import book.BookList;
import book.Book;
public class FindOperation extends FindBook implements IOperation{
@Override
public void word(BookList bookList){
System.out.println("查找图书!");
System.out.println("请输入你要查找的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int ret = findBook(bookList, name);
if(ret != -1){
System.out.println(bookList.getBook(ret));
}
else{
System.out.println("未找到此书!");
}
}
}
/*name.equals(bookList.getBook(i))*/
/*
int size = bookList.getUsedSize();
for(int i = 0; i < size; i++){
if(name.equals(bookList.getBook(i).getName())){
System.out.println("找到了这本书!");
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("没找到这本书!");*/
src/operation/IOperation.java
package operation;
import book.BookList;
/**
* 操作接口
*/
public interface IOperation {
void word(BookList bookList);
}
src/operation/ReturnOperation.java
package operation;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation extends FindBook implements IOperation{
@Override
public void word(BookList bookList){
System.out.println("归还图书!");
System.out.println("请输入你归还的图书!");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int ret = findBook(bookList, name);
if(ret != -1){
bookList.getBook(ret).setBorrowed(false);
System.out.println("归还成功!");
}
else{
System.out.println("没有找到此书!");
}
}
}
src/user/AdminUser.java
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name){
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu(){
System.out.println("hello " + + " 欢迎来到图书管理系统!");
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);
int choice = scanner.nextInt();
return choice;
}
}
src/user/NormalUser.java
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new DisplayOperation()
};
}
public int menu(){
System.out.println("hello" + + "欢迎来到图书管理系统");
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);
int choice = scanner.nextInt();
return choice;
}
}
src/user/User.java
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;//用户名
protected IOperation[] iOperation;
public User(String name){
= name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
this.iOperation[choice].word(bookList);
}
}
src/Main.java
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);
}
}
}