package Library
.book
;
public class Book {
private String name
;
private String author
;
private String id
;
private int price
;
private String type
;
private boolean isBorrowed
;
public Book(String name
, String author
, String id
, int price
, String type
, boolean isBorrowed
) {
this.name
= name
;
this.author
= author
;
this.id
= id
;
this.price
= price
;
this.type
= type
;
this.isBorrowed
= isBorrowed
;
}
public String
getId() {
return id
;
}
public String
getName(){
return name
;
}
@Override
public String
toString() {
return "Book{" +
"name='" + name
+ '\'' +
", author='" + author
+ '\'' +
", id='" + id
+ '\'' +
", price=" + price
+
", type='" + type
+ '\'' +
", isBorrowed=" + isBorrowed
+
'}';
}
public boolean isBorrowed() {
return isBorrowed
;
}
public void setBorrowed(boolean borrowed
) {
isBorrowed
= borrowed
;
}
}
package Library
.book
;
public class BookList {
private Book
[] books
= new Book[100];
private int size
;
public BookList() {
books
[0] = new Book("三国演义","罗贯中","001",100,"古典小说",false);
books
[1] = new Book("水浒传","施耐庵","002",100,"古典小说",false);
books
[2] = new Book("西游记", "吴承恩", "003", 100, "古典小说", false);
size
= 3;
}
public int getSize(){
return size
;
}
public Book
getBooks(int index
) {
return books
[index
];
}
public void setBooks(int index
,Book book
) {
books
[index
] = book
;
}
public void setSize(int size
) {
this.size
= size
;
}
}
package Library
.Operation
;
import Library
.book
.BookList
;
public interface IOperation {
void work(BookList bookList
);
}
package Library
.book
;
public class BookList {
private Book
[] books
= new Book[100];
private int size
;
public BookList() {
books
[0] = new Book("三国演义","罗贯中","001",100,"古典小说",false);
books
[1] = new Book("水浒传","施耐庵","002",100,"古典小说",false);
books
[2] = new Book("西游记", "吴承恩", "003", 100, "古典小说", false);
size
= 3;
}
public int getSize(){
return size
;
}
public Book
getBooks(int index
) {
return books
[index
];
}
public void setBooks(int index
,Book book
) {
books
[index
] = book
;
}
public void setSize(int size
) {
this.size
= size
;
}
}
package Library
.Operation
;
import Library
.book
.Book
;
import Library
.book
.BookList
;
import java
.util
.Scanner
;
public class BorrowBook implements IOperation {
@Override
public void work(BookList bookList
){
System
.out
.println("借阅书籍");
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入借阅数据id号码: ");
String id
= scanner
.next();
for(int i
= 0; i
< bookList
.getSize(); ++i
){
Book book
= bookList
.getBooks(i
);
if(!book
.getId().equals(id
)){
continue;
}
if(book
.isBorrowed()){
System
.out
.println("这本书已经被借走");
break;
}
System
.out
.println("借阅成功");
book
.setBorrowed(true);
}
}
}
package Library
.Operation
;
import Library
.book
.Book
;
import Library
.book
.BookList
;
import java
.util
.Scanner
;
public class DelBook implements IOperation {
@Override
public void work(BookList bookList
){
System
.out
.println("删除书籍信息");
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入删除书籍的id号码:");
String id
= scanner
.next();
int i
= 0;
for(; i
< bookList
.getSize(); ++i
){
Book book
= bookList
.getBooks(i
);
if(book
.getId().equals(id
)){
bookList
.setBooks(i
,bookList
.getBooks(bookList
.getSize() - 1));
bookList
.setSize(bookList
.getSize() - 1);
System
.out
.println("删除成功");
break;
}
}
if(i
>= bookList
.getSize()){
System
.out
.println("没有找到该书籍");
}
}
}
package Library
.Operation
;
import Library
.book
.BookList
;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList
){
System
.out
.println("Good Bye!");
System
.exit(0);
}
}
package Library
.Operation
;
import Library
.book
.Book
;
import Library
.book
.BookList
;
import java
.util
.Scanner
;
public class FindBook implements IOperation {
@Override
public void work(BookList bookList
){
System
.out
.println("查找书籍");
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入想要查阅书籍名称:");
String name
= scanner
.next();
int count
= 0;
for(int i
= 0; i
< bookList
.getSize(); ++i
){
Book book
= bookList
.getBooks(i
);
if(book
.getName().equals(name
)){
System
.out
.println(book
);
count
++;
}
}
if(count
== 0){
System
.out
.println("未找到该书");
}else {
System
.out
.println("找到 " + count
+ "本书" );
}
}
}
package Library
.Operation
;
import Library
.book
.BookList
;
public class PrintAllBook implements IOperation{
@Override
public void work(BookList bookList
){
System
.out
.println("打印所有书籍信息");
for(int i
= 0; i
< bookList
.getSize(); i
++){
System
.out
.println(bookList
.getBooks(i
));
}
System
.out
.println("共计" + bookList
.getSize() + "本书籍");
}
}
package Library
.Operation
;
import Library
.book
.Book
;
import Library
.book
.BookList
;
import java
.util
.Scanner
;
public class ReturnBook implements IOperation {
@Override
public void work(BookList bookList
){
System
.out
.println("归还书籍");
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入归还书籍的id号码:");
String id
= scanner
.next();
for(int i
= 0; i
< bookList
.getSize(); ++i
){
Book book
= bookList
.getBooks(i
);
if(!book
.getId().equals(id
)){
continue;
}
if(!book
.isBorrowed()){
System
.out
.println("这本书已经被归还");
break;
}
book
.setBorrowed(false);
}
}
}
package Library
.user
;
import Library
.Operation
.IOperation
;
import Library
.book
.BookList
;
abstract public class User {
protected String name
;
public User(String name
){
this.name
= name
;
}
protected IOperation
[] operations
;
public abstract int menu();
public void doOperation(int choice
, BookList bookList
) {
operations
[choice
].work(bookList
);
}
}
package Library
.user
;
import Library
.Operation
.*
;
import java
.util
.Scanner
;
public class Teacher extends User {
public Teacher(String name
) {
super(name
);
operations
= new IOperation[]{
new ExitOperation(),
new FindBook(),
new Addbook(),
new DelBook(),
new PrintAllBook()
};
}
@Override
public int menu() {
System
.out
.println("===========");
System
.out
.println("Hello " + name
);
System
.out
.println("1. 查阅书籍");
System
.out
.println("2. 增加书籍");
System
.out
.println("3. 删除书籍");
System
.out
.println("4. 打印所有书籍");
System
.out
.println("0. 退出登陆");
System
.out
.println("===========");
System
.out
.println("请输入您的选择:");
Scanner scanner
= new Scanner(System
.in
);
int choice
= scanner
.nextInt();
return choice
;
}
}
package Library
.user
;
import Library
.Operation
.*
;
import java
.util
.Scanner
;
public class Student extends User {
public Student(String name
) {
super(name
);
operations
= new IOperation[]{
new ExitOperation(),
new FindBook(),
new BorrowBook(),
new ReturnBook()
};
}
@Override
public int menu(){
System
.out
.println("===========");
System
.out
.println("Hello " + name
);
System
.out
.println("1. 查找图书");
System
.out
.println("2. 借阅图书");
System
.out
.println("3. 归还图书");
System
.out
.println("0. 退出登陆");
System
.out
.println("===========");
System
.out
.println("请输入您的选择:");
Scanner scanner
= new Scanner(System
.in
);
int choice
= scanner
.nextInt();
return choice
;
}
}
转载请注明原文地址: https://mac.8miu.com/read-514966.html