柚子快報(bào)邀請(qǐng)碼778899分享:java 圖書(shū)管理系統(tǒng)
柚子快報(bào)邀請(qǐng)碼778899分享:java 圖書(shū)管理系統(tǒng)
前言
這次文章將帶大家實(shí)現(xiàn)一個(gè)圖書(shū)管理系統(tǒng),界面如下:
找到對(duì)象
我們從上面就可以知道,我們需要?jiǎng)?chuàng)建兩個(gè)對(duì)象,一個(gè)是管理員對(duì)象,一個(gè)是普通用戶對(duì)象,由于這兩個(gè)對(duì)象都是用戶這一范疇,所以我們可以為他們創(chuàng)建一個(gè)父類,那我們可以將這三個(gè)類放在同一個(gè)包上,如下圖所示:
然后我們進(jìn)一步思考我們還需要什么類,由于這是一個(gè)圖書(shū)管理系統(tǒng),所以我們可以創(chuàng)建書(shū)這個(gè)類用來(lái)創(chuàng)建多本的書(shū),我們還可以創(chuàng)建一個(gè)書(shū)架用來(lái)存放書(shū)籍,這兩個(gè)類我們把他們放在同一個(gè)包上:
我們有很多個(gè)功能需要實(shí)現(xiàn),例如增加圖書(shū),刪除圖書(shū),查詢圖書(shū)等等…我們可以再創(chuàng)建一個(gè)包用來(lái)專門存放這些功能:
最后我們需要?jiǎng)?chuàng)建一個(gè)Main類用來(lái)組裝上面的代碼,使其成為一個(gè)管理系統(tǒng)。
代碼實(shí)現(xiàn)
book包
Book類
這里的書(shū)包括書(shū)名,作者,書(shū)的類型,價(jià)格以及書(shū)是否被借出的狀態(tài),這樣我們可以創(chuàng)建對(duì)應(yīng)的成員變量,為了有封裝的體現(xiàn),我們可以使用private修飾這些創(chuàng)元變量,然后通過(guò)Getter and Setter 來(lái)實(shí)現(xiàn)成員變量的訪問(wèn)就可以了.
由于我們會(huì)訪問(wèn)這本書(shū)的所有信息,我們就需要打印,這時(shí)候我們就要重寫(xiě)toString 方法,盡管IDEA編譯器會(huì)自動(dòng)幫我們實(shí)現(xiàn),但是對(duì)于書(shū)是否被借出的狀態(tài)打印,我們需要打印字符串(“未借閱” 和 “已借閱”)這兩種狀態(tài),但是這個(gè)成員變量是boolean類型的,所以我們?nèi)绻蝗バ薷膖oString 方法的話,我們就會(huì)得到false和true這兩種打印形式,那么我們就要稍微修改一下代碼,這時(shí)候我們可以使用三目表達(dá)式來(lái)進(jìn)行修改
如果直接使用編譯器自動(dòng)生成 toString 代碼:
public Book(String name, String author, String type, double price, boolean is_borrowed) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
this.is_borrowed = is_borrowed;
}
結(jié)合上述的要點(diǎn),我們可以得到下面的代碼:
public class Book {
private String name;
private String author;
private String type;
private double price;
private boolean is_borrowed;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
(is_borrowed == false ? " 未借閱": " 已借閱") +
'}';
}
public Book(String name, String author, String type, double price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isIs_borrowed() {
return is_borrowed;
}
public void setIs_borrowed(boolean is_borrowed) {
this.is_borrowed = is_borrowed;
}
}
BookList 類 (書(shū)架)
我們可以使用組合來(lái)將書(shū)放在代碼里,我們還可以先將三本書(shū)放在書(shū)架上,這時(shí)候我們可以通過(guò)構(gòu)造方法來(lái)初始化書(shū)架,我們還需要知道書(shū)架上目前有多少本書(shū),容量是多少,成員變量要相應(yīng)的寫(xiě)上去,并且通過(guò)getter和setter方法來(lái)來(lái)實(shí)現(xiàn)成員變量的訪問(wèn)…
public class BookList {
private Book[] books;
private int curSize;
private int count;
public BookList(){
books = new Book[10];
books[0] = new Book("三國(guó)演義","羅貫中","名著",30.12);
books[1] = new Book("西游記","吳承恩","名著",30.14);
books[2] = new Book("紅樓夢(mèng)","曹雪芹","名著",50.42);
curSize = 3;
count = 10;
}
public int getCurSize() {
return curSize;
}
public void setCurSize(int curSize) {
this.curSize = curSize;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Book getBooks(int i) {
return books[i];
}
public void setBooks(int pos,Book newBook) {
books[pos] = newBook;
}
}
user包
我們先來(lái)創(chuàng)建父類 User,需要姓名這一個(gè)成員變量,由于父類本身是要被繼承的,所以我們可以將其實(shí)現(xiàn)為抽象類,不同的身份有不同的菜單列表,既然都是菜單我們不妨在父類寫(xiě)一個(gè)抽象方法,提醒子類要記得寫(xiě)菜單,還有用戶會(huì)輸入不同的數(shù)字來(lái)實(shí)現(xiàn)不同的功能,這里我們可以創(chuàng)建一個(gè)功能實(shí)現(xiàn)的方法,這樣子我們就可以通過(guò)父類的引用來(lái)實(shí)現(xiàn)不同的子類所對(duì)應(yīng)的功能…
public abstract class User {
public String name;
protected Ioperate[] opertions;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void work(int choice, BookList bookList){
opertions[choice].operate(bookList);
}
}
管理者
public class Manager extends User{
public Manager(String name) {
super(name);
opertions = new Ioperate[] {
new Exit(),
new Find(),
new Add(),
new Delete(),
new Show()
};
}
@Override
public int menu() {
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.退出系統(tǒng)");
System.out.println("************************");
System.out.println("請(qǐng)輸入你的選擇:");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
普通用戶
public class DomesticConsumer extends User {
public DomesticConsumer(String name) {
super(name);
opertions = new Ioperate[] {
new Exit(),
new Find(),
new Borrow(),
new Return()
};
}
@Override
public int menu() {
System.out.println("*******普通用戶菜單*******");
System.out.println(" 1.查詢圖書(shū)");
System.out.println(" 2.借閱圖書(shū)");
System.out.println(" 3.歸還圖書(shū)");
System.out.println(" 0.退出系統(tǒng)");
System.out.println("************************");
System.out.println("請(qǐng)輸入你的選擇:");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
Ioperation包
接口
我們可以通過(guò)一個(gè)接口來(lái)實(shí)現(xiàn)功能的有機(jī)聯(lián)系,這樣我們就可以通過(guò)這個(gè)接口類型來(lái)實(shí)現(xiàn)不同的方法,我們知道這些方法幾乎都會(huì)操作書(shū)架,所以我們將書(shū)架作為參數(shù)進(jìn)行傳遞就可以了。
public interface Ioperate {
void operate(BookList bookList);
}
Add
我們直接采用尾插,我們需要判斷一下書(shū)架是否還能繼續(xù)放書(shū),需要將目前的圖書(shū)數(shù)目和書(shū)架容量進(jìn)行比較就可以了,接下來(lái)就是創(chuàng)建新書(shū),將新書(shū)放到書(shū)架即可,這時(shí)候我們要注意書(shū)架放書(shū)的代碼是否正確,我們使用IDEA編譯器只能實(shí)現(xiàn)Book[]類型的getter和setter,但是書(shū)的類型是Book,所以我們需要自己寫(xiě)一個(gè)放書(shū)的代碼:
public void setBooks(int pos,Book newBook) {
books[pos] = newBook;
}
為什么不直接寫(xiě)books[curSize] = newBook呢?
因?yàn)槲覀兒竺鎰h除圖書(shū)的代碼里也是需要對(duì)書(shū)架上的書(shū)進(jìn)行移動(dòng),會(huì)用到setter方法,所以為了代碼的通用性高,我們就多加了一個(gè)pos參數(shù)
public class Add implements Ioperate{
@Override
public void operate(BookList bookList) {
int count = bookList.getCount();
int curSize = bookList.getCurSize();
if(count == curSize){
System.out.println("書(shū)架已滿,無(wú)法再放入別的圖書(shū).......");
return;
}
System.out.println("請(qǐng)輸入你要增加的書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.println("請(qǐng)輸入作者名字");
String author = scan.nextLine();
System.out.println("請(qǐng)輸入圖書(shū)類型:");
String type = scan.nextLine();
System.out.println("請(qǐng)輸入圖書(shū)價(jià)格:");
double price = scan.nextDouble();
Book newBook = new Book(name,author,type,price);
bookList.setBooks(curSize,newBook);
bookList.setCurSize(curSize + 1);
}
}
在進(jìn)行刪除圖書(shū),查詢圖書(shū),這里我采用的是通過(guò)書(shū)名進(jìn)行遍歷訪問(wèn)的方式。 這時(shí)候我們就要小心,因?yàn)槲覀兪菍?duì)書(shū)架進(jìn)行操控的,所以我們要先拿到書(shū)這個(gè)對(duì)象,那我們就要看怎么拿出對(duì)應(yīng)的書(shū),由于編譯器只能實(shí)現(xiàn)Book[]類型的getter,所以我們還是要自己寫(xiě)一個(gè)拿書(shū)的代碼。 由于書(shū)名是String類型,所以可以使用equals方法經(jīng)行字符串的比對(duì)。所以我們拿到書(shū)后還要訪問(wèn)書(shū)的書(shū)名再進(jìn)行比對(duì)即可。
public Book getBooks(int i) {
return books[i];
}
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
//......
}
Delete
刪除書(shū)也很容易,我們需要把待刪除的書(shū)后面的書(shū)進(jìn)行前移
public class Delete implements Ioperate{
@Override
public void operate(BookList bookList) {
int curSize = bookList.getCurSize();
if(curSize == 0){
System.out.println("書(shū)架已空,無(wú)法繼續(xù)刪除......");
}
System.out.println("請(qǐng)輸入你要?jiǎng)h除圖書(shū)的書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for (int i = 0; i < curSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("刪除成功......");
for (int j = i; j < curSize - 1; j++) {
bookList.setBooks(j,bookList.getBooks(j+1));
}
bookList.setCurSize(curSize - 1);
return;
}
}
System.out.println("未查詢到該書(shū).....");
}
}
Borrow
public class Borrow implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("請(qǐng)輸入你要借閱圖書(shū)書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int curSize = bookList.getCurSize();
for (int i = 0; i < curSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
if(book.isIs_borrowed() == false){
System.out.println("該書(shū)已被你成功借閱......");
book.setIs_borrowed(true);
}else{
System.out.println("該書(shū)已被借閱,無(wú)法被再次借閱.....");
}
return;
}
}
System.out.println("未查詢到此書(shū)......");
}
}
Find
public class Find implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("輸入你要查詢的書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int count = bookList.getCurSize();
for (int i = 0; i < count; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println(book);
return;
}
}
System.out.println("未查詢到此書(shū)......");
}
}
Return
public class Return implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("請(qǐng)輸入你要?dú)w還書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int count = bookList.getCurSize();
for (int i = 0; i < count; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
if(book.isIs_borrowed() == true){
System.out.println("歸還成功......");
book.setIs_borrowed(false);
}else{
System.out.println("該圖書(shū)已被歸還到書(shū)架上,無(wú)需重復(fù)歸還......");
}
return;
}
}
System.out.println("未查詢到該圖書(shū).....");
}
}
Show
public class Show implements Ioperate{
@Override
public void operate(BookList bookList) {
int count = bookList.getCurSize();
for (int i = 0; i < count; i++) {
System.out.println(bookList.getBooks(i));
}
}
}
Exit
退出程序的代碼就是System.exit(數(shù)字),數(shù)字零表示正常退出的意思。
public class Exit implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("退出系統(tǒng)......");
System.exit(0);
}
}
登錄
我們?cè)費(fèi)ain類寫(xiě)一個(gè)login方法
public static User login(){
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的姓名:");
String name = scan.nextLine();
System.out.println("1.管理員 2.普通用戶");
System.out.println("請(qǐng)輸入你的身份:");
int identity = scan.nextInt();
if(identity == 1){
return new Manager(name);
}else{
return new DomesticConsumer(name);
}
}
主邏輯實(shí)現(xiàn)
我們先登錄,然后創(chuàng)建書(shū)架,接著打印菜單,用戶輸入指令,計(jì)算機(jī)進(jìn)行對(duì)應(yīng)的操作:
public static void main(String[] args) {
//創(chuàng)建書(shū)架
BookList bookList = new BookList();
//login
User user = login();
while(true){
//功能菜單選擇
int choice = user.menu();
//功能實(shí)現(xiàn)
user.work(choice,bookList);
}
}
最終代碼
Main類
import book.BookList;
import user.DomesticConsumer;
import user.Manager;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的姓名:");
String name = scan.nextLine();
System.out.println("1.管理員 2.普通用戶");
System.out.println("請(qǐng)輸入你的身份:");
int identity = scan.nextInt();
if(identity == 1){
return new Manager(name);
}else{
return new DomesticConsumer(name);
}
}
public static void main(String[] args) {
//創(chuàng)建書(shū)架
BookList bookList = new BookList();
//login
User user = login();
while(true){
//功能菜單選擇
int choice = user.menu();
//功能實(shí)現(xiàn)
user.work(choice,bookList);
}
}
}
user包
User
package user;
import Ioperation.Ioperate;
import book.BookList;
public abstract class User {
public String name;
protected Ioperate[] opertions;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void work(int choice, BookList bookList){
opertions[choice].operate(bookList);
}
}
Manager
package user;
import Ioperation.*;
import java.util.Scanner;
public class Manager extends User{
public Manager(String name) {
super(name);
opertions = new Ioperate[] {
new Exit(),
new Find(),
new Add(),
new Delete(),
new Show()
};
}
@Override
public int menu() {
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.退出系統(tǒng)");
System.out.println("************************");
System.out.println("請(qǐng)輸入你的選擇:");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
DomesticConsumer
package user;
import Ioperation.*;
import java.util.Scanner;
public class DomesticConsumer extends User {
public DomesticConsumer(String name) {
super(name);
opertions = new Ioperate[] {
new Exit(),
new Find(),
new Borrow(),
new Return()
};
}
@Override
public int menu() {
System.out.println("*******普通用戶菜單*******");
System.out.println(" 1.查詢圖書(shū)");
System.out.println(" 2.借閱圖書(shū)");
System.out.println(" 3.歸還圖書(shū)");
System.out.println(" 0.退出系統(tǒng)");
System.out.println("************************");
System.out.println("請(qǐng)輸入你的選擇:");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
book 包
Book
package book;
public class Book {
private String name;
private String author;
private String type;
private double price;
private boolean is_borrowed;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
(is_borrowed == false ? " 未借閱": " 已借閱") +
'}';
}
public Book(String name, String author, String type, double price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isIs_borrowed() {
return is_borrowed;
}
public void setIs_borrowed(boolean is_borrowed) {
this.is_borrowed = is_borrowed;
}
}
BookList
package book;
public class BookList {
private Book[] books;
private int curSize;
private int count;
public BookList(){
books = new Book[10];
books[0] = new Book("三國(guó)演義","羅貫中","名著",30.12);
books[1] = new Book("西游記","吳承恩","名著",30.14);
books[2] = new Book("紅樓夢(mèng)","曹雪芹","名著",50.42);
curSize = 3;
count = 10;
}
public int getCurSize() {
return curSize;
}
public void setCurSize(int curSize) {
this.curSize = curSize;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Book getBooks(int i) {
return books[i];
}
public void setBooks(int pos,Book newBook) {
books[pos] = newBook;
}
}
Ioperation包
Ioperate
package Ioperation;
import book.BookList;
public interface Ioperate {
void operate(BookList bookList);
}
Add
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Add implements Ioperate{
@Override
public void operate(BookList bookList) {
int count = bookList.getCount();
int curSize = bookList.getCurSize();
if(count == curSize){
System.out.println("書(shū)架已滿,無(wú)法再放入別的圖書(shū).......");
return;
}
System.out.println("請(qǐng)輸入你要增加的書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.println("請(qǐng)輸入作者名字");
String author = scan.nextLine();
System.out.println("請(qǐng)輸入圖書(shū)類型:");
String type = scan.nextLine();
System.out.println("請(qǐng)輸入圖書(shū)價(jià)格:");
double price = scan.nextDouble();
Book newBook = new Book(name,author,type,price);
bookList.setBooks(curSize,newBook);
bookList.setCurSize(curSize + 1);
}
}
Borrow
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Borrow implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("請(qǐng)輸入你要借閱圖書(shū)書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int curSize = bookList.getCurSize();
for (int i = 0; i < curSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
if(book.isIs_borrowed() == false){
System.out.println("該書(shū)已被你成功借閱......");
book.setIs_borrowed(true);
}else{
System.out.println("該書(shū)已被借閱,無(wú)法被再次借閱.....");
}
return;
}
}
System.out.println("未查詢到此書(shū)......");
}
}
Delete
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Delete implements Ioperate{
@Override
public void operate(BookList bookList) {
int curSize = bookList.getCurSize();
if(curSize == 0){
System.out.println("書(shū)架已空,無(wú)法繼續(xù)刪除......");
}
System.out.println("請(qǐng)輸入你要?jiǎng)h除圖書(shū)的書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for (int i = 0; i < curSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("刪除成功......");
for (int j = i; j < curSize - 1; j++) {
bookList.setBooks(j,bookList.getBooks(j+1));
}
bookList.setCurSize(curSize - 1);
return;
}
}
System.out.println("未查詢到該書(shū).....");
}
}
Exit
package Ioperation;
import book.BookList;
public class Exit implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("退出系統(tǒng)......");
System.exit(0);
}
}
Find
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Find implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("輸入你要查詢的書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int count = bookList.getCurSize();
for (int i = 0; i < count; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println(book);
return;
}
}
System.out.println("未查詢到此書(shū)......");
}
}
Return
package Ioperation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Return implements Ioperate{
@Override
public void operate(BookList bookList) {
System.out.println("請(qǐng)輸入你要?dú)w還書(shū)名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int count = bookList.getCurSize();
for (int i = 0; i < count; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
if(book.isIs_borrowed() == true){
System.out.println("歸還成功......");
book.setIs_borrowed(false);
}else{
System.out.println("該圖書(shū)已被歸還到書(shū)架上,無(wú)需重復(fù)歸還......");
}
return;
}
}
System.out.println("未查詢到該圖書(shū).....");
}
}
Show
package Ioperation;
import book.BookList;
public class Show implements Ioperate{
@Override
public void operate(BookList bookList) {
int count = bookList.getCurSize();
for (int i = 0; i < count; i++) {
System.out.println(bookList.getBooks(i));
}
}
}
柚子快報(bào)邀請(qǐng)碼778899分享:java 圖書(shū)管理系統(tǒng)
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。