運行環境: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 使用者
1、管理員
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/254530.html
標籤:java
