1. 程式人生 > 實用技巧 >Java(3)面向物件設計:圖書管理系統

Java(3)面向物件設計:圖書管理系統

一、功能調查與系統功能框架圖

1.功能調查:

圖書管理系統分為普通使用者與管理員兩種登入方式,然後通過輸入姓名、性別、年齡資訊進入系統,普通使用者可執行查詢、借閱、歸還圖書以及退出系統四大功能,而管理員擁有更多特權能執行增加、查詢、修改、刪除、列印、整理圖書以及退出功能。

2.系統功能框架圖

二、類的設計 (UML圖)

三、類說明

1.Test類:選單欄、使用者登入、輸出操作

import java.util.Scanner;

public class Test {

    public void choose() {
        while (true) {
            System.out.println("------------圖論科技圖書管理系統------------\n");
            System.out.print("請登入:1:普通使用者   2:管理員登入\n\n");
            Scanner in = new Scanner(System.in);

            int choose = in.nextInt();
            Scanner scan = new Scanner(System.in);
            User user = null;
            AdminPerson adminPerson = null;
            switch (choose) {
                case 1:
                    System.out.print("請輸入:姓名 \n");
                    String userName = scan.next();
                    System.out.print("請輸入:性別 \n");
                    String userSex = scan.next();
                    System.out.print("請輸入:年齡 \n");
                    int userAge = scan.nextInt();
                    user = new User(userName, userSex, userAge);
                    System.out.println("當前使用者:" + userName + " " + userSex + " " + userAge);
                    user.start();
                    break;
                case 2:
                    System.out.print("請輸入:姓名 \n");
                    String adminName = scan.next();
                    System.out.print("請輸入:性別 \n");
                    String adminSex = scan.next();
                    System.out.print("請輸入:年齡 \n");
                    int adminAge = scan.nextInt();
                    System.out.println("當前管理員:" + adminName + " " + adminSex + " " + adminAge);
                    adminPerson = new AdminPerson(adminName, adminSex, adminAge);
                    adminPerson.start();
                    break;
            }
        }
    }
    public static void main(String[] args){
        Test test = new Test();
        test.choose();
    }

}

2.Person抽象類:為普通使用者和管理員提供公有的查詢圖書操作並且包括getter/setter

類的屬性:(static)

人名:name、性別:sex、age:年齡

類的方法:

getter/setter自動生成

selectBook(book):查詢圖書資訊

import java.util.Scanner;

abstract class Person {
    protected static String name;
    protected static String sex;
    protected static int age;
    Scanner input = new Scanner(System.in);

    public static String getName() {
        return name;
    }

    public static String getSex() {
        return sex;
    }

    public static int getAge() {
        return age;
    }

    public static void setName(String name) {
        Person.name = name;
    }

    public static void setSex(String sex) {
        Person.sex = sex;
    }

    public static void setAge(int age) {
        Person.age = age;
    }

    /**
     * 查詢圖書資訊
     * @param book
     */
    public void selectBook(String[][] book) {
        String m;
        System.out.println("請輸入您要查詢的圖書名字:");
        m = input.next();
        if (Book.count == 0) {
            System.out.println("不好意思,無此書籍!");
        } else {
            boolean flag = true;
            while(flag){
            for (int i = 0; i < book.length; i++) {
                if (book[i][1].equals(m)) {
                    System.out.println("圖書編號:" + book[i][0]);
                    System.out.println("圖書名字:" + book[i][1]);
                    System.out.println("圖書作者:" + book[i][2]);
                    System.out.println("圖書數量:" + book[i][3]);
                    System.out.println("~~~~~~圖書資訊查詢完畢~~~~~~");
                    flag = false;
                    break;
                }else{
                    System.out.println("不好意思,無此書籍!");
                    flag = false;
                    break;
                }
              }
            }
        }
    }
}

3.User類:普通使用者可執行借閱、歸還圖書並且包括普通使用者操作函式可顯示選單欄

類的屬性:(private)

普通使用者的userName:姓名、userSex:性別、userSex:年齡

類的方法:

borrowBook(book):借閱圖書

returnBook(book1):歸還圖書

start():使用者操作函式

import java.util.Scanner;

public class User extends Person {
    //使用者姓名
    private static String userName;
    //使用者性別
    private static String userSex;
    //使用者年齡
    private static int userAge;
    //無參建構函式
    Book book = new Book();
    public User(){

    }
    //有參建構函式
    public User(String userName,String userSex,int userAge){

    }

