package book;
public class Book {
private String bookName;
private String isbn;//书号
private String author;
private String press;//出版社
private double price;
private boolean borrow;//书本状态(是否被借出)
public Book(){}
public Book(String bookName, String isbn, String author, String press,double price){
this.bookName = bookName;
this.isbn = isbn;
this.author = author;
this.press = press;
this.price = price;}
@Override
public String toString(){return"Book{"+"bookName='《"+ bookName +"》\'"+", isbn='"+ isbn +'\''+", author='"+ author +'\''+", press='"+ press +'\''+", price="+ price +(borrow == false ?",未被借出":",已被借出")+'}';}
public voidsetBookName(String bookName){
this.bookName=bookName;}
public String getBookName(){return bookName;}
public String getIsbn(){return isbn;}
public voidsetIsbn(String isbn){
this.isbn = isbn;}
public String getAuthor(){return author;}
public voidsetAuthor(String author){
this.author = author;}
public String getPress(){return press;}
public voidsetPress(String press){
this.press = press;}
public doublegetPrice(){return price;}
public voidsetPrice(double price){
this.price = price;}
public boolean isBorrow(){return borrow;}
public voidsetBorrow(boolean borrow){
this.borrow = borrow;}}
BookList 类
package book;
public class BookList {
private Book[] books = new Book[10];
private int bookNumber;
public boolean flag=true;//控制进出书架
public BookList(){
books[0]=new Book("老人与海","01223","海明威","北京大学出版社",39.9);
books[1]=new Book("骆驼祥子","01224","老舍","南京大学出版社",56.9);
books[2]=new Book("阿Q正传","01225","鲁迅","教育出版社",42.9);
bookNumber =3;}
public voidsetBooks(int pos,Book book){
books[pos]= book;}
public Book getBooks(int pos){return books[pos];}
public intgetBookNumber(){return bookNumber;}
public voidsetBookNumber(int bookNumber){
this.bookNumber = bookNumber;}}
IOpera 接口
package opera;
import book.BookList;
public interface IOpera {voidwork(BookList bookList);}
AddBook 类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddBook implements IOpera{
public voidwork(BookList bookList){int currNum = bookList.getBookNumber();
Scanner in = new Scanner(System.in);
System.out.println("请输入书名:");
String bookName = in.nextLine();
System.out.println("请输入书号:");
String isbn = in.nextLine();
System.out.println("请输入作者:");
String author = in.nextLine();
System.out.println("请输入出版社:");
String press = in.nextLine();
System.out.println("请输入价格:");double price = in.nextDouble();for(int i=0;i<currNum;i++){if(bookName.equals(bookList.getBooks(i).getBookName())){
System.out.println("该书已存在!");}}
Book book = new Book(bookName,isbn,author,press,price);
bookList.setBooks(currNum,book);
bookList.setBookNumber(currNum+1);}}
DelBook 类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelBook implements IOpera{
public voidwork(BookList bookList){
System.out.println("请输入要删除的书:");
Scanner in = new Scanner(System.in);
String bookName = in.nextLine();
FindBook findBook = new FindBook();int index = findBook.findBook(bookName,bookList);int currNum = bookList.getBookNumber();for(int i =index;i<currNum-1;i++){
Book book = bookList.getBooks(i+1);
bookList.setBooks(i,book);}
System.out.println("删除成功!");
bookList.setBookNumber(currNum-1);}}
AlterBook 类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AlterBook implements IOpera{
public voidwork(BookList bookList){
System.out.println("请输入要修改的书:");
Scanner in = new Scanner(System.in);
String bookName = in.nextLine();
FindBook findBook = new FindBook();int index = findBook.findBook(bookName,bookList);
System.out.println("请输入书名:");
String name = in.nextLine();
System.out.println("请输入书号:");
String isbn = in.nextLine();
System.out.println("请输入作者:");
String author = in.nextLine();
System.out.println("请输入出版社:");
String press = in.nextLine();
System.out.println("请输入价格:");double price = in.nextDouble();
Book newBook = new Book(name,isbn,author,press,price);
bookList.setBooks(index,newBook);}}
FindBook 类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindBook implements IOpera{
public intfindBook(String bookName,BookList bookList){int currNum = bookList.getBookNumber();for(int i=0;i<currNum;i++){
Book book = bookList.getBooks(i);if(book.getBookName().equals(bookName)){return i;}}return-1;}
public voidwork(BookList bookList){
System.out.println("请输入要查找的书名:");
Scanner in = new Scanner(System.in);
String bookName = in.nextLine();int index =findBook(bookName,bookList);if(index!=-1){
System.out.println(bookList.getBooks(index));}else{
System.out.println("未找到该书!");}}}
BorrowBook 类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowBook implements IOpera{
public voidwork(BookList bookList){
System.out.println("请输入需要借的书名:");
Scanner in = new Scanner(System.in);
String bookName = in.nextLine();
FindBook findBook = new FindBook();int index = findBook.findBook(bookName,bookList);if(index!=-1&& bookList.getBooks(index).isBorrow()==false){
bookList.getBooks(index).setBorrow(true);
System.out.println("借书成功!");}else{
System.out.println("未找到该书!");}}}
LendBook 类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class LendBook implements IOpera{
@Override
public voidwork(BookList bookList){
System.out.println("请输入需要归还的书:");
Scanner in = new Scanner(System.in);
String bookName = in.nextLine();
FindBook findBook = new FindBook();int index = findBook.findBook(bookName,bookList);if(index!=-1){
bookList.getBooks(index).setBorrow(false);
System.out.println("还书成功!");}else{
System.out.println("未找到该书!");}}}
ShowBook 类
package opera;
import book.Book;
import book.BookList;
public class ShowBook implements IOpera{
public voidwork(BookList bookList){int currNum = bookList.getBookNumber();for(int i =0;i<currNum;i++){
System.out.println(bookList.getBooks(i));}}}
ExitSystem 类
package opera;
import book.BookList;
public class ExitSystem implements IOpera{
public voidwork(BookList bookList){
bookList.flag=false;
System.out.println("已成功退出系统!");}}
User 类
package user;
import book.BookList;
import opera.IOpera;
abstract public class User {
protected String name;
protected IOpera[] iopera;
abstract public intmeue();
public voiddoWork(int choise,BookList bookList){
this.iopera[choise].work(bookList);}
public User(String name){
this.name = name;}
public String getName(){return name;}
public voidsetName(String name){
this.name = name;}}
AdminUser 类
package user;
import opera.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name){super(name);
this.iopera = new IOpera[]{
new ExitSystem(),
new FindBook(),
new AddBook(),
new DelBook(),
new AlterBook(),
new ShowBook()};}
public intmeue(){
System.out.println("---------");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.修改图书");
System.out.println("5.显示图书");
System.out.println("0.退出系统");
System.out.println("---------");
System.out.println("请输入你的操作:");
Scanner in = new Scanner(System.in);int choise = in.nextInt();return choise;}}
NormalUser 类
package user;
import opera.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name){super(name);
this.iopera=new IOpera[]{
new ExitSystem(),
new FindBook(),
new BorrowBook(),
new LendBook()};}
public intmeue(){
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 in = new Scanner(System.in);int choise = in.nextInt();return choise;}}
Test 类
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Test {//登录系统
public static User login(){
System.out.println("请输入你的姓名:");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.println("Hello "+ name +",欢迎来到图书管理系统!");
System.out.println("***********************************");
System.out.println("请选择你的身份: 1.管理员 2.普通用户");
System.out.println("***********************************");int choise = in.nextInt();if(choise ==1){return new AdminUser(name);}else{return new NormalUser(name);}}
public staticvoidmain(String[] args){
BookList bookList = new BookList();
User user =login();while(bookList.flag){int choise = user.meue();
user.doWork(choise,bookList);}}}