Java 图书管理小程序

菜单分为两种,一种管理员菜单,一种普通用户菜单。管理员菜单可以 增加图书,删除图书,搜索图书,显示全部图书,以及退出。普通用户菜单有 搜索图书,购买图书,退货图书,显示图书,以及退出。

管理员用户 与 普通用户 之间有许多共同点,我们可以把共同点提取出来单独形成一个 用户类,然后由管理员类 与 普通用户类 继承 用户类,程序启动后 当输入1进入管理员类,输入2进入普通用户类,分别打印不同的菜单 ,然后 用户在根据不同的菜单做出选择. 代码如下

test类

import book.bookLinkedlist;
import user.user;
import user.Adminuser;
import user.Regularusers;

import java.util.Scanner;

public class test {

    public static user login() {
        System.out.printf("请输入你的姓名:\n->:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你的身份:1.管理员 2.普通用户");
        System.out.printf("此处输入你的选择->:");
        int choice = scanner.nextInt();
        System.out.println();
        if (choice == 1) {
            return new Adminuser(name);
        }
        if (choice == 2) {
            return new Regularusers(name);
        } else
            return new Regularusers(name);
    }

    public static void main(String[] args) {

        bookLinkedlist bookLinkedlist = new bookLinkedlist();
        user user = login();
        while (true) {
            int choice = user.menu();
            user.work(choice, bookLinkedlist);
        }
    }
}

管理员类

package user;

import oper.*;

import java.util.Scanner;

public class Adminuser extends user {

    public Adminuser(String name) {

        user(name);

        Ioper = new Ioper[]{
                new Esc(),
                new Searchbooks(),
                new Addbook(),
                new Deletebook(),
                new Printbooks(),
        };
    }

