目录
实现的样子
大致思路
代码 (按照这个目录创建包)
首先是book包下,有两个类
book类
bookList类
然后是operation包下,有7个类和1个接口
AddOperation类
BorrowOperation类
DelOperation类
DisplayOperation类
ExitOperation类
FindOperation类
ReturnOperation类
IOperation接口
最后是user包下,有3个类
AdminUser类
NormalUser类
User类
还有一个外边的Main类
Main类
首先让我们来看一下系统实现以后大致的样子
如果说要做一个大致这样子的图书管理系统,那么我们要有大致的思路:
首先我们要创建三个包,user,operation,book
有两种不同的人,管理员和用户,总共有7个功能,也就是要写7个功能类
然后我们发现,这7个功能类都要定义这么一个work方法,而且都是对BookList进行操作的,因此我们可以设置一个接口IOperation,让这些功能类实现这个接口
然后我们再看User这个包,这里面有两种用户,一个是管理员,一个是普通用户,注意到他们都有各自的菜单,同时他们都有姓名等一些公共的属性,所以这里我们可以定义一个User父类,让AdminUser和 NormalUser去继承这个类。
现在你只有一个user,所以根据choice调用不同的操作这个方法自然也应该在user这个类中。首先在User内部定义一个成员变量,操作接口的数组,用于存放不同的操作类
然后在AdminUser和NormalUser按顺序初始化这些操作类,这里相当于IOperation接口发生了向上转型
在User里面定义doOperation方法,根据choice调用不同操作类的work方法对booklist数组进行操作
10.由于选择功能使用一次以后,不能直接退出,需要再次显示菜单,所以我们将选择菜单和调用相应方法这两个语句写到死循环中
11.接下来就只需要写那几个功能类就可以了
12.ExitOperation,System.exit(0);即可
13.AddOperation,很简单,让用户输入图书相应的信息,然后新建一个book类,初始化放到bookList的usedSize位置即可,之后别忘了更新usedSize
14.FindOperationn,fori遍历即可,每一次遍历可以先定义一个book对象,然后比较该对象的名称与输入的名称是否一样,如果找到了,提示并打印,然后直接return;这样就不用设置flag了,如果走出了for循环还没有return,那么就说明没找到,提示没找到即可。
15.DisplayOperation,fori遍历即可
16.BorrowOperation,ReturnOperation,对相应isBorrowed变量进行相应操作即可
代码亲测能正常运行,如果不想自己慢慢新建类的话,可以点击下面链接自取。不过其实新建类也挺快的,要注意不要把类的名字建错。有什么不会弄的都可以评论问我。
Library.rar-Java文档类资源-CSDN下载
package Library.book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorowed; //默认是flase,默认没有被借,所以可以不用初始化
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
//默认是flase,默认没有被借,所以可以不用初始化
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorowed() {
return isBorowed;
}
public void setBorowed(boolean borowed) {
isBorowed = borowed;
}
@Override
public String toString() {
return "book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorowed=" + isBorowed +
'}';
}
}
package Library.book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize = 0;
public BookList(){
books[0] = new Book("游戏人生","榎宫祐",12,"轻小说");
books[1] = new Book("见面5秒开始战斗","肠才藏",20,"漫画");
books[2] = new Book("碧蓝之海","井上竖二",25,"漫画");
this.usedSize = 3;
}
public Book getBook(int pos) { //取pos位置的那本书
return this.books[pos];
}
public void setBook(int pos, Book book) { //将pos位置的书置为book
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
package Library.operation;
import Library.book.Book;
import Library.book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/18
* Time: 22:43
* Description:新增图书
*/
public class AddOperation implements IOperation {
public void work(BookList bookList){
Scanner scanner = new Scanner(System.in);
System.out.println("新增图书");
//只需要放到当前数组的usedSize位置
System.out.println("请输入图书的名字:");
String name = 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,author,price,type);
int curSize = bookList.getUsedSize();
bookList.setBook(curSize,book);
bookList.setUsedSize(curSize+1);
System.out.println("图书添加成功!");
}
}
package Library.operation;
import Library.book.Book;
import Library.book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:02
* Description: 借阅书籍
*/
public class BorrowOperation implements IOperation {
public void work(BookList bookList){
System.out.println("借阅书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要借阅的图书名称:");
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
if (!book.isBorowed()){
book.setBorowed(true); //如果找到该书,且该书没有被借阅,那就借阅成功
System.out.println("图书借阅成功!");
}else {
System.out.println("该图书已被借阅!");
}
return;
}
}
System.out.println("该图书不存在!"); //走到这里说明这本书不存在
}
}
package Library.operation;
import Library.book.Book;
import Library.book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:03
* Description: 删除图书
*/
public class DelOperation implements IOperation{
public void work(BookList bookList){
Scanner scanner = new Scanner(System.in);
System.out.println("删除图书");
System.out.println("请输入要删除的图书名称:");
String name = scanner.next();
int n = bookList.getUsedSize();
for (int i = 0; i < n; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
for (int j = i; j < n-1; j++) {
bookList.setBook(j,bookList.getBook(j+1));
}
bookList.setUsedSize(n-1);
System.out.println("图书删除成功!");
return; //如果找到了并且删除成功,就return
}
}
System.out.println("对不起,没有找到您需要删除的书!"); //如果没有for循环中没有return,那就是没有找到
}
}
package Library.operation;
import Library.book.BookList;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:03
* Description: 显示书籍
*/
public class DisplayOperation implements IOperation {
public void work(BookList bookList){
System.out.println("显示书籍");
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
package Library.operation;
import Library.book.BookList;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:04
* Description: 退出系统
*/
public class ExitOperation implements IOperation {
public void work(BookList bookList){
System.out.println("退出系统");
System.exit(0); //退出系统
}
}
package Library.operation;
import Library.book.Book;
import Library.book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:04
* Description: 查找图书
*/
public class FindOperation implements IOperation{
public void work(BookList bookList){
Scanner scanner = new Scanner(System.in);
System.out.println("查找图书");
System.out.println("请输入图书的名称:");
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
System.out.println("找到该图书!");
System.out.println(book);
return; //如果找到了,就直接return出去
}
}
System.out.println("对不起,没有找到此书!"); //如果走到这一步,那么说明没有在for循环return出去,那就显示没找到
}
}
package Library.operation;
import Library.book.Book;
import Library.book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:04
* Description: 归还图书
*/
public class ReturnOperation implements IOperation{
public void work(BookList bookList){
System.out.println("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要归还的图书名称:");
String name = scanner.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
if (book.isBorowed()){
book.setBorowed(false); //如果找到该书,且该书已被借阅,那就归还成功
System.out.println("图书归还成功!");
}else {
System.out.println("该图书未被借阅!");
}
return;
}
}
System.out.println("该图书不存在!"); //走到这里说明这本书不存在
}
}
package Library.operation;
import Library.book.BookList;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:06
* Description: No Description
*/
public interface IOperation {
void work(BookList bookList);
}
package Library.user;
import Library.operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:19
* Description: No Description
*/
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.IOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu(){
System.out.println("========================================");
System.out.println("尊贵的管理员-"+this.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.print("请输入数字以使用相应的功能:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
return n;
}
}
package Library.user;
import Library.operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:19
* Description: No Description
*/
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.IOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu(){
System.out.println("========================================");
System.out.println("尊贵的用户-"+this.name+",欢迎使用本图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("========================================");
System.out.print("请输入数字以使用相应的功能:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
return n;
}
}
package Library.user;
import Library.book.BookList;
import Library.operation.IOperation;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:18
* Description: No Description
*/
public abstract class User {
protected String name;
IOperation[] IOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperations(int choice, BookList bookList){
IOperations[choice].work(bookList);
}
}
package Library;
import Library.book.BookList;
import Library.user.AdminUser;
import Library.user.NormalUser;
import Library.user.User;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 13498
* Date: 2021/11/22
* Time: 14:44
* Description: No Description
*/
public class Main {
public static User login(){
System.out.println("请输入您的姓名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入您的身份:1 -> 管理员 0 -> 普通用户");
int n = scanner.nextInt();
if (n == 1){
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList(); //准备了书籍
User user = login(); //向上转型
while(true){ //写成死循环,这样就会不断的调用菜单,直到选择退出系统
int choice = user.menu(); //动态绑定调用menu方法,choice表示你的选择
user.doOperations(choice,bookList);
}
}
}
OK,就这些了,代码能运行无bug,如果有什么地方出错了或者有什么不懂都可以评论问我。