    //借閱書籍
    public void borrowBook(String[][] book){
        System.out.println("請輸入您要借閱的圖書名字:");
        String m;
        int n = -1;
        m = input.next();
            for (int i = 0; i < book1.length; i++) {
                if (book1[i][1].equals(m)) {
                    n = i;
                    break;
                }
            }
            int p = Integer.parseInt(Book.book1[n][3]);
            if (p == 0) {
                System.out.println("不好意思,已經被借光了~");
            } else {
                Book.book1[n][3] = (p - 1) + "";
                System.out.println("借閱成功!");
            }
    }

    /**
     * 歸還圖書
     * @param book1
     */
    public void returnBook(String[][] book1){
        int BookId1,BookNum1;
        String BookName1,Author1;

        System.out.println("~~~~~~開始歸還圖書~~~~~~");
        System.out.println("請輸入歸還圖書名字:");
        BookName1 = input.next();
        for (int i = 0; i < Book.count; i++) {
            if (book1[i][1].equals(BookName1)) {
                    int p = Integer.parseInt(book1[i][3]);
                    book1[i][3] = (p+1) + "";
                    break;
                }
            }
        System.out.println("~~~~~~歸還成功~~~~~~");
    }

    /**
     * 使用者操作函式
     */
    public void start(){
        Scanner input = new Scanner(System.in);

        boolean flag = true;
        int number;
        while(flag) {
            System.out.println("---------------------------------------------------");
            System.out.print("請選擇:1:查詢書籍  2:借閱書籍  3:歸還書籍  4:退出\n\n");
            number = input.nextInt();
            switch (number) {
                case 1:
                    selectBook(Book.book1);
                    break;
                case 2:
                    borrowBook(Book.book1);
                    break;
                case 3:
                    returnBook(Book.book1);
                    break;
                case 4:
                    System.out.println("~~~~~~您已退出系統~~~~~~");
                    flag = false;
                    break;
                    default:
                    System.out.println("輸入錯誤");
                    break;
            }
        }
    }
}

4.Operate 介面:DAO介面包括管理、列印、修改圖書這些操作方法

public interface Operate {
    void arrangeBooks(String[][] book);//管理圖書
    void printBooksList(String[][] book);//列印圖書列表
    void alter_s(String[][] book1);//修改圖書
}

5.AdminPerson 類:管理員可執行整理、列印、修改圖書並且包括管理員操作函式可顯示選單欄

類的屬性:(private)

管理員adminName:姓名、adminSex:性別、adminAge:年齡

類的方法:

arrangeBooks(book1):整理圖書

printBooksList(book):列印圖書列表

alter_s(book1):修改圖書

import user.impl.Operate;
import java.util.Scanner;
public class AdminPerson extends Person implements Operate,Comparable<Book> {
    //使用者姓名
    private static String adminName;
    //使用者性別
    private static String adminSex;
    //使用者年齡
    private static int adminAge;

    Scanner input = new Scanner(System.in);

    Book book = new Book();

    //無參建構函式
    public AdminPerson(){

    }
    //有參建構函式
    public AdminPerson(String adminName,String adminSex,int adminAge){

    }

    /**
     * 按圖書編號整理書籍
     * @param book1
     */
    @Override
    public void arrangeBooks(String[][] book1) {
        String[] temp;
        for(int i = 0;i < Book.count-1;i++){
            for(int j=0;j < Book.count-1-i;j++) {
                if (book1[j][0].compareTo(book1[j + 1][0]) > 0) {
                    temp = book1[j];
                    book1[j] = book1[j + 1];
                    book1[j + 1] = temp;
                }
            }
        }
        System.out.println("整理完畢!");
    }

    /**
     * 列印圖書列表
     * @param book
     */
    @Override
    public void printBooksList(String[][] book) {
        if(Book.count == 0){
            System.out.println("倉庫書籍為空!");
        }else {
            System.out.println("所有圖書資訊如下:");
            for (int i = 0; i < Book.count; i++) {
                System.out.print("圖書編號:" + book[i][0] + "\t\t");
                System.out.print("圖書名字:" + book[i][1] + "\t\t");
                System.out.print("圖書作者:" + book[i][2] + "\t\t");
                System.out.print("圖書數量:" + book[i][3] + "\n");
            }
        }
    }

    /**
     * 修改圖書
     * @param book1
     * @return
     */
    @Override
    public void alter_s(String[][] book1) {
        String m;
        int o;
        int n = -1;
        System.out.println("請輸入您要修改的圖書名字:");
        m = input.next();
        for(int  i = 0;i < Book.count;i++){
            if(book1[i][1].equals(m)){
                n = i;
                break;
            }
        }
        System.out.println("請選擇您要修改的內容:1. 圖書編號  2. 圖書名字  3. 圖書作者  4.圖書數量");
        o = input.nextInt();
        System.out.println("請輸入您要修改的內容:");
        book1[n][o-1] = input.next()+"";
        System.out.println("~~~~~~圖書資訊已修改~~~~~~");
    }

