好友
阅读权限10
听众
最后登录1970-1-1
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiniDvd
{
class DVDMgr
{
请大神指教,我这代码有没有什么毛病,还能不能简化一点!
//创建对象
DVDSet set = new DVDSet();
#region 初始化DVD信息
public void Initial()
{
set.name[0] = "罗马假日";
set.state[0] = 0;
set.time[0] = "2010-7-1";
set.count[0] = 1;
set.name[1] = "风声鹤唳";
set.state[1] = 1;
set.name[2] = "浪漫满屋";
set.state[2] = 1;
}
#endregion
#region 菜单显示和切换
public void StarMenu()
{
Console.WriteLine("欢迎使用迷你DVD管理器");
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine("0:借出排行榜");
Console.WriteLine("1:新增DVD");
Console.WriteLine("2:查看DVD");
Console.WriteLine("3:删除DVD");
Console.WriteLine("4:借出DVD");
Console.WriteLine("5:归还DVD");
Console.WriteLine("6:退出");
Console.WriteLine("---------------------------------------------------------");
Console.Write("请选择:");
int chose = int.Parse(Console.ReadLine());
bool tuichu=false;
switch (chose)
{
case 0:
Console.WriteLine("--->排行榜");
Console.WriteLine("*******************************");
PaiHang();//调用排行榜的方法
break;
case 1:
Console.WriteLine("--->新增DVD");
Console.WriteLine("*******************************");
Add();//调用新增DVD方法
break;
case 2:
Console.WriteLine("--->查看DVD");
Console.WriteLine("*******************************");
Search();//调用查看DVD的方法
break;
case 3:
Console.WriteLine("--->删除DVD");
Console.WriteLine("*******************************");
ShanChu();//调用删除DVD的方法
break;
case 4:
Console.WriteLine("--->借出DVD");
Console.WriteLine("*******************************");
Jie();
break;
case 5:
Console.WriteLine("--->归还DVD");
Console.WriteLine("*******************************");
ReturnDvd();
break;
case 6:
Console.WriteLine("感谢您的使用!");
tuichu = true;
break;
}
if(!tuichu)
ReturnMain();
}
#endregion
#region 返回菜单方法
public void ReturnMain()
{
Console.WriteLine("*******************************");
Console.Write("输入0返回:");
int fanhui = int.Parse(Console.ReadLine());
if (fanhui == 0)
{
StarMenu();
}
else
{
Console.WriteLine("输入错误,程序结束!");
}
}
#endregion
#region 查看DVD
public void Search()
{
Console.WriteLine("序号\t\t状态\t\t名称\t\t借出日期");
for (int i = 0; i < set.name.Length; i++)
{
if (set.name[i] != null)
{
string state = "";
if (set.state[i] == 0) //判断DVD是否借出
{
state = "已借出";
}
else
{
state = "可借";
}
Console.WriteLine("{0}\t\t{1}\t\t{2}\t{3}", i + 1, state, set.name[i], set.time[i]);
}
}
}
#endregion
#region 新增DVD
public void Add()
{
Console.Write("请输入DVD名称:");
string name = Console.ReadLine();
bool add_dvd = false; //false表示添加失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name[i] == null)
{
set.name[i] = name;
Console.WriteLine("新增《{0}》成功!",name);
set.state[i] = 1;
add_dvd = true;
break;
}
}
if (!add_dvd)
{
Console.WriteLine("新增DVD失败!");
}
}
#endregion
#region 删除DVD
public void ShanChu()
{
Console.Write("请输入名称:");
string name = Console.ReadLine();
bool shanchu_dvd = false; //false 表示删除失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name[i]==name && set.state[i] == 1)
{
Console.WriteLine("删除成功!");
shanchu_dvd = true;
int num = i;
for (int j = i; i < set.name.Length; j++)
{
if (set.name[j + 1] == null)
{
break;
}
set.name[j] = set.name[j + 1];
set.state[j] = set.state[j + 1];
set.time[j] = set.time[j + 1];
num++;
}
set.name[num] = null;
set.state[num] = 0;
set.time[num] = null;
set.count[num] = 0;
break;
}
}
if (!shanchu_dvd)
{
Console.WriteLine("删除失败,《{0}》不存在或者已经被借出", name);
}
}
#endregion
#region 借出DVD
public void Jie()
{
Console.Write("请输入DVD名称:");
string name = Console.ReadLine();
Console.Write("请输入借出日期<年-月-日>");
string time = Console.ReadLine();
bool jie_dvd = false; //false表示借出失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name[i] == name && set.state[i] == 1)
{
Console.WriteLine("借出《{0}》成功!",name);
jie_dvd = true;
set.state[i] = 0;
set.time[i] = time;
set.count[i]++;
break;
}
else if (set.name[i] == name && set.state[i] == 0)
{
Console.WriteLine("借出《{0}》失败,该DVD已经被借出!",name);
jie_dvd = true;
break;
}
}
if (!jie_dvd)
{
Console.WriteLine("借出《{0}》失败,不存在该DVD",name);
}
}
#endregion
#region 归还DVD
public void ReturnDvd()
{
Console.Write("请输入DVD名称:");
string name = Console.ReadLine();
Console.Write("请输入归还日期(年-月-日):");
string time = Console.ReadLine();
bool guihuan_dvd = false; //false表示归还失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name[i] == name && set.state[i] == 0)
{
Console.WriteLine("归还《{0}》成功!",name);
DateTime time_jie = DateTime.Parse(set.time[i]);
DateTime time_guihuan = DateTime.Parse(time);
int days = (time_guihuan - time_jie).Days; //计算天数差
Console.WriteLine("借出日期为{0}",set.time[i]);
Console.WriteLine("归还日期为{0}",time);
Console.WriteLine("应付租金:{0}元(2元/天)",days*2);
set.state[i] = 1;
set.time[i] = null;
guihuan_dvd = true;
break;
}
else if (set.name[i] == name && set.state[i] == 1)
{
Console.WriteLine("归还《{0}》失败,DVD没有被借出",name);
guihuan_dvd = true;
break;
}
}
if (!guihuan_dvd)
{
Console.WriteLine("归还《{0}》失败,DVD不存在!",name);
}
}
#endregion
#region 排行榜
public void PaiHang()
{
Console.WriteLine("次数\t\t名称");
for (int i = 0; i < set.count.Length - 1; i++) //外层循环控制轮数
{
for (int j = 0; j <set.count.Length- 1 - i; j++)
{
if (set.count[j] < set.count[j + 1])
{
int temp_count = set.count[j];
set.count[j] = set.count[j + 1];
set.count[j + 1] = temp_count;
string temp_name = set.name[j];
set.name[j] = set.name[j + 1];
set.name[j + 1] = temp_name;
int temp_state = set.state[j];
set.state[j] = set.state[j + 1];
set.state[j + 1] = temp_state;
string temp_time = set.time[j];
set.time[j] = set.time[j + 1];
set.time[j + 1] = temp_time;
}
}
}
for (int i = 0; i < set.name.Length; i++)
{
if (set.name[i] != null)
{
Console.WriteLine("{0}\t\t{1}",set.count[i],set.name[i]);
}
}
}
#endregion
}
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|