欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java DVD租借系统

程序员文章站 2022-03-25 15:04:07
项目要求:在控制台输出一个DVD管理系统拥有六个功能:1、新增2、查看3、删除4、借出5、归还6、退出源码:DVD类:public class DVD { //DVD类 private String name; private int state; private int date; private int count; //private表示私有,增加安全性 //无参方法 public DVD() {...

项目要求:
在控制台输出一个DVD管理系统
拥有六个功能:
1、新增
2、查看
3、删除
4、借出
5、归还
6、退出
Java DVD租借系统

源码:
DVD类:

public class DVD {



    //DVD类
    private String name;
    private int state;
    private int date;
    private int count;


    //private表示私有,增加安全性


    //无参方法
    public DVD() {

    }

    //有参构造
    public DVD(String name, int state, int date, int count) {
        this.name = name;
        this.state = state;
        this.date = date;
        this.count = count;
    }

    public String getName() {
        return name;
    }

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

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public int getDate() {
        return date;
    }

    public void setDate(int date) {
        this.date = date;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


//toString方法

    @Override
    public String toString() {
        return "DVD [name='" + name +
                ", state=" + state +
                ", date=" + date +
                ", count=" + count +
                ']';
    }
}

实现类:

public class newGongNeng {

    DVD[] dvd = new DVD[6];

    //初始化
    void chushihuan() {
        dvd[0] = new DVD("诡秘之主", 1, 0, 18);
        dvd[1] = new DVD("完美世界", 0, 0, 22);
        dvd[2] = new DVD("悲惨世界", 0, 0, 10);
    }

    void qidong() {
        Scanner s = new Scanner(System.in);

        //菜单
        System.out.println("------------------------------");
        System.out.println("|-----欢迎使用 DVD租借器-----|");
        System.out.println("------------------------------");
        System.out.println("|           1.新增           |");
        System.out.println("|           2.查看           |");
        System.out.println("|           3.删除           |");
        System.out.println("|           4.租借           |");
        System.out.println("|           5.归还           |");
        System.out.println("|           6.退出           |");
        System.out.println("------------------------------");
        System.out.println("请选择:");
        int xuhao = s.nextInt();

        switch (xuhao) {
            case 1://新增
                xinzeng();
                fanhui();
                break;
            case 2://查看
                chakan();
                fanhui();
                break;
            case 3://删除
                shanchu();
                fanhui();
                break;
            case 4://租借
                zujie();
                fanhui();
                break;
            case 5://归还
                guihuan();
                fanhui();
                break;
            case 6://退出
                System.out.println("再见!");
                break;
            default:
                System.out.println("输入错误!");
        }

    }


    //返回方法
    void fanhui() {
        System.out.println("------------------------------------");
        System.out.println("返回输入0");
        Scanner s = new Scanner(System.in);
        //判断输入的是否为0
        if (s.nextInt() == 0) {
            qidong();
        } else {
            System.out.println("输入错误!");
            //失败的话重新调用,递归
            fanhui();
        }


    }

    //新增
    void xinzeng() {
        Scanner s = new Scanner(System.in);
        System.out.println("新增");
        System.out.print("请输入名称:");
        //获取输入的名称
        String name = s.next();
        //使用for循环查找内容为空的DVD
        for (int i = 0; i < dvd.length; i++) {
            //如果查找到为空的DVD 就将输入名称赋给它
            if (dvd[i] == null) {
                dvd[i] = new DVD(name, 0, 0, 0);
                System.out.println("成功!");
                break;
            }
        }
    }

    //查看
    void chakan() {
        System.out.println("查看");
        System.out.println("序号\t状态\t名称\t\t日期\t次数");
        //循环遍历整个数组
        for (int i = 0; i < dvd.length; i++) {
            //如果不为空,将判断是否借出的属性转换为文本
            if (dvd[i] != null) {
                String x = null;
                //如果stata为1 便为已借出
                if (dvd[i].getState() == 1) {
                    x = "已借出";
                } else if (dvd[i].getState() == 0) {
                    x = "未借出";
                }
                //打印
                System.out.println((i + 1) + "\t" + "\t" + x + "\t" + dvd[i].getName() + "\t" + dvd[i].getDate() + "号" + "\t" + "\t" + dvd[i].getCount() + "次");
            }
        }

    }

    //删除
    void shanchu() {
        Scanner s = new Scanner(System.in);
        System.out.println("删除");
        System.out.print("删除的名称:");
        //获得要删除的名称
        String name = s.next();
        //设一个o判断条件
        int o = -1;
        //遍历寻找名称对应的DVD
        for (int i = 0; i < dvd.length; i++) {
            if (dvd[i] != null) {
                if (dvd[i].getName().equals(name)) {
                    o = i;
                    break;
                }
            }
        }
        //如果找到对应DVD
        if (o != -1) {
            //如果已借出,则失败
            if (dvd[o].getState() == 1) {
                System.out.println("DVD已借出,暂时不能删除.");
            } else if (dvd[o].getState() == 0) {
                System.out.println("删除成功!");
                dvd[o] = null;
            }
        } else if (o == -1) {//没有找到
            System.out.println("没有此DVD");
        }
    }

    void zujie() {
        Scanner s = new Scanner(System.in);
        System.out.println("租借");
        System.out.print("输入租借的名称:");
        String name = s.next();
        int o = -1;
        for (int i = 0; i < dvd.length; i++) {
            if (dvd[i] != null) {
                if (dvd[i].getName().equals(name)) {
                    o = 1;
                    break;
                }
            }
        }
        if (o != -1) {
            if (dvd[o].getState() == 1) {
                System.out.println("DVD已借出.");
            } else if (dvd[o].getState() == 0) {
                System.out.print("输入日期:");
                int date = s.nextInt();
                dvd[o].setDate(date);
                dvd[o].setCount(dvd[o].getCount() + 1);
                dvd[o].setState(1);
                System.out.println("租借成功 !");
            }

        } else if (o == -1) {
            System.out.println("没有此DVD");
        }

    }

    void guihuan() {
        Scanner s = new Scanner(System.in);
        System.out.println("归还");
        System.out.print("归还名称:");
        String name = s.next();
        int o = -1;
        for (int i = 0; i < dvd.length; i++) {
            if (dvd[i] != null) {
                if (dvd[i].getName().equals(name)) {
                    o = 1;
                    break;
                }
            }
        }
        if (o != -1) {
            if (dvd[o].getState() == 1) {
                System.out.print("输入日期 :");
                int data = s.nextInt();
                System.out.println("租金为:" + (data - dvd[o].getDate()) * 2 + "元");
                dvd[o].setState(0);
                dvd[o].setDate(0);
            } else if (dvd[o].getState() == 0) {
                System.out.println("此DVD没有借出,归还失败");
            }

        } else if (o == -1) {
            System.out.println("没有此DVD");
        }

    }

}

本文地址:https://blog.csdn.net/qq_40469797/article/details/107662570

相关标签: Java小案例