图书管理系统Java

在这里插入图片描述所需创建的包,类和接口,相关的操作

书类

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean flg;

    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 isFlg() {

        return flg;
    }

    public void setFlg(boolean flags) {

        this.flg = flags;
    }

    @Override
    public String toString() {
        return "book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                //", flg=" + flg +
                ((flg == true) ? "      已借出" : "    未借出")+
                '}';
    }
}

Book List类相当于一个顺序表

package book;

import java.util.Arrays;

public class BookList {
    @Override
    public String toString() {
        return "BookList{" +
                "books=" + Arrays.toString(books) +
                ", usedSize=" + usedSize +
                '}';
    }

    private Book[] books = new Book[10];
    private  int usedSize;
public BookList(){
    books[0] = new Book("水浒传","施耐庵",29,"小说");
    books[1] = new Book("泰罗奥特曼","张三",22,"漫画");
    books[2] = new Book("迪迦奥特曼","李四",22,"漫画");
    this.usedSize = 3;
}

    public void setBooks(int pos, Book book){

    this.books[pos] = book;
    }

    public Book getBook(int pos){

        return this.books[pos];
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }


    public int getUsedSize(){

    return usedSize;
    }
    public void setUsedSize(int usedSize){

    this.usedSize = usedSize;
    }

}

IOperation接口

package operation;

import book.BookList;

public interface IOperation {
    void work(BookList booklist);
}

AddOperation类

添加图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("添加图书!!");
        System.out.println("请输入需要添加的书籍: ");
        String str1 = scanner.next();
        System.out.println("请输入作者: ");
        String str2 = scanner.next();
        System.out.println("请输入价格: ");
        int str3 = scanner.nextInt();
        System.out.println("请输入类型:");
        String str4 = scanner.next();
        for (int i = 0; i < bookList.getUsedSize() ; i++) {
            if (bookList.getBook(i).getName().equals(str1)){
                System.out.println(str1+"已存在,添加失败!");
                return;
            }
        }
        Book book = new Book(str1,str2,str3,str4);
        int ret = bookList.getUsedSize();
        bookList.setUsedSize(bookList.getUsedSize()+1);
        bookList.setBooks(ret,book);
        System.out.println("添加成功!");
    }

DelOperation类

删除图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        Scanner sc = new Scanner(System.in);
        System.out.println("删除图书!!");
        System.out.println("请输入要删除的书籍: ");
        String name = sc.next();
        int pos = -1;
        int i = 0;
        for (; i < bookList.getUsedSize() ; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("找到了!");
                pos = i;
                break;
            }
        }
        if (i>= bookList.getUsedSize()){
            System.out.println("没找到"+name+"这本书!");
            return;
        }
        int ret = bookList.getUsedSize();
        for (int j = pos; j < bookList.getUsedSize()-1 ; j++) {
            bookList.setBooks(i, bookList.getBook(i+1));
        }
        bookList.setUsedSize(ret-1);
        System.out.println("删除成功!");
    }
}

BorrowOperation类

借阅图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    public void work(BookList bookList){
        Scanner sc = new Scanner(System.in);
        System.out.println("借阅图书!!");
        System.out.println("请输入想要借阅的书籍: ");
        String name = sc.nextLine();
        int i = 0;
        Book book = bookList.getBook(i);
        for (; i < bookList.getUsedSize(); i++) {
            if (book.getName().equals(name)){
                System.out.println("借出成功!");
                break;
            }else{
                System.out.println("借出失败!");
                System.out.println("没有" +name +"这本书");
            }
            return;
        }
    }
}

FindOperation类

查找图书

package operation;

import book.Book;
import book.BookList;

import java.util.Objects;
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();
        for (int i = 0;  i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("找到这本书!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书!");
    }
}

DisplayOperation 类

显示图书

package operation;

import book.Book;
import book.BookList;

import java.util.Arrays;

public class DisplayOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("显示图书!!");
        for (int i = 0; i < bookList.getUsedSize() ; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

ReturnOperation类

归还图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation {
    @Override
    public void work(BookList booklist) {
        Scanner sc = new Scanner(System.in);
        System.out.println("归还图书!!");
        System.out.println("输入要归还的书籍: ");
        String name = sc.next();
        int i = 0;
        Book book = booklist.getBook(i);
        for (; i < booklist.getUsedSize(); i++) {
            if (book.getName().equals(name)) {
                System.out.println("归还成功!");
                break;
            } else {
                System.out.println("归还失败!");
                System.out.println("没有" + name + "这本书");
            }
            return;
        }
    }
}

User抽象类

package user;

import book.BookList;
import operation.IOperation;
import javax.imageio.stream.ImageOutputStreamImpl;

public abstract  class User {
    public String name;

    public IOperation[] iOperations;
    public User(String name){
        this.name = name;
    }
    abstract  public int  menu();
    public void doOperation(int choice , BookList bookList){
        iOperations[choice].work(bookList);
    }
}

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() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("=========" + "欢迎 "+this.name+"=========");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("4.退出系统");
        System.out.println("==========================================");
        int choice = scanner.nextInt();
        return choice;
    }
}

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 DisplayOperation()
        };
    }
    @Override
    public int  menu() {
        Scanner scanner = new Scanner(System.in);
            System.out.println("=========" + "欢迎 "+this.name+"=========");
            System.out.println("1.查找图书");
            System.out.println("2.新增图书");
            System.out.println("3.删除图书");
            System.out.println("4.显示图书");
            System.out.println("5.退出系统");
            System.out.println("==========================================");
            System.out.println("0.返回系统");
            int choice = scanner.nextInt();
            return choice;
    }
}

运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
有什么不足,欢迎大家指出来!小萌新太难了

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值