亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)

 更新時(shí)間:2021年03月31日 20:09:00   作者:顏問(wèn)兒  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文通過(guò)實(shí)例為大家分享了java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

一、背景介紹

通過(guò)一段時(shí)間java編程的學(xué)習(xí),需要一個(gè)比較綜合的實(shí)例來(lái)進(jìn)行編程的練習(xí),是一個(gè)對(duì)前段時(shí)間所學(xué)內(nèi)容進(jìn)行總合提升的一個(gè)契機(jī)。選擇了圖書(shū)管理系統(tǒng),會(huì)用到的javaSE知識(shí)有:變量、包、繼承、類(lèi)、接口、循環(huán)結(jié)構(gòu)等。是一個(gè)很綜合的典例。

二、核心需求

1.用戶可以登錄到系統(tǒng)上 分為 管理員、普通用戶兩種角色,這兩種不同的角色根據(jù)自己的身份可以實(shí)現(xiàn)不同的操作。

普通用戶

a)查閱某個(gè)書(shū)籍的信息
b)借閱書(shū)籍
c)   歸還書(shū)籍
d)退出程序

管理員

a)查閱某個(gè)書(shū)籍的信息
b)增加書(shū)籍
c)   刪除書(shū)籍
d)查看書(shū)籍列表
e)退出程序

程序框架結(jié)構(gòu)圖

三、代碼以及詳解

1.User類(lèi)

package booksystem;

import booksystem.operation.IOperation;

abstract public class User {
 protected String name;//定義書(shū)名
 protected IOperation[] operations;//定義一個(gè)接口數(shù)組
 public abstract int menu();//是用戶和管理員的父類(lèi),不進(jìn)行實(shí)例化,所以定義為抽象方法

 public void doOperation(int choice,BookList bookList){
 IOperation operation=this.operations[choice-1];
 operation.work(bookList);
 }
}

User類(lèi)是NormalUser類(lèi)和Admin類(lèi)的父類(lèi),由于不需要實(shí)例化,將menu()函數(shù)定義為了抽象函數(shù)。

2.NormalUser類(lèi)

package booksystem;

import booksystem.operation.*;
import booksystem.operation.IOperation;

import java.util.Scanner;

public class NormalUser extends User {

 public NormalUser(String name) {
 this.name = name;
 this.operations = new IOperation[]
 {
 new FindOperation(),
 new BorrowOperation(),
 new ReturnOperation(),
 new ExitOperation(),
 };
 }
 @Override
 public int menu(){
 System.out.println("~~~~~~~~~~~~~~~~~~");
 System.out.println("Hello"+name+"Welcome to use booksyetem");
 System.out.println("1.查閱書(shū)籍信息");
 System.out.println("2.借閱書(shū)籍");
 System.out.println("3.歸還書(shū)籍");
 System.out.println("4.退出系統(tǒng)");
 System.out.println("~~~~~~~~~~~~~~~~~~");
 System.out.println("請(qǐng)輸入您的選擇:");
 Scanner scanner=new Scanner(System.in);
 int choice=scanner.nextInt();
 return choice;//返回一個(gè)整型數(shù)
 }
}

NormalUser類(lèi)針對(duì)與普通用戶而編寫(xiě),在代碼中定義了一個(gè)接口數(shù)組,在其中添加了普通用戶需要用到的查閱、借閱、歸還、退出系統(tǒng)的四大功能,在menu()函數(shù)中也按照同樣的順序,menu()函數(shù)是重寫(xiě)父類(lèi)的,所以為了提醒編譯器,在函數(shù)頭前加上了 @Override進(jìn)行提示。

3.Admin類(lèi)

package booksystem;

import booksystem.operation.*;

import java.util.Scanner;

public class Admin extends User {
 public Admin(String name){
 this.name=name;
 this.operations=new IOperation[]{
 new FindOperation(),
 new AddOperation(),
 new DelOperation(),
 new DisplayOperation(),
 new ExitOperation(),
 };
 }
 @Override
 public int menu(){
 System.out.println("~~~~~~~~~~~~~~~~~~");
 System.out.println("Hello"+name+"Welcome to use booksyetem");
 System.out.println("1.查閱書(shū)籍信息");
 System.out.println("2.新增書(shū)籍信息");
 System.out.println("3.刪除書(shū)籍信息");
 System.out.println("4.退出系統(tǒng)");
 System.out.println("~~~~~~~~~~~~~~~~~~");
 System.out.println("請(qǐng)輸入您的選擇:");
 Scanner scanner=new Scanner(System.in);
 int choice=scanner.nextInt();
 return choice;
 }
}

Admin類(lèi)編寫(xiě)的思路和NormalUser類(lèi)的思路相同,區(qū)別在于用戶界面的不同,對(duì)應(yīng)要使用的功能也不同,分別是查閱、新增、刪除和退出系統(tǒng)。

4.Book類(lèi)

package booksystem;

public class Book {
 private String name;
 private String author;
 private double price;
 private String type;
 private boolean isBorrowed = false;

 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 + '\'' + ",isBorrow=" + isBorrowed + '}';
 }

 public String getName() {
 return name;
 }

 public boolean isBorrowed(){
 return isBorrowed;
 }

 public void setBorrowed(boolean borrowed){
 isBorrowed=borrowed;
 }

}

