目錄
一、該圖書管理系統(tǒng)涉及Java的知識點
二、該圖書管理系統(tǒng)包含的功能
一、該圖書管理系統(tǒng)涉及Java的知識點,如下:
- 數(shù)組的增刪查
- 抽象類
- 接口
- 面向?qū)ο蟮姆庋b、繼承和多態(tài)
二、該圖書管理系統(tǒng)包含的功能,如下:
- 圖書管理系統(tǒng)的使用人群分為兩種:①管理人員,②普通用戶
- 具體實現(xiàn):抽象類的繼承
User類(父類):
public abstract class User {
protected String name;
protected IOperation[] iOperations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doWork(int choice, BooklList booklList){
iOperations[choice].work(booklList);
}
}
AdminUser類(管理員)
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("========管理員菜單========");
System.out.println("hello "+this.name +"歡迎使用圖書管理系統(tǒng)");
System.out.println("1.查找圖書");
System.out.println("2.新增圖書");
System.out.println("3.刪除圖書");
System.out.println("4.顯示圖書");
System.out.println("0.退出系統(tǒng)");
System.out.println("==============================");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
?NormalUser類(普通用戶類)
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("========普通用戶菜單========");
System.out.println("hello "+this.name +"歡迎使用圖書管理系統(tǒng)");
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;
}
}
注:需要把上述的三各類放在同一個包里
?2. 設(shè)計一個Book類存放書的屬性,設(shè)計一個BookList類存放書的位置
具體實現(xiàn):在Book類中給所有的私有屬性提供了公共的接口,在BookList類中創(chuàng)建了一個名為books的數(shù)組并存放三本圖書另外設(shè)計了一個BookList的成員名為UsedSize來表示在books數(shù)組中存放書的數(shù)量。
Book類
public class Book {
private String name;//書名
private String author;//書的作者
private int price;//書的價格
private String type;//書的類型
private boolean isBorrowed;//是否借出
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)?" 已經(jīng)被借出 ":" 未被借出 ")+
'}';
}
}
BookList類
public class BooklList {
private Book[] books=new Book[1024];
private int usedSize;
public BooklList(){
books[0]=new Book("數(shù)據(jù)庫應(yīng)用原理","張三",45,"計算機");
books[1]=new Book("紅樓夢","曹雪芹",56,"小說");
books[2]=new Book("唐詩三百首","孫洙",35,"古詩詞");
this.usedSize=3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
/**
* 獲得pos位置的一本書
* @param pos
* @return
*/
public Book getPos(int pos){
if(pos>=0&&pos<books.length){
return this.books[pos];
}else
System.out.println("pos位置不合法");
return this.books[pos];
}
/**
* 添加pos下標(biāo)的一本書
* @param pos
* @param book
*/
public void setBook(int pos,Book book){
this.books[pos]=book;
}
}
注:?需要把上述兩個類放在當(dāng)一個包里
3.? 分別設(shè)計了七個類,每一個類實現(xiàn)一種功能;設(shè)計了一個接口把七個類串接起來
具體實現(xiàn):AddOperation(增加操作)、BorrowOperation(借閱操作)、DelOperation(刪除操作)、DisplayOperation(顯示操作)、ExitOperation(退出系統(tǒng))、FindOperation(查找操作)、ReturnOperation(歸還操作)和接口IOperation
AddOperation(增加操作)
public class AddOperation implements IOperation{
public void work(BooklList booklList){
System.out.println("新增圖書!");
System.out.println("請輸入圖書的名字:");
String name=scanner.nextLine();
System.out.println("請輸入圖書的作者");
String author=scanner.nextLine();
System.out.println("請輸入圖書類型");
String type=scanner.nextLine();
System.out.println("請輸入圖書價格");
int price=scanner.nextInt();
scanner.nextLine();
Book book=new Book(name,author,price,type);
int size=booklList.getUsedSize();
booklList.setBook(size,book);
booklList.setUsedSize(size+1);
System.out.println("新增圖書成功!");
}
}
?BorrowOperation(借閱操作)
public class BorrowOperation implements IOperation {
public void work(BooklList booklList) {
System.out.println("借閱圖書!");
System.out.println("請輸入你想借閱的圖書名字");
String name = scanner.nextLine();
int size = booklList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book = booklList.getPos(i);
if (name.equals(book.getName())) {
book.setBorrowed(true);
System.out.println("借閱成功?。?!");
System.out.println("借閱圖書信息如下:" + book);
return;
}
}
System.out.println("沒有找到你要借閱的圖書?。。?);
}
}
DelOperation(刪除操作)
public class DelOperation implements IOperation {
public void work(BooklList booklList) {
System.out.println("刪除圖書!");
System.out.println("請輸入你要刪除的圖書的名字");
String name = scanner.nextLine();
int currentSize = booklList.getUsedSize();
int index = 0;
int i = 0;
for (; i < currentSize; i++) {
Book book=booklList.getPos(i);
if (book.getName().equals(name)){
index=i;
break;
}
}
if (i>=currentSize){
System.out.println("沒有找打你要刪除的圖書?。。?);
return;
}
for (int j = index; j <currentSize-1 ; j++) {
Book book=booklList.getPos(j+1);
booklList.setBook(j,book);
}
booklList.setBook(currentSize-1,null);
booklList.setUsedSize(currentSize-1);
System.out.println("刪除成功?。?!");
}
}
?DisplayOperation(顯示操作)
public class DisplayOperation implements IOperation{
public void work(BooklList booklList){
System.out.println("打印圖書!");
int size=booklList.getUsedSize();
for (int i = 0; i <size ; i++) {
Book book=booklList.getPos(i);
System.out.println(book);
}
}
}
?ExitOperation(退出系統(tǒng))
public class ExitOperation implements IOperation{
public void work(BooklList booklList){
System.out.println("退出系統(tǒng)!");
System.exit(0);
}
}
?FindOperation(查找操作)
public class FindOperation implements IOperation{
public void work(BooklList booklList){
System.out.println("查找圖書!");
System.out.println("請輸入你要查找的圖書名字");
String name=scanner.nextLine();
int size=booklList.getUsedSize();
for (int i = 0; i <size ; i++) {
Book book=booklList.getPos(i);
if (name.equals(book.getName())) {
System.out.println("找到了這本書,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("沒有找到你需要借閱的圖書?。?!");
}
}
?ReturnOperation(歸還操作)
public class ReturnOperation implements IOperation {
public void work(BooklList booklList) {
System.out.println("歸還圖書!");
System.out.println("請輸入你要圖書的名字");
String name = scanner.nextLine();
int size = booklList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book = booklList.getPos(i);
if (name.equals(book.getName())) {
book.setBorrowed(false);
System.out.println("歸還成功?。?!");
System.out.println("歸還圖書信息如下:"+book);
return;
}
}
System.out.println("沒有你要歸還的圖書?。。?);
}
}
接口IOperation
public interface IOperation {
void work(BooklList booklList);
Scanner scanner=new Scanner(System.in);
}
注:上述的七個類和一個接口需要放在同一個包里
? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
3. 主類Main,包含一個靜態(tài)方法Login
Main類
public class Main {
public static User login(){
System.out.println("請輸入你的姓名");
Scanner scanner=new Scanner(System.in);
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) {
BooklList booklList=new BooklList();
User user=login();//向上轉(zhuǎn)型
while (true) {
int choice = user.menu();//動態(tài)綁定,根據(jù)choice選擇合適的操作
user.doWork(choice, booklList);
}
}
}
4.結(jié)果圖演示?
管理員功能演示
?普通用戶功能演示
文章來源:http://www.zghlxwxcb.cn/news/detail-472412.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-472412.html
到了這里,關(guān)于圖書管理系統(tǒng)(簡易版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!