這篇“Java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的代碼怎么寫(xiě)”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的代碼怎么寫(xiě)”文章吧。
創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的渝水網(wǎng)站建設(shè)公司,渝水接單;提供網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行渝水網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
此圖書(shū)管理系統(tǒng)借助IDEA開(kāi)發(fā)工具實(shí)現(xiàn)
圖書(shū)館系統(tǒng)一共有兩種身份的訪問(wèn):
1.管理員身份:

2.普通用戶身份:

我們一共有三個(gè)包分別是book,operations,user實(shí)現(xiàn).


main函數(shù)主要進(jìn)行大致流程的進(jìn)行,圖書(shū)庫(kù)的初始化,圖書(shū)管理系統(tǒng)的登錄,以及具體操作的選擇,即實(shí)施.
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//1.先初始化圖書(shū)庫(kù),以及初始化:
BookList bookList = new BookList();
//2.登錄
User user = login();//向上轉(zhuǎn)型,User接受管理員或者用戶對(duì)象
//3.打印菜單,進(jìn)行具體操作
while(true) {
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
}登錄功能:
public static User login() {
System.out.println("請(qǐng)輸入你的姓名: ");
Scanner scanner = new Scanner(System.in);
String userName = scanner.nextLine();
System.out.println("請(qǐng)輸入你的身份: 1-> 管理員 2-> 用戶");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(userName);
}else {
return new NormalUser(userName);
}
}
因?yàn)橛袃商紫到y(tǒng),管理員和普通用戶,所以我們將共同屬性提取出來(lái)一個(gè)User抽象類(lèi),以達(dá)到代碼復(fù)用
package user;
import book.BookList;
import operations.IOperation;
public abstract class User {
protected String name;
IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
iOperations[choice].work(bookList);
}
}package user;
import operations.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu() {
System.out.println("歡迎: "+name+"來(lái)到圖書(shū)館");
System.out.println("**********************************");
System.out.println("1. 查找圖書(shū)");
System.out.println("2. 新增圖書(shū)");
System.out.println("3. 刪除圖書(shū)");
System.out.println("4. 顯示圖書(shū)");
System.out.println("0. 退出圖書(shū)");
System.out.println("**********************************");
System.out.println("請(qǐng)輸入你的操作: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}package user;
import operations.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu() {
System.out.println("歡迎: "+name+"來(lái)到圖書(shū)館");
System.out.println("**********************************");
System.out.println("1. 查找圖書(shū)");
System.out.println("2. 借閱圖書(shū)");
System.out.println("3. 歸還圖書(shū)");
System.out.println("0. 退出圖書(shū)");
System.out.println("**********************************");
System.out.println("請(qǐng)輸入你的操作: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}我們對(duì)book的屬性進(jìn)行書(shū)寫(xiě),以及在BookList種對(duì)圖書(shū)庫(kù)的書(shū)進(jìn)行初始化.
package book;
public class Book {
private String name;//書(shū)名
private String author;//作者
private int price;//價(jià)格
private String type;//書(shū)的類(lèi)型
private boolean isBorrowed;//書(shū)默認(rèn)未借出
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 isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
","+ ((isBorrowed == true) ? "該書(shū)已借出" : "該書(shū)未借出" )+
'}';
}
}package book;
public class BookList {
public Book[] books = new Book[100];
public int usedSize;//用來(lái)存當(dāng)前共有多少本書(shū)
/**
* 事先通過(guò)代碼塊
*
* 事先存進(jìn)去三本書(shū)
*/
{
books[0] = new Book("java","高斯林",95,"IT");
books[1] = new Book("C++","姚琳",93,"IT");
books[2] = new Book("python","馬瑟斯",80,"IT");
this.usedSize = 3;
}
public Book getPos(int pos) {
//獲取某一位置的書(shū)
return books[pos];
}
public void setBooks(Book book,int pos) {
//存儲(chǔ)一本書(shū) 到指定位置
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}我們的圖書(shū)管理系統(tǒng)有很多具體的操作,為了后面方便多態(tài),以及檢驗(yàn)錯(cuò)誤,所以我們實(shí)現(xiàn)一個(gè)具體的IOperation接口,每一個(gè)具體的操作去實(shí)現(xiàn)這個(gè)接口.
package operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}增加圖書(shū):
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("新增圖書(shū)! ");
System.out.println("請(qǐng)輸入要新增的圖書(shū)的名字: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("請(qǐng)輸入要新增的圖書(shū)的作者: ");
String author = scanner.nextLine();
System.out.println("請(qǐng)輸入要新增的圖書(shū)的價(jià)格: ");
int price = scanner.nextInt();
System.out.println("請(qǐng)輸入要新增的圖書(shū)的類(lèi)型: ");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
//1.獲取當(dāng)前書(shū)存放的位置
int curSize = bookList.getUsedSize();
//2.把書(shū)放在指定位置
bookList.setBooks(book,curSize);
//3.更新書(shū)的個(gè)數(shù)
bookList.setUsedSize(curSize+1);
}
}借閱圖書(shū):
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("借閱圖書(shū)! ");
System.out.println("請(qǐng)輸入要借閱的圖書(shū)的名字: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
if(book.isBorrowed()) {
System.out.println("該書(shū)已經(jīng)被借出! ");
}else {
book.setBorrowed(true);
System.out.println("借閱圖書(shū)成功! ");
return;
}
}
}
System.out.println("沒(méi)有你要借閱的書(shū)! ");
}
}刪除圖書(shū):
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("刪除圖書(shū)! ");
System.out.println("請(qǐng)輸入要?jiǎng)h除的圖書(shū): ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//查找圖書(shū)是否有此圖書(shū),記錄下標(biāo)
int index = -1;
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
index = i;
break;
}
}
if(index == -1) {
System.out.println("沒(méi)有 "+name+"這本書(shū)!");
return;
}
for (int i = index; i < bookList.getUsedSize()-1; i++) {
Book book = bookList.getPos(i+1);
bookList.setBooks(book,i);
}
//刪除的書(shū),要置空
bookList.setBooks(null, bookList.getUsedSize()-1);
bookList.setUsedSize(bookList.getUsedSize()-1);
}
}顯示圖書(shū):
package operations;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("顯示圖書(shū)! ");
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
System.out.println(book);
}
}
}退出圖書(shū):
package operations;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系統(tǒng)! ");
System.exit(0);
}
}查找圖書(shū):
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
//查找圖書(shū)
System.out.println("查找圖書(shū)! ");
System.out.println("請(qǐng)輸入要查找的圖書(shū): ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
System.out.println("找到了! ");
System.out.println(book);
return;
}
}
System.out.println("沒(méi)有這本書(shū)! ");
}
}歸還圖書(shū):
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("歸還圖書(shū)! ");
System.out.println("請(qǐng)輸入要?dú)w還的圖書(shū)的名字: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
book.setBorrowed(false);
System.out.println("歸還圖書(shū)成功! ");
return;
}
}
System.out.println("沒(méi)有你要?dú)w還的書(shū)! ");
}
}以上就是關(guān)于“Java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的代碼怎么寫(xiě)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:Java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的代碼怎么寫(xiě)
分享地址:http://www.chinadenli.net/article12/gghjdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、域名注冊(cè)、響應(yīng)式網(wǎng)站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)