文章目录
- 前言
- 思维导图
- 一、创建图书相关的类
- 二、创建操作相关的类
- 1.新增图书
- 2.借阅图书
- 3.删除图书
- 4显示所有图书
- 5.退出系统
- 6.查找图书
- 7.归还图书
- 三、创建用户相关的类
- 普通用户类
- 管理员用户类
- 四、进行整合
- 五、运行结果
前言
最近了解到世界上公认的最有效率的学习方法“费曼学习法”,其中非常重要的一步叫做传授,就是检测知识的最终的途径是你有能力把它传播给另一个人。
下面是学习了Java后,实现图书管理系统简单的练习,包含了类和对象,继承,封装,多态,抽象类,接口和顺序表。
思维导图
一、创建图书相关的类
先创建 package book
创建 Book 类, 表示一本书
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed = false;
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 本书.
public class BookList {
private final Book[] books = new Book[100];
private int size = 0;
public BookList() {
books[0] = new Book("三国演义", "罗贯中", 100, "小说");
books[1] = new Book("水浒传", "施耐庵", 100, "小说");
books[2] = new Book("西游记", "吴承恩", 100, "小说");
this.size = 3;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void setBooks(int pos, Book book) {
this.books[pos] = book;
}
public Book getBook(int pos) {
return this.books[pos];
}
}
二、创建操作相关的类
先创建 package operation
public interface IOperation {
void work(BookList bookList);
}
抽象出 Operation 的好处: 让操作和操作之间低耦合, 让操作和用户之间低耦合。
接下来创建一组操作类, 每个类对应一个用户的动作.
1.新增图书
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();
Book book = new Book(name,author,price,type);
int curSize = bookList.getSize();
bookList.setBooks(curSize,book);
bookList.setSize(curSize+1);
System.out.println("新增成功!");
}
}
2.借阅图书
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.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有你要借阅的那本书");
}
}
3.删除图书
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.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
break;
}
}
if (i == bookList.getSize()) {
System.out.println("没有这本书!");
return;
}
for (int pos = i; pos < bookList.getSize()-1;pos++) {
Book book = bookList.getBook(pos+1);
bookList.setBooks(pos,book);
}
bookList.setSize(bookList.getSize()-1);
System.out.println("删除成功!");
}
}
4显示所有图书
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("显示所有图书");
for (int i =0; i < bookList.getSize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
5.退出系统
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(1);
}
}
6.查找图书
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.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
System.out.println(book);
System.out.println("查找成功!");
return;
}
}
}
}
7.归还图书
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.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
book.setBorrowed(false);
System.out.println("归还成功!");
}
}
System.out.println("没有你要归还的图书!");
}
}
三、创建用户相关的类
先创建 package user
创建 User 类, 这是一个抽象类
public abstract class User {
protected String name;
protected IOperation[] operations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
operations[choice].work(bookList);
}
}
普通用户类
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("=============================");
System.out.println("请输入您的选择: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
管理员用户类
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("=============================");
System.out.println("请输入您的选择: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
四、进行整合
创建 Main 类和 main 方法, 搭建整体逻辑
public class TextMain {
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) {
BookList bookList = new BookList();
User user = login();
while(true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
}
五、运行结果