❤️是你的期末大作業嗎?Java基礎語法就能實現的圖書管理系統 ❤️(建議收藏)

2021-09-23 08:00:02

一、簡介

在學習了Java語法的類、介面、繼承、多型、介面等基礎語法後的一簡單的專案練習。

具體功能為:
1、簡單的根據姓名的登入
2、管理員端:其功能為,查閱書籍、增加書籍、刪除書籍、列印書籍、按價格整理書籍、退出。
3、普通使用者端:查閱書籍、借閱書籍、歸還書籍、退出。

實現效果:

先輸入使用者姓名,在根據選擇決定身份為管理員或者是使用者。如果是管理員,就顯示管理員和它的選單,如果是使用者就顯示使用者的選單。
在這裡插入圖片描述

二、系統的設計

整個系統分為4部分,這裡分別用三個包和一個Main類實現。分別是Book包,Operation包,User包。

1、Book包:

包含book、bookList。兩個java檔案
book用來描述每本書的特徵。
bookList則表示整個書架。


1.1、book類:

package Book;

/**
 * @author 珍菇辣條
 */
public class book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean isBorrowed;

    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(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;
    }

    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 + '\'' +
                ((isBorrowed==true)?  "已借出  " :"  未借出") +
                '}';
    }


    public  double compare(book o1,book o2){
        return  o1.getPrice()-o2.getPrice();
    }
}


1.2、bookList類:

package Book;

/**
 * @author 珍菇辣條
 */
public class bookList {
    private  book[]books=new book[10];
    private int usedSize;
    public  bookList(){
       books[0]=new book("百年孤獨", "加西亞·馬爾克斯",  39.60,"小說");
       books[1]=new book("無聲告白", "伍綺詩", 35.00, "小說");
       books[2]=new book("沉思錄", "馬可·奧勒留", 32.00,"哲學");
       books[3]=new book("資料結構與演演算法", "汪沁", 45.00, "教科書");
        this.usedSize=4;
    }

