创建一个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{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true)?" 已借出 ":" 未借出 ")
/*", isBorrowed=" + isBorrowed */+
'}';
}
}
package book;
//书架类
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//有效的数据个数【实际放书的个数】
public BookList(){
books[0] = new Book("三国演义","罗贯中",30,"小说");
books[1] = new Book("西游记","吴承恩",35,"小说");
books[2] = new Book("红楼梦","曹雪芹",45,"小说");
usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book[] getBooks(){
return books;
}
}
使用user创建用户包
package user;
import Ioperation.*;
import java.util.Scanner;
//管理员类
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new Exit(),
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("*********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
package user;
import Ioperation.*;
import java.util.Scanner;
//普通用户类
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new Exit(),
new Findoperation(),
new Borrowoperation(),
new Retuoperation(),
};
}
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("**********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
package user;
import Ioperation.IOperation;
import book.BookList;
//抽象用户类
public abstract class User {
//这个类并不表示具体的对象
//抽象类
protected String name;
protected IOperation[] iOperations;
public User(String name){
this.name = name;
//这个构造方法是为了初始化父类的成员
}
public abstract int Menu();//对父类进行重写
public void doIoperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
创建Ioperation接口类
package Ioperation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
创建其他需要使用的类
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
//新增图书类
public class Addoperation implements IOperation {
public void work(BookList bookList){
System.out.println("新增图书");
//判满
int currentSize = bookList.getUsedSize();
if(currentSize == bookList.getBooks().length){
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("请输入价格:");
int price = scanner.nextInt();
System.out.println("请输入类型");
String type = scanner.nextLine();
Book newbook = new Book(name,author,price,type);
//判重
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book1 = bookList.getBooks(i);
if(book1.getName().equals(name)){
System.out.println("有这本书,无法继续添加");
return;
}
}
System.out.println("没有这本书");
//插入
bookList.setBooks(bookList.getUsedSize(),newbook);
bookList.setUsedSize(currentSize + 1);
System.out.println("新增图书成功!");
}
}
package Ioperation;
import book.Book;
import book.BookList;
//显示图书类
public class Showoperation implements IOperation {
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);
}
}
}
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
//借阅图书类
public class Borrowoperation implements IOperation {
public void work(BookList bookList){
System.out.println("借阅图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
//判重
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book1 = bookList.getBooks(i);
if(book1.isBorrowed()==true){
System.out.println("此书已借出");
return;
}
if(book1.getName().equals(name)){
book1.setBorrowed(true);
System.out.println();
System.out.println("借阅成功");
return;
}
}
System.out.println("借阅错误");
}
}
package Ioperation;
import book.Book;
import book.BookList;
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("请输入书名:");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int pos = 0;
int i = 0;
for (; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println(book);
pos = i;
break;
}
}
if(i == currentSize){
System.out.println("没找到此书");
}
//开始删除
for (int j = pos; j < currentSize - 1 ; j++) {
Book book = bookList.getBooks(j+1);
bookList.setBooks(j,book);
}
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!!!");
}
}
package Ioperation;
import book.BookList;
//退出图书类
public class Exit implements IOperation {
public void work(BookList bookList){
System.out.println("退出系统");
System.exit(0);
}
}
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
//查找图书类
public class Findoperation implements IOperation {
public void work(BookList bookList){
int currentSize = bookList.getUsedSize();
System.out.println("查找图书");
System.out.println("请输入你要查找的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < currentSize ; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println(book);
return;
}
}
System.out.println("没有找到此书");
}
}
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
//归还图书类
public class Retuoperation implements IOperation {
public void work(BookList bookList){
System.out.println("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
//判重
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book1 = bookList.getBooks(i);
if(book1.isBorrowed()==true){
book1.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("归还错误");
}
}
package Ioperation;
import book.Book;
import book.BookList;
//显示图书类
public class Showoperation implements IOperation {
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);
}
}
}
Main函数
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String username = scanner.nextLine();
System.out.println("欢迎" + username + "来到图书系统");
System.out.println("请输入你的身份:1.管理员 2.普通用户");
int choice = scanner.nextInt();
//如果是1 管理员对象
//如果是2 普通用户对象
if (choice == 1) {
return new AdminUser(username);
} else {
return new NormalUser(username);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.Menu();
//根据这个返回值,看调用的哪种方法
/*
* 已经做了:User user = login();
* 2.哪个方法
* 2.1 确定当前对象已经包含了这些方法
* 在调用子类构造方法的时候,构造方法会初始化好对应的操作对象
* */
user.doIoperation(choice, bookList);
}
}
}