DVD管理系统

 

 

import java.util.*;
import java.text.*;

class DVD {
    public static long charge(String dstr1,String dstr2){
        long charge=0;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        try {
           Date d1 = sd.parse(dstr1);
        Date d2 = sd.parse(dstr2);
        charge = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return charge;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        boolean flag = false;
     
        // DVD名称
        String[] names = new String[100];
        
        int[] states = new int[20];
        // DVD借出日期
        String[] dates = new String[20];
        // DVD借出次数
        int[] count = new int[20];

        // 初始化数据
        names[0] = "罗马假日";
        states[0] = 1;
        dates[0] = "2015-10-11";
        count[0] = 18;

        names[1] = "风声鹤唳";
        states[1] = 0;
        count[1] = 5;

        names[2] = "浪漫满屋";
        states[2] = 0;
        count[2] = 40;

        int num = -1;

    

        
        do {
            // 主菜单显示
            System.out.println("欢迎使用DVD管理器");
            System.out.println("*-----------------*");
            System.out.println("|   1、新增DVD    |");
            System.out.println("*-----------------*");
            System.out.println("|   2、查看DVD    |");
            System.out.println("*-----------------*");
            System.out.println("|   3、删除DVD    |");
            System.out.println("*-----------------*");
            System.out.println("|   4、借出DVD    |");
            System.out.println("*-----------------*");
            System.out.println("|   5、归还DVD    |");
            System.out.println("*-----------------*");
            System.out.println("|   6、退出DVD    |");
            System.out.println("*-----------------*");
            System.out.print("请输入所选:");
            int choice = input.nextInt();
            switch (choice) {
            case 1:
                // 新增
                System.out.println("---- 新增DVD ----");
                System.out.print("请输入DVD名称:");
                String name = input.next();
                // 标识能否插入 false:不能插入 true:能插入
                boolean flag1 = false;

                for (int i = 0; i < names.length; i++) {
                    // 插入DVD
                    if (names[i] == null) {
                        names[i] = name;
                        flag1 = true;
                        break;
                    }
                }
                if (flag1) {
                    System.out.println("新增《" + name + "》成功!");
                } else {
                    System.out.println("货架已满,不能新增!");
                }
                break;
            case 2:
                // 查看
                System.out.println("---- 查看DVD -----");
                System.out.println("序号\t" + "状态\t" + "名称\t\t" + "借出日期" + "\t借出次数");
                for (int i = 0; i < names.length; i++) {
                    if (names[i] != null) {
                        String state = (states[i] == 0) ? "可借" : "已借出";
                        String dateStr = (dates[i] == null) ? "\t" : (dates[i]);
                        String countStr = (count[i] == 0) ? "\t" : (count[i] + "次");

                        System.out.println(" " + (i + 1) + "\t" + state + "\t" + names[i] + "\t" + dateStr + "\t" + countStr);
                    }
                }
                break;
            case 3:
                // 删除
                System.out.println("---- 删除DVD -----");
                System.out.print("请输入要删除的DVD名称:");
                String delete = input.next();
                
                boolean flag2 = false;
                // 记录要删除的DVD位置
                int index = -1;
                // 找到要删除DVD的位置
                for (int i = 0; i < names.length; i++) {
                    if (names[i] != null && delete.equals(names[i]) && states[i] == 1) {
                       
                        flag2 = true;
                        System.out.println("该DVD已经借出,无法删除!");
                        break;
                    } else if (names[i] != null && delete.equals(names[i]) && states[i] == 0) {
                       
                        flag2 = true;
                        index = i;
                        System.out.println("删除《" + delete + "》成功!");
                        break;
                    }

                }
                // 找到DVD删除
                if (index != -1) {
                    // 根据上一步找到的下标位置index
                    for (int j = index; j < names.length; j++) {
                        
                        if (j != names.length - 1) {
                            names[j] = names[j + 1];
                            states[j] = states[j + 1];
                            dates[j] = dates[j + 1];
                            count[j] = count[j + 1];

                        }
                        names[names.length - 1] = null;
                        states[names.length - 1] = 0;
                        dates[names.length - 1] = null;
                        count[names.length - 1] = 0;

                    }
                }
                if (!flag2) {
                    System.out.println("对不起,没有匹配的DVD信息,无法删除!");
                }
                break;
            case 4:
                // 借出
                System.out.print("请输入要借阅的DVD名称:");
                String want = input.next();
                for (int i = 0; i <= names.length; i++) {
                    if (names[i] == null) {
                        System.out.println("没有找到匹配的DVD信息!");
                        break;
                    } else if (want.equals(names[i]) && states[i] == 1) {
                        System.out.println("《" + want + "》已被借出!");
                        break;
                    } else if (want.equals(names[i]) && states[i] == 0) {
                        System.out.print("请输入借出日期(年-月-日):");
                        dates[i] = input.next();
                        states[i] = 1;
                        count[i]++;
                        System.out.println("借出《" + want + "》成功!");
                        break;
                    }
                }

                break;
            case 5:
                // 归还
                System.out.print("请输入要归还的DVD名称:");
                String back = input.next();
                for (int i = 0; i <= names.length; i++) {
                    if (names[i] == null) {
                        System.out.println("没有找到匹配的DVD信息");
                        break;
                    } else if (back.equals(names[i]) && states[i] == 0) {
                        System.out.println(back + "未被借出,无需归还!");
                        break;
                    } else if (back.equals(names[i])  && states[i] == 1) {
                        System.out.print("请输入归还日期(年-月-日):");
                        // 记录归还日期
                        String backDate = input.next();
                        
                        states[i] = 1;
                        count[i]++;
                        System.out.println("《" + back + "》归还成功!");
                        System.out.println("《" + back + "》借阅日期:" + dates[i]);
                        System.out.println("《" + back + "》归还日期:" + backDate);
                        long day = charge(dates[i],backDate);
                        // 计算租金:1元/天
                        long money = day * 1;
                        //System.out.println("**********"+aa);
                        System.out.println("《" + back + "》应付租金:" + money + "元");

                        // 数据更新
                        states[i] = 0;
                        dates[i] = null;
                        break;
                    }
                }

                break;
            case 6:
                // 退出
                flag = true;
                break;
            default:
                // 输入要求以外的数字,默认退出
                flag = true;
                break;
            }
            if (flag) {
                // 想退出菜单
                num = -1;
                break;

            } else {
                // 不退出,继续进行主菜单选择
                System.out.println("*-*-*-*-*-*-*-*-*-*-*-*-*");
                System.out.println("请输入0返回:");
                num = input.nextInt();
            }
        } while (num == 0);
        // 程序退出,结束
        System.out.println("谢谢使用本系统!");
    }
}

 


 

菜鸟级,不足之处,还望指点

posted @ 2016-11-18 09:38  随行-LV  阅读(482)  评论(0编辑  收藏  举报