代码仓库地址:Java图书管理系统
1.前言
该项目将JavaSE的封装继承多态三大特性,使用了大量面向对象的操作,有利于巩固理解
(1)实现效果
2.实现步骤
第一步先把框架搭建起来,即创建出人:管理员和普通用户,书:书架
再实现内部的业务逻辑
(1)书book包下类的实现
此时我们先实现图书中的成员属性
1.Book类的实现
此时我们实现了图书所对应的成员变量,以及所对应的构造方法来初始化,Getter和Setter方法。toString方法。
- 注意:构造方法中不需要初始化布尔类型的islend,因为它默认是false未借出的状态
- 因为成员变量都是private修饰的,只能在当前类下访问,所以我们使用Getter和Setter使得它在其它类的实现下可以访问(即后期新增图书设置书名,作者等时)
- 重写toString方法是为了能够正确显示数据所对应的成员变量,而不是地址
package book;public class Book {private String name;private String author;private int price;private String type;private boolean islend;//是否被借出,默认为false未借出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 isIslend() {return islend;}public void setIslend(boolean islend) {this.islend = islend;}public Book(String name, String author, int price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +", islend=" + islend +'}';}
}
2.BookList类的实现
- usedSigned用来记录当前存放了多少书
-
private Book[] books = new Book[10];是指书架上能放十本书
- 当将BookList中初始化为三本书时,要将usedSized置为3
package book;public class BookList {private Book[] books = new Book[10];private int usedSized;public BookList() {books[0]=new Book("三国演义","罗贯中",89,"小说");books[1]=new Book("西游记","吴承恩",78,"小说");books[2]=new Book("红楼梦","曹雪芹",49,"小说");this.usedSized=3;}
}
(2)用户user包下类的实现
1.User类的实现
package user;public class User {protected String name;public User(String name) {this.name = name;}
}
2.AdminUser类的实现
package user;public class AdminUser extends User{public AdminUser(String name) {super(name);}public void menu() {System.out.println("*******管理员菜单******");}
}
3.NormalUser类的实现
package user;public class NormalUser extends User{public NormalUser(String name) {super(name);}public void menu() {System.out.println("*******普通用户菜单******");}
}
以上代码Main函数的实现情况
- AdminUser和NormalUser在login类中的返回值应返回User,因为它俩都继承了User
- 需要增加User类下的menu()方法,并将该类和该方法设为抽象类,不描述对象只用于继承以便当user调用不同用户下的菜单发生动态绑定
-
引用AdminUser和 NormalUser发生向上转型体现在不同用户下都存在的menu()方法
-
User user = login();此时发生动态绑定,有可能引用管理员用户,也有可能引用普通用户
-
user.menu(); //user调用的对象不一样,调用同一个方法会发生动态绑定
Main类
import user.AdminUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {public static User login() {System.out.println("请输入姓名:");Scanner scanner = new Scanner(System.in);String userName = scanner.nextLine();System.out.println("请输入你的身份: 1-》管理员 0-》普通用户");int choice = scanner.nextInt();if (choice == 1) {return new AdminUser(userName);} else {return new NormalUser(userName);}//引用AdminUser和 NormalUser发生向上转型体现在不同用户下都存在的menu()方法}public static void main(String[] args) {//此时发生动态绑定,有可能引用管理员用户,也有可能引用普通用户User user = login();user.menu(); //user调用的对象不一样,调用同一个方法会发生动态绑定}
}
实现
完善不同用户下的menu菜单
1.管理员用户下的menu
@Overridepublic int menu() {System.out.println("*******管理员菜单******");System.out.println("1. 查找图书");System.out.println("2. 新增图书");System.out.println("3. 删除图书");System.out.println("4. 显示图书");System.out.println("0. 退出系统");System.out.println("请输入你的操作: ");Scanner scanner=new Scanner(System.in);int choice=scanner.nextInt();return choice;}
1.普通用户下的menu
@Overridepublic int menu() {System.out.println("*******普通用户菜单******");System.out.println("1. 查找图书");System.out.println("2. 借阅图书");System.out.println("3. 归还图书");System.out.println("0. 退出系统");System.out.println("请输入你的操作: ");Scanner scanner=new Scanner(System.in);int choice=scanner.nextInt();return choice;}
重写menu设置了int类型的返回值,所以要修改User下抽象类menu的返回值
此时不同用户下调用menu菜单返回的choice是一个整型值,我们定义一个变量来接收这个值,以便后续当返回choice调用不同用户下所对应的功能
(3)接口及不同用户下不同操作的实现
//IOperation接口
package operation;import book.BookList;public interface IOperation {public void work(BookList bookList);
}//AddOperation类
package operation;import book.BookList;public class AddOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("新增图书......");}
}//BorrowedOperation类
package operation;import book.BookList;public class BorrowedOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书......");}
}//DelOperation类
package operation;import book.BookList;public class DelOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("删除图书......");}
}//ExitOperation类
package operation;import book.BookList;public class ExitOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统......");}
}//FindOperation类
package operation;import book.BookList;public class FindOperation implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("查找图书......");}
}//ReturnOperation类
package operation;import book.BookList;public class ReturnOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("归还图书......");}
}//ShowOperation类
package operation;import book.BookList;public class ShowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("展示图书......");}
}
- 为了区分哪些是管理员的操作哪些是普通用户的操作,我们使用接口的方法来组织这些对象
- 并在User类下定义一个调用接口的数组,protected IOperation[] iOperations;(不能定义在对应的用户下面,因为不同的用户会继承User类,这样就不用重复定义)
- 因为AdminUser和NormalUser继承了User类所以它俩都继承了User类下定义的数组
- 不同用户下对应的操作不一样,所以当我们继承User类中定义的数组时,在不同的用户下进行不同的初始化
5.对应的下标要相对应
6.当User发生动态绑定引用管理员用户或者普通用户时会返回choice,我们又在不同用户下初始化了ioperation数组,那我们怎样操作当输入choice时调用相应用户所对应的类进而再发生动态绑定输出对应的类
(4)具体业务的实现
1.FindOperation
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class FindOperation implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("查找图书......");System.out.println("请输入你要查找的图书名:");Scanner scanner = new Scanner(System.in);String bookName = scanner.nextLine();int CurrentSize = bookList.getUsedSized();for (int i = 0; i < CurrentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(bookName)) {System.out.println("找到了这本书:");System.out.println(book);return;}}System.out.println("没有你要找的书");}
}
2.ShowOperation
package operation;import book.Book;
import book.BookList;public class ShowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("展示图书......");int CurrentSize = bookList.getUsedSized();for (int i = 0; i < CurrentSize; i++) {Book book = bookList.getBook(i);System.out.println(book);}}
}
3.AddOperation
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class AddOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("新增图书......");if(bookList.isFull()) {System.out.println("书架满了,不能新增了");return;}Scanner scanner = new Scanner(System.in);System.out.println("请输入你要新增图书的书名:");String name = scanner.nextLine();System.out.println("请输入你要新增图书的作者:");String author = scanner.nextLine();System.out.println("请输入你要新增图书的类型:");String type = scanner.nextLine();System.out.println("请输入你要新增图书的价格:");int price = scanner.nextInt();Book book = new Book(name,author,price,type);int currentSize = bookList.getUsedSized();//把书放到指定的位置bookList.setBook(currentSize,book);//修改usedSizebookList.setUsedSized(currentSize+1);System.out.println("新增图书成功");/*for (int i = 0; i < currentSize; i++) {Book temp = bookList.getBook(i);if(temp.getName().equals(name)) {System.out.println("已经存在,不能增加!");return;}}*/}
}
4.DelOperation
删除图书和添加图书类似
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class DelOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("删除图书......");System.out.println("请输入你要删除的图书名:");Scanner scanner = new Scanner(System.in);String bookName = scanner.nextLine();int CurrentSize = bookList.getUsedSized();int pos = -1;int i = 0;for (i = 0; i < CurrentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(bookName)) {pos = i;break;}}if (i >= CurrentSize) {System.out.println("没有你要删除的图书");return;}//开始删除for (int j = pos; j < CurrentSize-1; j++) {//bookList[j] = bookList[j+1]Book book = bookList.getBook(j+1);bookList.setBook(j,book);}bookList.setUsedSized(CurrentSize-1);bookList.setBook(CurrentSize-1,null);System.out.println("删除成功");}
}
5.ExitOperation
package operation;import book.BookList;public class ExitOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统......");int CurrentSize = bookList.getUsedSized();for (int i = 0; i < CurrentSize; i++) {bookList.setBook(i,null);}System.exit(0);}}
6.BorrowedOperation
只需找到该图书后改变它是否被借出的状态即可
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class BorrowedOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书......");System.out.println("请输入你要借阅的图书名:");Scanner scanner = new Scanner(System.in);String bookName = scanner.nextLine();int CurrentSize = bookList.getUsedSized();int pos = -1;int i = 0;for (i = 0; i < CurrentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(bookName)) {book.setIslend(true);System.out.println("借阅成功");return;}}System.out.println("借阅失败");}
}
7.ReturnOperation
同借书改变状态即可
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class ReturnOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("归还图书......");System.out.println("请输入你要归还的图书名:");Scanner scanner = new Scanner(System.in);String bookName = scanner.nextLine();int CurrentSize = bookList.getUsedSized();int pos = -1;int i = 0;for (i = 0; i < CurrentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(bookName)) {book.setIslend(false);System.out.println("归还成功");return;}}System.out.println("归还失败");}
}