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

DVD管理器

程序员文章站 2022-12-20 08:19:46
某音像店有一批DVD,通过对外出租DVD盈利。现在需要你为该店开发一 个迷你DVD管理器,实现DVD碟片的管理,包括如下功能: 新增DVD 查看DVD 删除DVD 借出DVD 归还DVD,并计算租金DVD管理菜单,实现菜单切换创建DVDMenu类public class DVDMenu { Scanner input = new Scanner(System.in); DVDSet dvdSet = new DVDSet(); /** * 显示菜单内容 *...

某音像店有一批DVD,通过对外出租DVD盈利。现在需要你为该店开发一 个迷你DVD管理器,实现DVD碟片的管理,包括如下功能: 新增DVD 查看DVD 删除DVD 借出DVD 归还DVD,并计算租金

DVD管理菜单,实现菜单切换

创建DVDMenu类

public class DVDMenu {
    Scanner input = new Scanner(System.in);
    DVDSet dvdSet = new DVDSet();

    /**
     * 显示菜单内容
     */
    public void showMenu(){
        System.out.print("\n");
        System.out.println("\t欢 迎 使 用 迷 你 DVD 管 理 器");
        System.out.println("------------------------------------------");
        System.out.println("\t\t\t1.新增DVD");
        System.out.println("\t\t\t2.查看DVD");
        System.out.println("\t\t\t3.删除DVD");
        System.out.println("\t\t\t4.借出DVD");
        System.out.println("\t\t\t5.归还DVD");
        System.out.println("\t\t\t6.排行榜");
        System.out.println("\t\t\t7.退  出");
        System.out.println("-------------------------------------------");
    }

    /**
     * 实现菜单选择
     * @return
     */
    public int startMenu() {
        int getNum;
        showMenu();
        System.out.print("请选择:");
        if (input.hasNextInt()) {
            getNum = input.nextInt();
            if (getNum >= 1 && getNum <= 7) {
                return getNum;
            } else {
                System.out.println("选择错误,请重新输入");
                return 00;
            }
        } else {
            String str = input.next();
            System.out.println("只能输入整数");
            return 00;
        }
    }

    /**
     * 返回主菜单
     */
    public int returnMain(){
        int choMenu;
        showMenu();
        System.out.print("请选择:");
        if (input.hasNextInt()){
            choMenu = input.nextInt();
            return choMenu;
        }else {
            String str = input.next();
            System.out.println("只能输入整数");
            return 00;
        }
    }
}

添加DVD信息,进行初始化,修改DVD信息

创建DVDMgr类

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @Desecription 添加DVD信息初始化,以及DVD信息修改
 * @Auther ZhengShouke
 * @Date 2020/7/17 13:36
 */
public class DVDMgr {
    DVDSet dvdSets = new DVDSet();
    Scanner input = new Scanner(System.in);

    /**
     * 对DVD信息进行初始化
     */
    public void initial() {
        //是否继续标识符
        boolean isGo = false;
        //输入错误标识符
        boolean no = false;
        //判断时间是否输入正确
        boolean getTime = false;
        int i = 0;
        while (!isGo) {
            System.out.print("请输入第" + (i + 1) + "张DVD的名称:");
            dvdSets.name[i] = input.next();
            //将名称左右的空格去掉
            dvdSets.name[i] = dvdSets.name[i].trim();
            /*String类型的字符串转换成StringBuffer类型的字符串
              以便于名称的统一*/
            StringBuffer nameBuffer = new StringBuffer(dvdSets.name[i]);
            for (int j = 0; j < 1; j++){
                nameBuffer.insert(j,'《');
            }
            nameBuffer.append("》");
            //再将StringBuffer类型的字符串转换为String类型
            dvdSets.name[i] = nameBuffer.toString();
            do {
                System.out.print("请输入第" + (i + 1) + "张DVD的租借状态(可租/已租出):");
                dvdSets.state[i] = input.next();
                //判断输入的DVD的出租状态,若已租出则输入租出日期
                if ("已租出".equals(dvdSets.state[i])) {
                    do {
                        System.out.print("请输入第" + (i + 1) + "张租出日期(yyyy-MM-dd):");
                        dvdSets.date[i] = input.next();
                        getTime = timeJudge(dvdSets.date[i]);
                        if (getTime){
                            dvdSets.rank[i]++;
                            break;
                        }else {
                            System.out.println("时间输入错误");
                        }
                    }while (!getTime);
                    no = false;
                }else if ("可租".equals(dvdSets.state[i])){
                    no = false;
                }else {
                    System.out.print("输入错误,");
                    no = true;
                }
            }while (no);
            i++;
            System.out.print("是否继续添加DVD信息?(Y/N)");
            isGo = isGoon();
        }
    }

