碼字不易,對你有幫助 點贊/轉發/關注 支持一下作者
微信搜公衆號:不會編程的程序圓
看更多幹貨,獲取第一時間更新
【數據結構輕鬆學】系列 Github :https://github.com/hairrrrr/EasyJava
本文的代碼已上傳至 Github
快期末了,如果各位對自己的課設沒有頭緒,不妨借鑑一下這個簡易的圖書管理系統。
如果你有問題,歡迎與我交流!
圖解實現難點
實現
book 包
Book.java
package book;
/*
* Description: 圖書類
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 18:52
*/
public class Book{
private String name;
private String author;
private String type;
private boolean isBorrowed;
public Book(String name, String author, String type) {
this.name = name;
this.author = author;
this.type = type;
}
// 注:alt + insert 建可以快速生成
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 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 + '\'' +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
BookList.java
package book;
/*
* Description: 書單
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 18:57
*/
public class BookList {
private Book[] books = new Book[100];
private int usedSize = 0;
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
// 初始化三本書
public BookList(){
books[0] = new Book("挪威的森林", "村上春樹", "小說");
books[1] = new Book("海邊的卡夫卡", "村上春樹", "小說");
books[2] = new Book("不能承受的生命之輕", "米蘭·昆德拉", "小說");
this.usedSize = 3;
}
}
operation 包
AddBook.java
package operation;
/*
* Description: 新增圖書
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:15
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddBook implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("========= 新增圖書 =========");
Scanner scan = new Scanner(System.in);
System.out.println("請輸入:書名");
String name = scan.nextLine();
System.out.println("請輸入:作者");
String author = scan.nextLine();
System.out.println("請輸入:書籍類型");
String type = scan.nextLine();
// 書單是否借出就不用單獨的初始化了,不初始化默認爲 false
int size = bookList.getUsedSize();
bookList.getBooks()[size] = new Book(name, author, type);
bookList.setUsedSize(size + 1); // 不能寫 size++ !
System.out.println("========= 成功! =========");
}
}
BorrowBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:15
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 借閱圖書 =========");
System.out.println("請輸入:書名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int i;
Book book = null;
for(i = 0; i < bookList.getUsedSize(); i++){
book = bookList.getBooks()[i];
if(book.getName().equals(name)){
break;
}
}
if(i == bookList.getUsedSize()) {
System.out.println("抱歉,未能找到 " + "《" + name + "》");
return;
}
if(book.isBorrowed()){
System.out.println("抱歉," + "《" + name + "》" + "已經借出,過兩天再來看看吧");
}
else {
book.setBorrowed(true);
System.out.println("========= 借書成功! =========");
}
}
}
DeleteBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:15
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DeleteBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 刪除圖書 =========");
System.out.println("請輸入:書名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int i;
Book book = null;
int usedSize = bookList.getUsedSize();
for(i = 0; i < usedSize; i++){
book = bookList.getBooks()[i];
if(book.getName().equals(name)){
break;
}
}
if(i == usedSize) {
System.out.println("抱歉,未能找到 " + "《" + name + "》");
return;
}
for(; i < usedSize - 1; i++){
bookList.getBooks()[i] = bookList.getBooks()[i + 1];
}
// 如果是尾刪,我們只是簡單的將數組已經使用的大小減一;對象依然被數組引用,會造成內存泄漏
bookList.getBooks()[usedSize - 1] = null;
bookList.setUsedSize(usedSize - 1);
System.out.println("========= 刪除成功 =========");
}
}
DisplayBooks.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:16
*/
import book.Book;
import book.BookList;
public class DisplayBooks implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("========= 展示圖書 =========");
for(int i = 0; i < bookList.getUsedSize(); i++){
System.out.println(bookList.getBooks()[i].toString());
/*上面的語句等價於下面的:
Book[] books = bookList.getBooks();
Book book = books[i];
System.out.println(book.toString());*/
}
}
}
Exit.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:17
*/
import book.BookList;
public class Exit implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("========= 退出系統 =========");
System.exit(0);
}
}
FindBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:16
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 查詢圖書 =========");
System.out.println("請輸入:書名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++){
Book book = bookList.getBooks()[i];
if(book.getName().equals(name)){
System.out.println(book.toString());
return;
}
}
System.out.println("抱歉,未能找到 " + "《" + name + "》");
}
}
ReturnBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:16
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 歸還圖書 =========");
System.out.println("請輸入:書名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++){
Book book = bookList.getBooks()[i];
if(book.getName().equals(name)){
bookList.getBooks()[i].setBorrowed(false);
System.out.println("========= 歸還歸還 =========");
return;
}
}
System.out.println("抱歉,未能找到 " + "《" + name + "》");
}
}
IOperation.java
package operation;
import book.BookList;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:17
*/
public interface IOperation {
void work(BookList bookList);
}
user 包
Admin.java
package user;
/*
* Description: 管理員
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:18
*/
import book.BookList;
import operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name){
super(name);
this.operations = new IOperation[]{
null,
new FindBook(),
new AddBook(),
new DeleteBook(),
new DisplayBooks(),
new Exit(),
};
}
@Override
public int menu() {
System.out.println("Hello, " + this.name);
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("***********************************");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
@Override
public void doOperation(BookList bookList, int choice) {
this.operations[choice].work(bookList);
}
}
NormalUser.java
package user;
/*
* Description: 普通用戶
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:19
*/
import book.BookList;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.operations = new IOperation[]{
null,
new FindBook(),
new BorrowBook(),
new ReturnBook(),
new DisplayBooks(),
new Exit(),
};
}
@Override
public int menu() {
System.out.println("Hello, " + this.name);
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("***********************************");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
@Override
public void doOperation(BookList bookList, int choice) {
this.operations[choice].work(bookList);
}
}
User.java
package user;
/*
* Description: 用戶的父類
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:28
*/
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] operations;
public User(String name){
this.name = name;
}
public abstract int menu();
public abstract void doOperation(BookList bookList, int choice);
}
Main.java
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:19
*/
import book.BookList;
import user.Admin;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scan = new Scanner(System.in);
System.out.println("請輸入用戶姓名");
String name = scan.nextLine();
System.out.println("請輸入選擇:0 -> 管理員 1 -> 用戶");
int choice = scan.nextInt();
if(choice == 0){
// 向上轉型
return new Admin(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
// 書單
BookList bookList = new BookList();
while(true) {
// 用戶:
User user = login();
// 應用運行時綁定,父類引用會使用子類的 menu 方法
int choice = user.menu();
user.doOperation(bookList, choice);
}
}
}
查看【0 基礎學 Java】更詳細的目錄: https://github.com/hairrrrr/EasyJava
不要忘記 star 呦~
歡迎大家在 評論區/私信 提出問題/指正錯誤,謝謝觀看。
我是程序圓,我們下次再見。