欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaSE實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼

 更新時(shí)間:2022年08月25日 10:06:00   作者:XIN-XIANG榮  
這篇博客是在學(xué)習(xí)了一部分Java基礎(chǔ)語(yǔ)法之后的練習(xí)項(xiàng)目,通過(guò)這個(gè)小項(xiàng)目的練習(xí),對(duì)Java中的類和對(duì)象,抽象類和接口等進(jìn)行熟悉理解。快跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧

前言

這篇博客是在學(xué)習(xí)了一部分Java基礎(chǔ)語(yǔ)法之后的練習(xí)項(xiàng)目,通過(guò)這個(gè)小項(xiàng)目的練習(xí),對(duì)Java中的類和對(duì)象,抽象類和接口,面向?qū)ο蟮睦^承、多態(tài)和封裝、組合等進(jìn)行熟悉理解;抽象出不同的對(duì)象,將對(duì)象進(jìn)行合理的設(shè)計(jì),完成對(duì)象之間的交互,面向?qū)ο筮M(jìn)行編程。

1. 項(xiàng)目需求

圖書管理系統(tǒng),面向管理員和普通用戶使用,對(duì)管理員的開放的功能有:添加圖書,刪除圖書,查找圖書,顯示圖書和退出程序等;對(duì)普通用戶的開放的功能有:查找圖書,借閱圖書,歸還圖書和退出程序。

2. 實(shí)現(xiàn)思路

先抽象提取出不同的對(duì)象,首先想到的對(duì)象是用戶和書,

用戶可分為管理員和普通用戶,將管理員和普通用戶共有的屬性設(shè)置為一個(gè)父類User,管理員和普通用戶繼承User,可以先將父類設(shè)置為普通類,在寫碼過(guò)程如果合適可以修改為抽象類;

系統(tǒng)對(duì)書進(jìn)行管理,書本身就可以抽象為一個(gè)對(duì)象;對(duì)多本書進(jìn)行操作管理,書架也可以抽象為一個(gè)對(duì)象;所以書和書架用兩個(gè)類來(lái)實(shí)現(xiàn),書架可以設(shè)計(jì)為一個(gè)數(shù)組;

這里系統(tǒng)對(duì)書進(jìn)行管理,其實(shí)就是對(duì)書架對(duì)應(yīng)數(shù)組進(jìn)行操作,不同的操作可以通過(guò)一個(gè)標(biāo)準(zhǔn)去實(shí)現(xiàn),也就是可以定義一個(gè)接口,每一個(gè)操作類去實(shí)現(xiàn)這個(gè)接口,完成向上轉(zhuǎn)型,通過(guò)接口實(shí)現(xiàn)多態(tài);

然后寫代碼的過(guò)程中思考完善各個(gè)對(duì)象的成員,最終實(shí)例化對(duì)象,通過(guò)不同對(duì)象之間的交互完成管理系統(tǒng)。

3. 代碼實(shí)現(xiàn)

包的設(shè)計(jì)

book包

Book類

package book;

public class Book {
    private String name;//書名
    private String author;//書的作者
    private int price;//書的價(jià)格
    private String type;//書的類型
    private boolean borrowed;//是否被借出,默認(rèn)false

    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 getBorrowed() {
        return borrowed;
    }

    public void setBorrowed(Boolean borrowed) {
        this.borrowed = borrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", borrowed=" + ((borrowed==true) ? "已借出" : "未借出") +
                '}';
    }
}

BookList類

package book;

public class BookList {
    private Book[] books = new Book[100];
    private int usedsize;
    //書架當(dāng)中默認(rèn)有三本書

    public BookList() {
        this.books[0] = new Book("三國(guó)演義","羅貫中",66,"小說(shuō)");
        this.books[1] = new Book("西游記","吳承恩",68,"小說(shuō)");
        this.books[2] = new Book("紅樓夢(mèng)","曹雪芹",64,"小說(shuō)");
        this.usedsize = 3;
    }

    public int getUsedsize() {
        return usedsize;
    }

    public void setUsedsize(int usedsize) {
        this.usedsize = usedsize;
    }


    public Book getBooks(int pos){
        return this.books[pos];
    }

    public void setBooks(Book book, int pos) {
        this.books[pos] = book;
    }
}

operations包

IOperation接口

package operations;

import book.BookList;

public interface IOperation {
    void work(BookList bookList);
}

AddOperation類

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增圖書");
        //輸入圖書信息
        System.out.println("輸入新增圖書名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("輸入新增圖書作者:");
        String author = scanner.nextLine();
        System.out.println("輸入新增圖書價(jià)格:");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("輸入新增圖書類型:");
        String type = scanner.nextLine();

        Book book = new Book(name, author, price, type);
        //獲取存書位置
        int currentSize = bookList.getUsedsize();
        //把書放到書架(數(shù)組)
        bookList.setBooks(book, currentSize);
        //書的總數(shù)加1
        bookList.setUsedsize(currentSize+1);
        System.out.println("添加成功!");
    }
}

BorrowOperation類

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借閱圖書");
        System.out.println("輸入要借閱圖書的名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                if(book.getBorrowed()){
                    System.out.println("該書已經(jīng)被借出!");
                    return;
                }else {
                    book.setBorrowed(true);
                    System.out.println("借閱成功!");
                    return;
                }
            }
        }
        System.out.println("沒(méi)有找到你要借閱的書");
    }
}