    public int menu() {
        System.out.println("===========管理员菜单============");
        System.out.println("hello " + name + " 欢迎来到图书网站");
        System.out.println("0.退出系统");
        System.out.println("1.查找图书");
        System.out.println("2.添加图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("===============================");

        System.out.printf("此处输入你的选择->:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        System.out.println();
        return choice;
    }
}

普通用户类

package user;
import oper.*;

import java.util.Scanner;

public class Regularusers extends user {

    public Regularusers(String name) {
        user(name);
        Ioper = new Ioper[]{

                new Esc(),
                new Searchbooks(),
                new Lendbooks(),
                new Return(),
                new Printbooks(),
        };
    }



    public int menu() {
        System.out.println("===========普通用户菜单============");
        System.out.println("hello " + name + " 欢迎来到图书网站");
        System.out.println("0.退出系统");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("4.显示图书");
        System.out.println("================================");

        System.out.printf("此处输入你的选择->:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        System.out.println();
        return choice;
    }
}

用户类

package user;
import book.bookLinkedlist;
import oper.Ioper;

public abstract class user {

    public String name;
    public Ioper[] Ioper;

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

    public abstract int menu();

    public void work(int choice, bookLinkedlist bookLinkedlist){
        Ioper[choice].work(bookLinkedlist);
    }

}

图书类

package book;

public class book {
    public String name;//书名
    public String author;//作者
    public String classify;//分类
    public int price;//价格
    public boolean state;//状态

    public book(String name, String author, String classify, int price) {
        this.name = name;
        this.author = author;
        this.classify = classify;
        this.price = price;
    }

    public boolean getBook(){//状态
        return this.state;
    }

    public void setBook(boolean state) {//状态
        this.state = state;
    }

    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 getClassify() {//分类
        return classify;
    }

    public void setClassify(String classify) {//分类
        this.classify = classify;
    }

    public int getPrice() {//价格
        return price;
    }

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

    @Override
    public String toString() {
        return "书名:" + name + "   " +
                "作者:" + author + "   " +
                "分类:" + classify + "   " +
                "价格:" + price +
                "元" + (!(this.state) ?"   " +
                "未售出":"   已售出");
    }

}

书架类

package book;

public class bookLinkedlist {

    public int maxsize = 10;
    public book[] book = new book[maxsize];
    public int size;

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public bookLinkedlist() {
        book[0] = new book("三国演义", "王文杰", "历史小说", 58);
        book[1] = new book("西游记", "王文杰", "奇幻小说", 58);
        book[2] = new book("水浒传", "王文杰", "恶意小说", 99999);
        book[3] = new book("三体","刘慈欣","科幻小说",88);
        size = 4;
    }


}

增加一个图书的代码

package oper;

import book.book;
import book.bookLinkedlist;

import java.util.Scanner;

public class Addbook implements Ioper {
    public void work(bookLinkedlist bookLinkedlist) {//添加书籍
        System.out.println("新增图书");
        if (bookLinkedlist.size == bookLinkedlist.maxsize) {
            System.out.println("容量已满 增加图书失败");
            return;
        }
        Scanner scanner = new Scanner(System.in);

        System.out.printf("请输入书籍名字\n->:");
        String name = scanner.nextLine();
        System.out.printf("请输入作者名字\n->:");
        String author = scanner.nextLine();
        System.out.printf("请输入分类名字\n->:");
        String classify = scanner.nextLine();
        System.out.printf("请输入价格\n->:");
        int price = scanner.nextInt();

        book book = new book(name,author,classify,price);

        bookLinkedlist.book[bookLinkedlist.size] = book;

        bookLinkedlist.size++;
        System.out.println("增加书籍成功");
    }


}

删除一个图书的代码

package oper;

import book.bookLinkedlist;

import java.util.Scanner;

public class Deletebook implements Ioper {
    public void work(bookLinkedlist bookLinkedlist) {//删除书籍
        int counter = -1;
        System.out.printf("请输入要删除书籍的名称\n->:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookLinkedlist.getSize(); i++) {
            if (bookLinkedlist.book[i].name.equals(name)) {
                counter = i;
            }
        }
        if (counter != -1) {
            while (counter+1 < bookLinkedlist.getSize()) {
                bookLinkedlist.book[counter] = bookLinkedlist.book[counter + 1];
                counter++;
            }
            System.out.println("成功删除书籍");
            bookLinkedlist.setSize((bookLinkedlist.getSize()-1));
            return;
        }
        System.out.println("没有找到此书籍");
    }
}

退出程序

package oper;

import book.bookLinkedlist;

public class Esc implements Ioper {

    public void work(bookLinkedlist bookLinkedlist) {//打印书籍
        System.out.println("已退出");
        System.exit(0);
    }

}


接口 作用是被实现

package oper;

import book.bookLinkedlist;

public interface Ioper {
    public abstract void work(bookLinkedlist bookLinkedlist );
}

借书

package oper;

import book.bookLinkedlist;

import java.util.Scanner;

public class Lendbooks implements Ioper{
    public void work(bookLinkedlist bookLinkedlist) {//借书
        System.out.printf("请输入书籍的名称\n->:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookLinkedlist.getSize(); i++) {
            if (bookLinkedlist.book[i].name.equals(name)) {
               bookLinkedlist.book[i].state = true;
                System.out.println("借书成功");
                return;
            }
        }
        System.out.println("搜索书籍失败");

    }

}

打印所有书籍

package oper;

import book.bookLinkedlist;

public class Printbooks implements Ioper {
    public void work(bookLinkedlist bookLinkedlist) {//打印全部图书

        for (int i = 0; i < bookLinkedlist.size; i++) {
            System.out.print("书名->:" + bookLinkedlist.book[i].name + "   ");
            System.out.print("作者->:" + bookLinkedlist.book[i].author + "   ");
            System.out.print("分类->:" + bookLinkedlist.book[i].classify + "   ");
            System.out.print("价格->:" + bookLinkedlist.book[i].price + "元  ");

            String arr = "恶意小说";
            if(bookLinkedlist.book[i].classify.equals(arr)){
                System.out.println("禁止出售");
                continue;
            }
            if (!(bookLinkedlist.book[i].state)) {
                System.out.println("未售出");
            } else {
                System.out.println("已售出");
            }
        }
        System.out.println();
    }
}

还书

package oper;

import book.bookLinkedlist;

import java.util.Scanner;

public class Return implements Ioper {
    public void work(bookLinkedlist bookLinkedlist) {//还书
        System.out.printf("请输入书籍的名称\n->:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookLinkedlist.getSize(); i++) {
            if (bookLinkedlist.book[i].name.equals(name)) {
                bookLinkedlist.book[i].state = false;
                System.out.println("还书成功");
                return;
            }
        }
        System.out.println("搜索书籍失败");
    }

}

搜索某一本书

package oper;

import book.bookLinkedlist;

import java.util.Scanner;

public class Searchbooks implements Ioper {
    public void work(bookLinkedlist bookLinkedlist) {//搜索书籍
        System.out.printf("请输入搜索书籍的名称\n->:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookLinkedlist.getSize(); i++) {
            if (bookLinkedlist.book[i].name.equals(name)) {
                System.out.println("已找到该书籍,以下为详细信息");
                System.out.println(bookLinkedlist.book[i]);
                System.out.println();
                return;
            }
        }
System.out.println("没找到此书籍");
    }
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值