提示:以下是本篇文章正文内容,下面案例可供参考
1.图书管理系统分为图书管理员,普通用户
2.既然分为两类用户就要实现与其相对应的菜单
3.管理员我们要实现查找图书、增添图书、删除图书、显示图书、以及退出系统
4.普通用户我们要实现查找图书、借阅图书、归还图书、以及退出系统
以上就是我们要实现的一些简单功能
1.首先我们是图书管理系统,我们要抽象出图书Book这个类,并且加上图书名称、作者、价格、类型、以及借出的状态这些属性。
2.我们还要想到我们的书应该放在哪里,所以抽象出书架BookList这个类,然后在一个数组中存放我们的书。
3.因为我们分为两种用户,所以可以抽象User这个类,将这个类作为普通用户NormallUser,管理员Adminuser的父类
4.最重要的就是如何实现我们的逻辑操作,我们创建一个operation的包,实现IOperation的这个接口,然后我们分别让查找,增添,删除,显示,借阅,归还,退出系统分别实现这个接口。
5.最后要在Main这个包下面,实现登录的操作利用循环打印对应的菜单
package Book;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:23
*/
public class Book {
private String name; //书名
private String author;//代表作者
private int price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出 //默认是未被借出的
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 "BookList{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true) ? "已经借出":"未借出")+
'}';
}
}
package Book;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:24
*/
//书架
public class BookList {
//最多放10本书
private Book[] books = new Book[10];
private int usedSize; //实时记录 当前books这个数组中有多少本书
public BookList(){
books[0]=new Book("三国演义","罗贯中",19,"小说");
books[1]=new Book("西游记","吴承恩",39,"小说");
books[2]=new Book("红楼梦","曹雪芹",59,"小说");
usedSize =3;
}
//此时pos位置一定是合法的
//获取这本书
public Book getBook(int pos){
return books[pos];
}
//此时pos位置一定是合法的 book是你要放的书
public void setBooks(int pos,Book book){
books[pos]=book;
}
//实时获取当前书的数量
public int getUsedSize(){
return usedSize;
}
//实时的修改书的数量(例如增加和删除书的时候)
public void setUsedSize(int size){
usedSize = size;
}
}
package user;
import operation.ExitOperation;
import operation.BorrowOperation;
import operation.FindOperation;
import operation.IOoperation;
import operation.ReturnOperation;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:59
*/
public class NormallUser extends User {
public NormallUser(String name) {
super(name);
this.iOoperations =new IOoperation[]{
new ExitOperation(), //0下标是退出系统
new FindOperation(), //根据数字定义该操作位于数组下标几的位置
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu(){
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("请输入你的操作:");
Scanner scanner =new Scanner(System.in);
int choice =scanner.nextInt();
return choice;
}
}
package user;
import operation.ExitOperation;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:59
*/
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOoperations =new IOoperation[]{
new ExitOperation(), //0下标是退出系统
new FindOperation(),
new AddOperation(), //根据数字定义该操作位于数组下标几的位置
new DelOperation(),
new DisplayOperation()
};
}
public int menu(){
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("请输入你的操作:");
Scanner scanner =new Scanner(System.in);
int choice =scanner.nextInt();
return choice;
}
}
package operation;
import Book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:47
*/
public interface IOoperation {
void work(BookList bookList);
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:48
*/
public class AddOperation implements IOoperation{
@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("请输入图书的价格:");
int price = scanner.nextInt();
Book book =new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功!");
}
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:54
*/
public class BorrowOperation implements IOoperation{
@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 book =bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
}
}
package operation;
import Book.Book;
import java.util.Scanner;
import Book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:50
*/
public class DelOperation implements IOoperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入你要删除图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int index = 0;
int currentSize = bookList.getUsedSize();
int i = 0;
for (; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
index = i;
break;
}
}
//1、遇到了break 2、没有找到 结束了for循环
if(i == currentSize) {
System.out.println("没有你要删除的图书!");
return;
}
for (int j = index; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1];
Book book =bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(currentSize-1,null);
bookList.setUsedSize(currentSize-1);
System.out.println("删除结束");
}
}
package operation;
import Book.Book;
import Book.BookList;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:51
*/
public class DisplayOperation implements IOoperation{
@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);
System.out.println(book);
}
}
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:49
*/
public class FindOperation implements IOoperation {
@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 book =bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书!");
System.out.println(book);
return;
}
}
//说明没有找到
System.out.println("没有你要找的图书!");
}
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:55
*/
public class ReturnOperation implements IOoperation {
@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 book =bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
}
}
package operation;
import Book.BookList;
import operation.IOoperation;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 21:52
*/
public class ExitOperation implements IOoperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
package Main;
import Book.BookList;
import user.AdminUser;
import user.NormallUser;
import user.User;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 刘尚辉
* Date: 2022-05-18
* Time: 22:12
*/
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 NormallUser(name);
}
}
public static void main(String[] args) {
BookList bookList =new BookList();//准备图书
//登录-->user这个引用引用那个对象 看返回值
User user = login();
//打印菜单
while (true){
int choice = user.menu();//动态绑定
user.doOperation(choice,bookList);
}
}
}
提示:以上就是简单实现图书管理系统的简单实现
Hello world 我们下期见!