【Java】面向对象编程--简单版图书管理代码练习

🍌前言

今天的内容主要是利用前面所学的知识点:类,抽象类,封装,继承,多态,接口等进行的一个简单的代码练习。
主要要求:
1、用户登录
2、管理端
                 查找图书
                 新增图书
                 删减图书
                 显示图书列表
                 退出系统
                
3、用户端
                 查找图书
                 借阅图书
                 归还图书
                 退出系统
我们可以将以上内容分为对书的创建初始化,对用户的操作,对书的操作。

🥬主函数

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->管理员  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 us = login();//向上转型
        while (true) {
            int choice = us.menu();//多态绑定
            us.doWork(choice, bookList);
        }
    }
}

🥬书的创建

Book:
package book;
//创建图书
public class Book {
    private String name;//书名
    private String author;//书的作者
    private String type;//书的类型
    private int price;//书的价格
    boolean isBorrowed;//书是否被借出
//在这里我们就不对isBorrowed进行构造,在最后打印的时候对他进行判断就好
    public Book(String name, String author, String type, int price) {
        this.name = name;
        this.author = author;
        this.type = type;
        this.price = price;
    }

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

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

BookList:

package book;
//创建书架
public class BookList {
   private Book[] books=new Book[10];//创建一个数组当书架放书,假设一共可以放10本
    public int usedSize;//书架上有效放书数
    public BookList(){//假设一开始书架上就放了三本书
        books[0]=new Book("红楼梦","曹雪芹","文学",55);
        books[1]=new Book("西游记","吴承恩","文学",65);
        books[2]=new Book("水浒传","施耐庵","文学",23);
        usedSize=3;
    }
    public int getUsedSize(){
        return usedSize;
    }
    public void setUsedSize(int usedSize){
        this.usedSize=usedSize;
    }

    //获取一本书的位置
    public Book getpos(int pos){
        return this.books[pos];
    }
    //在下标为pos的位置上放一本书(添加)
    public void setBook(int pos,Book book){
        this.books[pos]=book;
    }
}

🥬对用户的操作

创建用户user类:

package user;
import book.BookList;
import operation.IOperation;

public abstract class User {
    protected String name;
    protected  IOperation[] iOperations;//创建操作类数组,通过返回的数字用于对书的操作
    public User(String name){
        this.name=name;
    }
    public abstract int menu();//用于动态绑定
    public void doWork(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
}

创建管理员用户类AdminUser ,是 user 的子类.

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()
        };
    }
    public int menu(){
        System.out.println("========管理员的菜单========");
        System.out.println("欢迎"+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.print("请选择你要的操作: ");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;//返回选择的数字
    }
}

创建普通用户类NormalUser, 是 user 的子类.

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("欢迎"+this.name+"来到图书馆");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("============================");
        System.out.print("请选择你要的操作: ");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

🥬对书的操作

创建接口 IOperation:

package operation;
import book.BookList;
import java.util.Scanner;
public interface IOperation {
    Scanner scanner=new Scanner(System.in);
    void work(BookList bookList);
}

创建增加图书类AddOperation,继承接口 IOperation

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("新增图书!");
        System.out.println("请输入图书的名字:");
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格:");
        int price = scanner.nextInt();


        Book book=new Book(name,author,type,price);//新创建一本书
        int size= bookList.getUsedSize();
        bookList.setBook(size,book);
        bookList.setUsedSize(size+1);
        System.out.println("新增图书成功");
    }
}

创建删减图书类DelOperation,继承接口 IOperation

package operation;

import book.Book;
import book.BookList;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删减图书");
        System.out.println("请输入要删减图书的名字:");
        String name=scanner.nextLine();
        int size= bookList.getUsedSize();
        int index=0;
        int i=0;
        for (i = 0; i <size ; i++) {
            Book book = bookList.getpos(i);
            if(name.equals(book.getName())) {
               index=i;
               break;
            }
        }
        if(i>=size){
            System.out.println("没有你要删除的图书");
        }
        //删除就是要将j+1下标的信息放进j下标里面信息去,这样子j里面的信息就被删除了
        for (int j = index; j < size-1; j++) {
         Book book=bookList.getpos(j+1);//获取j+1下标放的书的信息给j下标
         bookList.setBook(j,book);
        }
        bookList.setBook(size,null);//删除之后,把最后一个下标信息置为null
        bookList.setUsedSize(size-1);//下标减1
        System.out.println("删除成功");
    }
}

创建查找图书类FindOperation,继承接口 IOperation

package operation;

import book.BookList;
import book.Book;
public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("查找图书!");
        System.out.println("请输入要查找书的名字");
        String name = scanner.nextLine();
        int size = bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getpos(i);//找到这本书的信息
            if(name.equals(book.getName())) {//比较名字是否相同
                System.out.println("找到了这本书,信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书!");
    }
}

创建显示图书列表类DisplayOperation,继承接口 IOperation

package operation;
import book.Book;
import book.BookList;


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

创建借阅图书列表类BorrowOperation,继承接口 IOperation

package operation;

import book.Book;
import book.BookList;


public class BorrowOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("借阅图书");
        System.out.println("请输入你要借阅的图书的名字");
        String name=scanner.nextLine();
        int size= bookList.getUsedSize();
        for(int i=0;i<size;i++){
            Book book = bookList.getpos(i);
            if(name.equals(book.getName())) {
                book.setBorrowed(true);
                System.out.println("借阅成功");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要借阅的书");
    }
}

创建归还图书列表类ReturnOperation,继承接口 IOperation

package operation;

import book.Book;
import book.BookList;

public class ReturnOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("归还图书");
        System.out.println("请输入你要归还的图书的名字");
        String name=scanner.nextLine();
        int size= bookList.getUsedSize();
        for(int i=0;i<size;i++){
            Book book = bookList.getpos(i);
            if(name.equals(book.getName())) {
                book.setBorrowed(true);
                System.out.println("归还成功");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要归还的书");
    }
}

创建退出系统类ExitOperation,继承接口 IOperation

package operation;

import book.BookList;

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

}

🥬小结

以上就是今天的内容,如有什么问题欢迎大家在评论区留言✌✌✌。

评论 20 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

菜菜不恰菜

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值