预约控制台应用程序

class Booking
    {
        private string datetime, people, time, week;
        string path = "D:\\Booking.txt";
        StreamWriter sw;
        StreamReader sr;
        public void addbook()
        {

            Console.Write("请输入约定日期<YYYY-MM-DD>:");
            datetime = Console.ReadLine();
            Console.Write("预约人:");
            people = Console.ReadLine();
            Console.Write("请输入约定时间:");
            time = Console.ReadLine();
            Console.Write("请输入约定星期几:");
            week = Console.ReadLine();

            FileInfo fi = new FileInfo(path);
            sw = fi.AppendText();
            sw.Write(datetime + "\t");
            sw.Write(people + "\t");
            sw.Write(time + "\t");
            sw.Write(week);
            sw.WriteLine("\t");
            sw.Close();
            Console.WriteLine("写入成功");


        }
        public void Viewbook()
        {
            sr = new StreamReader(path);
            Console.WriteLine("所有预约信息:");
            string matter = sr.ReadToEnd();
            Console.WriteLine("预约日期\t姓名\t时间\t星期几");
            Console.WriteLine(matter);
        }
        public void searchbook()
        {
            try
            {
                sr = new StreamReader(path);
                Console.Write("请输入你要查询的预定信息的日期:");
                string datebook = Console.ReadLine();
                string matter = sr.ReadLine();
                while (matter != null)
                {
                    if (datebook == (matter.Substring(0, 10)))
                    {
                        Console.WriteLine(matter);
                    }
                    matter = sr.ReadLine();

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void Exitbook()
        {

        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Booking bk = new Booking();
            Console.WriteLine("1.添加预约信息");
            Console.WriteLine("2.显示所有预约信息");
            Console.WriteLine("3.查询信息");
            Console.WriteLine("4.Exit");
            Console.Write("请选择(1-4):");
            try
            {
                int num = int.Parse(Console.ReadLine());
                switch (num)
                {
                    case 1: bk.addbook(); break;
                    case 2: bk.Viewbook(); break;
                    case 3: bk.searchbook(); break;
                    case 4: bk.Exitbook(); break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }

posted @ 2012-02-23 15:57  ComBat  阅读(200)  评论(0编辑  收藏  举报