- 博主简介:努力学习的预备程序媛一枚~
- 博主主页: @是瑶瑶子啦
- 所属专栏: Java岛冒险记【从小白到大佬之路】
文章目录
- 一、项目简介(流程图)
- 二、设计思路(UML图)
- 三、代码
- 四、总结&反思:
一、项目简介(流程图)
二、设计思路(UML图)
三、代码
Book
类:
package book;
/**
* Created with IntelliJ IDEA.
* Description:
* Author: yaoyao2024
* Date: 2023-07-13
* Time: 20:42
*/
public class Book {
private String name;
private int price;
private String type;
private String author;
private boolean isBorrowed;
public Book(String name, int price, String type, String author) {
this.name = name;
this.price = price;
this.type = type;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", type='" + type + '\'' +
", author='" + author + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
}
BookList
package book;
/**
* Created with IntelliJ IDEA.
* Description:
* Author: yaoyao2024
* Date: 2023-07-13
* Time: 20:47
*/
public class BookList {
private Book[] books = new Book[10];
private int bookNum = 0;
public void setBooks(int pos, Book book) {
books[pos] = book;
}
public int getBookNum() {
return bookNum;
}
public Book getBookByIndex(int index){
return books[index];
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
public BookList(){
books[0] = new Book("红楼梦",12,"小说","曹雪芹");
books[1] = new Book("三国演义",13,"小说","罗贯中");
books[2] = new Book("水浒传",14,"小说","施耐庵");
this.bookNum = 3;
}
}
- 接口:
IOperation
package Operation;
import book.BookList;
public interface IOperation {
public void work(BookList bookList);
}
- 实现接口类:
AddOperation
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
if(bookList.getBookNum()>10){
System.out.println("数量已满,无法添加!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
// scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入图书价格:");
int price = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入图书类型:");
String type = scanner.nextLine();
Book book = new Book(name,price,type,author);
bookList.setBooks(bookList.getBookNum(),book);
bookList.setBookNum(bookList.getBookNum()+1);
System.out.println("新增图书成功!");
}
}
- 实现接口类:
BorrowOperation
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要借阅的图书的名字:");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBookNum(); i++) {
Book book = bookList.getBookByIndex(i);
if (name.equals(book.getName()) && !book.isBorrowed()) {
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("借阅失败!");
}
}
- 实现接口的子类
DelOperation
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);
System.out.println("请输入你要删除的图书的名字:");
for (int i = 0; i < bookList.getBookNum(); i++) {
String name = scanner.nextLine();
Book book = bookList.getBookByIndex(i);
if (name.equals(book.getName())) {
for( int j = i+1; j < bookList.getBookNum();j++){
bookList.setBooks(j-1,bookList.getBookByIndex(j));
}
bookList.setBookNum(bookList.getBookNum()-1);
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("退出系统");
System.exit(0);
}
}
- 实现接口的类:
FindOperation
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你要查找图书名字:");
String name = scanner.nextLine();
for (int i = 0; i <bookList.getBookNum(); i++) {
Book book = bookList.getBookByIndex(i);
if(name.equals(book.getName())){
System.out.println("找到了!");
System.out.println(book);
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) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你想归还图书的名字:");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBookNum(); i++) {
if (name.equals(bookList.getBookByIndex(i).getName())){
bookList.getBookByIndex(i).setBorrowed(false);
System.out.println("归还图书成功!");
return;
}
}
System.out.println("找不到该图书!");
return;
}
}
- 实现接口的类:
ShowOperation
package Operation;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示所有图书");
for (int i = 0; i < bookList.getBookNum(); i++) {
System.out.println(bookList.getBookByIndex(i));
}
}
}
- 抽象类:
User
package user;
import Operation.IOperation;
import book.BookList;
public abstract class User {
protected String name;
public abstract int menu();
protected IOperation[] operations;
public void doOperation(int choice, BookList bookList){
operations[choice].work(bookList);
}
public User(String name) {
this.name = name;
}
}
- 继承抽象类的管理员类
Admin
package user;
import Operation.*;
import java.util.Scanner;
public class Admin extends User{
public Admin(String name) {
super(name);
operations = new IOperation[]{new ExitOperation(),new FindOperation(),new AddOperation(),new DelOperation(),new ShowOperation()};
}
@Override
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.print("请输入你的选择:");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
}
- 子类:
NormalUser
package user;
import Operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
operations = new IOperation[] {new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()};
}
@Override
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("0、退出系统");
System.out.print("请输入你的选择:");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
}
- 主逻辑,main方法存在类:
Main
import book.BookList;
import user.Admin;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你的姓名:");
String name = scanner.nextLine();
System.out.println("请选择你的角色:1、管理员 0、普通成员");
int choice = scanner.nextInt();
if(choice == 1){
return new Admin(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();
user.doOperation(choice,bookList);
}
}
}
四、总结&反思:
-
Iopertation:定义一个接口(规范),所有的操作都必须实现这个接口
-
不同用户有自己不同的菜单打印方式,所以把这个抽象方法定义在抽象类User,中,且在子类进行重写,即可用父类引用调用打印菜单方法时触发动态绑定,调用各自的打印菜单的方法(多态的好处,不用再去判断哪个是普通用户。哪个是管理员,减少了“圈复杂度)!
-
最重要!!!巧妙的是在User子类的构造方法中初始化父类的ioperations数组,这样根据子类创建的初始化过程,子类在初始化的时候将父类属性也初始化了,而巧的是,我们父类引用指向恰巧只能访问父类属性,但是这个属性巧妙的和子类的类型相关联,很巧妙!
这样我们即使只有父类引用时,访问属性,也访问的是子类各自类型对应的操作!!!
-
Java岛冒险记【从小白到大佬之路】
-
LeetCode每日一题–进击大厂
-
Go语言核心编程
-
算法