JavaSE實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼
前言
這篇博客是在學(xué)習(xí)了一部分Java基礎(chǔ)語法之后的練習(xí)項(xiàng)目,通過這個(gè)小項(xiàng)目的練習(xí),對Java中的類和對象,抽象類和接口,面向?qū)ο蟮睦^承、多態(tài)和封裝、組合等進(jìn)行熟悉理解;抽象出不同的對象,將對象進(jìn)行合理的設(shè)計(jì),完成對象之間的交互,面向?qū)ο筮M(jìn)行編程。
1. 項(xiàng)目需求
圖書管理系統(tǒng),面向管理員和普通用戶使用,對管理員的開放的功能有:添加圖書,刪除圖書,查找圖書,顯示圖書和退出程序等;對普通用戶的開放的功能有:查找圖書,借閱圖書,歸還圖書和退出程序。
2. 實(shí)現(xiàn)思路
先抽象提取出不同的對象,首先想到的對象是用戶和書,
用戶可分為管理員和普通用戶,將管理員和普通用戶共有的屬性設(shè)置為一個(gè)父類User,管理員和普通用戶繼承User,可以先將父類設(shè)置為普通類,在寫碼過程如果合適可以修改為抽象類;
系統(tǒng)對書進(jìn)行管理,書本身就可以抽象為一個(gè)對象;對多本書進(jìn)行操作管理,書架也可以抽象為一個(gè)對象;所以書和書架用兩個(gè)類來實(shí)現(xiàn),書架可以設(shè)計(jì)為一個(gè)數(shù)組;
這里系統(tǒng)對書進(jìn)行管理,其實(shí)就是對書架對應(yīng)數(shù)組進(jìn)行操作,不同的操作可以通過一個(gè)標(biāo)準(zhǔn)去實(shí)現(xiàn),也就是可以定義一個(gè)接口,每一個(gè)操作類去實(shí)現(xiàn)這個(gè)接口,完成向上轉(zhuǎn)型,通過接口實(shí)現(xiàn)多態(tài);
然后寫代碼的過程中思考完善各個(gè)對象的成員,最終實(shí)例化對象,通過不同對象之間的交互完成管理系統(tǒng)。
3. 代碼實(shí)現(xiàn)
包的設(shè)計(jì)