    /**
     * 合法和放沒放滿目前沒有考慮
     * 預設放到順序表的最後
     * @param pos
     * @param book
     */
    public  void  setBooks(int pos,book book){
        this.books[pos]=book;
    }
    public  book getBook(int pos){
        return  this.books[pos];
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public book[] getBooks(){
        return  this.books;
    }
}


2、Operation包:

將操作寫成介面。後面讓每個方法繼承這個介面
Operation包包含了兩種身份的所有方法。先在包裡將所有的類方法全部實現。在具體整合到User包裡的管理員和普通使用者裡。用一個陣列來囊括這些方法。


2.1、增加書籍:

增加書籍會提示管理員一步步輸入需要增加書籍的資訊。如果書架裡已經用了這本書,那就不會再新增到書架上。


package Opreation;
import Book.bookList;
import Book.book;

import java.util.Scanner;

public class AddOperation implements IOperaion{
    @Override
    public  void work(bookList bookList){
        System.out.println("新增書籍");
        Scanner scanner=new Scanner(System.in);
        System.out.println("輸入新增圖書的名字");
        String name=scanner.nextLine();
        System.out.println("輸入圖書的作者");
        String Authorname=scanner.nextLine();
        System.out.println("輸入圖書的型別");
        String sype=scanner.nextLine();
        System.out.println("輸入圖書的價格");
        int price=scanner.nextInt();

        int currentSize=bookList.getUsedSize();
        for (int i = 0; i <bookList.getUsedSize() ; i++) {
            book book=bookList.getBook(i);
            if(name.equals(book.getName())){
                System.out.println("書架已經有這本書了,不要再新增了哦: ");
                return;
            }
        }

        String type = null;
        String author=null;
        book book=new book(name, author, price, type);
        bookList.setBooks(currentSize, book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增成功");
    }
}


2.2、刪除書籍:

在刪除之前先檢查書架上是否包含要刪除的書,如果沒有就不能刪除。

package Opreation;
import Book.bookList;
import Book.book;

import java.util.Scanner;

public class DelOperation implements IOperaion {
    @Override
    public  void work(bookList bookList){
        System.out.println("刪除書籍");
        Scanner scanner=new Scanner(System.in);
        System.out.println("要刪除那本書?");
        String name=scanner.nextLine();
        int currentSize=bookList.getUsedSize();
        int index=-1;
        for (int i = 0; i <currentSize ; i++) {
            book  book=bookList.getBook(i);
            if(name.equals(book.getName())){
                //找到了
                index=i;
            }
        }
        if(index==-1){
            //沒找到
            System.out.println("沒有你要刪除的書 ~.~");
            return;
        }else {
            for (int i = index; i <currentSize-1 ; i++) {
                book book1=bookList.getBook(i+1);
                bookList.setBooks(i, book1);
            }
            //防止記憶體漏失
            bookList.setBooks(currentSize-1, null);
            //控制usedSize
            bookList.setUsedSize(currentSize-1);
        }

    }
}


2.3、借閱書籍

這個很好實現,只需要遍歷書籍找到這本書。把書籍的狀態改成true就可以啦。

package Opreation;
import Book.bookList;
import  Book.book;
import java.util.Scanner;

public class BorrowOperation  implements IOperaion{
    @Override
    public  void work(bookList bookList){
        System.out.println("借閱書籍");
        Scanner scanner=new Scanner(System.in);
        System.out.println("輸入要借閱圖書的名字");
        String name=scanner.nextLine();
        int currentSize=bookList.getUsedSize();
        for (int i = 0; i <currentSize ; i++) {
            book book=bookList.getBook(i);
            if(name.equals(book.getName())&& !book.isBorrowed()){
                //找到了,就借出去唄。把它的狀態改成true。
                book.setBorrowed(true);
                System.out.println("給你借出去啦");
                return;
            }
        }
        System.out.println("沒有這本書呀或者已經被借出 ");
    }
}


2.4、歸還書籍

和借閱書籍相同,將修改後的已借出,修改成未借出即可。

package Opreation;
import Book.book;
import  Book.bookList;

import java.util.Scanner;

public class ReturnOperation implements  IOperaion{
    @Override
    public  void work(bookList bookList){
        System.out.println("歸還書籍");
        Scanner scanner=new Scanner(System.in);
        System.out.println("輸入要歸還圖書的名字");
        String name=scanner.nextLine();
        int currentSize=bookList.getUsedSize();
        for (int i = 0; i <currentSize ; i++) {
            book book=bookList.getBook(i);
            if(name.equals(book.getName())){
                //找到了,就歸還唄。把它的狀態改成false。
                book.setBorrowed(false);
                System.out.println("已歸還!");
                return;
            }
        }
        System.out.println("沒有這本書呀,還不了");
    }
    }



2.5、顯示書籍

遍歷書籍,並列印每一本書的資訊即可。

package Opreation;
import Book.book;
import Book.bookList;
public class DisPlayOperation implements  IOperaion{
    @Override
    public  void work(bookList bookList){
        int currentSize=bookList.getUsedSize();
        for (int i = 0; i <currentSize ; i++) {
            book book=bookList.getBook(i);
            System.out.println(book);
            }
        }
    }



2.6、查詢書籍

通過書籍的名字遍歷書架,查詢書籍,如果名字沒有對的上的,認為沒有這本書。

package Opreation;
import Book.bookList;
import Book.book;
import jdk.swing.interop.SwingInterOpUtils;

import java.awt.print.Book;
import java.util.Scanner;

public class FindOperation implements IOperaion{
    @Override
    public  void work(bookList bookList){

        System.out.println("這是給好兄弟查詢書籍的:  ");
        Scanner scanner=new Scanner(System.in);
        System.out.println("好兄弟找查詢那本書?  ");
        String name= scanner.nextLine();
        for (int i = 0; i <bookList.getUsedSize(); i++) {
            book book=bookList.getBook(i);
            if(name.equals(book.getName())){
                System.out.println("找到這本書了,資訊如下:  ");
                System.out.println(book);
                return;
            }
        }
        System.out.println("確實是沒有這本書哦!");
    }
}

2.7、整理書籍

將書架的書按照價格降序排序。需要先給book類實現比較方法。排序用的是直接插入排序。

package Opreation;
import Book.bookList;
import  Book.book;
import java.util.Scanner;

public class SortOperation implements  IOperaion {

