溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用java實現圖書館管理系統

發布時間:2021-09-26 18:01:33 來源:億速云 閱讀:150 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關如何使用java實現圖書館管理系統,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體內容如下

思路:所有包都在book_manage包里

利用面向對象的多態特性去除了很多if-else的判斷,不同的子類在父類所對應的方法不同。

1.首先建立一個book包

包里面有2個類,一個是Book,這個類里面包含一本書的全部信息另外一個類是BookList,這個類是用來管理每一個書,通過這個類來尋找每一本書。private Book[] books = new Book[100];Book數組里面存放所有的書。

2.再建立一個包Operation 這個類里面有一個OI接口,通過對接口里面的Work方法重寫,來實現管理員身份和普通用戶身份的不同操作。

3.最后建立一個User包,里面有三個類,User,Admin,NormalUser

Admin和NormalUser都繼承自User.User里秒你有一個數組protected IO[] operation;這個數組里面包含了用戶或者管理員所具備的操作。通過對數組的索引來確定具體需要調用的操作方法。

下面來看看代碼吧:

book包

Book類

package book_manager.book;public class Book { private String name; private String id; private String author; private int price; private String type; private boolean isBorrow; public Book(String name, String id, String author, int price,  String type, boolean isBorrow) { this.name = name; this.id = id; this.author = author; this.price = price; this.type = type; this.isBorrow = isBorrow; } @Override //Object中內置的類,用來格式化打印book的信息 public String toString() { return "Book{" +  "name='" + name + '\'' +  ", id='" + id + '\'' +  ", author='" + author + '\'' +  ", price=" + price +  ", type='" + type + '\'' +  ", isBorrow=" + isBorrow +  '}'; } public String getName(){ return name; } public boolean isBorrow(){ return isBorrow; } public void setBorrow(boolean bool){ this.isBorrow=bool; } public String getId(){ return id; }}

BookList類

package book_manager.book;import java.util.Arrays;public class BookList {  private Book[] books = new Book[100];  private int size;  public BookList(){  books[0] = new Book("金瓶梅",    "001", "蘭陵笑笑生", 100,    "古典名著", false);  books[1] = new Book("水滸傳",    "002", "施耐庵", 100,    "古典名著", false);  books[2] = new Book("西游記",    "003", "吳承恩", 100,    "古典名著", false);  size = 3; } public int getSize(){   return size; } public void setBooks(int index,Book book){   books[index]=book; } public void setSize(int size){   this.size=size; } public Book getBook(int index){   return books[index]; }}

Operation包:

ADD類

package book_manager.Operation;import book_manager.book.*;import java.util.Scanner;public class ADD implements IO{ @Override public void work(BookList bookList) {  Scanner scanner = new Scanner(System.in);  System.out.println("請輸入書名");  String name =scanner.next();  System.out.println("請輸入序號");  String id = scanner.next();  System.out.println("請輸入作者");  String author =scanner.next();  System.out.println("請輸入價格");  int price = scanner.nextInt();  System.out.println("請輸入類型");  String type = scanner.next();  Book book = new Book(name, id,    author, price, type, false);  bookList.setBooks(bookList.getSize(),book);  bookList.setSize(bookList.getSize()+1);  System.out.println("添加成功"); }}

Borrow類

package book_manager.Operation;import book_manager.book.Book;import book_manager.book.BookList;import java.util.Scanner;public class Borrow implements IO{ @Override public void work(BookList bookList) {  int i=0;  int flag=0;  Scanner scan = new Scanner(System.in);  System.out.println("請輸入需要借閱的書名");  String name = scan.next();  for(;i<bookList.getSize();i++){   if(name.equals(bookList.getBook(i).getName())){    if(bookList.getBook(i).isBorrow()==false){     System.out.println("借閱成功");     flag=1;     bookList.getBook(i).setBorrow(true);    }    }   }  if(flag==0){   System.out.println("不好意思,借閱失敗");  } }}

Delete類

package book_manager.Operation;import book_manager.book.BookList;import java.util.Scanner;public class Delete implements IO{ public void work(BookList bookList){  Scanner scanner = new Scanner(System.in);  System.out.println("請輸入想要刪除的編號");  String id = scanner.next();  for(int i=0;i<bookList.getSize();i++){   if(bookList.getBook(i).getId().equals(id)){    bookList.setBooks(i,bookList.getBook(bookList.getSize()));    bookList.setSize(bookList.getSize()-1);    System.out.println("刪除成功");   }   else{    System.out.println("刪除失敗");   }  } }}

Exit類

package book_manager.Operation;import book_manager.book.BookList;public class Exit implements IO{ @Override public void work(BookList bookList) {  System.out.println("退出成功");  System.exit(0); }}

Find類

package book_manager.Operation;import book_manager.book.BookList;import java.util.Scanner;public class Find implements IO{ @Override public void work(BookList bookList) {  int i=0;  int count=0;  Scanner scan = new Scanner(System.in);  System.out.println("請輸入需要查找的書名");  String name = scan.next();  for(;i<bookList.getSize();i++){   if(name.equals(bookList.getBook(i).getName())){    count++;   }  }  if(count==0){   System.out.println("不好意思,沒有找到");  }  else{   System.out.println("找到了,共計"+count+"本");  } }}

IO接口

package book_manager.Operation;import book_manager.book.BookList;public interface IO { abstract public void work(BookList bookList);}

PrintAll類

package book_manager.Operation;import book_manager.book.BookList;public class PrintAll implements IO{ @Override public void work(BookList bookList) {  for(int i=0;i<bookList.getSize();i++){   System.out.println(bookList.getBook(i));  } }}

Return類

package book_manager.Operation;import book_manager.book.BookList;import java.util.Scanner;public class Return implements IO{ @Override public void work(BookList bookList) {  int i=0;  int flag=0;  Scanner scan = new Scanner(System.in);  System.out.println("請輸入需要歸還的ID");  String id = scan.next();  for(;i<bookList.getSize();i++){   if(id.equals(bookList.getBook(i).getId())){    if(bookList.getBook(i).isBorrow()==true){     System.out.println("歸還成功");     bookList.getBook(i).setBorrow(false);     flag=1;    }    else{     System.out.println("歸還失敗");    }   }  }  if(flag==0){   System.out.println("不好意思,沒有此書");  } }}

user包:

User類

package book_manager.user;import book_manager.Operation.*;import book_manager.Operation.IO;import book_manager.book.BookList;abstract public class User { String name; protected IO[] operation; public User(String name){  this.name=name; } abstract public int menu();//該方法被重寫 public void doOperation(int choice, BookList bookList) {  operation[choice].work(bookList); }}

Admin類

package book_manager.user;import book_manager.Operation.*;import java.util.Scanner;public class Admin extends User{ public Admin(String name){  super(name);  operation=new IO[]{    new Exit(),    new Find(),    new ADD(),    new Delete(),    new PrintAll(),  }; } public int menu() {  System.out.println("============");  System.out.println("hello " + name);  System.out.println("1. 查找書籍");  System.out.println("2. 增加書籍");  System.out.println("3. 刪除書籍");  System.out.println("4. 打印所有信息");  System.out.println("0. 退出");  System.out.println("============");  System.out.println("請輸入您的選擇: ");  Scanner scanner = new Scanner(System.in);  int choice = scanner.nextInt();  return choice; } }

NormalUser

package book_manager.user;import book_manager.Operation.*;import java.util.Scanner;public class NormalUser extends User{ public NormalUser(String name){  super(name);  operation=new IO[]{    new Exit(),    new Find(),    new Borrow(),    new Return(),    new PrintAll()  }; } public int menu() {  System.out.println("============");  System.out.println("hello " + name);  System.out.println("1. 查找圖書");  System.out.println("2. 借閱圖書");  System.out.println("3. 歸還圖書");  System.out.println("4. 查看全部書籍");  System.out.println("0. 退出");  System.out.println("============");  System.out.println("請輸入您的選擇: ");  Scanner scanner = new Scanner(System.in);  int choice = scanner.nextInt();  // close 本質上是在關閉 System.in  // 由于后面還需要用到 System.in, 此處不能盲目關閉.  // scanner.close();  return choice; }}

最后還有一個Test類,里面放了main函數

package book_manager;import book_manager.book.BookList;import book_manager.user.Admin;import book_manager.user.NormalUser;import book_manager.user.User;import java.util.Scanner;public class Test { public static void main(String[] args) {  BookList list = new BookList();  User user = login();  //通過不同的choice和身份調用不同的Operation方法   while(true){   int choice = user.menu();   user.doOperation(choice, list);  } } public static User login(){  Scanner scanner = new Scanner(System.in);  System.out.println("請輸入你的姓名");  String name = scanner.next();  System.out.println("請輸入你的身份");  System.out.println("1.普通用戶 2.管理員");  int role= scanner.nextInt();  if(role==1){   return new NormalUser(name);  }  else{   return new Admin(name);  } }}

關于“如何使用java實現圖書館管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女