book包
Book類
package book;
public class Book {
private String name;//書名
private String author;//書的作者
private int price;//書的價(jià)格
private String type;//書的類型
private boolean borrowed;//是否被借出,默認(rèn)false
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 getBorrowed() {
return borrowed;
}
public void setBorrowed(Boolean borrowed) {
this.borrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", borrowed=" + ((borrowed==true) ? "已借出" : "未借出") +
'}';
}
}
BookList類
package book;
public class BookList {
private Book[] books = new Book[100];
private int usedsize;
//書架當(dāng)中默認(rèn)有三本書
public BookList() {
this.books[0] = new Book("三國演義","羅貫中",66,"小說");
this.books[1] = new Book("西游記","吳承恩",68,"小說");
this.books[2] = new Book("紅樓夢","曹雪芹",64,"小說");
this.usedsize = 3;
}
public int getUsedsize() {
return usedsize;
}
public void setUsedsize(int usedsize) {
this.usedsize = usedsize;
}
public Book getBooks(int pos){
return this.books[pos];
}
public void setBooks(Book book, int pos) {
this.books[pos] = book;
}
}
operations包
IOperation接口
package operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
AddOperation類
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增圖書");
//輸入圖書信息
System.out.println("輸入新增圖書名稱:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("輸入新增圖書作者:");
String author = scanner.nextLine();
System.out.println("輸入新增圖書價(jià)格:");
int price = scanner.nextInt();
scanner.nextLine();
System.out.println("輸入新增圖書類型:");
String type = scanner.nextLine();
Book book = new Book(name, author, price, type);
//獲取存書位置
int currentSize = bookList.getUsedsize();
//把書放到書架(數(shù)組)
bookList.setBooks(book, currentSize);
//書的總數(shù)加1
bookList.setUsedsize(currentSize+1);
System.out.println("添加成功!");
}
}
BorrowOperation類
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借閱圖書");
System.out.println("輸入要借閱圖書的名稱:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedsize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if (name.equals(book.getName())){
if(book.getBorrowed()){
System.out.println("該書已經(jīng)被借出!");
return;
}else {
book.setBorrowed(true);
System.out.println("借閱成功!");
return;
}
}
}
System.out.println("沒有找到你要借閱的書");
}
}
DelOperation類
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("刪除圖書");
System.out.println("輸入要?jiǎng)h除圖書的名稱:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedsize();
//找到并記錄要?jiǎng)h除書的下標(biāo)
int index = -1;
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if (name.equals(book.getName())){
index = i;
break;
}
}
if(-1 == index) {
System.out.println("沒有找到要?jiǎng)h除的這本書!");
}else {
for (int i = index; i < currentSize-1; i++) {
Book book = bookList.getBooks(i+1);
bookList.setBooks(book, i);
}
//每次刪除,都要將原來最后一本書位置的引用置空
bookList.setBooks(null,currentSize-1);
bookList.setUsedsize(currentSize-1);
System.out.println("刪除成功");
}
}
}
DisplayOperation類
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("顯示圖書");
int currentSize = bookList.getUsedsize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
System.out.println(book);
}
}
}
ExitOperation類
package operations;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系統(tǒng)");
System.exit(0);
}
}
FindOperation類
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找圖書");
System.out.println("輸入要查找圖書的名稱:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedsize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if (name.equals(book.getName())){
System.out.println("找到了,該書信息如下:");
System.out.println(book);
return;
}
}
System.out.println("沒有找到這本書!");
}
}
ReturnOperation類
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("歸還圖書");
System.out.println("輸入要?dú)w還圖書的名稱:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedsize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if (name.equals(book.getName())){
if(book.getBorrowed()){
book.setBorrowed(false);
System.out.println("歸還成功!");
return;
}else {
System.out.println("該書未借出!");
return;
}
}
}
System.out.println("沒有找到你要?dú)w還的書");
}
}
user包
User類
public abstract class User {
protected String name;
protected IOperation[] iOperation;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
iOperation[choice].work(bookList);
}
}
AdminUser類
package user;
import operations.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
System.out.println("---------------------------------");
System.out.println("Hello 管理員:>"+""+name+" 歡迎來到圖書管理系統(tǒng)!");
System.out.println(" 1.查找圖書 2.新增圖書");
System.out.println(" 3.刪除圖書 4.顯示圖書");
System.out.println(" 0.退出系統(tǒng)");
System.out.println("---------------------------------");
System.out.println("請輸入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
NormalUser類
package user;
import operations.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("---------------------------------");
System.out.println("Hello 用戶:>"+name+" 歡迎來到圖書管理系統(tǒng)!");
System.out.println(" 1.查找圖書 2.借閱圖書");
System.out.println(" 3.歸還圖書 0.退出系統(tǒng)");
System.out.println("---------------------------------");
System.out.println("請輸入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
Main類
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
System.out.println("請輸入你的姓名:");
Scanner scanner = new Scanner(System.in);
String userName = scanner.nextLine();
System.out.println("請確認(rèn)你的身份:> |1->管理員|0->普通用戶|");
int choice = scanner.nextInt();
if(1==choice){
return new AdminUser(userName);
}else{
return new NormalUser(userName);
}
}
public static void main(String[] args) {
//準(zhǔn)備數(shù)據(jù)
BookList bookList = new BookList();
//登錄
User user = login();
while (true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
}
4. 實(shí)現(xiàn)效果
管理員

普通用戶

以上就是JavaSE實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaSE圖書管理系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Spring Data JPA使用@Query注解(Using @Query)
本篇文章主要介紹了詳解Spring Data JPA使用@Query注解(Using @Query),具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
java實(shí)現(xiàn)voctor按指定方式排序示例分享
這篇文章主要介紹了java實(shí)現(xiàn)voctor按指定方式排序示例,需要的朋友可以參考下2014-03-03
Java設(shè)計(jì)模式之java模板方法模式詳解
這篇文章主要介紹了Java設(shè)計(jì)模式模板方法模式(Template)用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09