    /**
     * 增加DVD信息
     */
    public void addDVD() {
        System.out.println("---->新增DVD");
        System.out.print("\n");
        boolean isGo = false;
        boolean no = false;
        boolean getTime = false;
        while (!isGo) {
            System.out.print("请输入要增加的DVD名称:");
            String name = input.next();
            for (int i = 0; i < dvdSets.name.length; i++) {
                //判断位置是否为空
                if (dvdSets.name[i] == null) {
                    dvdSets.name[i] = name;

                    /**
                     * 进行名称的统一
                     */
                    dvdSets.name[i] = dvdSets.name[i].trim();
                    StringBuffer nameBuffer = new StringBuffer(dvdSets.name[i]);
                    for (int j = 0; j < 1; j++){
                        nameBuffer.insert(j,'《');
                    }
                    nameBuffer.append("》");
                    dvdSets.name[i] = nameBuffer.toString();

                    do {
                        System.out.print("请输入DVD的租借状态(可租/已租出):");
                        dvdSets.state[i] = input.next();
                        //判断输入的DVD的出租状态,若已租出则输入租出日期
                        if ("已租出".equals(dvdSets.state[i])) {
                            do {
                                System.out.print("请输入租出日期(yyyy-MM-dd):");
                                dvdSets.date[i] = input.next();
                                getTime = timeJudge(dvdSets.date[i]);
                                if (getTime){
                                    dvdSets.rank[i]++;
                                    break;
                                }else {
                                    System.out.println("时间输入错误");
                                }
                            }while (!getTime);
                            no = false;
                        }else if ("可租".equals(dvdSets.state[i])){
                            no = false;
                        }else {
                            System.out.print("输入错误,");
                            no = true;
                        }
                    }while (no);

                    System.out.println("DVD" + dvdSets.name[i] + "信息增加成功");
                    break;
                }
            }
            System.out.print("是否继续增加DVD信息?(Y/N):");
            isGo = isGoon();
            /*String no = input.next();
            if ("N".equals(no)) {
                isGo = true;
            }*/
        }
    }

    /**
     * 查看DVD信息
     */
    public void seeDVD() {
        System.out.println("---->查看DVD");
        System.out.print("\n");
        System.out.println("序号\t名称\t\t状态\t借出日期");
        int j = 1;
        for (int i = 0; i < dvdSets.name.length; i++) {
            if (dvdSets.name[i] != null) {
                System.out.print(j + "\t" + dvdSets.name[i] + "\t\t");
                if ("已租出".equals(dvdSets.state[i])) {
                    System.out.println(dvdSets.state[i] + "\t" + dvdSets.date[i]);
                } else {
                    System.out.println(dvdSets.state[i]);
                }
            }
            j++;
        }
        System.out.println("****************************************************");
        int num;
        do {
            System.out.print("输入0返回:");
            if (input.hasNextInt()){
                num = input.nextInt();
                if (num != 0){
                    System.out.println("输入错误,返回请输入0:");
                    num =1;
                }
            }else {
                String str = input.next();
                System.out.println("输入错误,返回请输入0:");
                num = 1;
            }
        }while (num == 1);
    }

