簡易的圖書管理系統
- 概述
- 創建的包和類
- 主函式Main
- book層
- operation層
- user層
- 運行結果
概述
簡易的圖書管理系統中所包含的知識點主要為面向物件中的封裝,繼承,多型,介面,抽象類,包,順序表的增刪查改等技術,能夠實作管理員的普通用戶的對圖書管理的部分功能,詳細如下
創建的包和類

主函式Main
Main.java
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
/**
* ClassName: Main
* Description:
* date: 2021/4/23 21:16
* @author wt
* @since JDK 1.8
*/
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入姓名:");
String name = scanner.nextLine();
System.out.println("請選擇你的身份 1.管理員 2.普通用戶 ");
int choice = scanner.nextInt();
if (choice == 1) {
return new AdminUser(name);
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
user.doOpeartion(choice,bookList);
}
}
}
book層
Book.java
package book;
/**
* ClassName: Book
* Description:書的屬性
* date: 2021/4/23 15:02
* @author wt
* @since JDK 1.8
*/
public class Book {
private String name;//書名
private String author;//作者
private int price; //價格
private String type; //型別
private boolean flg; //是否借出
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 isFlg() {
return flg;
}
public void setFlg(boolean flg) {
this.flg = flg;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((flg == true)? "以借出" : "未借出")+
'}';
}
}
BookList.java
package book;
/**
* ClassName: BookList
* Description:
* date: 2021/4/23 15:03
* 書架
* @author wt
* @since JDK 1.8
*/
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList() {
books[0] = new Book("三國演義","羅貫中",56,"小說");
books[1] = new Book("西游記","吳承恩",77,"小說");
books[2] = new Book("水滸傳","施耐庵",66,"小說");
this.usedSize = 3;
}
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book getBooks(int pos) {
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
operation層
IOpeartion .java定義的一個介面類
package operation;
import book.BookList;
/**
* ClassName: IOpeartion
* Description:
* date: 2021/4/23 20:25
* 介面
* @author wt
* @since JDK 1.8
*/
public interface IOpeartion {
void work(BookList bookList);
}
下面所以的對圖書的操作類都繼承這個介面,
Add.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Add
* Description:添加圖書
* date: 2021/4/23 20:29
* 新增圖書
* @author wt
* @since JDK 1.8
*/
public class Add implements IOpeartion {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("新增圖書");
System.out.println("請輸入書名:");
String name = scanner.nextLine();
System.out.println("請輸入作者:");
String author = scanner.nextLine();
System.out.println("請輸入型別:");
String type = scanner.nextLine();
System.out.println("請輸入價格:");
int price = scanner.nextInt();
int curSize = bookList.getUsedSize();
Book book = new Book(name,author,price,type);
int pos = bookList.getUsedSize();
bookList.setBooks(pos,book);
bookList.setUsedSize(curSize+1);
System.out.println("新增成功!");
}
}
Borrow.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Borrow
* Description:
* date: 2021/4/23 20:31
* 借閱圖書
* @author wt
* @since JDK 1.8
*/
public class Borrow implements IOpeartion{
@Override
public void work(BookList bookList) {
System.out.println("借閱圖書");
System.out.println("請輸入書名:");
Scanner scanner = new Scanner(System.in);
String bookname = scanner.nextLine();
System.out.println();
for (int i = 0; i < bookList.getUsedSize(); i++) {
//將i下標的這本書放到book里
Book book = bookList.getBooks(i);
//取出這個書的書名與要輸入的書名bookname比較
if (book.getName().equals(bookname)) {
book.setFlg(true);
System.out.println("借閱成功!");
return;
}
}
//
System.out.println("沒有這本書!!!");
}
}
Del.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Del
* Description:
* date: 2021/4/23 20:32
* 洗掉圖書
* @author wt
* @since JDK 1.8
*/
public class Del implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("洗掉圖書");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入書名:");
String name = scanner.nextLine();
int pos = -1;
for (int i = 0; i <bookList.getUsedSize() ; i++) {
Book book = bookList.getBooks(i);
if (book.getName().equals(name)) {
pos = i;
break;
}
}
if (pos == -1) {
System.out.println("沒有這本書!");
return;
}
//有效個數用curSize表示
int curSize = bookList.getUsedSize();
for (int i = pos; i < curSize-1; i++) {
//順序表洗掉操作
//[i] = [i+1]
Book book = bookList.getBooks(i+1);
bookList.setBooks(i,book);
}
bookList.setUsedSize(curSize-1);
//curSize的一直沒有變化,curSize-1始終表示最后一個有效個數的位置的下標
//置為空,因為books為陣列,存放的為參考型別,需要釋放記憶體
bookList.setBooks(curSize-1,null);
}
}
Display.java
package operation;
import book.BookList;
/**
* ClassName: Display
* Description:顯示圖書
* date: 2021/4/23 20:33
* 顯示圖書
* @author wt
* @since JDK 1.8
*/
public class Display implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("顯示圖書");
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.getBooks(i));
}
}
}
Exit.java
package operation;
import book.BookList;
/**
* ClassName: Exit
* Description:
* date: 2021/4/23 20:34
* 退出系統
* @author wt
* @since JDK 1.8
*/
public class Exit implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("退出系統");
System.exit(0);
}
}
Find.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Find
* Description:查找圖書
* date: 2021/4/23 20:35
*
* @author wt
* @since JDK 1.8
*/
public class Find implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("查找圖書");
System.out.println("請輸入書名:");
Scanner scanner = new Scanner(System.in);
String bookname = scanner.nextLine();
System.out.println();
for (int i = 0; i < bookList.getUsedSize(); i++) {
//將i下標的這本書放到book里
Book book = bookList.getBooks(i);
//取出這個書的書名與要輸入的書名bookname比較
if (book.getName().equals(bookname)) {
System.out.println("找到這本書:");
System.out.println(book);
return;
}
}
//
System.out.println("沒有這本書!!!");
}
}
Return.java
package operation;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Return
* Description:
* date: 2021/4/23 20:36
* 歸還圖書
* @author wt
* @since JDK 1.8
*/
public class Return implements IOpeartion {
@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++) {
if (bookList.getBooks(i).getName().equals(name)) {
bookList.getBooks(i).setFlg(false);
System.out.println("歸還成功!");
return;
}
}
System.out.println("沒有這本書!!!");
}
}
user層
User.java
package user;
import book.BookList;
import operation.IOpeartion;
/**
* ClassName: User
* Description:
* date: 2021/4/23 19:22
* @author wt
* @since JDK 1.8
*/
public abstract class User {
public String name;
//1.準備一個介面陣列 存盤每一個物件
public IOpeartion[] IOpeartions;
public User(String name) {
this.name = name;
}
public abstract int menu();
//3.
public void doOpeartion(int choice, BookList bookList) {
this.IOpeartions[choice].work(bookList);
}
}
Admin.java
package user;
import operation.*;
import java.util.Scanner;
/**
* ClassName: AdminUser
* Description:
* date: 2021/4/23 20:40
* 管理員
* @author wt
* @since JDK 1.8
*/
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.IOpeartions = new IOpeartion[] {
new Exit(),
new Find(),
new Add(),
new Del(),
new Display(),
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
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.退出系統");
System.out.println("========================================");
int choice = scanner.nextInt();
return choice;
}
}
NormalUser.java
package user;
import operation.*;
import java.util.Scanner;
/**
* ClassName: NormalUser
* Description:
* date: 2021/4/23 20:41
* 普通用戶
* @author wt
* @since JDK 1.8
*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.IOpeartions = new IOpeartion[] {
new Exit(),
new Find(),
new Borrow(),
new Return()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("=======" + "Hello "+this.name + "=======");
System.out.println("1.查找圖書");
System.out.println("2.借閱圖書");
System.out.println("3.歸還圖書");
System.out.println("0.退出系統");
System.out.println("========================================");
int choice = scanner.nextInt();
return choice;
}
}
運行結果
1.管理員身份登錄

2.普通用戶身份登錄

如有需要原始碼檔案
鏈接: Github原始碼地址.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/280679.html
標籤:java
