文章目录
个人主页
图书系统逻辑分析
该程序设置有三个包,user 包,book 包,operation包。
- book包中包含对于书的一些信息和操作;
- operation包中包含有对 书 的所有操作功能;
- user包中包含用户的一些信息和操作
抽离整个图书管理系统的对象: 用户 、 书、 书架
book包中包含对于书的一些信息和操作
book包中存在有 Book类,BookList类
Book类中,有图书的属性:图书名,作者,类型,价格,是否被借出。
Book类中的成员变量都被 private 修饰,所以使用 Getter and Setter 来访问和设置 成员变量的值。Book类中也重写了 toString 方法,用于后序显示图书的操作,这里注意重写的toString方法中,关于 isBorrowed 的修改,使用 isBorrowed == true ? "已借出" : "未借出"
动态表示图书状态
package book;
public class Book {
private String name;
private String author;
private String type;
private float price;
private boolean isBorrowed;
public Book(String name, String author, String type, float price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
", " + (isBorrowed == true ? "已借出" : "未借出") +
'}';
}
}
BookList类中 定义books数组用来存储图书,初始化数组容量为10,后序添加图书操作中如果数组满了,再进行扩容;
定义 usedSize表示数组中有效图书的数量;使用构造方法对数组books初始化,即每次创建一个bookList 对象books数组里面都是默认存储三本书,默认有效图书数量为3
定义findBook方法,用来查找 pos下标位置的图书并返回,后序主要被操作图书的各种类所调用
定义setBook方法,用来新增一本图书
定义getUsedSize方法,用来得到数组中有效图书的数量,后序主要被操作图书的各种类所调用
定义setUsedSize方法,用来更改数组中有效图书的数量,后序被 删除图书的类 (DeltOperation) 和 新增图书的类(AddOperation) 中所调用
package book;
import java.util.Arrays;
public class BookList {
private Book[] books = new Book[10];//初始化数组容量为10
private int usedSize;//书架上的有效书的数量
//默认每次都是三本书
public BookList() {
books[0] = new Book("三国演义","罗贯中","小说",10);
books[1] = new Book("西游记","吴承恩","小说",15);
books[2] = new Book("红楼梦","曹雪芹","小说",20);
usedSize = 3;
}
/**
* 找到 pos 位置的书并返回
* @param pos
* @return
*/
public Book findBook(int pos) {
return books[pos];
}
/**
* 在 pos 位置新增一本书
* @param pos
* @param book
*/
public void setBook(int pos, Book book) {
//如果满了,进行扩容
if(usedSize == books.length) {
books = Arrays.copyOf(books,2*books.length);
}
books[pos] = book;
usedSize++;
}
/**
* 获取书架上的有效书本的个数并返回
* @return
*/
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
operation包中包含有对 书 的所有操作功能
operation包中定义有一个Ioperation接口,和AddOperation类,FindOperation类,BorrowedOperation类,ReturnOperation类,ShowOperation类,ExitOperation类,DeltOperation类。这些类都对应着增添图书功能,查找图书功能,借阅图书功能,归还图书功能,显示图书功能,退出图书系统功能,删除图书功能。
Ioperation接口中定义work(BookList bookList)方法(此方法是抽象方法),表示对图书的操作
让所有类实现Ioperation接口,按照需求重写work方法。
【这里使用接口的原因:后续会把用户操作菜单中的各种操作放入一个数组中,数组是相同元素的集合,把这些操作类都实现Ioperation接口,那么数组的元素类型就可以设置为Ioperation,满足了数组的定义。如果不使用接口,那么这些类就无法放入同一个数组中,因为每个元素的类型都不一样】
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("AddOperation::");
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("BorrowedOperation::");
}
}
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("DelOperation::");
}
}
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("ExitOperation::");
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {System.out.println("FindOperation::");
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("ReturnOperation::");
}
}
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("ShowOperation::");
}
}
user包中包含用户的一些信息和操作
user包中有User类, AdiminUser类,NormalUser类,其中AdiminUser类 和 NormalUser类都继承了User类,为了满足选择不同的身份,进而打印出不同的menu菜单,我们在父类User里定义一个menu()方法,子类按照需求重写menu()方法
【注意】按照上述描述,可以把User设置成抽象类,把没有具体实现的menu方法设置为抽象方法
打印出菜单后,可以把操作的功能放入一个数组中,但是不同用户会显示不同的操作功能,因此数组内容由身份决定,所以如何实现作为不同身份,操作不同的数组? -> 可以在父类User类中定义 array数组,
在子类的构造方法中初始化数组,填充不同的功能元素,这样在选择不同的身份时,在各自的构造方法中就完成了数组内容的初始化。此时的数组内容就会根据身份而创建。
不同身份的数组已经准备好,选择一个数字进行操作
应该这样调用类中的方法: array[choice].work(bookList)
所以可以在User类中定义 doWork(int choice,BookList bookList)方法,用来实现所选择的操作
// User类
package users;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] array;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doWork(int input, BookList bookList) {
array[input].work(bookList);
}
}
// AdiminUser类
package users;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
array = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name +"来到图书系统");
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("❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
return input;
}
}
// NormalUser类
package users;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
array = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name +"来到图书系统");
System.out.println("❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀普通用户菜单❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
return input;
}
}
创建 Main类 ,实现登录操作
import book.BookList;
import users.AdminUser;
import users.NormalUser;
import users.User;
import java.util.Scanner;
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scanner.nextLine();
int choice = 0;
do {
System.out.println("请选择你的身份: 1:管理员 2:普通用户");
choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else if(choice == 2){
return new NormalUser(name);
}else {
System.out.println("选择错误,请重新选择(1 or 2)");
}
}while(choice != 1 || choice != 2);
return null;
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while(true) {
int ret = user.menu();
user.doWork(ret,bookList);
}
}
}
各种操作功能的实现
ShowOperation类 ------显示图书操作
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.findBook(i));
}
}
}
FindOperation类 ------查找图书操作
package operation;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.findBook(i).getName().equals(name)) {
System.out.println("成功找到该图书,信息如下:");
System.out.println(bookList.findBook(i));
return;
}
}
System.out.println("该图书尚未添加...");
}
}
AddOperation类 ------新增图书操作
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入作者名:");
String author = scanner.nextLine();
System.out.println("请输入图书类型:");
String type = scanner.nextLine();
System.out.println("请输入价格:");
float price = scanner.nextFloat();
Book book = new Book(name,author,type,price);
bookList.setBook(bookList.getUsedSize(), book);
System.out.println("新增图书成功");
}
}
DeltOperation类 ------删除图书操作
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入将要删除的图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.findBook(i).getName().equals(name)) {
for (int j = i+1; j < bookList.getUsedSize(); j++) {
Book tmp = bookList.findBook(j-1);
tmp = bookList.findBook(j);
}
bookList.setUsedSize(bookList.getUsedSize()-1);
System.out.println("删除图书成功");
return;
}
}
System.out.println("该图书尚未添加...");
return;
}
}
BorrowedOperation类 ------借阅图书操作
package operation;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.findBook(i).getName().equals(name)) {
if(bookList.findBook(i).isBorrowed()) {
System.out.println("该图书已被借出...");
return;
}
bookList.findBook(i).setBorrowed(true);
System.out.println("借阅图书成功");
return;
}
}
System.out.println("该图书尚未添加...");
}
}
ReturnOperation类 ------归还图书操作
package operation;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("请输入图书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.findBook(i).getName().equals(name)) {
if(!bookList.findBook(i).isBorrowed()) {
System.out.println("该图书未被借出...");
return;
}
bookList.findBook(i).setBorrowed(false);
System.out.println("归还图书成功");
return;
}
}
System.out.println("该图书尚未添加...");
}
}
ExitOperation类 ------退出系统操作
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出程序...");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
bookList.setBook(i,null);
}
System.exit(0);
}
}
完整代码
---------完整代码请点击 本人码云链接---------
标签:管理系统,System,bookList,简单,println,import,public,图书,out From: https://blog.csdn.net/2401_83147199/article/details/143215859