    @Override
    public  void work(bookList bookList){
        System.out.println("整理圖書!");

        int currentSize=bookList.getUsedSize();
        book book1=bookList.getBook(0);
        book books[]=bookList.getBooks();
        for (int i = 1; i <currentSize ; i++) {
            book tmp=books[i];
            int j=i-1;
            for (; j >=0 ; j--) {
                if(compare(books[j],tmp)<0){
                    books[j+1]=books[j];
                }
                else {
                    break;
                }
            }
            books[j+1]=tmp;
        }
        System.out.println("已按價格降序整理完畢");
    }
    public  double compare(book o1,book o2){
        return  o1.getPrice()-o2.getPrice();
    }

2.8、退出系統

即為退出程式。

package Opreation;
import  Book.bookList;
public class ExitOperation implements IOperaion{
    @Override
    public  void work(bookList bookList){
        System.out.println("退出系統咯");
        System.exit(1);
    }
}

3、 User包:

包含管理員和普通使用者,和User類。每種身份包含不同的方法、選單。管理員個普通使用者繼承自User。

3.0 User

package USER;

import Opreation.IOperaion;
import Book.bookList;

public abstract class User {
    protected String name;
    protected IOperaion[] iOperaions;
    public  User(String name){
        this.name=name;
    }
    public abstract int menu();
    public void doOperatopm(int choice,bookList bookList){
        this.iOperaions[choice].work(bookList);
    }

}

3.1管理員

package USER;

import Opreation.*;

import java.util.Scanner;

public  class AdminUser extends  User {
public AdminUser(String name){
    super(name);
    this.iOperaions=new IOperaion[]{
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DelOperation(),
            new DisPlayOperation(),
            new SortOperation()
    };
}

    @Override
    public int menu() {
        Scanner scanner=new Scanner(System.in);
        System.out.println("~~~~~~~~~~~~~");
        System.out.println("歡迎"+this.name +",這裡是管理員選單");
        System.out.println("1、查詢圖書");
        System.out.println("2、新增圖書");
        System.out.println("3、刪除圖書");
        System.out.println("4、顯示圖書");
        System.out.println("5、整理書籍(按價格)");
        System.out.println("0、退出系統");
        System.out.println("~~~~~~~~~~~~~");
        int choice=scanner.nextInt();
        return  choice;

    }
}

3.2普通使用者

package USER;

import Opreation.*;

import java.util.Scanner;

public class NormarUser extends  User{

    public NormarUser(String name) {
        super(name);
        //就把普通使用者的操作,封裝/儲存到介面陣列重
        this.iOperaions=new IOperaion[] {
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),

        };
    }


    @Override
    public int  menu() {
        Scanner scanner=new Scanner(System.in);
        System.out.println("~~~~~~~~~~~~~");
        System.out.println("歡迎"+this.name +",這裡是使用者選單");
        System.out.println("1、查詢圖書");
        System.out.println("2、借閱圖書");
        System.out.println("3、歸還圖書");
        System.out.println("0、退出系統");
        System.out.println("~~~~~~~~~~~~~");
        int choice=scanner.nextInt();
        return  choice;
    }
}

3、程式的執行

通過一個Main類來調動所有的類。

import USER.AdminUser;
import USER.NormarUser;
import USER.User;
import Book.bookList;
import java.util.Scanner;

/**
 * @author 珍菇辣條
 */
public class Main {
    public static User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入你的姓名 : ");
        String name = scanner.nextLine();
        System.out.println(name+"你的身份是?:  1->管理員  0->:使用者");
        int choice = scanner.nextInt();
        if (choice == 1) {
            return new AdminUser(name);
        } else {
            return new NormarUser(name);
        }
    }

    public static void main(String[] args) {
        bookList bookList = new bookList();
        User user = login();
        /**
         * 列印選單,並讓使用者選擇身份,要操作的事情
         */
        while (true) {
            int choice = user.menu();
            //根據選單的選項,來呼叫合適的方法
            user.doOperatopm(choice, bookList);
        }
    }
}



三、結尾

以上就是圖書管理系統的所有程式碼。祝大家學習順利。工作順利。

在這裡插入圖片描述