    /**
     * 删除DVD信息
     */
    public void deleteDVD() {
        System.out.println("---->删除DVD");
        System.out.print("\n");
        //是否继续删除标识符
        boolean isGo = false;
        //是否删除成功标识符
        boolean complete = false;
        while (!isGo){
            System.out.print("请输入要删除的DVD名称:");
            String name = input.next();

            StringBuffer nameBuffer = new StringBuffer(name);
            for (int j = 0; j < 1; j++){
                nameBuffer.insert(j,'《');
            }
            nameBuffer.append("》");
            name = nameBuffer.toString();

            for (int i = 0; i < dvdSets.name.length; i++) {
                //判断DVD状态是否可租,可租的情况下可以删除
                if (name.equals(dvdSets.name[i]) && "可租".equals(dvdSets.state[i])) {
                    for (int j = i; j < dvdSets.name.length - 1; j++) {
                        //删除的位置后面的DVD信息依次前移一位
                        dvdSets.name[j] = dvdSets.name[j + 1];
                        dvdSets.state[j] = dvdSets.state[j + 1];
                        dvdSets.date[j] = dvdSets.date[j + 1];
                    }
                    //如果可以删除,则需将最后一个元素置空
                    dvdSets.name[dvdSets.name.length - 1] = null;
                    dvdSets.state[dvdSets.state.length - 1] = null;
                    dvdSets.date[dvdSets.date.length - 1] = null;
                    System.out.println("删除成功");
                    complete = true;
                    break;

                    //如果该DVD处于已出租状态则不能删除
                } else if (name.equals(dvdSets.name[i]) && "已租出".equals(dvdSets.state[i])) {
                    System.out.println("DVD" + dvdSets.name[i] + "已租出,不可删除");
                    complete = true;
                    break;
                } else {
                    complete = false;
                }
            }
            if (!complete){
                System.out.println("没有找到有关" + name + "DVD的信息");
            }
            System.out.print("是否继续删除DVD信息?(Y/N):");
            isGo = isGoon();
            /*String no;
            do {
                no = input.next();
                if ("Y".equals(no)){
                    isGo = false;
                }else if ("N".equals(no)) {
                    isGo = true;
                }else {
                    System.out.println("输入错误,请重新选择:");
                }
            }while (!"Y".equals(no) || !"N".equals(no));*/
        }
    }

    /**
     * 借出DVD
     */
    public void lendDVD() {
        System.out.println("---->借出DVD");
        System.out.print("\n");
        boolean isGo = false;
        //租出成功标识符
        boolean complete = false;
        boolean getTime = false;
        while (!isGo){
            System.out.print("请输入要借的DVD名称:");
            String name = input.next();

            //同样进行名称的统一
            StringBuffer nameBuffer = new StringBuffer(name);
            for (int j = 0; j < 1; j++){
                nameBuffer.insert(j,'《');
            }
            nameBuffer.append("》");
            name = nameBuffer.toString();

            for (int i = 0; i < dvdSets.name.length; i ++){
                //判断是否为可租状态
                if (name.equals(dvdSets.name[i]) && "可租".equals(dvdSets.state[i])){
                    dvdSets.state[i] = "已租出";
                    do {
                        System.out.print("请输入租出日期(yyyy-MM-dd):");
                        dvdSets.date[i] = input.next();
                        getTime = timeJudge(dvdSets.date[i]);
                        if (getTime){
                            dvdSets.rank[i]++;
                            break;
                        }else {
                            System.out.println("时间输入错误");
                        }
                    }while (!getTime);
                    dvdSets.date[i] = input.next();
                    dvdSets.rank[i]++;
                    System.out.println("借出成功");
                    complete = true;
                    break;
                }else if (name.equals(dvdSets.name[i]) && "已租出".equals(dvdSets.state[i])){
                    System.out.println("对不起," + name + "已租出");
                    complete = true;
                    break;
                }else {
                    complete = false;
                }
            }
            if (!complete){
                System.out.println("没有找到有关" + name + "的信息");
            }
            System.out.print("是否继续借出DVD?(Y/N):");
            isGo = isGoon();
        }
    }

