java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java图书馆管理系统

java实现图书馆管理系统

作者:lijiangjiajun

这篇文章主要为大家详细介绍了java实现图书馆管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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);
 }

 }
}

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文