
book - Book
java
package book;
public class Book {
private String name; //书的名字
private String author;//书的作者
private double price;//书的价格
private String type;//书的类型
private boolean isBorrowed;//书是否被借出
public Book(String name, String author, double 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 double getPrice() {
return price;
}
public void setPrice(double 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=" + (isBorrowed == true ? "已借出" : "未借出") +
'}';
}
}
book - BookList
java
package book;
public class BookList {
private Book[] books;
private int usedSize; //表示当前暑假有几本书
public BookList(){
this.books = new Book[10];
books[0] = new Book("三国演义","罗贯中",6,"小说");
books[1] = new Book("西游记","吴承恩",8,"小说");
books[2] = new Book("红楼梦","曹雪芹",10,"小说");
this.usedSize = 3;
}
public Book getBook(int pos){
return this.books[pos];
}
public void setBook(Book book,int pos){
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
opera - IOPeration
java
package opera;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
opera - AddOperation
java
package opera;
import book.BookList;
import book.Book;
import java.util.Scanner;
public class AddOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("新增图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入新增图书的名字:");
String name = scanner.nextLine();
System.out.print("请输入新增图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入新增图书价格:");
int price = scanner.nextInt();
System.out.println("请输入新增图书的类型");
scanner.nextLine();
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)){
System.out.println("已经有这本书");
}
}
bookList.setBook(book,currentSize);
bookList.setUsedSize(currentSize+1);
}
}
opera - BorrowOperation
java
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
System.out.println("请输入你想要借阅的图书名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功");
break;
}
System.out.println("没有你想要借阅的图书"+ name);
}
}
}
opera - DelOperation
java
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
System.out.println("请输入想要删除的图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int flag = 0;
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)){
flag = i;
break;
}
}
for (int j = 0; j < currentSize - 1; j++) {
Book book = bookList.getBook(j+1);
bookList.setBook(book,j);
}
bookList.setUsedSize(currentSize - 1);
bookList.setBook(null,currentSize - 1);
}
}
opera - ExitOperation
java
package opera;
import book.BookList;
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0); //用于终结程序
}
}
opera - FindOperation
java
package opera;
import book.BookList;
import book.Book;
import java.util.Scanner;
public class FindOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
System.out.print("请输入想要查找的图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("查到了");
System.out.println(book);
}else {
System.out.println("没有这本书");
}
}
}
}
opera - ReturnOpertion
java
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOpertion implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
System.out.println("请输入你想要归还的图书名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功");
}
System.out.println("没有你想要归还的图书"+ name);
}
}
}
opera - ShowOperation
java
package opera;
import book.BookList;
public class ShowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
System.out.println(bookList.getBook(i));
}
}
}
user - AdminUser
java
package user;
import opera.*;
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 ShowOperation()
};
}
@Override
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.退出系统");
Scanner scanner = new Scanner(System.in);
int choice= scanner.nextInt();
return choice;
}
}
user - NormalUser
java
package user;
import book.BookList;
import opera.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPeration = new IOPeration[]{
new FindOperation(),
new BorrowOperation(),
new ReturnOpertion(),
new ExitOperation()
};
}
@Override
public int menu(){
System.out.println("_________________");
System.out.println("hello,"+name+"~");
System.out.println("0.查找图书!");
System.out.println("1.借阅图书!");
System.out.println("2.归还图书!");
System.out.println("3.退出系统!");
Scanner scanner = new Scanner(System.in);
int choice= scanner.nextInt();
return choice;
}
}
user - User
java
package user;
import book.BookList;
import opera.IOPeration;
public abstract class User {
protected String name;
protected IOPeration[] ioPeration;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doWork(int choice, BookList bookList){
this.ioPeration[choice].work(bookList);
}
}
Main
import book.BookList;
import user.NormalUser;
import user.AdminUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
System.out.print("请输入姓名:");
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) {
User user = login();
BookList bookList = new BookList();
while (true){
int choice = user.menu();
user.doWork(choice,bookList);
}
}
}