这篇文章将为大家详细讲解有关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(;iDelete类
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;iExit类
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(;iIO接口
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;iReturn类
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(;iuser包:
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如何实现图书馆管理系统”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
网站名称:java如何实现图书馆管理系统
文章分享:http://cxhlcq.com/article/pdhcgh.html