C#實現DVD借出歸還管理系統

用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
    }

代碼展示完畢!

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: