图书室系统--java面向对象应用


前言

学习完了java面向对象部分,我结合以往的知识点:抽象类,接口,动态绑定,类和对象来实现一个小型的图书室系统

重点在于如何搭好整个框架,运用面向对象的思想,这也是java语言的特点


一、我们所需要的类

结合我们现实,如果面对一个图书室系统,我们一定会需要书:书的属性包括名字,作者,价格,类型…你可以结合实际继续补充,书架:一个图书列表(这里我用数组实现,再次强调,我们训练的是面向对象的思想,所以不追求性能,你也可以使用集合类实现),数组的大小(实时记录);用户,用户通过不同的权限划分:管理员用户,普通用户;操作:借书,还书,找书,显示书,增加书,删除书,而这些操作可以通过写一个接口实现更好的复用…
除此之外,我们还需要一个Main类来整合我们的逻辑
至此分析,我们的系统已经很清楚了,项目目录如下:
目录

二、搭建整体框架

1.book包

(1)Book类

代码如下:

package book;

/**
 * Created with IntelliJ IDEA.
 * Description:书
 * User: xinyu
 * Date: 2023-03-30
 * Time: 19:11
 */
public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;
//    构造方法不加isBorrowed,因为boolea默认就是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)?",已经借出":",未借出") +
                '}';
    }
}

(2)BookList类

package book;

/**
 * Created with IntelliJ IDEA.
 * Description:书架
 * User: xinyu
 * Date: 2023-03-30
 * Time: 19:11
 */
public class BookList {
    private Book[] books=new Book[10];//最多可以放10本书
    private int usedSize;//实时记录,当前books这个数组中有多少本书
    public BookList(){
        books[0]=new Book("三国演义","罗贯中",19,"小说");
        books[1]=new Book("西游记","吴承恩",29,"小说");
        books[2]=new Book("红楼梦","曹雪芹",9,"小说");
        usedSize=3;
    }

    /**
     *
     * @param pos 此时pos位置一定是合法的
     * @return 一本书
     */
    public Book getBook(int pos){
        return books[pos];
    }

    /**
     *
     * @param pos 此时pos位置一定是合法的
     * @param book 是你要放的书
     */
    public void setBooks(int pos,Book book){
        books[pos]=book;
    }

    /**
     *
     * @return 当前的书的个数
     */
    public int getUsedSize(){
        return usedSize;
    }

    /**
     *
     * @param size 要设置的书的个数
     */
    public void setUsedSize(int size){
        usedSize=size;
    }
}

2.operation包

(1)IOperation接口

代码如下:

package operation;

import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xinyu
 * Date: 2023-03-30
 * Time: 19:46
 */
public interface IOperation {
    void work(BookList bookList);
}

(2)该包里的其他类都是在实现业务逻辑,不在此赘述

3.user包

(1)User抽象类

package user;

import book.BookList;
import operation.IOperation;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xinyu
 * Date: 2023-03-30
 * Time: 19:59
 */
public abstract class User {
    protected String name;//用户名
    protected IOperation[] iOperations;
    public User(String name){
        this.name=name;
    }
    public abstract int menu();
    public void doOperation(int choice , BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}

(2)AdminUser类

package user;

import book.BookList;
import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xinyu
 * Date: 2023-03-30
 * Time: 20:00
 */
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()
        };
    }
    public int menu(){
        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.退出系统!");
        Scanner sc=new Scanner(System.in);
        int choice= sc.nextInt();
        return choice;
    }

}

(3)NormalUser类

package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xinyu
 * Date: 2023-03-30
 * Time: 20:00
 */
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("hello "+this.name+"欢迎来到工大图书室");
        System.out.println("1.查找图书!");
        System.out.println("2.借阅图书!");
        System.out.println("3.归还图书!");
        System.out.println("0.退出系统!");
        Scanner sc=new Scanner(System.in);
        int choice= sc.nextInt();
        return choice;
    }
}

4.Main类

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ${USER}
 * Date: ${YEAR}-${MONTH}-${DAY}
 * Time: ${TIME}
 */
public class Main {
    public static User login(){
        System.out.println("请输入你的名字:");
        Scanner sc =new Scanner(System.in);
        String name= sc.nextLine();
        System.out.println("请输入你的身份:1.管理员  2.普通用户");
        int choice =sc.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=login();
        while(true) {
            int choice = user.menu();//动态绑定,向上转型
            user.doOperation(choice, bookList);
        }
    }
}

总结

结合以往的知识点:抽象类,接口,动态绑定,类和对象来实现一个小型的图书室系统

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值