查阅书籍
增加书籍
删除书籍
打印书籍列表
退出
3、用户端
查询书籍
借阅书籍
归还书籍
退出**
代码如下:
package Book;
public class Book implements Comparable {
private String nema;
private String author;
private Float price;
private String type;
private Boolean brrower;
public Book(String nema, String author, Float price, String type) {
this.nema = nema;
this.author = author;
this.price = price;
this.type = type;
this.brrower = false;
}
public Book() {
this.nema = “0”;
this.author = “0”;
this.price = 0f;
this.type = “0”;
this.brrower = false;
}
@Override
public String toString() {
return “Book{” +
“nema= '” + nema + ‘’’ +
“, author= '” + author + ‘’’ +
", price= " + price +
“, type= '” + type + ‘’’ +
(brrower ? " 已借出" : " 未借出") +
‘}’;
}
public String getNema() {
return nema;
}
public void setNema(String nema) {
this.nema = nema;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Boolean getBrrower() {
return brrower;
}
public void setBrrower(Boolean brrower) {
this.brrower = brrower;
}
@Override
public int compareTo(Book o) {
return (int) (o.getPrice() - this.getPrice());
}
}
package Book;
import java.util.Arrays;
public class BookList {
private Book[] books;
private int usedSize;
public BookList() {
books = new Book[10];
books[0] = new Book(“HappyDay”, “YaYa”, 19.9f, “little”);
books[1] = new Book(“SunDay”, “XiaoXiao”, 29.9f, “little”);
books[2] = new Book(“EveryDay”, “DiDi”, 39.9f, “little”);
for (int i = 3; i < 10; i++) {
books[i] = new Book();
}
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(Book book) {
this.books[usedSize] = book;
this.usedSize++;
}
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
public void disPlay() {
for (int i = 0; i < this.usedSize; i++) {
System.out.println(books[i].toString());
}
}
public void sortBookList() {
Arrays.sort(this.books);
}
}
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(“ok”);
Scanner scanner = new Scanner(System.in);
Book book = new Book();
System.out.println(“输入书名” + " 作者" + " 价格" + " 类型");
book.setNema(scanner.next());
book.setAuthor(scanner.next());
book.setPrice(scanner.nextFloat());
book.setType(scanner.next());
bookList.setBooks(book);
System.out.println(“成功!!!”);
}
}
package Operation;
import Book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println(“输入你要借的书名”);
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBooks(i).getNema().equals(name)) {
if(bookList.getBooks(i).getBrrower()) {
System.out.println(“此书已借出!”);
return;
}
bookList.getBooks(i).setBrrower(true);
System.out.println(“借书成功”);
return;
}
}
System.out.println(“无此书!!!”);
}
}
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(“输入你要删的书名”);
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBooks(i).getNema().equals(name)) {
for (int j = i; j < bookList.getUsedSize() - 1; j++) {
Book book = bookList.getBooks(j + 1);
bookList.setBook(j, book);
}
bookList.setUsedSize(bookList.getUsedSize() - 1);
System.out.println(“删除成功!”);
return;
}
}
System.out.println(“无此书!”);
}
}
package Operation;
import Book.BookList;
import java.util.Arrays;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
bookList.disPlay();
}
}
package Operation;
import Book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“EXIT”);
System.exit(0);
}
}
package Operation;
import Book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“输入你要查找的书名”);
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBooks(i).getNema().equals(name)) {
System.out.println(“成功找到此书!”);
byte Id = 0;
do {
System.out.println(“是否查看此书所有信息 1 是 0 否”);
Id = scanner.nextByte();
}while (Id != 1 && Id != 0);
if(Id == 1) {
System.out.println(bookList.getBooks(i).toString());
}
return;
}
}
System.out.println(“无此书!”);
}
}
package Operation;
import Book.BookList;
public interface IOperation {
void work(BookList bookList);
}
package Operation;
import Book.BookList;
public class NeatenOperation implements IOperation {
@Override
public void work(BookList bookList) {
bookList.sortBookList();
}
}
package Operation;
import Book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“输入你要归还的书名”);
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBooks(i).getNema().equals(name)) {
if( ! bookList.getBooks(i).getBrrower()) {
System.out.println(“此书未被借出,无需归还!”);
return;
}
bookList.getBooks(i).setBrrower(false);
System.out.println(“还书成功!”);
return;
}
}
System.out.println(“无此书!”);
}
}
package User;
import Operation.*;
import Operation.IOperation;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
new NeatenOperation()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println(“================================”);
System.out.println(“=Hello “+ super.getName() +” 欢迎来到Knowledgable图书系统=”);
System.out.println(“1.查找图书”);
System.out.println(“2.新增书籍”);
System.out.println(“3.删除书籍”);
System.out.println(“4.打印书籍”);
System.out.println(“5.整理书籍”);
System.out.println(“0.退出系统”);
return scanner.nextInt();
}
}