java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
本文通過(guò)實(shí)例為大家分享了java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、背景介紹通過(guò)一段時(shí)間java編程的學(xué)習(xí),需要一個(gè)比較綜合的實(shí)例來(lái)進(jìn)行編程的練習(xí),是一個(gè)對(duì)前段時(shí)間所學(xué)內(nèi)容進(jìn)行總合提升的一個(gè)契機(jī)。選擇了圖書(shū)管理系統(tǒng),會(huì)用到的javaSE知識(shí)有:變量、包、繼承、類、接口、循環(huán)結(jié)構(gòu)等。是一個(gè)很綜合的典例。
二、核心需求1.用戶可以登錄到系統(tǒng)上 分為 管理員、普通用戶兩種角色,這兩種不同的角色根據(jù)自己的身份可以實(shí)現(xiàn)不同的操作。
普通用戶
a)查閱某個(gè)書(shū)籍的信息b)借閱書(shū)籍c) 歸還書(shū)籍d)退出程序
管理員
a)查閱某個(gè)書(shū)籍的信息b)增加書(shū)籍c) 刪除書(shū)籍d)查看書(shū)籍列表e)退出程序
程序框架結(jié)構(gòu)圖
1.User類
package booksystem;import booksystem.operation.IOperation;abstract public class User { protected String name;//定義書(shū)名 protected IOperation[] operations;//定義一個(gè)接口數(shù)組 public abstract int menu();//是用戶和管理員的父類,不進(jìn)行實(shí)例化,所以定義為抽象方法 public void doOperation(int choice,BookList bookList){ IOperation operation=this.operations[choice-1]; operation.work(bookList); }}
User類是NormalUser類和Admin類的父類,由于不需要實(shí)例化,將menu()函數(shù)定義為了抽象函數(shù)。
2.NormalUser類
package booksystem;import booksystem.operation.*;import booksystem.operation.IOperation;import java.util.Scanner;public class NormalUser extends User { public NormalUser(String name) { this.name = name; this.operations = new IOperation[] { new FindOperation(), new BorrowOperation(), new ReturnOperation(), new ExitOperation(), }; } @Override public int menu(){ System.out.println('~~~~~~~~~~~~~~~~~~'); System.out.println('Hello'+name+'Welcome to use booksyetem'); System.out.println('1.查閱書(shū)籍信息'); System.out.println('2.借閱書(shū)籍'); System.out.println('3.歸還書(shū)籍'); System.out.println('4.退出系統(tǒng)'); System.out.println('~~~~~~~~~~~~~~~~~~'); System.out.println('請(qǐng)輸入您的選擇:'); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice;//返回一個(gè)整型數(shù) }}
NormalUser類針對(duì)與普通用戶而編寫(xiě),在代碼中定義了一個(gè)接口數(shù)組,在其中添加了普通用戶需要用到的查閱、借閱、歸還、退出系統(tǒng)的四大功能,在menu()函數(shù)中也按照同樣的順序,menu()函數(shù)是重寫(xiě)父類的,所以為了提醒編譯器,在函數(shù)頭前加上了 @Override進(jìn)行提示。
3.Admin類
package booksystem;import booksystem.operation.*;import java.util.Scanner;public class Admin extends User { public Admin(String name){ this.name=name; this.operations=new IOperation[]{ new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation(), new ExitOperation(), }; } @Override public int menu(){ System.out.println('~~~~~~~~~~~~~~~~~~'); System.out.println('Hello'+name+'Welcome to use booksyetem'); System.out.println('1.查閱書(shū)籍信息'); System.out.println('2.新增書(shū)籍信息'); System.out.println('3.刪除書(shū)籍信息'); System.out.println('4.退出系統(tǒng)'); System.out.println('~~~~~~~~~~~~~~~~~~'); System.out.println('請(qǐng)輸入您的選擇:'); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; }}
Admin類編寫(xiě)的思路和NormalUser類的思路相同,區(qū)別在于用戶界面的不同,對(duì)應(yīng)要使用的功能也不同,分別是查閱、新增、刪除和退出系統(tǒng)。
4.Book類
package booksystem;public class Book { private String name; private String author; private double price; private String type; private boolean isBorrowed = false; public Book(String name, String author, double price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } @Override public String toString() { return 'Book{' + 'name=' + name + ’’’ + ',author' + author + ’’’ + ',price=' + price + ',type=’' + ’’’ + type + ’’’ + ',isBorrow=' + isBorrowed + ’}’; } public String getName() { return name; } public boolean isBorrowed(){ return isBorrowed; } public void setBorrowed(boolean borrowed){ isBorrowed=borrowed; }}
Book類針對(duì)書(shū)籍,定義了有關(guān)書(shū)的屬性,作者、價(jià)格、名字、類別,重寫(xiě)了toString函數(shù),以及對(duì)于書(shū)的幾個(gè)常用的操作功能函數(shù),getName,以及判讀是否借出和定義書(shū)籍借出狀態(tài)的函數(shù)。
5.BookList類
package booksystem;public class BookList { private Book[] books=new Book[100];//定義一book數(shù)組 private int size=0; public BookList(){ books[0]=new Book('計(jì)算機(jī)網(wǎng)絡(luò)教程','郝文源',125,'專業(yè)書(shū)籍'); books[1]=new Book('盜墓筆記','南派三叔',150,'網(wǎng)絡(luò)小說(shuō)'); books[2]=new Book('三體','劉慈欣',178,'科幻小說(shuō)'); size = 3; }//給book數(shù)組中初始化一些書(shū) public Book getBook(int index){ return books[index]; } public void setBook(int index,Book book) { books[index]=book; } public int getSize(){ return size; } public void setSize(int size){ this.size=size; }}
BookList類中定義了一個(gè)book數(shù)組,并給數(shù)組中初始化了一些書(shū),定義了常用的功能函數(shù)
6.Main類
package booksystem;import java.util.Scanner;public class Main { public static void main(String[] args){ Object o=null; BookList booklist= new BookList(); User user=login();//上轉(zhuǎn)型,這里先調(diào)用了login()函數(shù),返回一個(gè)Admin對(duì)象或NormalUser對(duì)象 while(true){ int choice=user.menu(); user.doOperation(choice,booklist); }//在進(jìn)行退出系統(tǒng)的功能時(shí),會(huì)一直進(jìn)行循環(huán),menu()函數(shù)最終會(huì)返回一個(gè)整型數(shù),對(duì)應(yīng)選擇操作中的一項(xiàng) }public static User login() { System.out.println('請(qǐng)輸入您的姓名'); Scanner scanner = new Scanner(System.in); String name = scanner.next(); System.out.println('請(qǐng)輸入您的角色:1 管理員 0 普通用戶');//根據(jù)不同的選擇創(chuàng)建對(duì)應(yīng)的對(duì)象 int who = scanner.nextInt(); if (who == 1) { return new Admin(name); } return new NormalUser(name);}}
主函數(shù)中主要實(shí)現(xiàn)了login()函數(shù),根據(jù)登錄系統(tǒng)用戶的選擇,決定不同的身份,返回兩種對(duì)象中的一種,在while循環(huán)中,只要不進(jìn)行exit功能,循環(huán)便會(huì)一直執(zhí)行。
7.IOperation
package booksystem.operation;import booksystem.BookList;public interface IOperation { void work(BookList bookList);}
9.AddOperation
package booksystem.operation;import booksystem.Book;import booksystem.BookList;import java.util.Scanner;public class AddOperation implements IOperation { @Override public void work(BookList bookList){ Scanner scanner=new Scanner(System.in); System.out.println('新增書(shū)籍'); System.out.println('請(qǐng)輸入新書(shū)籍的名稱'); String name=scanner.next(); System.out.println('請(qǐng)輸入新書(shū)籍的作者'); String author=scanner.next(); System.out.println('請(qǐng)輸入新書(shū)籍的價(jià)格'); double price=scanner.nextDouble(); System.out.println('請(qǐng)輸入新書(shū)籍的類別'); String type=scanner.next(); Book newBook=new Book(name,author,price,type); int curSize=bookList.getSize(); bookList.setBook(curSize,newBook); bookList.setSize(curSize+1); System.out.println('新增書(shū)籍成功'); }}
10.BorrowOperation
package booksystem.operation;import booksystem.Book;import booksystem.BookList;import java.util.Scanner;public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner=new Scanner(System.in); System.out.println('借閱書(shū)籍'); System.out.println('請(qǐng)輸入要借閱的書(shū)籍的名稱'); String name=scanner.next(); int i=0; for(;i<bookList.getSize();i++) { if(bookList.getBook(i).getName().equals(name)){ break; } } if(i>=bookList.getSize()){ System.out.println('未找到指定的書(shū)籍,無(wú)法借閱!'); return; } Book currentBook=bookList.getBook(i); if(currentBook.isBorrowed()){ System.out.println('該書(shū)籍已經(jīng)被借出!'); return; } bookList.getBook(i).setBorrowed(true); System.out.println('借書(shū)成功!'); }}
11.DelOperation
package booksystem.operation;import booksystem.BookList;import java.util.Scanner;public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { Scanner scanner=new Scanner(System.in); System.out.println('刪除書(shū)籍'); System.out.println('請(qǐng)輸入要?jiǎng)h除的書(shū)籍的名稱'); String name=scanner.next(); int i=0; for(;i<bookList.getSize();i++) { if(bookList.getBook(i).getName().equals(name)){ break; } } if(i>=bookList.getSize()){ System.out.println('您輸入的書(shū)籍在+'+name+'在系統(tǒng)中沒(méi)有找到!刪除失敗!'); return; } if(i==bookList.getSize()-1) { bookList.setSize(bookList.getSize()-1); System.out.println('刪除成功!'); return; } bookList.setBook(i,bookList.getBook(bookList.getSize()-1)); bookList.setSize(bookList.getSize()-1); System.out.println('刪除成功!'); }}
13.ExitOperation
package booksystem.operation;import booksystem.BookList;public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println('退出程序'); System.exit(0); }}
14.FindOperation
package booksystem.operation;import booksystem.BookList;public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println('退出程序'); System.exit(0); }}
15.ReturnOperation
package booksystem.operation;import booksystem.BookList;import booksystem.Book;import java.util.Scanner;public class ReturnOperation implements IOperation{@Override public void work(BookList bookList){ Scanner scanner=new Scanner(System.in); System.out.println('歸還書(shū)籍'); System.out.println('請(qǐng)輸入要還的書(shū)籍的名稱'); String name=scanner.next(); int i=0; for(;i<bookList.getSize();i++) { Book book=bookList.getBook(i); if(book.getName().equals(i)) { break; } } if(i>=bookList.getSize()) { System.out.println('書(shū)籍沒(méi)有找到,無(wú)法歸還'); return; } Book currentBook=bookList.getBook(i); if(!currentBook.isBorrowed()) { System.out.println('這本書(shū)沒(méi)有借出,無(wú)法歸還'); } currentBook.setBorrowed(false); System.out.println('歸還書(shū)籍成功'); return;}}四、編程截圖及測(cè)試圖
包和類放置圖
運(yùn)行截圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決2. 父div高度不能自適應(yīng)子div高度的解決方案3. ASP動(dòng)態(tài)include文件4. 不要在HTML中濫用div5. Vue中原生template標(biāo)簽失效如何解決6. XML入門的常見(jiàn)問(wèn)題(三)7. XML 非法字符(轉(zhuǎn)義字符)8. vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. js開(kāi)發(fā)中的頁(yè)面、屏幕、瀏覽器的位置原理(高度寬度)說(shuō)明講解(附圖)
