迷你DVD管理器

迷你DVD管理器

需求说明:为某音像店开发一个迷你DVD管理器,实现如下图所示的DVD碟片的管理功能。功能具体包括:新增、查看、删除、借出、归还和退出。

这道题用数组可以实现,最近学了集合,所以了写一篇用List集合实现的DVD管理器,希望对和我一样的初学者有所帮助。首先我先设计了四个类,一个DvdInfo类,封装其名称,借出状态和借出日期。包含有参和无参构造方法。一个DvdOperate类,对DVD管理器的增删改查,借阅,归还方法的在这里实现。一个DvdMenu类,DVD管理器的界面设计在这里面实现,包括显示菜单,选择菜单,返回菜单。最后一个DvdTest类,也就是主函数所在的类,实现对程序的测试。测试结果在文末也会展示^_^

DvdInfo的类:

package com.dvd;

public class DvdInfo {
	public String name;
	public int state;// (1:可借,0:不可借)
	public String date;

	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 String getDate() {
		return date;
	}

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

	public DvdInfo(int state, String name) {

		this.name = name;
		this.state = state;
	}

	public DvdInfo(int state, String name, String date) {
		this.name = name;
		this.state = state;
		this.date = date;

	}

	public DvdInfo() {

	}
}
DvdOperate类:
package com.dvd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class DvdOperate {
	List<DvdInfo> list = new ArrayList<DvdInfo>();
	Scanner input = new Scanner(System.in);
	// 创建DVD对象
	DvdInfo d1 = new DvdInfo(1, "罗马假日", "2013-7-1");
	DvdInfo d2 = new DvdInfo(0, "风声鹤唳", "");
	DvdInfo d3 = new DvdInfo(0, "浪漫满屋", "");

	public void initial() { // 初始化集合
		list.add(d1);
		list.add(d2);
		list.add(d3);
	}

	public void addDVD() {
		System.out.println("--- >新增DVD");
		if (list.size() == 6) {
			System.out.println("对不起,货架已满");
		}
		System.out.println("请输入DVD名称:");
		String name = input.next();
		DvdInfo dvd01 = new DvdInfo(0, name, "");
		list.add(dvd01);// 通过list中的add方法添加DVD
		System.out.println("新增《" + name + "》成功!");
	}

	public void lookDVD() {
		System.out.println("---> 查看DVD");
		System.out.println("序号\t状态\t名称\t借出日期");
		Iterator<DvdInfo> it = list.iterator();
		int count = 0;
		while (it.hasNext()) {
			DvdInfo next = it.next();
			String state = (next.getState() == 0) ? "可借" : "不可借";// 三目运算符判断若状态是0则为可借,否则不可借
			count++;
			System.out.println(count + "\t" + state + "\t" + next.getName() + "\t" + next.getDate());
		}
	}

	public void delDVD() {
		System.out.println("--->删除DVD");
		System.out.println("可删除的DVD如下:");
		System.out.println("序号\t状态\t名称");
		Iterator<DvdInfo> it = list.iterator();
		int count = 0;// 序号初始化
		while (it.hasNext()) {
			DvdInfo dvd1 = (DvdInfo) it.next();
			if (dvd1.getName() != null) {
				if (dvd1.getState() == 0) {
					String state = (dvd1.getState() == 0) ? "可借" : "不可借";
					count++;
					System.out.println(count + "\t" + state + "\t" + dvd1.getName());
				}
			}

		}
		System.out.print("请选择要删除的DVD序号:");
		int id = input.nextInt();
		System.out.println("删除" + list.get(id).getName() + "成功");
		list.remove(id);// 调用list中的remove()删除相应索引的元素
	}

	public void borrowDVD() {
		System.out.println("--->借出DVD");
		boolean isborrow = false;
		System.out.println("可借的DVD如下:");
		System.out.println("序号\t状态\t名称");
		int count = 0;// 序号初始化
		Iterator<DvdInfo> it = list.iterator();
		while (it.hasNext()) {
			DvdInfo dvd3 = (DvdInfo) it.next();
			if (dvd3.getName() != null && dvd3.getState() == 0) {// 遍历list,DVD存在且状态为可借,输出DVD信息
				String state = (dvd3.getState() == 0) ? "可借" : "不可借";
				count++;
				System.out.println(count + "\t" + state + "\t" + dvd3.getName());
			}
		}
		System.out.println("请输入要借的DVD名称:");
		String name3 = input.next();
		Iterator<DvdInfo> ite = list.iterator();
		while (ite.hasNext()) {
			DvdInfo dvd4 = (DvdInfo) ite.next();
			if (dvd4.getName() != null && name3.equals(dvd4.getName()) && dvd4.getState() == 0) {// 如果需要借的DVD在list中存在且状态可借
				System.out.println("请输入借出的日期:");
				String date = input.next();
				dvd4.setDate(date);
				dvd4.setState(1);
				System.out.println("借出《" + dvd4.getName() + "》成功!");
				isborrow = true;
				break;
			}
		}
		if (!isborrow) {
			System.out.println("您要借出的DVD名称不存在,请您核实后再进行操作!!!");// 无此DVD提示
		}
	}

	public void returnDVD() {
		System.out.println("--->归还DVD");
		boolean isreturn = true;
		System.out.println("请输入归还的DVD名称:");
		String name4 = input.next();
		Iterator<DvdInfo> it = list.iterator();
		while (it.hasNext()) {
			DvdInfo dvd = (DvdInfo) it.next();
			if (dvd.getName() != null && name4.equals(dvd.getName()) && dvd.getState() == 1) {// 归还的DVD状态为不可借
				isreturn = true;
				System.out.println("请输入归还日期:");
				String returnDate = input.next();
				double charge = 0;
				SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");// 设置时间格式为年-月-日
				try {
					java.util.Date d1 = sd.parse(dvd.getDate());
					java.util.Date d2 = sd.parse(returnDate);
					charge = (d2.getTime() - d1.getTime()) / (24 * 3600 * 1000); // 计算租金
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("归还成功!");
				System.out.println("借出日期为:" + dvd.getDate());
				System.out.println("归还日期为:" + returnDate);
				System.out.println("应付租金(元):" + charge);
				dvd.setDate(null);// 归还之后设置时间为空
				dvd.setState(0);// 归还后设置状态为可借
				isreturn = false;
			} else if (dvd.getName() != null && name4.equals(dvd.getName()) && dvd.getState() == 0) {
				isreturn = false;
				System.out.println("此DVD未借出,归还失败!");
				break;
			}
		}
		if (isreturn) {
			System.out.println("您要归还的DVD不在借出之列,请您核对后再进行操作!!!");
		}
	}
}

DvdMenu类:

package com.dvd;

import java.text.ParseException;
import java.util.Scanner;

public class DvdMenu {
	//显示菜单
	public static void showMenu(DvdOperate dvd) throws ParseException {
		System.out.println("欢迎使用迷你DVD管理器");
		System.out.println("--------------------------------------------");
		System.out.println("1.新增DVD");
		System.out.println("2.查看DVD");
		System.out.println("3.删除DVD");
		System.out.println("4.借出DVD");
		System.out.println("5.归还DVD");
		System.out.println("6.退出DVD");
		System.out.println("--------------------------------------------");
		choiceMenu(dvd);
	}

	// 选择菜单
	public static void choiceMenu(DvdOperate dvd) throws ParseException {

	        System.out.print("请选择:");
	        Scanner input = new Scanner(System.in);
	        int ch = input.nextInt();
	        switch (ch) {
	        // 新增
	        case 1:
	            dvd.addDVD();
	            returnMenu(dvd);
	            break;
	        // 查看
	        case 2:
	            dvd.lookDVD();
	            returnMenu(dvd);
	            break;
	        // 删除
	        case 3:
	            dvd.delDVD();
	            returnMenu(dvd);
	            break;
	        // 借出
	        case 4:
	            dvd.borrowDVD();
	            returnMenu(dvd);
	            break;
	        // 归还
	        case 5:
	            dvd.returnDVD();
	            returnMenu(dvd);
	            break;
	        // 退出
	        case 6:
	            System.out.println("谢谢使用!");
	            input.close();//输入流关闭
	            break;
	        default:
	            System.out.println("输入有误!");
	            returnMenu(dvd);
	        }
}
	public static void returnMenu(DvdOperate dvd) throws ParseException {
        System.out.print("输入0返回:");
        Scanner input = new Scanner(System.in);
        int i = input.nextInt();
        if (i == 0) {
            showMenu(dvd);
        } else {
            System.out.println("系统退出!");
            input.close();
        }
    }
}

DVDTest类:

package com.dvd;

import java.text.ParseException;

public class DVDTest {
public static void main(String[] args) throws ParseException {
	DvdOperate dvd = new DvdOperate();
	dvd.initial();
	DvdMenu.showMenu(dvd);
}
}

测试结果:

增加DVD:

查看DVD:


删除DVD:

借出DVD:

归还DVD:

退出程序:

  • 7
    点赞
  • 23
    收藏
  • 打赏
    打赏
  • 2
    评论
<p><span style="max-width: 100%; box-sizing: border-box; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff; color: #ff0000;"><span style="max-width: 100%; box-sizing: border-box; font-size: 16px;"><strong>Socket编程系列初步计划:</strong></span></span><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Socket编程系列之1:</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Linux-API</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">网络编程入门实战</strong><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Socket编程系列之2:</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Windows-API</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">网络编程入门实战</strong><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Socket编程系列之3:</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Muduo</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">网络编程入门实战</strong><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Socket编程系列之4:</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Libevent</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">网络编程入门实战</strong><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Socket编程系列之5:</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Libcurl</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">网络编程入门实战</strong><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Socket编程系列之6:</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">Boost-ASIO</strong><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> </span><strong style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">网络编程入门实战</strong><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><span style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;">----------------------------------------------------</span><br style="color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;" /><span style="max-width: 100%; box-sizing: border-box; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff; color: #0000ff;"><span style="max-width: 100%; box-sizing: border-box; font-size: 18px;"><strong>Socket编程系列之1:Linux-API网络编程入门实战</strong></span></span></p> <ol style="list-style: outside none none; margin: 0px; padding: 0px; color: #333333; font-family: 'Hiragino Sans GB', 'Microsoft Yahei', arial, 宋体, 'Helvetica Neue', Helvetica, STHeiTi, sans-serif; font-size: 14px; background-color: #ffffff;"> <li style="margin: 0px; padding: 0px;" value="NaN">Linux编程环境</li> <li style="margin: 0px; padding: 0px;" value="NaN">Linux文件系统</li> <li style="margin: 0px; padding: 0px;" value="NaN">TCP/IP协议简介</li> <li style="margin: 0px; padding: 0px;" value="NaN">应用层协议简介</li> <li style="margin: 0px; padding: 0px;" value="NaN">TCP网络编程基础</li> <li style="margin: 0px; padding: 0px;" value="NaN">IO复用机制</li> <li style="margin: 0px; padding: 0px;" value="NaN">UDP网络编程基础</li> <li style="margin: 0px; padding: 0px;" value="NaN">UDP单播多播广播</li> <li style="margin: 0px; padding: 0px;" value="NaN">套接字选项</li> <li style="margin: 0px; padding: 0px;" value="NaN">原始套接字与洪水攻击</li> <li style="margin: 0px; padding: 0px;" value="NaN">网络服务器选型分析</li> <li style="margin: 0px; padding: 0px;" value="NaN">http手撕代码与流程</li> <li style="margin: 0px; padding: 0px;">TCP滑动窗口机制分析</li> </ol> <p> </p>

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论 2

打赏作者

TNTZS666

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值