老鐵們好~~在學習繼承,封裝,多態(tài),接口等語法后,為了鞏固知識,我們來寫一個小項目,加深對知識的理解,話不多說,咱們開始今天的學習吧!
1. 功能介紹及效果演示
進入程序后,提示輸入姓名,選擇身份,程序會根據(jù)選擇的身份彈出對應的菜單
管理員身份:
普通用戶身份:
查找功能:
輸入要查找的書名,如果有這本書就輸出這本書的信息,如果沒有輸出"沒有你要查找的書"
新增功能:
新增功能只有管理員用戶才能擁有,新增書籍需要輸入書籍相關(guān)信息
刪除功能:
刪除功能同樣只有管理員用戶才能擁有,輸入刪除的書名即可,如果書架沒有這本書提示沒找到
顯示功能:
顯示當前書架上的書籍及相關(guān)信息
修改功能:
管理員用戶專屬功能~輸入要修改的書的書名,接著提示輸入修改之后的信息
2. 整體框架的搭建
我們創(chuàng)建三個包:book,user,operation,book包中保管書相關(guān)的類,operation包中保管功能實現(xiàn)相關(guān)的類,user包中保管用戶相關(guān)的類;同時創(chuàng)建一個Main類,Main是程序的入口
2.1 book包
book包中有兩個類,一個是Book,一個是BookList
book類中包含了一本書的相關(guān)信息,書名作者,價格等,boolean類型的成員變量isBorrowed用于表示是否被借出,在類中如果不初始化默認是false,所以在構(gòu)造方法中沒有傳參數(shù)isBorrowed,成員變量都是private修飾的,其他類不能直接訪問,所以我們借助編譯器生成對應的set和get方法用于設(shè)置和獲取這些成員變量
編譯器生成get和set方法:
鼠標右鍵,點擊Generate…,
接著點擊Getter and Setter
這里我們還要重寫toString方法,讓我們輸出的對象是書籍的信息:
@Override
public String toString() {
return "書名:" + name +
"\t\t作者:" + author +
"\t\t價格:" + price +
"\t\t類型:" + type + "\t\t" +
(isBorrowed == true ? "已借出" : "未借出");
}
以下是Book類的代碼
public class Book {
private String name;//書名
private String author;//作者
private double price;//價格
private String type;//類型
private boolean isBorrowed;//是否被借出
public Book(String name, String author, double 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 double getPrice() {
return price;
}
public void setPrice(double 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 "書名:" + name +
"\t\t作者:" + author +
"\t\t價格:" + price +
"\t\t類型:" + type + "\t\t" +
(isBorrowed == true ? "已借出" : "未借出");
}
}
Book包中的另一個類是BookList,表示書架.成員變量是Book類的數(shù)組和當前書籍的數(shù)量
為了讓程序運行時,書架上默認有四本書,所以在構(gòu)造方法中對數(shù)組進行了初始化,設(shè)置數(shù)組大小是100,表示書架最多能放100本書.
public class BookList {
private Book[] books;//Book類的數(shù)組
private int usedSize;//當前書的數(shù)量
public BookList() {
this.books = new Book[100];
books[0] = new Book("西游記", "吳承恩", 99.9, "小說");
books[1] = new Book("水滸傳", "施耐庵", 99.9, "小說");
books[2] = new Book("紅樓夢", "曹雪芹", 99.9, "小說");
books[3] = new Book("三國演義", "羅貫中", 99.9, "小說");
this.usedSize = 4;
}
public Book getBook(int pos) {
//獲取書架上pos位置的書
return books[pos];
}
public void setBook(int pos, Book book) {
//在書架的pos位置放book這本書
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
2.2 operation包
operation包中主要包含了新增,刪除,修改等功能的實現(xiàn)(后面介紹),這里我們重點關(guān)注Ioperation這個接口,
public interface Ioperation {
void work(BookList bookList);
}
Ioperation要被Add,Borrow等這些類來實現(xiàn),需要重寫work方法,在work方法中實現(xiàn)各種功能
2.3 user包
user包中包含User類,NormalUser類,AdminUser類.為了實現(xiàn)不同用戶彈出對應的菜單,我們將User類定義為抽象類,讓管理員用戶這個類(AdminUser)和普通用戶類(NormalUser)繼承USer類,將User中的菜單方法重寫
public abstract class User {
protected String name;
public abstract int menu();
protected Ioperation[] operations;
public void Working(int choice, BookList bookList) {
this.operations[choice].work(bookList);
}
public User(String name) {
this.name = name;
}
}
管理員用戶:
public class AdminUser extends User {
@Override
public int menu() {
System.out.println(super.name + " 您好!歡迎來到圖書管理系統(tǒng)");
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(" 0.退出系統(tǒng)");
System.out.println("請輸入你的操作>>>");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
public AdminUser(String name) {
super(name);
this.operations = new Ioperation[]{
new Exit(),
new Search(),
new Add(),
new Delete(),
new Show(),
new Change()
};
}
}
普通用戶:
public class NormalUser extends User {
@Override
public int menu() {
System.out.println(super.name + " 您好!歡迎來到圖書管理系統(tǒng)");
System.out.println("*****普通用戶菜單*****");
System.out.println(" 1.查找圖書");
System.out.println(" 2.借閱圖書");
System.out.println(" 3.歸還圖書");
System.out.println(" 0.退出系統(tǒng)");
System.out.println("請輸入你的操作>>>");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
public NormalUser(String name) {
super(name);
this.operations = new Ioperation[]{
new Exit(),
new Search(),
new Borrow(),
new Return()
};
}
}
2.4 Main
Main中只要包含main方法(程序的入口)和login方法(用于登錄)
在login方法中:根據(jù)用戶的輸入來返回對應的對象.如果是管理員,返回AdminUser對象;如果是普通用戶,返回NormalUser對象.在main方法中:因為login返回的可能是普通用戶也可能是管理員用戶,所以使用USer類型的對象user來接收(向上轉(zhuǎn)型).接著user調(diào)用menu方法,如果user接收的是管理員用戶則調(diào)用管理員用戶的menu,否則調(diào)用普通用戶的menu(多態(tài)).接著調(diào)用Working方法來實現(xiàn)具體的功能
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.Working(choice, bookList);
}
}
}
3. 相關(guān)功能的實現(xiàn)
3.1 Add(新增圖書)
新增圖書的邏輯: 讓用戶輸入新增的圖書的信息并保存,new一個Book的對象,new對象的同時將信息通過參數(shù)進行初始化,將new好的書加在架BookList(也就是調(diào)用setBook方法),同時將UsedSize(當前書的數(shù)量)加1
public class Add implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("新增圖書");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入書名");
String bookName = scanner.nextLine();
System.out.println("請輸入作者");
String author = scanner.nextLine();
System.out.println("請輸入價格");
Double price = scanner.nextDouble();
System.out.println("請輸入書的類型");
String type = scanner.next();
Book book = new Book(bookName, author, price, type);
bookList.setBook(bookList.getUsedSize(), book);
bookList.setUsedSize(bookList.getUsedSize() + 1);
}
}
3.2 Borrow(借閱圖書)
借閱圖書的邏輯: 用戶輸入要借閱的書的名字,循環(huán)遍歷書架(BookList),如果找到了這本書,將isBorrowed設(shè)置為true表示已經(jīng)被借閱,如果沒找到則提示"沒找到這本書"
public class Borrow implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("借閱圖書");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入要借閱的書名");
String bookName = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (bookName.equals(book.getName())) {
book.setBorrowed(true);
System.out.println("借閱成功!!");
return;
}
}
System.out.println("沒找到這本書");
}
}
3.3 Change(修改信息)
修改信息的邏輯: 用戶輸入書名,循環(huán)遍歷書架,如果找到這本書,記錄下標.
接著讓用戶輸入修改之后的信息,通過set方法重新設(shè)置相關(guān)屬性.如果沒找到這本書,輸出"沒有找到這本書"
public class Change implements Ioperation{
@Override
public void work(BookList bookList) {
System.out.println("請輸入要修改的書名");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(bookName)) {
//找到了
System.out.println("請輸入修改后的書名");
String newBookName = scanner.nextLine();
System.out.println("請輸入修改后的作者");
String newBookAuthor = scanner.nextLine();
System.out.println("請輸入修改后的價格");
Double newBookPrice = scanner.nextDouble();
System.out.println("請輸入修改后的類型");
String newBookType = scanner.next();
book.setName(newBookName);
book.setAuthor(newBookAuthor);
book.setPrice(newBookPrice);
book.setType(newBookType);
book.setBorrowed(false);
System.out.println("修改成功!!!");
return;
}
System.out.println("沒有找到這本書!!!");
}
}
}
3.4 Delete(刪除圖書)
刪除圖書的邏輯 用戶輸入書名,循環(huán)遍歷書架,如果找到了這本書,記錄下標,如果沒找到則return,并且輸出"沒找到你要刪除的書",找到書之后如何刪除?
如圖,假設(shè)要刪除pos位置的書,只需要將pos+1位置覆蓋掉pos位置的數(shù)據(jù)即可,覆蓋之后再將最后一個位置置空(使用setBook)
public class Delete implements Ioperation {
@Override
public void work(BookList bookList) {
int pos = -1;
System.out.println("刪除圖書");
System.out.println("請輸入要刪除的書名");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(bookName)) {
pos = i;
break;
}
}
if (i >= bookList.getUsedSize()) {
System.out.println("沒找到你要刪除的書");
return;
}
//開始刪除操作
for (int j = pos; j < bookList.getUsedSize() - 1; j++) {
Book book = bookList.getBook(j + 1);//把j+1下標的書先拿出來
bookList.setBook(j, book);//j+1的書(book)給j
}
bookList.setUsedSize(bookList.getUsedSize() - 1);
bookList.setBook(bookList.getUsedSize(), null);//刪除最后一個位置的書
System.out.println("刪除成功!!");
}
}
3.5 Exit(退出系統(tǒng))
退出前先將書架里面的全部書籍都置空,置空后調(diào)用系統(tǒng)函數(shù)exit退出
public class Exit implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系統(tǒng)");
for (int i = 0; i < bookList.getUsedSize(); i++) {
bookList.setBook(i, null);
}
System.exit(0);
}
}
3.6 Return(歸還圖書)
歸還圖書的邏輯: 用戶輸入書名,循環(huán)遍歷書架,如果找到這本書,將書的sBorrowed屬性設(shè)置為false即可
public class Return implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("歸還圖書");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入要歸還的書名");
String bookName = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (bookName.equals(book.getName())) {
book.setBorrowed(false);
System.out.println("歸還成功!!");
return;
}
}
System.out.println("沒找到這本書");
}
}
3.7 Search(查找)
查找的邏輯: 用戶輸入書名,循環(huán)遍歷書架,如果找到了,打印這本書這個對象(重寫了toString),如果沒找到,輸出"沒找到你要查找的書!!!"
public class Search implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("請輸入要查找的書名");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(bookName)) {
System.out.println("找到了");
System.out.println(book);
return;
}
}
System.out.println("沒找到你要查找的書!!!");
}
}
3.8 Show(展示書架)
展示書架的邏輯: 因為我們重寫了toString,所以循環(huán)打印book對象即可文章來源:http://www.zghlxwxcb.cn/news/detail-842601.html
public class Show implements Ioperation {
@Override
public void work(BookList bookList) {
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
今天的內(nèi)容就到這里,感謝老鐵們的閱讀~文章來源地址http://www.zghlxwxcb.cn/news/detail-842601.html
到了這里,關(guān)于Java寶典-實戰(zhàn)小項目:圖書管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!