【JAVA】—簡單的圖書管理系統
阿新 • • 發佈:2021-01-19
【JAVA】—簡單的圖書管理系統
簡介
物品:書籍 書架
人物:使用者 管理員
操作:增加 刪除 查詢 修改
相關類
Book 類-書籍
private String name;//書名 private String author;//作者 private Integer price;//價格 private String type;//型別 private boolean status;//是否借出 預設false public Book(String name, String author, Integer price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; }
Book List 類-書架
public int usedSize; private Book[] books; public BookList() { this.books = new Book[10]; //在引用操作前書架就有書 books[0] = new Book("三國","羅貫中",12,"小說"); books[1] = new Book("西遊記","吳承恩",32,"小說"); books[2] = new Book("水滸傳","施耐庵",52,"小說"); this.usedSize = 3; }
operation類-操作
先建立 package operation
//這是一個介面其好處在於: 讓操作和操作之間低耦合, 讓操作和使用者之間低耦合
public interface IOperation {
void work(BookList bookList);
}
接下來建立一組操作類, 每個類對應一個使用者的動作.
AddOperation DelOperation FindOperation RemoveOperation DisplayOperation BorrowOperation ReturnOperation ExitOperation
User 類-抽象類
User 類是一個抽象類, 每個子類需要做兩件事情
- 初始化對應的運算元組
- 實現 Menu 選單
public String name;
public IOperation[] operations;
public User(String name) {
this.name = name;
}
public abstract int menu();//使用者操作選單
public void doOperation(int choice, BookList bookList){
//看這個陣列當中 存的到底是哪些操作
this.operations[choice].work(bookList);
}
NormalUser類-普通使用者
public NormalUser(String name) {
super(name);
this.operations =new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("普通使用者選單");
System.out.println("=======================");
System.out.println("hello" + this.name+"歡迎來到圖書系統");
System.out.println("1.查詢圖書");
System.out.println("2.借閱圖書");
System.out.println("3.歸還圖書");
System.out.println("0.退出系統");
int choice = scanner.nextInt();
return choice;
}
AdminUser類-管理員
public AdminUser(String name) {
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("管理員選單");
System.out.println("=======================");
System.out.println("hello" + this.name+"歡迎來到圖書系統");
System.out.println("1.查詢圖書");
System.out.println("2.新增圖書");
System.out.println("3.刪除圖書");
System.out.println("4.顯示圖書");
System.out.println("0.退出系統");
int choice = scanner.nextInt();
return choice;
}
功能
1、簡單的登入
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入你的姓名");
String name = scanner.nextLine();
System.out.println("請輸入你的身份:1-》管理員 0:普通使用者");
int choice = scanner.nextInt();
if (choice == 1){
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
2、管理端
- 查閱書籍
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查詢書籍");
Scanner scanner = new Scanner(System.in);
System.out.println("輸入要查詢的書名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有這本書!");
System.out.println(book);
return;
}
}
System.out.println("沒有這本書!");
}
}
- 增加書籍
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("增加書籍 ");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入圖書的名字");
String name = scanner.nextLine();
System.out.println("請輸入圖書的作者");
String author = scanner.nextLine();
System.out.println("請輸入圖書的價格");
int price = scanner.nextInt();
System.out.println("請輸入圖書的型別");
String type = scanner.next();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
}
- 刪除書籍
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("刪除書籍");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入要刪除的書名");
String name = scanner.nextLine();
int i =0;
for (; i <bookList.getUsedSize() ; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
break;
}
}
if (i>=bookList.getUsedSize()){
System.out.println("no");
return;
}
int pos = i;
for (int j = pos; j <bookList.getUsedSize()-1 ; j++) {
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
int currentSize = bookList.getUsedSize();
bookList.setUsedSize(currentSize-1);
System.out.println("刪除書籍完畢!");
}
}
- 列印書籍列表
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("展示書籍");
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
- 退出
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系統");
System.exit(1);
}
}
3、使用者端
- 查詢書籍
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查詢書籍");
Scanner scanner = new Scanner(System.in);
System.out.println("輸入要查詢的書名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有這本書!");
System.out.println(book);
return;
}
}
System.out.println("沒有這本書!");
}
}
- 借閱書籍
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查詢書籍");
Scanner scanner = new Scanner(System.in);
System.out.println("輸入要查詢的書名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有這本書!");
System.out.println(book);
return;
}
}
System.out.println("沒有這本書!");
}
}
- 歸還書籍
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("歸還書籍");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入要歸還的書名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
book.setStatus(false);
System.out.println(book);
return;
}
}
System.out.println("沒有你要歸還的這本書!");
}
}
- 退出
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系統");
System.exit(1);
}
}
原始碼
GitHub原始碼
https://github.com/ZHANGHAHAAHA/javatest/tree/master/Study_01_18/src