    /**
     * 归还DVD
     */
    public void returnDVD(){
        System.out.println("---->归还DVD");
        System.out.print("\n");
        boolean isGo = false;
        boolean complete = false;
        boolean getTime = false;
        //归还日期
        String date;
        while (!isGo){
            System.out.print("请输入要归还的DVD名称:");
            String name = input.next();

            StringBuffer nameBuffer = new StringBuffer(name);
            for (int j = 0; j < 1; j++){
                nameBuffer.insert(j,'《');
            }
            nameBuffer.append("》");
            name = nameBuffer.toString();

            for (int i = 0; i < dvdSets.name.length; i++){
                if (name.equals(dvdSets.name[i]) && "已租出".equals(dvdSets.state[i])){
                    do {
                        System.out.print("请输入归还日期(yyy-MM-dd):");
                        date = input.next();
                        getTime = timeJudge(date);
                        if (getTime){
                            if (date.compareTo(dvdSets.date[i]) > 0){
                                System.out.println("归还成功");
                                //调用租金计算方法
                                double money = getRent(dvdSets.date[i], date);
                                System.out.println("租金为:" + money + "元");
                                dvdSets.state[i] = "可租";
                                complete = true;
                                break;
                            }else {
                                System.out.println("归还日期不能早于借出日期");
                                complete = true;
                                getTime = false;
                            }
                        }else {
                            System.out.println("时间输入错误");
                            complete = true;
                            getTime = false;
                        }
                    }while (!getTime);
                }else if (name.equals(dvdSets.name[i]) && "可租".equals(dvdSets.state[i])){
                    System.out.println("DVD" + name + "没有外租,无法进行归还");
                    complete = true;
                    break;
                }/*else {
                    complete = false;
                }*/
            }
            if (!complete){
                System.out.println("没有找到有关" + name + "的信息");
            }
            System.out.print("是否继续归还DVD?(Y/N):");
            isGo = isGoon();
        }
    }

