【java实现图书小程序】

程序描述

该图书小程序面向的对象是图书管理员和图书普通用户

实现的功能:

对于图书管理员拥有的功能包括:
1.查找图书
2.新增图书
3.删除图书
4.显示图书
0.退出系统
对于图书普通用户拥有的功能包括:
1.查找图书
2.借阅图书
3.归还图书
0.退出系统

代码实现

实现了抽象类、接口、继承、多态、向上转型的整合

1.对于用户,新建包User实现用户的管理。User类作为两种用户类型的父类,两种用户的操作目录不一样,所以将操作目录定义为抽象方法,在两种用户类里面实现方法的重写。

public abstract class User {
    private String name;
    public IOperation[] iOperations ;//这里没有分配空间
    public String getName() {
        return name;
    }

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

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

User里面定义了用户的名字、操作数组属性,操作目录menu、以及根据用户输入的数字choice进行相关的work操作,用于各个操作方法的实现。

对于两种用户对象,分别建立相应的类:
1.1 AdminUser:

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.getName() + ",欢迎来到图书小练习");
        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 scanner = new Scanner(System.in);
        int choice2 = scanner.nextInt();
        return choice2;
    }
}

1.2 NormalUser:

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() {
        System.out.println("普通用户菜单:");
        System.out.println("*************************");
        System.out.println("hello " + this.getName() + ",欢迎来到图书小练习");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("*************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice2 = scanner.nextInt();
        return choice2;
    }
}

类中重写操作目录方法menu(),并将目录中的操作集合整合于操作数组中,数组中的每个元素表示一个对象,每个对象对应着相应操作的操作方法。利用数组整合操作的目的是为了能够通过输入操作的数字实现操作,也就是1.查找图书、2.新增图书、3.删除图书、4.显示图书、0.退出系统。而里面的每种操作都通过相应的类进行实现。见后面的Operation包。

2.对于图书,新建包Book,实现Book类以及BookList类的定义,BookList类表示书架,用来存放书籍。
2.1Book类

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;;//是否被借出,false表示未被借出,默认值是false

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
    public void setName(String name){
        this.name = name;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

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

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

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

    public String getName(){
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public int getPrice() {
        return price;
    }

    public String getType(){return type;}

    public boolean isBorrowed() {
        return isBorrowed;
    }

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

Book类中包含了每本书的书名、作者、价格、类型以及是否借出 五个属性,以及构造方法、每个属性的get和set方法、每本书的信息打印toString()方法。
2.2BookList类

package book;

public class BookList{
    private Book[] bookLists = new Book[10];//书架的容量,可以放10本书
    private int usedSize;//记录当前书架上有几本书

    public Book[] getBookLists() {
        return bookLists;
    }

    public void setBookLists(Book[] bookLists) {
        this.bookLists = bookLists;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public BookList(){
        bookLists[0] = new Book("三国演义","罗贯中",89,"小说");
        bookLists[1] = new Book("西游记","施耐庵",78,"小说");
        bookLists[2] = new Book("红楼梦","曹雪芹",49,"小说");
        this.usedSize = 3;
    }

    public Book getBook(int pos){
        return bookLists[pos];
    }
    public void setBooks(int pos,Book book){
        bookLists[pos] = book;
    }
}

BookList类用来存放Book,里面包含了:
1)书架的容量、已经使用的Size两个属性;
2)属性的get和set方法;
3)对书架的初始书籍进行了初始化,里面有三本书;
4)getBook方法来通过下标位置得到书架上的该本书;
5)setBook方法在相应位置上新增一本书。

3.操作包Operation,用于存放管理员用户和普通用户的操作方法。
在具体写每种操作之前通过定义一个接口来规范每种操作:

public interface IOperation {//对图书进行操作的统一标准接口,所有对图书的操作都对需要按照这个规范进行
    public void work(BookList bookList);//对书进行查找、删除等操作都是对书架进行的操作
}

每种操作在实现时需要重写该接口的work方法。

3.1管理员操作:
1)查找图书

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书");
        System.out.println("请输入所要查找的图书的名字:");
        Scanner scanner = new Scanner(System.in);
        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("没有找到该本书!");
    }
}

2)新增图书

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 type = scanner.nextLine();
        System.out.println("图书价格:");
        int price = scanner.nextInt();
//        System.out.println("图书类型");
//        String type = scanner.nextLine();//但是在java里面最好是先输入字符串再输数字,不然会将图书类型认为是一个回车
        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();
        bookList.setBooks(currentSize,book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("图书新增成功!");
//        scanner.close();
    }//新增图书功能的实现就是业务逻辑
}

3)删除图书

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int delIndex = 0;
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书的名字:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        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++) {
            Book book1 = bookList.getBook(j+1);
            bookList.setBooks(j,book1);
        }
        bookList.setBooks(currentSize-1,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除图书成功!");

    }
}

4)显示图书

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);
        }
    }
}

0)退出系统

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

3.2 普通用户操作:
1)查找图书
同管理员一样的查找操作,无需重写
2)借阅图书

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);
                    System.out.println("借阅成功!");
                }
                return;
            }
        }
        System.out.println("没有你要借阅的图书!");
    }
}

3)归还图书

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(false);
                System.out.println("归还成功!");
                return ;
            }
        }
        System.out.println("没有你要归还的图书!");
    }
}

0)退出系统
同管理员一样的退出系统操作,无需重写

4.利用Main实现各个类的联系:

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 choice1 = scanner.nextInt();
        if (choice1 == 1){
             return new AdminUser(name);//将返回值设为User类型,发生了向上转型
        }else{
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        //user最终指向哪个用户是不确定的,需要根据用户的输入进行判断,不知道是管理员还是普通用户,所以直接使用User作为接受的类型,User是管理员和普通用户的父类
        User user = login();
        while(true){
            //根据用户身份的不同打印操作菜单
            int choice = user.menu();
            //根据choice调用指定的操作
            user.doOperation(choice,bookList);
        }
    }
}

Main里面包含了登录login()方法,以及主函数main()

小结

java是面向对象程序语言设计,面向对象实质就是对象与对象之间进行协作来完成相应的目标,以上程序通过用户对象、书籍对象、书架对象以及各操作方法对象进行协作,实现对书架上图书的查找、新增、借阅、归还以及显示等,完成不同用户对图书进行不同操作的目标。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值