图书管理系统

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);

}

}

}

相关推荐
山猪打不过家猪23 分钟前
ASP.NET Core Clean Architecture
java·数据库·asp.net
AllowM23 分钟前
【LeetCode Hot100】除自身以外数组的乘积|左右乘积列表,Java实现!图解+代码,小白也能秒懂!
java·算法·leetcode
Biomamba生信基地1 小时前
两天入门R语言,周末开讲
开发语言·r语言·生信
RAN_PAND1 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
Bio Coder1 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言
Tiger Z1 小时前
R 语言科研绘图第 27 期 --- 密度图-分组
开发语言·程序人生·r语言·贴图
不会Hello World的小苗1 小时前
Java——列表(List)
java·python·list
二十七剑2 小时前
jvm中各个参数的理解
java·jvm
life_time_3 小时前
C语言(22)
c语言·开发语言