本篇内容介绍了“怎么用C#实现DVD借出归还管理系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一共分为三个类分别是:DVD(启动类),XinXi(信息类),GongNeng(功能类)
代码部分(如下):
DVD(启动类):
class DVD
{
static void Main(string[] args)
{
GongNeng gongNeng = new GongNeng();
gongNeng.initial(); //初始化
gongNeng.XianShi(); //遍历初始化信息
gongNeng.CaiDan(); //选项菜单
}
}
XinXi(信息类):
class XinXi
{
private string _name;//名称
private int state;//借出状态
private string date;//时间
//属性封装
public string Name { get => _name; set => _name = value; }//名称
public int State { get => state; set => state = value; }//借出状态
public string Date { get => date; set => date = value; }//时间
}
GongNeng(功能类):
因为功能类使用了正则表达式来判断日期格式,所以在使用的时候头部还需加上:
using System.Text.RegularExpressions; // RegularExpressions为正则表达式,Text为文本
class GongNeng
{
XinXi[] xinxi = new XinXi[10];//对象数组存储数据
//初始化信息
#region 初始化信息
public void initial() {
xinxi[0] = new XinXi();
xinxi[0].Name = "罗马假日";
xinxi[0].State = 0;
xinxi[0].Date = "2010-7-1";
xinxi[1] = new XinXi();
xinxi[1].Name = "风声鹤唳";
xinxi[1].State = 1;
xinxi[1].Date = "";
xinxi[2] = new XinXi();
xinxi[2].Name = "浪漫满屋";
xinxi[2].State = 1;
xinxi[2].Date = "";
}
#endregion
//显示初始化信息
#region 显示初始化信息
public void XianShi() {
Console.WriteLine("********初始化信息如下:********");
foreach (XinXi item in xinxi) {
if (item != null) {
Console.WriteLine(item.Name+"\t"+item.State+"\t"+item.Date);
}
}
Console.WriteLine("********************************");
}
#endregion
//DVD菜单
#region DVD菜单
public void CaiDan()
{
Console.WriteLine("------------欢迎使用明宇迷你DVD管理器------------");
Console.WriteLine("1.新增DVD\n2.查看DVD\n3.删除DVD\n4.借出DVD\n5.归还DVD\n6.退出");
Console.WriteLine("-------------------------------------------------");
Console.Write("请输入您的选择:");
int xuanZe = int.Parse(Console.ReadLine());
switch (xuanZe)
{
case 1:
//新增DVD
XinZeng();
break;
case 2:
//查看DVD
ChaXun();
break;
case 3:
//删除DVD
ShanChu();
break;
case 4:
//借出DVD
JieChu();
break;
case 5:
//归还DVD
GuiHuan();
break;
case 6:
//退出
Console.WriteLine("欢迎下次光临!");
break;
default:
//无选项
Console.WriteLine("对不起,没有该选项!");
break;
}
}
#endregion
//输入0,返回
#region 输入0,返回
public void FanHui() {
do {
Console.WriteLine("输入0,返回:");
string Ling = Console.ReadLine();
if (Ling.Equals("0")) {
CaiDan();
}
} while (true);
}
#endregion
//新增DVD
#region 新增DVD
public void XinZeng() {
bool flag = true;
Console.WriteLine("此处实现新增DVD----->");
Console.WriteLine("请输入DVD名称:");
string dvdName = Console.ReadLine();
for (int i=0; i< xinxi.Length; i++) {
if (xinxi[i] == null) {
flag = false;
xinxi[i] = new XinXi();
xinxi[i].Name = dvdName;
xinxi[i].State = 1;
xinxi[i].Date = "";
Console.WriteLine("名称为{0}的DVD添加成功!", xinxi[i].Name);
break;
}
}
if (flag) {
Console.WriteLine("对不起,存储空间已满");
}
FanHui();//返回
}
#endregion
//查询DVD
#region 查询DVD
public void ChaXun() {
Console.WriteLine("此处实现查询DVD----->");
Console.WriteLine("{0,-8}{1,-10}{2,-8}", "名称","状态","时间");
string zhuangTai = String.Empty;
foreach (XinXi item in xinxi)
{
if (item != null)
{
if (item.State == 0) {
zhuangTai = "以借出";
} else if (item.State == 1) {
zhuangTai = "未借出";
}
Console.WriteLine("{0,-8}{1,-10}{2,-8}",item.Name, zhuangTai, item.Date);
}
}
FanHui();//返回
}
#endregion
//判断对应下标
#region 判断对应下标
public XinXi Duan(string dvdName) {
foreach (XinXi item in xinxi) {
if (item != null && item.Name.Equals(dvdName)) {
return item;
}
}
return null;
}
#endregion
//删除DVD
#region 删除DVD
public void ShanChu() {
Console.WriteLine("此处实现删除DVD----->");
Console.WriteLine("请输入DVD名称:");
string dvdName = Console.ReadLine();
XinXi renWu = Duan(dvdName);
if (renWu == null) {
Console.WriteLine("对不起,没有该DVD");
return;
}
for (int i=0; i< xinxi.Length; i++) {
if (renWu == xinxi[i] && xinxi[i].State == 1) {
for (int j = i; j < xinxi.Length-1; j++) {
xinxi[j] = xinxi[j + 1];
}
int nu = xinxi.Length - 1;
xinxi[nu] = null;
Console.WriteLine("订单删除成功!");
break;
} else if (renWu == xinxi[i] && xinxi[i].State == 0) {
Console.WriteLine("对不起,订单为以借出状态不能删除!");
break;
}
}
FanHui();//返回
}
#endregion
//借出DVD
#region 借出DVD
public void JieChu() {
Console.WriteLine("此处实现借出DVD----->");
Console.WriteLine("请输入DVD名称:");
string dvdName = Console.ReadLine();
XinXi renWu = Duan(dvdName);
if (renWu == null)
{
Console.WriteLine("对不起,没有该DVD");
FanHui();//返回
}
if (renWu.State == 0) {
Console.WriteLine("对不起,该DVD以被人抢先借走了!");
FanHui();//返回
}
Console.WriteLine("请输入借出日期(年-月-日):");
string riQi = Console.ReadLine();
bool flag = Money(riQi);
if (!flag)
{
Console.WriteLine("对不起,您输入的日期不正确!");
FanHui();//返回
}
else {
renWu.State = 0;
renWu.Date = riQi;
Console.WriteLine("借出DVD成功!");
}
FanHui();//返回
}
#endregion
//判断借出日期格式
#region 判断借出日期格式
public bool Money(string riQi)
{
string monval = @"^\d{4}-\d{1,2}-\d{1,2}$";
return Regex.IsMatch(riQi, monval);
}
#endregion
//归还DVD
#region 归还DVD
public void GuiHuan() {
Console.WriteLine("此处实现归还DVD----->");
Console.WriteLine("请输入DVD名称:");
string dvdName = Console.ReadLine();
XinXi renWu = Duan(dvdName);
if (renWu == null)
{
Console.WriteLine("对不起,没有该DVD");
FanHui();//返回
}
if (renWu.State == 1)
{
Console.WriteLine("对不起,该DVD还没有借出不可归还!");
FanHui();//返回
}
Console.WriteLine("请输入归还日期(年-月-日):");
string riQi = Console.ReadLine();
bool flag = Money(riQi);
if (!flag)
{
Console.WriteLine("对不起,您输入的日期不正确!");
FanHui();//返回
}
else
{
string jieChu = renWu.Date;
if (String.Compare(jieChu, riQi) < 0)
{
//字符串转换为日期格式
DateTime dat1 = Convert.ToDateTime(jieChu);//借出
DateTime dat2 = Convert.ToDateTime(riQi);//日期
TimeSpan span = dat2.Subtract(dat1);
//求借出与归还之间的差值
int cha = span.Days + 1;
renWu.State = 1;
renWu.Date = "";
Console.WriteLine("\n归还《{0}》成功!", renWu.Name);
Console.WriteLine("借出日期为:{0}", dat1);
Console.WriteLine("归还日期为:{0}", dat2);
Console.WriteLine("应付租金为:{0}",cha);
}
else {
Console.WriteLine("对不起,归还日期不能小于借出日期!");
FanHui();//返回
}
}
FanHui();//返回
}
#endregion
}
代码展示完毕!
“怎么用C#实现DVD借出归还管理系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!