图书管理系统
一定要努力 坚持!
注:
项目重点不在业务逻辑的实现,而是在于框架,如何使用学习过的面向对象的思想来创建与实现。
冲冲冲 !
一、book包
book包实现了两个java文件:
1. Book.java (用于存储一本书的信息)
package book;
public class Book {
private String name;
private String author;
private String type;
private double price;
boolean isBorrowed; // 默认false
// 保证其隐私:封装
public Book(String name, String author, String type, double price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
// 这个不必写到构造方法中,默认就是false
/*this.isBorrowed = isBorrowed;*/
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean getIsBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
// 重写toString方法
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
", " + ((isBorrowed == false)?("未借出"):("已借出")) +
'}';
}
}
2. BookList.java (书架:用于陈列书,以Book类来创建数组)
package book;
public class BookList {
// 书架:作为一个数组,类型是Book,存储书的信息
private Book book[] = new Book[10];
private int usedSize; //已经存在的书本数量
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
// 以下是错误写法
/* private String name;
private String author;
private String type;
private float price;
boolean isBorrowed;
public BookList(Book[] book, String name, String author, String type, float price, boolean isBorrowed) {
this.book = book;
this.name = name;
this.author = author;
this.type = type;
this.price = price;
this.isBorrowed = isBorrowed;
}*/
public BookList() {
// 构造方法:先进行数组初始化--初始化几本书
book[0] = new Book("三国演义","罗贯中","小说",29.8);
book[1] = new Book("哈利波特","JK罗琳","小说",59.5);
book[2] = new Book("飞鸟集","泰戈尔","诗集",30);
usedSize = 3; //同一个类中可以直接赋值
}
// 此时pos一定合法
public Book getBook(int pos) {
return book[pos];
}
public void setBook(int pos, Book books) {
book[pos] = books;
}
}
二、operation包
1. IOperation.java (其实是interface)
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
2. FindOperation.java (查找图书)
import java.util.Scanner;
// 查找图书
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入您想要查找的图书名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
if(bookList.getBook(i).getName().equals(name)) {
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("抱歉 未找到此书!");
}
}
3. BorrowOperation.java (借阅图书)
package operation;
import book.BookList;
import java.util.Scanner;
// 借阅操作
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入您想要借阅的图书名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int i =0;
for (i = 0; i < currentSize; i++) {
if(bookList.getBook(i).getName().equals(name)) {
if(bookList.getBook(i).getIsBorrowed()== false) {
bookList.getBook(i).setBorrowed(true);
System.out.println("借阅成功!");
break;
}
}
}
if(i == currentSize) {
System.out.println("抱歉 不存在该图书!");
}
}
}
4. ReturnOperation.java (归还图书)
package operation;
import book.BookList;
import java.util.Scanner;
//归还操作
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入您想要归还的图书名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int i=0;
for (i = 0; i < currentSize; i++) {
if(bookList.getBook(i).getName().equals(name)) {
if(bookList.getBook(i).getIsBorrowed() == true) {
bookList.getBook(i).setBorrowed(false);
System.out.println("归还成功!");
break;
} else {
System.out.println("抱歉 该书未被借阅!");
break;
}
}
}
if(i == currentSize) {
System.out.println("抱歉 不存在该图书!");
}
}
}
5. AddOperation.java (新增图书)
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("请输入类型:");
String type = scanner.nextLine();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
// 构造一本书 用来传入参数
Book book = new Book(name,author,type,price);
int currentSize = bookList.getUsedSize();
// 注意传入的参数!!
/*bookList.setBook(currentSize, bookList.getBook());*/
// 修改后
bookList.setBook(currentSize, book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功!");
}
}
6. DelOperation.java (删除图书)
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 currentSize = bookList.getUsedSize();
// 进行覆盖
int i = 0;
int index = -1;
for (i = 0; i < currentSize; i++) {
if(bookList.getBook(i).getName().equals(name)) {
index= i; //进行标记
break;
}
}
// 跳出循环
// 1. i >= currentSize
if(i == currentSize) {
System.out.println("抱歉 未找到该图书!");
}
// 2. 找到图书
for (int j = index; j < currentSize-1; j++) {
bookList.setBook(j, bookList.getBook(j+1));
}
// 置空
bookList.setBook(currentSize-1,null);
bookList.setUsedSize(currentSize-1);
System.out.println("删除图书成功!");
}
}
7. DisOperation.java (显示图书信息)
package operation;
import book.BookList;
// 显示图书信息
public class DisOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("显示图书信息!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
System.out.println(bookList.getBook(i));
}
}
}
8. ExitOperation.java (退出系统)
package operation;
import book.BookList;
// 退出系统
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
三、User包
1. User.java (抽象类)
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String name) {
this.name = name;
}
public abstract int choice();
protected IOperation[] iOperation; // 只是声明,没有进行分配和初始化
public abstract void doOperation(int choice, BookList bookList);
}
2. NormalUser.java (普通用户)
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new DisOperation()
};
}
@Override
public int choice() {
System.out.println("hello "+ getName() +" 欢迎来到图书管理系统!");
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;
}
public void doOperation(int choice, BookList bookList) {
this.iOperation[choice].work(bookList);
}
}
3. AdminUser.java (管理员)
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperation = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisOperation()
};
}
@Override
public int choice() {
System.out.println("hello "+ getName() +" 欢迎来到图书管理系统!");
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;
}
public void doOperation(int choice, BookList bookList) {
this.iOperation[choice].work(bookList);
}
}
四、sum包 (进行整合)
Main.java
package sum;
import book.BookList;
import user.AdminUser;
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.println("请输入您的名字:");
String name = scanner.nextLine();
System.out.println("请输入您的身份:1.管理员 2.普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
} else if(choice == 2) {
return new NormalUser(name);
} else {
System.out.println("输入错误 请重新输入!");
return login();
}
}
public static void main(String[] args) {
// 首先准备书架
BookList bookList = new BookList();
// 然后进行登录
User user = login();
while(true) {
//进行菜单选择:即进行项目选项
int choice = user.choice(); //发生动态绑定
// 要确定具体的是哪个角色的选项
user.doOperation(choice,bookList);
}
}
}
THINK
主要掌握接口以及抽象类,方法的重写以及向上转型, 自定义类型的数组的创建等。