大家好,又见面了,我是你们的朋友全栈君。
//Library.book包
//book包下 Book类
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;
}
}
//book包下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;
}
}
//Library包下Opeartion包
//Opeartion包下IOperation接口
package Library.Operation;
import Library.book.BookList;
public interface IOperation {
void work(BookList bookList);
}
//Opeartion包下AddBook类
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;
}
}
//Opeartion包下BorrowBook类
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);
}
}
}
//Opeartion包下DelBook类
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("没有找到该书籍");
}
}
}
//Opeartion包下ExitOperation类
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);
}
}
//Opeartion包下FindBook类
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 + "本书" );
}
}
}
//Opeartion包下PrintAllBook类
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() + "本书籍");
}
}
//Opeartion包下ReturnBook类
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);
}
}
}
//Library包下user包
//user包下User类
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);
}
}
//user包下Teacher类
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;
}
}
//user包下Students类
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;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156880.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...