最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaSE實(shí)現(xiàn)圖書(shū)管理系統(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)行熟悉理解??旄S小編一起學(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)目需求

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

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

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

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

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

這里系統(tǒng)對(duì)書(shū)進(jìn)行管理,其實(shí)就是對(duì)書(shū)架對(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;//書(shū)名
    private String author;//書(shū)的作者
    private int price;//書(shū)的價(jià)格
    private String type;//書(shū)的類型
    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;
    //書(shū)架當(dāng)中默認(rèn)有三本書(shū)

    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("新增圖書(shū)");
        //輸入圖書(shū)信息
        System.out.println("輸入新增圖書(shū)名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("輸入新增圖書(shū)作者:");
        String author = scanner.nextLine();
        System.out.println("輸入新增圖書(shū)價(jià)格:");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("輸入新增圖書(shū)類型:");
        String type = scanner.nextLine();

        Book book = new Book(name, author, price, type);
        //獲取存書(shū)位置
        int currentSize = bookList.getUsedsize();
        //把書(shū)放到書(shū)架(數(shù)組)
        bookList.setBooks(book, currentSize);
        //書(shū)的總數(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("借閱圖書(shū)");
        System.out.println("輸入要借閱圖書(shū)的名稱:");
        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("該書(shū)已經(jīng)被借出!");
                    return;
                }else {
                    book.setBorrowed(true);
                    System.out.println("借閱成功!");
                    return;
                }
            }
        }
        System.out.println("沒(méi)有找到你要借閱的書(shū)");
    }
}

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("刪除圖書(shū)");
        System.out.println("輸入要?jiǎng)h除圖書(shū)的名稱:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedsize();

        //找到并記錄要?jiǎng)h除書(shū)的下標(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除的這本書(shū)!");
        }else {
            for (int i = index; i < currentSize-1; i++) {
                Book book = bookList.getBooks(i+1);
                bookList.setBooks(book, i);
            }
            //每次刪除,都要將原來(lái)最后一本書(shū)位置的引用置空
            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("顯示圖書(shū)");
        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("查找圖書(shū)");
        System.out.println("輸入要查找圖書(shū)的名稱:");
        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("找到了,該書(shū)信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("沒(méi)有找到這本書(shū)!");
    }
}

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("歸還圖書(shū)");
        System.out.println("輸入要?dú)w還圖書(shū)的名稱:");
        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("該書(shū)未借出!");
                    return;
                }
            }
        }
        System.out.println("沒(méi)有找到你要?dú)w還的書(shū)");
    }
}

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)到圖書(shū)管理系統(tǒng)!");
        System.out.println("      1.查找圖書(shū)  2.新增圖書(shū)");
        System.out.println("      3.刪除圖書(shū)  4.顯示圖書(shū)");
        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)到圖書(shū)管理系統(tǒng)!");
        System.out.println("      1.查找圖書(shū)  2.借閱圖書(shū)");
        System.out.println("      3.歸還圖書(shū)  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)圖書(shū)管理系統(tǒng)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaSE圖書(shū)管理系統(tǒng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java Socket編程詳解及示例代碼

    Java Socket編程詳解及示例代碼

    本文主要講解Java Socket編程,這里整理了詳細(xì)的技術(shù)資料及簡(jiǎn)單的示例代碼幫助大家學(xué)習(xí)參考,有需要的小伙伴可以參考下本文內(nèi)容
    2016-09-09
  • SpringBoot整合Zookeeper詳細(xì)教程

    SpringBoot整合Zookeeper詳細(xì)教程

    Curator是Netflix公司開(kāi)源的?套zookeeper客戶端框架,Curator是對(duì)Zookeeper?持最好的客戶端框架。Curator封裝了?部分Zookeeper的功能,?如Leader選舉、分布式鎖等,減少了技術(shù)?員在使?Zookeeper時(shí)的底層細(xì)節(jié)開(kāi)發(fā)?作
    2022-12-12
  • 解決Mybatis返回update后影響的行數(shù)問(wèn)題

    解決Mybatis返回update后影響的行數(shù)問(wèn)題

    這篇文章主要介紹了解決Mybatis返回update后影響的行數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • JAVA對(duì)字符串進(jìn)行32位MD5加密的實(shí)踐

    JAVA對(duì)字符串進(jìn)行32位MD5加密的實(shí)踐

    本文主要介紹了JAVA對(duì)字符串進(jìn)行32位MD5加密的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java實(shí)現(xiàn)消消樂(lè)中的消除功能

    Java實(shí)現(xiàn)消消樂(lè)中的消除功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)消消樂(lè)中的消除功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Maven如何打入依賴中指定的部分jar包

    Maven如何打入依賴中指定的部分jar包

    當(dāng)項(xiàng)目運(yùn)行的環(huán)境里已經(jīng)有一個(gè)jar包是pom文件依賴其他項(xiàng)目的jar包,所以最后得到的項(xiàng)目jar包中還需要打入其他項(xiàng)目的最新代碼,接下來(lái)通過(guò)本文給大家介紹Maven打入依賴jar包的操作工程,需要的朋友參考下吧
    2021-06-06
  • java稀疏數(shù)組的示例代碼

    java稀疏數(shù)組的示例代碼

    這篇文章主要介紹了java稀疏數(shù)組,稀疏數(shù)組,記錄一共有幾行幾列,有多少個(gè)不同值,把具有不同值的元素和行里了及值記錄在一個(gè)小規(guī)模的數(shù)組中,從而縮小程序的規(guī)模,對(duì)java稀疏數(shù)組相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-07-07
  • JavaCV實(shí)戰(zhàn)之調(diào)用攝像頭基礎(chǔ)詳解

    JavaCV實(shí)戰(zhàn)之調(diào)用攝像頭基礎(chǔ)詳解

    這篇文章主要介紹了使用JavaCV框架對(duì)攝像頭進(jìn)行各種處理的基礎(chǔ)理論詳解,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JavaCV有一定的幫助,需要的可以了解一下
    2022-01-01
  • IDEA上運(yùn)行Flink任務(wù)的實(shí)戰(zhàn)教程

    IDEA上運(yùn)行Flink任務(wù)的實(shí)戰(zhàn)教程

    這篇文章主要介紹了IDEA上運(yùn)行Flink任務(wù)的實(shí)戰(zhàn)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 詳解java 拼音首字母搜索內(nèi)容功能的示例

    詳解java 拼音首字母搜索內(nèi)容功能的示例

    這篇文章主要介紹了詳解java 拼音首字母搜索內(nèi)容功能的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論

宁晋县| 启东市| 合江县| 新津县| 巴中市| 高要市| 元朗区| 宁远县| 凤凰县| 河津市| 百色市| 循化| 兴海县| 雷波县| 德清县| 裕民县| 松原市| 东丰县| 宁德市| 阳山县| 江永县| 嘉禾县| 河东区| 吉林省| 瑞昌市| 通化市| 博乐市| 镇坪县| 巫山县| 永济市| 江油市| 资溪县| 海盐县| 四川省| 囊谦县| 漳平市| 新竹县| 卓资县| 梓潼县| 石阡县| 苏尼特左旗|