🎉🎉🎉哈喽!!!大家好,这里是 我不是小明同学
🎆欢迎各位小伙伴关注➕点赞➕留言➕收藏
🎆我坚信努力奔跑才能与幸运不期而遇。
🎆🎉🎉🎉作者知识水平有限,若有什么错误或者需改进之处希望大家指出,若是你有更好的代码希望能给博主留言,博主希望能在CSDN与各位一起进步。
目录
2、user包下的User类、AdminUser类和NormalUser类
一、用户界面
1、管理员界面
2、普通用户界面
二、思路分析
我们要实现一个图书管理系统,首先需要有书,而书需要放在书架上,所以我们要有书架。有了书架我们就可以从书架上面拿书、放书。接下来分析书的属性,书有书名、作者、类型、价格以及是否借出的状态。最后分析图书管理系统的使用者,使用者分为两种,一种是管理员,另一种是普通用户,管理员和普通用户各自的操作在前面我们都已经看过,就不再赘述了。接下来我们就可以一步一步的用代码写出来。
三、具体方法的实现
1、Book包下的Book类和BookList类
首先我们把Book类写好
package Book;
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//类型
private boolean isBorrow;//是否借出
//构造方法,初始化新书
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 isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
//用于后面打印图书信息
@Override
public String toString() {
return "BookList{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrow==false) ? "未借阅":"已借阅")+
'}';
}
}
Book类写好后我们需要一个书架,也就是BookList类
package Book;
public class BookList {
Book[] books=new Book[10];//假设书架只能容纳10本书,这个数量可以调整
public int usedSize;//书架上书的个数
//我们先初始化3本书
public BookList() {
books[0]=new Book("西游记","吴承恩",10,"小说");
books[1]=new Book("水浒传","施耐庵",108,"小说");
books[2]=new Book("三国演义","罗贯中",19,"小说");
usedSize=3;
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(Book book,int pos) {
books[pos]=book;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public int getUsedSize() {
return usedSize;
}
}
2、user包下的User类、AdminUser类和NormalUser类
User 父类
package user;
import Book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;//用户名
protected IOperation[] iOperations;//用户的操作
public User(String name) {
this.name=name;
}
public abstract int menu();
public void DoOperation(int choice, BookList bookList) {
this.iOperations[choice].work(bookList);
}
}
AdminUser类
package user;
import Book.BookList;//用户需要操作书架
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 DisplayOperation()
};
}
@Override
public int menu() {
System.out.println("hello "+name+" 欢迎使用图书管理系统");
System.out.println("===========管理员=========");
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;
}
}
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 BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("hello " + name + " 欢迎使用图书管理系统");
System.out.println("==========普通用户=========");
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;
}
}
3、operation包下的各种类和接口
操作类抽象出来的一个公共接口
package operation;
import Book.BookList;
public interface IOperation {
void work(BookList bookList);
// 接下来实现该接口的类会对work方法进行不同方式的重写,以此来实现他们各自的操作
}
添加操作
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.print("请输入你要添加的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.print("请输入你要添加的书的作者:");
String author=scanner.nextLine();
System.out.print("请输入你要添加的书的类型:");
String type=scanner.nextLine();
System.out.print("请输入你要添加到书的价格:");
int price=scanner.nextInt();
Book book=new Book(name,author,price,type);
int currentSize=bookList.usedSize;
bookList.setBooks(book,currentSize);
bookList.setUsedSize(currentSize+1);
System.out.println("添加成功!");
}
}
借阅操作
package operation;
import Book.*;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("请输入你要借阅的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize=bookList.usedSize;
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBooks(i);
if(book.getName().equals(name)) {
book.setBorrow(true);//在借阅前书的isBorrow默认为false,所以我们只需将false改为true
System.out.println("借阅成功!");
return;
}
}
}
}
删除操作
package operation;
import Book.*;
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("请输入你要删除的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize=bookList.usedSize;
for (int i = 0; i <currentSize ; i++) {//遍历图书,找到需要删除的书
Book book=bookList.getBooks(i);
if(book.getName().equals(name)) {
//找到需要删除的书后再分两种情况,如果该书不是最后一本,则将后面的书整体向前移动一步
while(i<currentSize-1) {
bookList.setBooks(bookList.getBooks(i+1),i);
i++;
}
//如果要删除的书是最后一本,或者已经移动完书,则将书的数目减一
if(i==currentSize-1) {
bookList.setUsedSize(i);
}
}
}
System.out.println("删除成功!");
}
}
显示图书操作
package operation;
import Book.Book;
import Book.BookList;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
for (int i = 0; i < bookList.usedSize; i++) {
Book book=bookList.getBooks(i);
System.out.println(book.toString());
}
System.out.println("显示成功!");
}
}
查找图书操作
package operation;
import Book.*;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("请输入你要查找的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize=bookList.usedSize;
for (int i = 0; i <currentSize; i++) {
Book book=bookList.getBooks(i);
if (book.getName().equals(name)) {
System.out.println(book.toString());
System.out.println("查找成功!");
return;
}
}
System.out.println("没有找到你要找的书!");
}
}
归还图书操作
package operation;
import Book.*;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("请输入你要归还的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize=bookList.usedSize;
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBooks(i);
if(book.getName().equals(name)) {
book.setBorrow(false);
}
}
System.out.println("归还成功!");
}
}
退出系统
package operation;
import Book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出成功!");
System.exit(0);
}
}
4、Main方法
public class Main {
public static User login() {
System.out.println("请输入姓名:");
Scanner scanner=new Scanner(System.in);
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);
}
}
}