目录
图书管理系统
项目目标
- 熟悉类与类之间的关系
- 掌握抽象类,接口,继承,封装,多态,向上转型,顺序表等知识点的应用
一:简介
该项目主要是,利用前面所学的知识点:类,抽象类,接口,封装,继承,多态,向上转型,顺序表等进行的一个简单的代码练习。
二:核心需求
1.简单的登录
2.管理员端
- 查找图书
- 新增图书
- 删除图书
- 显示所有图书
- 退出系统
3.普通用户端
- 查找图书
- 借阅图书
- 归还图书
- 退出系统
三:类的设计
1.创建图书相关的类
先创建package book
创建Book类,表示一本书
package book;
public class Book {//创建Book类表示一本书
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
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 isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
创建BookList类,用来保存N本书
package book;
public class BookList {//创建BookList类用来保存N本书
private Book[] books = new Book[100];//假设最大存放100本书
private int usedSize = 0;
public BookList(){//在构造方法中弄几个初始值方便后续测试
books[0] = new Book("三国演义","罗贯中",100,"小说");
books[1] = new Book("水浒传","施耐庵",100,"小说");
books[2] = new Book("西游记","吴承恩",100,"小说");
this.usedSize = 3;
}
//我们可以把所有的操作都写到这个类当中,因为每一个操作都是操作books
public void setBooks(int pos,Book book){
this.books[pos] = book;
}
public Book getBook(int pos){
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
2.创建操作相关的类
先创建package operation
再创建一个IOperation接口
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
接下来创建一组操作类,每个类对应一个用户的动作
先把空类创建好,不要着急实现细节
抽象出Operation的好处:让操作和操作之间低耦合,让操作和用户之间低耦合
3.创建用户相关的类
先创建package user
创建User类,这是一个抽象类
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] operations;
public User(String name){
this.name = name;
}
public abstract int menu();
public void doOperation(BookList bookList,int choice) {
this.operations[choice].work(bookList);
}
}
创建普通用户类,是User的子类
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("===============");
System.out.println("hello "+ this.name + " 欢迎来到图书管理系统");
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;
}
}
创建管理员用户类,这也是User的子类
package user;
import operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name){
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
System.out.println("===============");
System.out.println("hello "+ 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("===============");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
4.进行整合
创建TestMain类搭建整体逻辑
import book.BookList;
import user.Admin;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class TestMain {
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名: ");
String name = scanner.nextLine();
System.out.println("请输入你的身份:1--》管理员,2--》普通用户");
int choice = scanner.nextInt();
if (choice == 1){
return new Admin(name);
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
//1.准备书籍
BookList bookList = new BookList();
//2.登录
User user = login();
while (true) {
int choice = user.menu();//这里就用到了多态,引用谁的对象就调用谁的menu
//选择是几,就可以对应哪个方法了
user.doOperation(bookList, choice);
}
}
}
5.实现具体的每个Operation
- 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);
System.out.println("请输入图书的名字: ");
String name = scanner.nextLine();
System.out.println("请输入图书的作者: ");
String author = scanner.nextLine();
System.out.println("请输入图书的价格: ");
int price = scanner.nextInt();
System.out.println("请输入图书的类型: ");
String type = scanner.next();//nextInt和nextLine不能在一起用,所以要将这里的nextLine改为next
Book book = new Book(name,author,price,type);
int curSize = bookList.getUsedSize();
bookList.setBooks(curSize,book);
bookList.setUsedSize(curSize+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.getUsedSize();i++){
Book book = bookList.getBook(i);
if (book.getName().equals(name)){//判断输入的书名和库里的书名是否相等,目的是判断库里有没有要找的书
//可以借阅
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("请输入删除图书的名字:");
String name = scanner.nextLine();
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
break;
}
}
if (i == bookList.getUsedSize()){
System.out.println("没有这本书!");
return;
}
//删除的逻辑和顺序表一样
int pos = i;
for (; pos < bookList.getUsedSize()-1; pos++) {
Book book = bookList.getBook(pos+1);
bookList.setBooks(pos,book);
}
bookList.setUsedSize(bookList.getUsedSize()-1);
System.out.println("删除成功");
}
}
- DisOperation
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation {//显示所有书籍
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
for (int i = 0;i < bookList.getUsedSize();i++){
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
- ExitOperation
package operation;
import book.BookList;
public class ExitOperation implements IOperation{//退出系统
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(1);
}
}
- FindOperation
package operation;
import book.Book;
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);
System.out.println("请输入要查找图书的名字: ");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
System.out.println(book);
System.out.println("查找成功!");
return;
}
}
System.out.println("没有你要查找的那本书啊!");
}
}
- ReturnOperation
package operation;
import book.Book;
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);
System.out.println("请输入要归还图书的名字");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize() ; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("没有你要归还的图书!");
}
}
代码目录: