1. Package
(書-包)book
(書-類)Book
package book;
/**
* Created with IntelliJ IDEA.
* Description:書
* User: 86155
* Date: 2022-05-18
* Time: 10:12
*/
public class Book {
private String name;//書名
private String author;//作者
private int price;//價(jià)格
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)?", 已借出":", 未借出") +
'}';
}
}
(書架-類)
package book;
/**
* Created with IntelliJ IDEA.
* Description:書架
* User: 86155
* Date: 2022-05-18
* Time: 10:12
*/
public class BookList {
private Book[] books = new Book[10];//書架最多可以放10本書
private int usedSize;//記錄書架上放了幾本書
//構(gòu)造函數(shù)
public BookList(){
//書架上先放幾本書
books[0] = new Book("三國(guó)演義","羅貫中",19,"小說(shuō)");
books[1] = new Book("水滸傳","施耐庵",29,"小說(shuō)");
books[2] = new Book("西游記","吳承恩",23,"小說(shuō)");
usedSize = 3;
}
//get set 公開(kāi)接口
//給到下標(biāo)可以從書架上得到書的信息
public Book getBook(int pos){//pos位置一定是合法的
return books[pos];
}
//往書架上放書
public void setBooks(int pos,Book book){
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
操作-包)operation
(操作接口)IOperation
package operation;
import book.Book;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 10:55
*/
public interface IOperation {
//通過(guò)一個(gè)規(guī)范的接口來(lái)訪問(wèn)操作我的書架
void work(BookList bookList);
}
(添加圖書)AddOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 10:57
*/
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
//創(chuàng)建出這本書
System.out.println("新增圖書!");
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入圖書的名字:");
String name = scanner.nextLine();
System.out.println("請(qǐng)輸入圖書的作者:");
String author = scanner.nextLine();
System.out.println("請(qǐng)輸入圖書的價(jià)錢:");
int price = scanner.nextInt();
scanner.nextLine();
System.out.println("請(qǐng)輸入圖書的類型:");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
//把它放到書架上
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增書籍成功!");
}
}
(借閱圖書)BorrowOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:03
*/
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借閱圖書!");
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入你要借閱圖書的名字:");
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("借閱成功!");
return;
}
}
System.out.println("沒(méi)有你要找的書");
}
}
(刪除圖書)DelOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 10:59
*/
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("刪除圖書!");
System.out.println("請(qǐng)輸入你要輸出圖書的名字:");
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)){
for (int j = i; j < currentSize; j++) {
bookList.setBooks(j,bookList.getBook(j+1));
}
bookList.setUsedSize(currentSize-1);
System.out.println("刪除成功!");
return;
}
}
System.out.println("沒(méi)有你要找的書");
}
}
(顯示圖書)DisplayOperation
package operation;
import book.Book;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:00
*/
public class DisplayOperation 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));
}
}
}
(退出系統(tǒng))ExitOperation
package operation;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:02
*/
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系統(tǒng)!");
System.exit(0);
}
}
(查找圖書)FindOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 10:58
*/
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找圖書!");
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入你要查找圖書的名字:");
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);
return;
}
}
System.out.println("沒(méi)有你要找的書");
}
}
(歸還圖書)ReturnOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:04
*/
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("歸還圖書!");
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入你要?dú)w還圖書的名字:");
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("歸還成功!");
return;
}
}
System.out.println("沒(méi)有你要找的書");
}
}
(用戶-包)user
(用戶父類)User
package user;
import book.BookList;
import operation.IOperation;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:08
*/
//不管是普通人員還是管理人員,其父類都是User
public abstract class User {
protected String name;//用戶名
protected IOperation[] iOperations;//接口數(shù)組 - 菜單選擇的操作
public User(String name){
this.name = name;
}
public abstract int menu();
//用來(lái)訪問(wèn)接口數(shù)組的哪個(gè)函數(shù)來(lái)操作書架
public void doOperation(int choice , BookList bookList){
this.iOperations[choice].work(bookList);//操作數(shù)選擇了幾,就調(diào)用這個(gè)函數(shù)的work方法
}
}
(管理員)AdminUser
package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:12
*/
public class AdminUser extends User{
//顯示的幫助父類構(gòu)造
public AdminUser(String name){
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
//菜單
public int menu(){
System.out.println("歡迎"+this.name+"管理圖書館!");
System.out.println("1.查找圖書");
System.out.println("2.新增圖書");
System.out.println("3.刪除圖書");
System.out.println("4.顯示圖書");
System.out.println("0.退出系統(tǒng)");
System.out.println("請(qǐng)輸入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
(普通用戶)NormalUser
package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:11
*/
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
//菜單
public int menu(){
System.out.println("歡迎"+this.name+"來(lái)到圖書館!");
System.out.println("1.查找圖書");
System.out.println("2.借閱圖書");
System.out.println("3.歸還圖書");
System.out.println("0.退出系統(tǒng)");
System.out.println("請(qǐng)輸入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
(開(kāi)始函數(shù))Main
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import javax.jws.soap.SOAPBinding;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86155
* Date: 2022-05-18
* Time: 11:32
*/
public class Main {
public static User login(){
System.out.println("請(qǐng)輸入你的用戶名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("請(qǐng)輸入你的身份:1:管理員 0:普通用戶");
int choice = scanner.nextInt();
if(choice == 1){
return new AdminUser(name);
}else{
return new NormalUser(name);
}
}
//開(kāi)始整合
public static void main(String[] args) {
//1.準(zhǔn)備圖書
BookList bookList = new BookList();
//2.登錄
User user = login();
while(true){
//3.根據(jù)登錄的身份打印菜單 多態(tài)
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
}