图书管理系统

book - Book

java 复制代码
package book;

public class Book {
    private String name; //书的名字
    private String author;//书的作者
    private double price;//书的价格
    private String type;//书的类型
    private boolean isBorrowed;//书是否被借出

    public Book(String name, String author, double 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 double getPrice() {
        return price;
    }

    public void setPrice(double 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 == true ?  "已借出" : "未借出") +
                '}';
    }
}

book - BookList

java 复制代码
package book;

public class BookList {
    private Book[] books;
    private int usedSize; //表示当前暑假有几本书
    public BookList(){
        this.books = new Book[10];
        books[0] = new Book("三国演义","罗贯中",6,"小说");
        books[1] = new Book("西游记","吴承恩",8,"小说");
        books[2] = new Book("红楼梦","曹雪芹",10,"小说");
        this.usedSize = 3;
    }
    public Book getBook(int pos){
        return this.books[pos];
    }
    public  void setBook(Book book,int pos){
        books[pos] = book;
    }
    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

}

opera - IOPeration

java 复制代码
package opera;

import book.BookList;
public interface IOPeration {
    void work(BookList bookList);
}

opera - AddOperation

java 复制代码
package opera;

import book.BookList;
import book.Book;

import java.util.Scanner;

public class AddOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入新增图书的名字:");
        String name = scanner.nextLine();

        System.out.print("请输入新增图书的作者:");
        String author = scanner.nextLine();

        System.out.println("请输入新增图书价格:");
        int price = scanner.nextInt();

        System.out.println("请输入新增图书的类型");
        scanner.nextLine();
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)){
                System.out.println("已经有这本书");
            }
        }
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize+1);
    }
}

opera - BorrowOperation

java 复制代码
package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        System.out.println("请输入你想要借阅的图书名字:");
        Scanner scanner = new Scanner(System.in);
        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("借阅成功");
                break;
            }
            System.out.println("没有你想要借阅的图书"+ name);
        }

    }
}

opera - DelOperation

java 复制代码
package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入想要删除的图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int flag = 0;
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)){
                flag = i;
                break;
            }
        }
        for (int j = 0; j < currentSize - 1; j++) {
            Book book = bookList.getBook(j+1);
            bookList.setBook(book,j);
            
        }
        bookList.setUsedSize(currentSize - 1);
        bookList.setBook(null,currentSize - 1);
    }
}

opera - ExitOperation

java 复制代码
package opera;

import book.BookList;

public class ExitOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0); //用于终结程序
    }
}

opera - FindOperation

java 复制代码
package opera;

import book.BookList;
import book.Book;

import java.util.Scanner;

public class FindOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书");
        System.out.print("请输入想要查找的图书的名字:");
        Scanner scanner = new Scanner(System.in);
        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);
            }else {
                System.out.println("没有这本书");
            }
        }
    }
}

opera - ReturnOpertion

java 复制代码
package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOpertion implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        System.out.println("请输入你想要归还的图书名字:");
        Scanner scanner = new Scanner(System.in);
        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("归还成功");
            }
            System.out.println("没有你想要归还的图书"+ name);
        }

    }
}

opera - ShowOperation

java 复制代码
package opera;

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++) {
            System.out.println(bookList.getBook(i));
        }
    }
}

user - AdminUser

java 复制代码
package user;

import opera.*;

import java.util.Scanner;
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.ioPeration =new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()
        };
    }
    @Override
    public int menu(){
        System.out.println("____________________________________");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");

        Scanner scanner = new Scanner(System.in);
        int choice= scanner.nextInt();
        return choice;
    }
}

user - NormalUser

java 复制代码
package user;
import book.BookList;
import opera.*;

import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.ioPeration = new IOPeration[]{
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOpertion(),
                new ExitOperation()
        };
    }
    @Override
    public int menu(){
        System.out.println("_________________");
        System.out.println("hello,"+name+"~");
        System.out.println("0.查找图书!");
        System.out.println("1.借阅图书!");
        System.out.println("2.归还图书!");
        System.out.println("3.退出系统!");

        Scanner scanner = new Scanner(System.in);
        int choice= scanner.nextInt();
        return choice;
    }
}

user - User

java 复制代码
package user;

import book.BookList;
import opera.IOPeration;

public abstract class User {
    protected String name;
    protected IOPeration[] ioPeration;

    public User(String name) {
        this.name = name;
    }

    public abstract int menu();
    public void doWork(int choice, BookList bookList){
        this.ioPeration[choice].work(bookList);
    }

}

Main

import book.BookList;

import user.NormalUser;

import user.AdminUser;

import user.User;

import java.util.Scanner;

public class Main {

public static User login(){

System.out.print("请输入姓名:");

Scanner scanner = new Scanner(System.in);

String name = scanner.nextLine();

System.out.println("请选择你的身份:1->管理员,2->用户");

int choice = scanner.nextInt();

if(choice == 1 ){

return new AdminUser(name);

} else {

return new NormalUser(name);

}

}

public static void main(String[] args) {

User user = login();

BookList bookList = new BookList();

while (true){

int choice = user.menu();

user.doWork(choice,bookList);

}

}

}

相关推荐
hqxstudying3 分钟前
Maven的使用
java·maven
未来并未来4 分钟前
Sentinel 流量控制安装与使用
开发语言·python·sentinel
shangjg315 分钟前
Eureka 心跳续约机制
java·分布式·spring cloud·eureka
Halo_tjn17 分钟前
Java IO
java·开发语言
沐土Arvin25 分钟前
三次握手建立连接,四次挥手释放连接——TCP协议的核心机制
java·网络·tcp/ip
阿维的博客日记31 分钟前
用volatile修饰数组代表什么意思,Java
java·juc·volatile
蔡蓝32 分钟前
设计模式-组合模式
java·设计模式·组合模式
我命由我1234541 分钟前
STM32 开发 - 中断案例(中断概述、STM32 的中断、NVIC 嵌套向量中断控制器、外部中断配置寄存器组、EXTI 外部中断控制器、实例实操)
c语言·开发语言·c++·stm32·单片机·嵌入式硬件·嵌入式
雨果talk42 分钟前
【一文看懂Spring循环依赖】Spring循环依赖:从陷阱破局到架构涅槃
java·spring boot·后端·spring·架构
东皇太星42 分钟前
Python 100个常用函数全面解析
开发语言·python