小项目 图书管理系统

第一个小项目 图书管理系统

1.需求

1.简单登录
2、管理端
2.1整理书籍(该功能为可扩展功能)
2.2查阅书籍
2.3增加书籍
2.4删除书籍
2.5打印书籍列表
2.6退出
3、用户端
3.1查询书籍
3.2借阅书籍
3.3归还书籍
3.4退出

2.再确定对象

1.书
1.1书本
1.2书架
2.增删改查功能
3.用户
3.1管理员
3.2普通用户

3.然后实现功能

package book;

public class Book {
    private String name;  //书名
    private String auther;// 作者
    private int price; //价格
    private String type; //类型
    private boolean isBorrowed; //书是否被借出
//构造方法
    public Book(String name, String auther, int price, String type) {
        this.name = name;
        this.auther = auther;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuther() {
        return auther;
    }

    public void setAuther(String auther) {
        this.auther = auther;
    }

    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 + '\'' +
                ", auther='" + auther + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                (isBorrowed ? "已借出" : "未借出")+
                '}';
    }
}
package book;

public class BookList {
    public  Book[] elem = new Book[10];
    public int useSize;
    public BookList () {
        this.elem[0] = new Book("三国演义","罗贯中",89,"小说");
        this.elem[1] = new Book("西游记","吴承恩",56,"小说");
        this.elem[2] = new Book("Java编程思想","大牛",109,"学习");
        this.useSize = 3;
    }
//    尾插法
    public void setBook(int pos,Book book) {
        this.elem[pos] = book;
    }

    public Book getBook (int pos) {
        return this.elem[pos];
    }

    public int getUseSize() {
        return useSize;
    }

    public void setUseSize(int useSize) {
        this.useSize = useSize;
    }
}

package operation;

import book.Book;
import book.BookList;
public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("新增图书");
        System.out.println("请输入书名:");
        String name = this.sc.next();
        System.out.println("请输入作者:");
        String auther = this.sc.next();
        System.out.println("请输价格:");
        int price = this.sc.nextInt();
        System.out.println("请输入种类:");
        String type = this.sc.next();

        Book book = new Book(name,auther,price,type);
        int currentSize = bookList.getUseSize();
        bookList.setBook(currentSize,book);
        bookList.setUseSize(currentSize + 1);

    }
}

package operation;

import book.Book;
import book.BookList;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        String name = this.sc.next();
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                book.setBorrowed(true);
                System.out.println("借书成功");
                break;
            }
        }
    }
}

package operation;

import book.Book;
import book.BookList;

/**
 * @author WeiTao
 * @date 2020/10/28
 * @time 11:22
 */
public class DelOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("删除图书:");
        String name = this.sc.next();
        int currentation = bookList.getUseSize();
        for (int i = 0; i < currentation; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                if (i == currentation - 1) {
                    bookList.setUseSize(currentation - 1);
                }else {
                    bookList.setBook(i, bookList.getBook(i + 1));
                    bookList.setUseSize(currentation - 1);
                }
                System.out.println("删除成功");
            }
        }
    }
}

package operation;

import book.Book;
import book.BookList;

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

package operation;

import book.BookList;


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

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("查找图书");
        String name = this.sc.next();
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println(book);
                break;
            }
        }
    }
}

package operation;

import book.BookList;

import java.util.Scanner;


public interface IOperation {
     Scanner sc = new Scanner(System.in);
     void work(BookList bookList);
}

package operation;

import book.Book;
import book.BookList;


public class ReturnOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        String name = this.sc.next();
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                book.setBorrowed(false);
                System.out.println("还书成功");
                break;
            }
        }
    }
}

package user;

import operation.*;

import java.util.Scanner;


public class Admin extends User{
    public Admin(String name) {
        super(name);
        this.operations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }

    @Override
    public int menu() {
        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("0.退出程序");
        System.out.println("******************************");
        System.out.println("您的选择是:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;

    }
}

package user;

import operation.*;

import java.util.Scanner;


public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.operations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println("****************************");
        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("***************************");
        System.out.println("您的选择是:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

package user;

import book.BookList;
import operation.IOperation;


public abstract class User {
    protected String name;
    protected IOperation[] operations;

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

    public abstract int menu();

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

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

import java.util.Scanner;


public class Main {

    public static User login() {
        System.out.println("请输入你的名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        System.out.println("请输入你的身份,1是管理员,0是普通用户");
        int choice = sc.nextInt();
        if (choice == 1) {
            return new Admin(name);
        }else {
            return new NormalUser(name);
        }

    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();//向上转型且发生动态绑定
        while (true) {
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值