Book類(lèi)針對(duì)書(shū)籍,定義了有關(guān)書(shū)的屬性,作者、價(jià)格、名字、類(lèi)別,重寫(xiě)了toString函數(shù),以及對(duì)于書(shū)的幾個(gè)常用的操作功能函數(shù),getName,以及判讀是否借出和定義書(shū)籍借出狀態(tài)的函數(shù)。

5.BookList類(lèi)

package booksystem;

public class BookList {
 private Book[] books=new Book[100];//定義一book數(shù)組
 private int size=0;

 public BookList(){
 books[0]=new Book("計(jì)算機(jī)網(wǎng)絡(luò)教程","郝文源",125,"專(zhuān)業(yè)書(shū)籍");
 books[1]=new Book("盜墓筆記","南派三叔",150,"網(wǎng)絡(luò)小說(shuō)");
 books[2]=new Book("三體","劉慈欣",178,"科幻小說(shuō)");
 size = 3;
 }//給book數(shù)組中初始化一些書(shū)
 public Book getBook(int index){
 return books[index];
 }
 public void setBook(int index,Book book)
 {
 books[index]=book;
 }
 public int getSize(){
 return size;
 }
 public void setSize(int size){
 this.size=size;
 }
}

BookList類(lèi)中定義了一個(gè)book數(shù)組,并給數(shù)組中初始化了一些書(shū),定義了常用的功能函數(shù)

6.Main類(lèi)

package booksystem;

import java.util.Scanner;
public class Main {
 public static void main(String[] args){
 Object o=null;
 BookList booklist= new BookList();

 User user=login();//上轉(zhuǎn)型,這里先調(diào)用了login()函數(shù),返回一個(gè)Admin對(duì)象或NormalUser對(duì)象

 while(true){
 int choice=user.menu();
 user.doOperation(choice,booklist);
 }//在進(jìn)行退出系統(tǒng)的功能時(shí),會(huì)一直進(jìn)行循環(huán),menu()函數(shù)最終會(huì)返回一個(gè)整型數(shù),對(duì)應(yīng)選擇操作中的一項(xiàng)
 }

public static User login() {
 System.out.println("請(qǐng)輸入您的姓名");
 Scanner scanner = new Scanner(System.in);
 String name = scanner.next();
 System.out.println("請(qǐng)輸入您的角色:1 管理員 0 普通用戶");//根據(jù)不同的選擇創(chuàng)建對(duì)應(yīng)的對(duì)象
 int who = scanner.nextInt();
 if (who == 1) {
 return new Admin(name);
 }
 return new NormalUser(name);
}
}

主函數(shù)中主要實(shí)現(xiàn)了login()函數(shù),根據(jù)登錄系統(tǒng)用戶的選擇,決定不同的身份,返回兩種對(duì)象中的一種,在while循環(huán)中,只要不進(jìn)行exit功能,循環(huán)便會(huì)一直執(zhí)行。

7.IOperation

package booksystem.operation;

import booksystem.BookList;

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

9.AddOperation

package booksystem.operation;

import booksystem.Book;

import booksystem.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
 @Override
 public void work(BookList bookList){
 Scanner scanner=new Scanner(System.in);
 System.out.println("新增書(shū)籍");
 System.out.println("請(qǐng)輸入新書(shū)籍的名稱");
 String name=scanner.next();
 System.out.println("請(qǐng)輸入新書(shū)籍的作者");
 String author=scanner.next();
 System.out.println("請(qǐng)輸入新書(shū)籍的價(jià)格");
 double price=scanner.nextDouble();
 System.out.println("請(qǐng)輸入新書(shū)籍的類(lèi)別");
 String type=scanner.next();
 Book newBook=new Book(name,author,price,type);
 int curSize=bookList.getSize();
 bookList.setBook(curSize,newBook);
 bookList.setSize(curSize+1);
 System.out.println("新增書(shū)籍成功");
 }
}

10.BorrowOperation

package booksystem.operation;

import booksystem.Book;

import booksystem.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
 @Override
 public void work(BookList bookList)
 {
 Scanner scanner=new Scanner(System.in);
 System.out.println("借閱書(shū)籍");
 System.out.println("請(qǐng)輸入要借閱的書(shū)籍的名稱");
 String name=scanner.next();

 int i=0;
 for(;i<bookList.getSize();i++)
 {
 if(bookList.getBook(i).getName().equals(name)){
 break;
 }
 }
 if(i>=bookList.getSize()){
 System.out.println("未找到指定的書(shū)籍,無(wú)法借閱!");
 return;
 }
 Book currentBook=bookList.getBook(i);
 if(currentBook.isBorrowed()){
 System.out.println("該書(shū)籍已經(jīng)被借出!");
 return;
 }
 bookList.getBook(i).setBorrowed(true);
 System.out.println("借書(shū)成功!");
 }


}

11.DelOperation

package booksystem.operation;