    /**
     * 管理員操作函式
     */
    public void start(){
        boolean flag = true;
        int number;
        while(flag){
            System.out.println("---------------------------------------------------------------------------");
            System.out.print("請選擇:1:增加書籍  2:查詢書籍  3:修改書籍  4:刪除書籍  5:列印書籍列表 6:整理書籍  7: 退出\n\n");
            number = input.nextInt();
            switch (number) {
                case 1:
                    book.add(book);
                    break;
                case 2:
                    selectBook(Book.book1);
                    break;
                case 3:
                    alter_s(Book.book1);
                    break;
                case 4:
                    book.delete();
                    break;
                case 5:
                    printBooksList(Book.book1);
                    break;
                case 6:
                    arrangeBooks(Book.book1);
                    break;
                case 7:
                    System.out.println("~~~~~~您已退出系統~~~~~~");
                    flag = false;
                    break;
                default :
                    System.out.println("輸入錯誤");
                    break;
            }
        }
    }

    @Override
    public int compareTo(Book o) {
        return book.getBookId()-o.getBookId();
    }
}

6.Book類:管理員可執行增加、刪除圖書操作

類的屬性:(public)

BookId:書的ID、BookName:書名、Author:作者、BookNum:書的數量

類的方法:

getter/setter自動生成

add(book):增加圖書

delete():刪除圖書

import java.util.Scanner;

public class Book {
    Scanner input = new Scanner(System.in);
    protected int BookId;//書的ID
    protected String BookName;//書的名字
    protected String Author;//書的作者
    protected int BookNum;//書的數量
    protected static int count;

    static String[][] book1 = new String[5][4];//存放圖書資訊的陣列

    public Book(){


    }

    public Book(int BookId,String BookName,String Author,int BookNum){
        this.BookId = BookId;
        this.BookName = BookName;
        this.Author = Author;
        this.BookNum = BookNum;
    }

    public Scanner getInput() {
        return input;
    }

    public void setInput(Scanner input) {
        this.input = input;
    }

    public int getBookId() {
        return BookId;
    }

    public void setBookId(int bookId) { this.BookId = bookId; }

    public String getBookName() {
        return BookName;
    }

    public void setBookName(String bookName) {
        this.BookName = bookName;
    }

    public String getAuthor() {
        return Author;
    }

    public void setAuthor(String author) {
        this.Author = author;
    }

    public int getBookNum() {
        return BookNum;
    }

    public void setBookNum(int bookNum) {
        this.BookNum = bookNum;
    }

    /**
     * 增加圖書以及圖書資訊函式
     * @param book
     */
    public void add(Book book){
        int BookId1,BookNum1;
        String BookName1,Author1;
        System.out.println("~~~~~~開始新增圖書資訊~~~~~~");
        System.out.println("圖書ID:");
        BookId1 = input.nextInt();
        book.setBookId(BookId1);
        System.out.println("圖書名字:");
        BookName1 = input.next();
        book.setBookName(BookName1);
        System.out.println("圖書作者:");
        Author1 = input.next();
        book.setAuthor(Author1);
        System.out.println("圖書數量:");
        BookNum1 = input.nextInt();
        book.setBookNum(BookNum1);
        System.out.println("~~~~~~新增成功~~~~~~");

        book1[count][0] = book.BookId+"";
        book1[count][1] = book.BookName;
        book1[count][2] = book.Author;
        book1[count][3] = book.BookNum+"";
        System.out.println("圖書編號:"+book1[count][0]);
        System.out.println("圖書名字:"+book1[count][1]);
        System.out.println("圖書作者:"+book1[count][2]);
        System.out.println("圖書數量:"+book1[count][3]);

        count++;
    }

    /**
     * 用於刪除圖書資訊
     */
    public void delete(){
        System.out.println("輸入您要刪除的圖書名字:");
        String m;
        m = input.next();
        for(int i = 0; i < count;i++) {
            if(book1[i][1].equals(m)) {
                book1[i] = null;
                book1[i] = book1[i + 1];
                break;
            }
        }
        System.out.println("~~~~~~圖書資訊已被刪除~~~~~~");
        count--;
    }
}

四、系統的包(package)的規劃設計

1.package.user.action

包含:Test、Person、User、AdminPerson、Book類

2.package user.imp1

包含:Operate介面