JAVA-图书系统管理系统

目录

0.写图书管理系统思路

1.主函数

2.用户界面

3.端口

4.书籍信息管理

5. 书籍存储

6.查找图书

7.退出系统

8.普通成员菜单

8.1借阅图书

8.2归还图书

9.管理员界面

9.1新增图书

9.2删除图书

9.3显示图书


0.写图书管理系统思路

        明确系统方向,将图书系统分为三大模块

  • 用户界面
  • 图书管理
  • 操作系统管理

        在src下面分别创建三个包,分别为book、operation和user


 

1.主函数

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        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 user=login();
        while(true){
            int choice= user.menu();

            user.doOperation(choice,bookList);
        }



    }
}

2.用户界面

package user;

import book.BookList;
import operation.IOPeration;

public abstract class User {
    protected String name;
    public IOPeration[] ioPerations;//这里没有分配空间

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

    public void doOperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}

3.端口

package operation;

import book.BookList;

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

 

4.书籍信息管理

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;//书籍是否被借出,不用创建此构造方法,因为系统默认的就是未接出,为false类型

    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 "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrowed == true) ? " 已借出" : " 未借出")+
                //", isBorrowed=" + isBorrowed +
                '}';
    }
}

5. 书籍存储

package book;

public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;//记录当前书架上有多少本书

    public BookList() {
        books[0] = new Book("三国演义", "罗贯中", 19, "小说");
        books[1] = new Book("西游记", "吴承恩", 29, "小说");
        books[2] = new Book("水浒传", "施耐庵", 8, "小说");
        this.usedSize = 3;
    }

    public Book getBook(int pos){
        return books[pos];
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public void setBooks(int pos,Book book){
        books[pos]=book;//数组的pos位置放一本书
    }

}

6.查找图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOPeration {
    @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("没有你要查找的书");
    }
}

7.退出系统

package operation;

import book.BookList;

public class ExitOperation implements IOPeration{

    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

 

8.普通成员菜单

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()
        };
    }
    public int menu(){
        System.out.println("普通用户菜单");
        System.out.println("------------------");
        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("------------------");
        System.out.println("请输入你的操作");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }

}

8.1借阅图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOPeration {
    @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)){
                if(book.isBorrowed()){
                    System.out.println("该书已被借出!");
                }else{
                    book.setBorrowed(true);
                }
                return;
            }
        }
        System.out.println("没有你要借阅的图书!");
    }
}

8.2归还图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOPeration {
    @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);
                return;
            }
        }
        System.out.println("没有你要归还的图书!");

    }
}

9.管理员界面

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 ShowOperation()
        };
    }
    public int menu(){
        System.out.println("管理员菜单");
        System.out.println("------------------");
        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("5.退出系统");
        System.out.println("------------------");
        System.out.println("请输入你的操作");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

9.1新增图书

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.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 tybe= scanner.nextLine();
        System.out.println("请输入图书的价格");
        int prince= scanner.nextInt();


        Book book=new Book(name,author,prince,tybe);
        int currentSize= bookList.getUsedSize();
        bookList.setBooks(currentSize,book);

        bookList.setUsedSize(currentSize+1);

        System.out.println("新增图书成功");
    }
}

9.2删除图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    @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();
        int delIndex = -1;
        int i=0;
        for (;i< currentSize;i++){
            Book book =bookList.getBook(i);
            if(book.getName().equals(name)){
                delIndex=i;
                break;
            }
        }

        if(i ==currentSize){
            System.out.println("没有你删除的这本书!");
            return;
        }
        for (int j=delIndex;j<currentSize -1 ;j++){
            //[j]=[j+1]
            Book book= bookList.getBook(j+1);//将j+1下标的书赋值给j下标
            bookList.setBooks(j,book);//下标j这本书为book
        }

        bookList.setBooks(currentSize-1,null);

        bookList.setUsedSize(currentSize-1);

        System.out.println("删除图书");

    }
}

9.3显示图书

package operation;

import book.Book;
import book.BookList;

import java.sql.SQLOutput;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值