import booksystem.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
 @Override
 public void work(BookList bookList)
 {
 Scanner scanner=new Scanner(System.in);
 System.out.println("刪除書(shū)籍");
 System.out.println("請(qǐng)輸入要?jiǎng)h除的書(shū)籍的名稱");
 String name=scanner.next();

 int i=0;
 for(;i<bookList.getSize();i++)
 {
 if(bookList.getBook(i).getName().equals(name)){
 break;
 }
 }
 if(i>=bookList.getSize()){
 System.out.println("您輸入的書(shū)籍在+"+name+"在系統(tǒng)中沒(méi)有找到!刪除失敗!");
 return;
 }
 if(i==bookList.getSize()-1)
 {
 bookList.setSize(bookList.getSize()-1);
 System.out.println("刪除成功!");
 return;
 }
 bookList.setBook(i,bookList.getBook(bookList.getSize()-1));
 bookList.setSize(bookList.getSize()-1);
 System.out.println("刪除成功!");
 }
}

13.ExitOperation

package booksystem.operation;

import booksystem.BookList;

public class ExitOperation implements IOperation{
 @Override
 public void work(BookList bookList)
 {
 System.out.println("退出程序");
 System.exit(0);
 }
}

14.FindOperation

package booksystem.operation;

import booksystem.BookList;

public class ExitOperation implements IOperation{
 @Override
 public void work(BookList bookList)
 {
 System.out.println("退出程序");
 System.exit(0);
 }
}

15.ReturnOperation

package booksystem.operation;

import booksystem.BookList;

import booksystem.Book;
import java.util.Scanner;

public class ReturnOperation implements IOperation{
@Override
 public void work(BookList bookList){
 Scanner scanner=new Scanner(System.in);
 System.out.println("歸還書(shū)籍");
 System.out.println("請(qǐng)輸入要還的書(shū)籍的名稱");
 String name=scanner.next();
 int i=0;
 for(;i<bookList.getSize();i++)
 {
 Book book=bookList.getBook(i);
 if(book.getName().equals(i))
 {
 break;
 }
 }
 if(i>=bookList.getSize())
 {
 System.out.println("書(shū)籍沒(méi)有找到,無(wú)法歸還");
 return;
 }
 Book currentBook=bookList.getBook(i);
 if(!currentBook.isBorrowed())
 {
 System.out.println("這本書(shū)沒(méi)有借出,無(wú)法歸還");
 }
 currentBook.setBorrowed(false);
 System.out.println("歸還書(shū)籍成功");
 return;
}
}

四、編程截圖及測(cè)試圖

包和類(lèi)放置圖

運(yùn)行截圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java圖片格式轉(zhuǎn)換的三段代碼

    java圖片格式轉(zhuǎn)換的三段代碼

    這篇文章主要分享了java圖片格式轉(zhuǎn)換的三段代碼,小編查找資料整理了java圖片格式轉(zhuǎn)換的三段不同代碼,推薦給大家。
    2015-11-11
  • java單機(jī)接口限流處理方案詳解

    java單機(jī)接口限流處理方案詳解

    這篇文章主要為大家詳細(xì)介紹了java單機(jī)接口限流處理方案,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Spring Boot中快速操作Mongodb數(shù)據(jù)庫(kù)指南

    Spring Boot中快速操作Mongodb數(shù)據(jù)庫(kù)指南

    這篇文章主要給大家介紹了關(guān)于Spring Boot中如何快速操作Mongodb的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法

    java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • 淺談spring-boot的單元測(cè)試中,@Before不被執(zhí)行的原因

    淺談spring-boot的單元測(cè)試中,@Before不被執(zhí)行的原因

    這篇文章主要介紹了淺談spring-boot的單元測(cè)試中,@Before不被執(zhí)行的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Java Socket通信之聊天室功能

    Java Socket通信之聊天室功能

    這篇文章主要為大家詳細(xì)介紹了Java Socket通信之聊天室功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • spring cloud openfeign 源碼實(shí)例解析

    spring cloud openfeign 源碼實(shí)例解析

    這篇文章主要介紹了spring cloud openfeign 源碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • springboot中的pom文件?project報(bào)錯(cuò)問(wèn)題

    springboot中的pom文件?project報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了springboot中的pom文件?project報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring?Boot?內(nèi)置工具類(lèi)ReflectionUtils的實(shí)現(xiàn)

    Spring?Boot?內(nèi)置工具類(lèi)ReflectionUtils的實(shí)現(xiàn)

    ReflectionUtils是一個(gè)反射工具類(lèi),它封裝了Java反射的操作,使得我們能夠更輕松地操作和訪問(wèn)類(lèi)的方法、字段,本文主要介紹了Spring?Boot?內(nèi)置工具類(lèi)ReflectionUtils的實(shí)現(xiàn),感興趣的可以了解一下
    2023-11-11
  • spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟

    spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟

    在Java中,@Scheduled注解是用于指定定時(shí)任務(wù)的執(zhí)行規(guī)則的,這篇文章給大家介紹spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟,感興趣的朋友一起看看吧
    2023-12-12

最新評(píng)論