    /**
     * 结算租金
     */
    public double getRent(String date1, String date2){
        double money = 0;
        //将日期字符串转换成Date
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = sd.parse(date1);
            Date d2 = sd.parse(date2);
            money = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000) / 10;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return money;
    }

    /**
     * 借出排行榜
     */
    public void getRank(){
        System.out.println("---->排行榜");
        System.out.print("\n");
        System.out.println("****************************************");
        System.out.println("次数\t名称");
        for (int i = 0; i < dvdSets.name.length - 1; i++){
            for (int j = i + 1; j < dvdSets.name.length; j++){
                if (dvdSets.rank[i] < dvdSets.rank[j]){
                    int temp = dvdSets.rank[j];
                    dvdSets.rank[j] = dvdSets.rank[i];
                    dvdSets.rank[i] = temp;
                    String name = dvdSets.name[j];
                    dvdSets.name[j] = dvdSets.name[i];
                    dvdSets.name[i] = name;
                    String state = dvdSets.state[j];
                    dvdSets.state[j] = dvdSets.state[i];
                    dvdSets.state[i] = state;
                    String date = dvdSets.date[j];
                    dvdSets.date[j] = dvdSets.date[i];
                    dvdSets.date[i] = date;
                }
            }
        }
        for (int i = 0; i < dvdSets.name.length; i++){
            if (dvdSets.name[i] != null){
                System.out.println(dvdSets.rank[i] + "\t" + dvdSets.name[i]);
            }
        }
        System.out.println("****************************************");

        int num;
        do {
            System.out.print("输入0返回:");
            if (input.hasNextInt()){
                num = input.nextInt();
                if (num != 0){
                    System.out.println("输入错误,返回请输入0:");
                    num =1;
                }
            }else {
                String str = input.next();
                System.out.println("输入错误,返回请输入0:");
                num = 1;
            }
        }while (num == 1);
    }

    /**
     * 判断是否继续的程序块
     */
    public boolean isGoon(){
        boolean isGo = false;
        String no;
        do {
            no = input.next();
            if ("Y".equals(no)){
                isGo = false;
            }else if ("N".equals(no)) {
                isGo = true;
            }else {
                System.out.println("输入错误,请重新选择:");
            }
        }while (!"Y".equals(no) && !"N".equals(no));
        return isGo;
    }

    /**
     * 时间输入判断
     * @param time
     * @return
     */
    public boolean timeJudge(String time) {
        //通过布尔值的返回,来判断时间格式是否正确
        boolean getTime = false;
        //yearDraw、monthDraw、dayDraw分别对应分开的年月日
        int yearDraw = Integer.parseInt(time.substring(0,4));
        int monthDraw = Integer.parseInt(time.substring(5,7));
        int dayDraw = Integer.parseInt(time.substring(8));
        if (time.length() == 10){
            if (yearDraw > 0 && yearDraw < 9999){
                if (monthDraw == 1 || monthDraw == 3 || monthDraw == 5 || monthDraw == 7
                        || monthDraw == 8 || monthDraw == 10 || monthDraw == 12){
                    if (dayDraw >= 1 && dayDraw <= 31){
                        getTime = true;
                        return getTime;
                    }
                }else if (monthDraw == 4 || monthDraw == 6 || monthDraw == 9
                        || monthDraw == 11 ){
                    if (dayDraw >= 1 && dayDraw <= 30){
                        getTime = true;
                        return getTime;
                    }
                    //判断2月天数
                }else if (monthDraw == 2){
                    if (yearDraw % 4 == 0){
                        if (dayDraw >= 1 && dayDraw <= 29){
                            getTime = true;
                            return getTime;
                        }
                    }else {
                        if (dayDraw >= 1 && dayDraw <= 28){
                            getTime = true;
                            return getTime;
                        }
                    }
                }
            }
        }
        return getTime;
    }
}

保存DVD信息 每张DVD的信息包括名称、是否可借的状态以及借出的日期

创建DVDSet类

/**
 * 保存DVD信息 每张DVD的信息包括名称、是否可借的状态以及借出的日期
 */
public class DVDSet {
    String[] name = new String[50]; //DVD名称数组
    String[] state = new String[50];  //DVD借出状态:0已借出/1可借
    String[] date = new String[50];  //DVD借出日期
    int[] rank = new int[50]; // DVD借出次数
}

编写测试类

/**
 * @Desecription TODO
 * @Auther ZhengShouke
 * @Date 2020/7/18 13:25
 */
public class TestDVD {
    public static void main(String[] args) {
        DVDSet dvdSet = new DVDSet();
        DVDMenu dvdMenu = new DVDMenu();
        DVDMgr dvdMgr = new DVDMgr();

        int choice;
        boolean isGo = false;

        dvdMgr.initial();
        while (!isGo){
            choice = dvdMenu.startMenu();
            switch (choice){
                case 1:
                    dvdMgr.addDVD();
                    break;
                case 2:
                    dvdMgr.seeDVD();
                    break;
                case 3:
                    dvdMgr.deleteDVD();
                    break;
                case 4:
                    dvdMgr.lendDVD();
                    break;
                case 5:
                    dvdMgr.returnDVD();
                    break;
                case 6:
                    dvdMgr.getRank();
                    break;
                case 7:
                    isGo = true;
                    break;
                default: ;
            }
        }
    }
}

运行效果

DVD管理器
其余功能自行验证

原创,转载请标明出处
源码地址
提取码:m6q2

本文地址:https://blog.csdn.net/weixin_49757953/article/details/109643699

相关标签: Java学习 java