DelOperation類

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("刪除圖書");
        System.out.println("輸入要?jiǎng)h除圖書的名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedsize();

        //找到并記錄要?jiǎng)h除書的下標(biāo)
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if(-1 == index) {
            System.out.println("沒(méi)有找到要?jiǎng)h除的這本書!");
        }else {
            for (int i = index; i < currentSize-1; i++) {
                Book book = bookList.getBooks(i+1);
                bookList.setBooks(book, i);
            }
            //每次刪除,都要將原來(lái)最后一本書位置的引用置空
            bookList.setBooks(null,currentSize-1);
            bookList.setUsedsize(currentSize-1);
            System.out.println("刪除成功");
        }
    }
}

DisplayOperation類

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("顯示圖書");
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }
}

ExitOperation類

package operations;

import book.BookList;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系統(tǒng)");
        System.exit(0);
    }
}

FindOperation類

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找圖書");
        System.out.println("輸入要查找圖書的名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                System.out.println("找到了,該書信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("沒(méi)有找到這本書!");
    }
}

ReturnOperation類

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("歸還圖書");
        System.out.println("輸入要?dú)w還圖書的名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                if(book.getBorrowed()){
                    book.setBorrowed(false);
                    System.out.println("歸還成功!");
                    return;

                }else {
                    System.out.println("該書未借出!");
                    return;
                }
            }
        }
        System.out.println("沒(méi)有找到你要?dú)w還的書");
    }
}

user包

User類

public abstract class User {
    protected String name;
    protected IOperation[] iOperation;

    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList){
        iOperation[choice].work(bookList);
    }
}

AdminUser類

package user;

import operations.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperation = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println("---------------------------------");
        System.out.println("Hello 管理員:>"+""+name+" 歡迎來(lái)到圖書管理系統(tǒng)!");
        System.out.println("      1.查找圖書  2.新增圖書");
        System.out.println("      3.刪除圖書  4.顯示圖書");
        System.out.println("           0.退出系統(tǒng)");
        System.out.println("---------------------------------");
        System.out.println("請(qǐng)輸入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NormalUser類

package user;

import operations.*;

import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperation = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println("---------------------------------");
        System.out.println("Hello 用戶:>"+name+" 歡迎來(lái)到圖書管理系統(tǒng)!");
        System.out.println("      1.查找圖書  2.借閱圖書");
        System.out.println("      3.歸還圖書  0.退出系統(tǒng)");
        System.out.println("---------------------------------");
        System.out.println("請(qǐng)輸入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

Main類

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        System.out.println("請(qǐng)輸入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        System.out.println("請(qǐng)確認(rèn)你的身份:> |1->管理員|0->普通用戶|");
        int choice = scanner.nextInt();
        if(1==choice){
            return new AdminUser(userName);
        }else{
            return new NormalUser(userName);
        }
    }

    public static void main(String[] args) {
        //準(zhǔn)備數(shù)據(jù)
        BookList bookList = new BookList();
        //登錄
        User user = login();

        while (true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }
}

4. 實(shí)現(xiàn)效果

管理員

普通用戶

以上就是JavaSE實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaSE圖書管理系統(tǒng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一文帶你徹底明白什么是Java注解

    一文帶你徹底明白什么是Java注解

    Java注解可以說(shuō)是我們編碼過(guò)程中最常用的,本文將給大家介紹Java注解的概念、作用以及如何使用注解來(lái)提升代碼的可讀性和靈活性,需要的可以參考一下
    2023-05-05
  • 詳解Spring Data JPA使用@Query注解(Using @Query)

    詳解Spring Data JPA使用@Query注解(Using @Query)

    本篇文章主要介紹了詳解Spring Data JPA使用@Query注解(Using @Query),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Java精確計(jì)算BigDecimal類詳解

    Java精確計(jì)算BigDecimal類詳解

    這篇文章主要介紹了Java精確計(jì)算BigDecimal類的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • java實(shí)現(xiàn)voctor按指定方式排序示例分享

    java實(shí)現(xiàn)voctor按指定方式排序示例分享

    這篇文章主要介紹了java實(shí)現(xiàn)voctor按指定方式排序示例,需要的朋友可以參考下
    2014-03-03
  • Jenkins安裝以及郵件配置詳解

    Jenkins安裝以及郵件配置詳解

    這篇文章主要介紹了Jenkins安裝以及郵件配置相關(guān)問(wèn)題,并通過(guò)圖文給大家做了詳細(xì)講解步驟,需要的朋友參考下吧。
    2017-12-12
  • Java Atomic類及線程同步新機(jī)制原理解析

    Java Atomic類及線程同步新機(jī)制原理解析

    這篇文章主要介紹了Java Atomic類及線程同步新機(jī)制原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java設(shè)計(jì)模式之java模板方法模式詳解

    Java設(shè)計(jì)模式之java模板方法模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式模板方法模式(Template)用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 詳解Java中Method的Invoke方法

    詳解Java中Method的Invoke方法

    這篇文章主要介紹了詳解Java中Method的Invoke方法,需要的朋友可以參考下
    2017-10-10
  • Javaweb實(shí)現(xiàn)郵件發(fā)送

    Javaweb實(shí)現(xiàn)郵件發(fā)送

    這篇文章主要為大家詳細(xì)介紹了Javaweb實(shí)現(xiàn)郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 淺析JAVA中toString方法的作用

    淺析JAVA中toString方法的作用

    以下是對(duì)在JAVA中toString方法的作用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07

最新評(píng)論