图书管理系统
1 分析图书管理系统如何实现
首先我们需要区分出管理员与普通用户,利用继承和多态可以实现用户需求;
普通用户:查找图书,借阅图书,归还图书,退出系统。
管理员: 查找图书,新增图书,删除图书,展示图书,退出系统。
我们可以将各个操作都封装起来,单独的设计成一个类,且这些操作都需要遵循一定的规范,而接口实际上就是定义了一种规范,这里可以使用接口来进行实现。
2 将图书管理系统的功能具体实现
分为3个包 book 、operation、user
1、book:用来存放书架和书籍
2、operation:用来实现用户的各种功能
3、user:用户界面进行选择
由此可分配包里的方法
book:
BookList,
Book
operation:
AddOperation
BorrowedOperation
DelOperation
ExitOperation
FindOperation
IOPeration (操作接口的实现)
ReturnOperation
ShowOperation
user
AdminUser
NormalUser
User
框架搭好后用main方法进行测试
3代码测试
Main
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-》管理员 0-》普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(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);
}
}
}
book
Booklist
{
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 == true) ? " 已经被借出":" 未被借出")+//这里因为bollean类型默认为,所以要进行判断
'}';
}
}
BookList
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//计数器
public BookList() {
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",23,"小说");;
this.books[2] = new Book("红楼梦","曹雪芹",8,"小说");;
this.usedSize = 3;//定义书的数量,在add和del中可修改
}
public Book getBook(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
operation
AddOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
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();
scanner.nextLine();
System.out.println("请输入类型:");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
//如果有这本书 就不能添加了
for (int i = 0; i < currentSize; i++) {
Book book1 = bookList.getBook(i);
//因为是添加,所以要判断书架里面是否存在相同的书
if(book1.getName().equals(name)) {
System.out.println("书名重复,不能添加!");
return;//结束循环
}
}//后面的功能模式基本相同
//此时默认为放到数组最后
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);//书架数量+1
}
}
BorrowedOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation 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();
//如果有这本书 就不能添加了
for (int i = 0; i < currentSize; i++) {
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)) {
if(!book1.isBorrowed()) {
book1.setBorrowed(true);
System.out.println("借阅成功!");
}else {
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("删除图书!");
//1、找到你要删除的书
int currentSize = bookList.getUsedSize();
if(currentSize == 0) {
System.out.println("书架为空,不能删除!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
int index = -1;
//如果有这本书 就不能添加了
for (int i = 0; i < currentSize; i++) {
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)) {
index = i;
break;
}
}
//2、index != -1 有这本书 开始删除
if(index == -1) {
System.out.println("没有你要删除的书!");
return;
}
for (int i = index; i < currentSize-1; i++) {
Book book1 = bookList.getBook(i+1);
bookList.setBooks(i,book1);
}
//当书放删掉之后 需要维持usedSize
bookList.setUsedSize(currentSize-1);
}
}
ExitOperation
package operation;
import book.Book;
import book.BookList;
public class ExitOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
bookList.setBooks(i,null);
}
bookList.setUsedSize(0);
System.exit(0);
}
}
FindOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration{
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();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("找到了你查找的书:");
System.out.println(book);
return;
}
}
System.out.println("不好意思,你好像没有这本书!!");
}
}
IOPeration
package operation;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);//基于书架进行操作
}
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();
int currentSize = bookList.getUsedSize();
//如果有这本书 就不能添加了
for (int i = 0; i < currentSize; i++) {
Book book1 = bookList.getBook(i);
if(book1.getName().equals(name)) {
book1.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("没有你要归还的图书!");
}
}
ShowOperation
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("显示图书!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
//Book book = bookList[i];
System.out.println(book);
}
}
}
user
AdminUser
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};//匿名内部类
}
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;//此时要返回,不然输入数字无反应
}
}
NormalUser
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};
}
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;
}
}
User
package user;
import book.BookList;
import operation.IOPeration;
public abstract class User {
protected String name;//姓名
public IOPeration[] ioPerations;//未初始化大小
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
ioPerations[choice].work(bookList);
}
}
代码中有简单的批注
以上就是所有内容,感谢观看