Java面向物件-- “ 圖書管理系統 ”
執行環境:IDEA
1 需求
1、實現根據不同身份登陸;
2、使用者身份:管理員、普通使用者
管理員實現功能:增加圖書、刪除圖書、檢視所有書籍的列表、查閱某個書籍的資訊、退出程式;
普通使用者:借閱書籍、歸還書籍、查閱某個書籍的資訊、退出程式。
2 程式碼實現
我們找到這個系統的一個個的物件,逐步來實現。
2.1 Book、BookList
圖書管理系統,首先最重要的就是書。
在建立類裡面的屬性時,明確封裝是面向物件的重要核心之一,設計這個 book 類的時候,book 類的屬性,儘可能封裝起來,用 private,使其他的程式猿能不用理解具體細節,降低使用成本。
package Test1_28_LibraryManagementSystem;
public class Book {
private String name;
private String author;
private double price;
private String type;
// 預設是false ,但是顯示的寫出來可讀性更好
private boolean isBorrowed = false;
// 每本書的名字、價格、作者、型別,可能不同
// 使用構造方法,構造的時候以傳入引數的形式,確定具體的值
// 構造例項時,顯示的建立相關引數,來傳入相關資訊
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
// 重寫 toString 方法才能在列印書籍資訊時,以字串的樣子打印出來
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
// getter / setter 和封裝是相悖的 ,但是封裝並不是百分百藏起來,完全包裹起來呼叫者不好使用。
public String getName() {
return name;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
}
為了記錄所有書籍資訊,建立一個書籍列表
package Test1_28_LibraryManagementSystem;
public class BookList {
Book books[] = new Book[100];
int size = 0;
// 這樣就寫死了這些書,建立例項它們就存在
public BookList() {
books[0] = new Book("西遊記","吳承恩",50.0,"古典名著");
books[1] = new Book("水滸傳","施耐庵",50.0,"古典名著");
books[2] = new Book("三國演義","羅貫中",50.0,"古典名著");
books[3] = new Book("紅樓夢","曹雪芹",50.0,"古典名著");
size = 4;
}
// 通過 getter和setter 來獲取和修改書籍列表
public Book getBook(int index) {
return books[index];
}
public void setBook(int index,Book book) {
books[index] = book;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
2.2 使用者
管理員
package Test1_28_LibraryManagementSystem;
import Test1_28_LibraryManagementSystem.IOperation.*;
import java.util.Scanner;
public class Admin extends User{
public Admin(String name) {
this.name = name;
this.operations = new IOperation[]{
new AddOperation(),
new DelOperation(),
new DisplayBookList(),
new FindOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
System.out.println("*********************");
System.out.println(" 1、新增書籍 ");
System.out.println(" 2、刪除書籍 ");
System.out.println(" 3、展示書籍列表 ");
System.out.println(" 4、查詢某本書籍 ");
System.out.println(" 5、退出系統 ");
System.out.println("*********************");
System.out.println("請輸入您的選擇:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
2、普通使用者
package Test1_28_LibraryManagementSystem;
import Test1_28_LibraryManagementSystem.IOperation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
this.name = name;
this.operations = new IOperation[]{
new BorrowOperation(),
new ReturnOperation(),
new FindOperation(),
new ExitOperation()
};
}
public int menu(){
System.out.println("*********************");
System.out.println(" 1、借閱書籍 ");
System.out.println(" 2、歸還書籍 ");
System.out.println(" 3、查詢某本書籍 ");
System.out.println(" 4、退出系統 ");
System.out.println("*********************");
System.out.println("請輸入您的選擇:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
3、使用者
建立抽象類 User ,這樣在使用時,呼叫者可以不關注是管理員還是普通使用者,管理員和使用者繼承 User 。
package Test1_28_LibraryManagementSystem;
import Test1_28_LibraryManagementSystem.IOperation.IOperation;
abstract public class User {
protected String name;
protected IOperation[] operations;
public abstract int menu();
public void doOperation(int choice,BookList bookList){
this.operations[choice - 1].work(bookList);
}
}
2.3 操作
設計使用者操作,每種類都可以設定成一個類,每個操作類中都有一個 work 方法,public 修飾、返回void、傳入BookList。因為他們有共性,就可以把共性資訊提取出來,做成一個介面。(消除重複 並且 在不相關的類之間建立聯絡)
當前 IOperation 這個介面其實就是把所有的使用者操作給統一規範起來了,什麼樣的類作為一種使用者的操作?只要實現了這個介面,就可以作為使用者的操作。
1、介面
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
public interface IOperation {
void work(BookList bookList);
}
注意:
如果多各類之間,公共的部分只有方法的話,此時使用介面;
如果多個類之間,公共的部分不光有方法還有屬性,此時使用繼承(普通父類 / 抽象父類)。
2.3.1 管理員操作
1、新增書籍
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.Book;
import Test1_28_LibraryManagementSystem.BookList;
import java.util.Scanner;
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.next();
System.out.println("請輸入書籍作者的名字:");
String author = scanner.next();
System.out.println("請輸入書籍的價格:");
Double price = scanner.nextDouble();
System.out.println("請輸入書籍的型別:");
String type = scanner.next();
Book newBook = new Book(name,author,price,type);
bookList.setBook(bookList.getSize(),newBook);
bookList.setSize(bookList.getSize() + 1);
}
}
2、刪除書籍
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
import java.util.Scanner;
import java.util.jar.JarOutputStream;
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.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
if(name.equals(bookList.getBook(i).getName())) {
if(i == bookList.getSize() - 1) {
bookList.setSize(bookList.getSize() - 1);
System.out.println("刪除成功");
return;
}
bookList.setBook(i,bookList.getBook(bookList.getSize() - 1));
bookList.setSize(bookList.getSize() - 1);
System.out.println("刪除成功");
return;
}
}
if (i >= bookList.getSize() - 1) {
System.out.println("未找到《" + name + "》,刪除失敗");
}
}
}
3、展示書籍列表
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
public class DisplayBookList implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("展示書籍列表 >> ");
for (int i = 0; i < bookList.getSize(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
4、查詢某本書籍資訊
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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);
System.out.println("請輸入要查詢書籍的書名:");
String name = scanner.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
if(bookList.getBook(i).getName().contains(name)) {
System.out.println(bookList.getBook(i));
System.out.println("查詢成功");
return;
}
}
if(i >= bookList.getSize() - 1) {
System.out.println("未找到《" + name + "》,查詢失敗");
}
}
}
5、退出系統
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系統 >> ");
System.exit(0);
}
}
2.3.2 使用者操作
1、借閱書籍
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
import java.util.Scanner;
public class BorrowOperation 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 i = 0;
for (; i < bookList.getSize(); i++) {
if(name.equals(bookList.getBook(i).getName())) {
if(!bookList.getBook(i).isBorrowed()) {
bookList.getBook(i).setBorrowed(true);
System.out.println("借閱成功");
return;
}
System.out.println("《" + name + "》已被借閱,借閱失敗");
return;
}
}
if(i >= bookList.getSize() - 1) {
System.out.println("未找到 《" + name + "》,借閱失敗");
}
}
}
2、歸還書籍
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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);
System.out.println("請輸入要歸還書籍的書名:");
String name = scanner.next();
for (int i = 0; i < bookList.getSize(); i++) {
if(name.equals(bookList.getBook(i).getName())) {
if(!bookList.getBook(i).isBorrowed()) {
System.out.println("《" + name + "》未被借閱,歸還失敗");
return;
}
bookList.getBook(i).setBorrowed(false);
System.out.println("歸還成功");
return;
}
}
System.out.println("未找到《" + name + "》,歸還失敗");
}
}
3、查詢某本書的資訊( 與管理員的查詢相同 )
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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);
System.out.println("請輸入要查詢書籍的書名:");
String name = scanner.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
if(bookList.getBook(i).getName().contains(name)) {
System.out.println(bookList.getBook(i));
System.out.println("查詢成功");
return;
}
}
if(i >= bookList.getSize() - 1) {
System.out.println("未找到《" + name + "》,查詢失敗");
}
}
}
4、退出系統( 與管理員的查詢相同 )
package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系統 >> ");
System.exit(0);
}
}
2.4 呼叫
package Test1_28_LibraryManagementSystem;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookList bookList = new BookList();
// 向上轉型,login返回管理員或普通使用者
User user = login();
while (true) {
//多型
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
private static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入您的姓名:");
String name = scanner.next();
System.out.println(name + "歡迎使用!");
System.out.println("請選擇您的身份:");
System.out.println(" 0、管理員 1、使用者 ");
int who = scanner.nextInt();
if(who == 0){
return new Admin(name);
}
return new NormalUser(name);
}
}