您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关如何使用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实现图书馆管理系统”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。