Javase期末经典项目—图书管理系统
文章目录
- Javase期末经典项目---图书管理系统
- 1.功能介绍
- 2.详细代码
- bookList包
- Book类
- BookList类
- user包
- User类
- Adminl类
- Normal类
- operations包
- AddOperations类
- BorrowOperations类
- DelOperations类
- DisplayOperation类
- ExitOperations类
- FindOperations类
- Operations接口
- BookManger--管理器
- 3.效果演示
- 管理员
- 普通用户
1.功能介绍
这个小项目运用了面向对象语言的封装,继承,多态,以及接口,抽象类,组合等基础语法知识完成了实现了不同用户对应了不同的操作菜单,并对管理员用户实现了增删改差图书,对普通用户提供了借书,查书等操作。下面给出容易你们理解的类,接口之间的关系:组织关系:从上图中我们很容易看出,实现不同的用户是通过User类派生出两个子类Admin类和Normal类来实现的,根据用户的输入,向上转型为user类,也即是se语法中的多态(父类引用指向子类对象)
接下来就是书类,和书架类,书类包含了书的相关信息,书架类中有书类组成的数组,用到了组合。难点就是怎么根据不同的用户来实现对应的操作,我们发现对应的操作都是操作操作的书架中的内容,是一种行为,那么se中的语法就表示了某种行为,所以我们很容易的想到了接口并定义为Operations,对应的操作类去实现给接口并重写里面的抽象方法!我们怎么更具不同的用户来组织操作了?没错,有用到了多态,我们需要更具不同的用户来产生不同的Operations数组,里面的元素是对应操作对象的引用。我们通过这个接口数组来实现相关操作。而这个接口数组我们不同的用户需要不同的空间,我们可以定义在User基类中(当然你也可以在对应的用户类中来定义该接口数组,只不过就少了一处多态的美感,也就是在用到该数组是需要向下转型的,我第一次就是这么干的),我们并不知道这个数组的大小,所以我们只是定义这个数组,不分配空间。在子类调用父类构造方法的时候去初始化!下面给出详细的相关代码:(当然有的地方并没有考虑特殊情况,你们在写的时候可以写一些优化。)
2.详细代码
bookList包
Book类
package bookList;
/**
* 书
*
* @author CY
* @date 2022 /08/09 23:15
*/
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
/**
* Instantiates a new Book.
*
* @param name the name
* @param author the author
* @param price the price
* @param type the type
*/
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets name.
*
* @param name the name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets author.
*
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* Sets author.
*
* @param author the author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* Gets price.
*
* @return the price
*/
public int getPrice() {
return price;
}
/**
* Sets price.
*
* @param price the price
*/
public void setPrice(int price) {
this.price = price;
}
/**
* Gets type.
*
* @return the type
*/
public String getType() {
return type;
}
/**
* Sets type.
*
* @param type the type
*/
public void setType(String type) {
this.type = type;
}
/**
* Is borrowed boolean.
*
* @return the boolean
*/
public boolean isBorrowed() {
return isBorrowed;
}
/**
* Sets borrowed.
*
* @param borrowed the borrowed
*/
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + (isBorrowed==true?"借出":"未借出")+
'}';
}
}
BookList类
package bookList;
/**
* The type Book list.
*
* @author CY
* @date 2022 /08/09 23:29
*/
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
/**
* Instantiates a new Book list.
*/
public BookList() {
books[0] = new Book("三国演义", "罗贯中", 89, "小说");
books[1] = new Book("西游记", "吴承恩", 78, "小说");
books[2] = new Book("红楼梦", "曹雪芹", 49, "小说");
this.usedSize = 3;
}
/**
* Gets used size.
*
* @return the used size
*/
public int getUsedSize() {
return usedSize;
}
/**
* Sets used size.
*
* @param usedSize the used size
*/
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
/**
* Get books book [ ].
*
* @return the book [ ]
*/
public Book[] getBooks() {
return books;
}
/**
* Sets books.
*
* @param books the books
*/
public void setBooks(Book[] books) {
this.books = books;
}
}
user包
User类
package user;
import bookList.BookList;
import operations.Operations;
/**
* 用户类
*
* @author CY
* @date 2022 /08/09 21:55
*/
public abstract class User {
protected Operations[] ots;
private String name;
/**
* Instantiates a new User.
*
* @param name the name
*/
public User(String name) {
this.name = name;
}
/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets name.
*
* @param name the name
*/
public void setName(String name) {
this.name = name;
}
/**
* Menu int.
*
* @return the int
*/
public abstract int menu();
public void doOperations(BookList bookList, int choices) {
ots[choices].work(bookList);
}
}
Adminl类
package user;
import operations.*;
import java.util.Scanner;
/**
* 管理员
*
* @author CY
* @date 2022 /08/09 22:09
*/
public class Admin extends User {
//第一次写的,定义在子类的接口数字。
// private final Operations[] ots = new Operations[]{
// new ExitOperations(),
// new AddOperation(),
// new DelOperations(),
// new FindOperations(),
// new DisplayOperation()
// };
/**
* Instantiates a new Admin.
*
* @param name the name
*/
public Admin(String name) {
super(name);
this.ots = new Operations[]{
new ExitOperations(),
new AddOperation(),
new DelOperations(),
new FindOperations(),
new DisplayOperation()
};
}
/**
* Get ots operations [ ].
*
* @return the operations [ ]
*/
public Operations[] getOts() {
return ots;
}
@Override
public int menu() {
System.out.println("欢迎你,专注于写bug的管理员:" + getName());
System.out.println("----------------------------");
System.out.println("0.退出系统!");
System.out.println("1.新增图书!");
System.out.println("2.删除图书!");
System.out.println("3.查找图书!");
System.out.println("4.展示图书!");
System.out.println("----------------------------");
System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}
Normal类
package user;
import operations.*;
import java.util.Scanner;
/**
* 用户
*
* @author CY
* @date 2022 /08/09 22:10
*/
public class Normal extends User {
// private final Operations[] ots = new Operations[]{
// new ExitOperations(),
// new FindOperations(),
// new BorrowOperations(),
// new DisplayOperation()
// };
/**
* Instantiates a new Normal.
*
* @param name the name
*/
public Normal(String name) {
super(name);
this.ots = new Operations[]{
new ExitOperations(),
new FindOperations(),
new BorrowOperations(),
new DisplayOperation()
};
}
/**
* Get ots operations [ ].
*
* @return the operations [ ]
*/
public Operations[] getOts() {
return ots;
}
@Override
public int menu() {
System.out.println("欢迎你,尊贵的用户:" + getName());
System.out.println("----------------------------");
System.out.println("0.退出系统!");
System.out.println("1.查找图书!");
System.out.println("2.借阅书籍!");
System.out.println("3.展示书籍!");
System.out.println("----------------------------");
System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}
operations包
AddOperations类
package operations;
import bookList.Book;
import bookList.BookList;
import java.util.Arrays;
import java.util.Scanner;
/**
* The type Add operation.
*
* @author CY
* @date 2022 /08/09 23:41
*/
public class AddOperation implements Operations {
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
Scanner sc = new Scanner(System.in);
System.out.println("请输入name:");
String name = sc.nextLine();
System.out.println("请输入author:");
String author = sc.nextLine();
System.out.println("请输入type:");
String type = sc.nextLine();
System.out.println("请输入price:");
int price = sc.nextInt();
Book book = new Book(name, author, price, type);
Book[] books = Arrays.copyOf(bookList.getBooks(), bookList.getUsedSize() + 1);
books[bookList.getUsedSize()] = book;
bookList.setBooks(books);
bookList.setUsedSize(bookList.getUsedSize() + 1);
System.out.println();
}
}
BorrowOperations类
package operations;
import bookList.Book;
import bookList.BookList;
import java.util.Objects;
import java.util.Scanner;
/**
* The type Borrow operations.
*
* @author CY
* @date 2022 /08/09 23:42
*/
public class BorrowOperations implements Operations {
@Override
public void work(BookList bookList) {
System.out.println("借阅书籍!");
System.out.println("请输入你要借阅的书名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (Book book : bookList.getBooks()) {
if (Objects.equals(name, book.getName())) {
System.out.println("借阅成功,请按时归还!");
book.setBorrowed(true);
System.out.println();
return;
}
}
System.out.println("对不起,没有找到这本书!");
System.out.println();
}
}
DelOperations类
package operations;
import bookList.Book;
import bookList.BookList;
import java.util.Objects;
import java.util.Scanner;
/**
* The type Borrow operations.
*
* @author CY
* @date 2022 /08/09 23:42
*/
public class BorrowOperations implements Operations {
@Override
public void work(BookList bookList) {
System.out.println("借阅书籍!");
System.out.println("请输入你要借阅的书名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (Book book : bookList.getBooks()) {
if (Objects.equals(name, book.getName())) {
System.out.println("借阅成功,请按时归还!");
book.setBorrowed(true);
System.out.println();
return;
}
}
System.out.println("对不起,没有找到这本书!");
System.out.println();
}
}
DisplayOperation类
package operations;
import bookList.BookList;
/**
* The type Display operation.
*
* @author CY
* @date 2022 /08/09 23:45
*/
public class DisplayOperation implements Operations {
@Override
public void work(BookList bookList) {
System.out.println("展示书籍!");
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println("------------------------------------------------------------------------------------");
System.out.println(bookList.getBooks()[i]);
if (i == bookList.getUsedSize() - 1) {
System.out.println("------------------------------------------------------------------------------------");
}
}
System.out.println();
}
}
ExitOperations类
package operations;
import bookList.BookList;
/**
* The type Exit operations.
*
* @author CY
* @date 2022 /08/09 23:47
*/
public class ExitOperations implements Operations {
@Override
public void work(BookList bookList) {
System.out.println("此次的离别是为了更好的重逢,期待与你的下一次见面!");
System.exit(0);
}
}
FindOperations类
package operations;
import bookList.BookList;
import java.util.Objects;
import java.util.Scanner;
/**
* The type Find operations.
*
* @author CY
* @date 2022 /08/09 23:49
*/
public class FindOperations implements Operations {
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入你要查找的图书名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if (Objects.equals(name, bookList.getBooks()[i].getName())) {
System.out.println("我们找到了这本书,详细信息如下:");
System.out.println(bookList.getBooks()[i]);
System.out.println();
return;
}
}
System.out.println("对不起,没有找到这本书!");
System.out.println();
}
}
Operations接口
package operations;
import bookList.BookList;
/**
* The interface Operations.
*
* @author CY
*/
public interface Operations {
/**
* Work.
*
* @param bookList the book list
*/
void work(BookList bookList);
}
BookManger–管理器
import bookList.BookList;
import user.Admin;
import user.Normal;
import user.User;
import java.util.Arrays;
import java.util.Scanner;
/**
* The type Book manger.
*
* @author CY
* @date 2022 /08/09 22:16
*/
public class BookManger {
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
BookList bookList = new BookList();
System.out.println("请输入你的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("1.管理员;2.用户");
int choice = sc.nextInt();
if (choice == 1) {
User user = new Admin(name);
while (true) {
int choices = user.menu();
user.doOperations(bookList, choices);
}
} else {
User user = new Normal("name");
while (true) {
int choices = user.menu();
user.doOperations(bookList, choices);
}
}
//这里是对应的没有实现接口数组继承的版本,也就需要向下转型才能使用,也没有任何问题,但最好还是用instanceof判断一下
// if (choice == 1) {
// User user = new Admin(name);
// while (true) {
// int choices = user.menu();
// Admin user1 = (Admin) user;
// if (user1.getOts().length - 1 >= choices) {
// user1.getOts()[choices].work(bookList);
// } else {
// System.out.println("管理员" + user.getName() + "你好,你的操作非法!");
// System.out.println();
// }
// }
//
// } else {
// User user = new Normal(name);
// while (true) {
// int choices = user.menu();
// Normal user2 = (Normal) user;
// if (user2.getOts().length - 1 >= choices) {
// user2.getOts()[choices].work(bookList);
// } else {
// System.out.println("亲爱的" + user.getName() + "用户,你的输入非法!");
// System.out.println();
// }
// }
// }
}
}
3.效果演示
管理员
普通用户
最后也可以在我的码云去找到对应的源码:图书管理系统