2011-1-18

 最后,我们练习了下校园饭卡管理系统

在这个系统中,包括办卡、充值、消费、挂失、注销,在办卡时,我们同过使用ID、姓名、充值金额三项,把ID和姓名做为唯一标准,也就是说ID和姓名都是唯一的,并且在这些步骤中,我们要在完成操作是,把相应的记录写在一个文件中;在办卡时,我们先要读取记录,来判定上次办的卡的最后一个ID是多少,然后来确定这一个ID,之前我们要先定义一个ID=1,当我们读取时若发现记录为空,便以定义的这一个作为第一个ID,如果我们从记录中找到了ID,则把这个读到的ID+1再赋值给定义这个ID,这是办卡的过程,在充值时,因为无法用刷卡的方式,所以通过输入ID来充值,卡的挂失不影响充值;在消费时,我们也是通过输入ID,然后输入消费金额,这是系统会判断该卡是否已经挂失,若没有挂失,则进行消费,因为每一次操作都有记录,挂失的卡我们会在最后加上挂失两个字,所以,判断时通过判断转变后记录中的元素个数,来确定该卡是否已挂失;在挂失时,假如也忘记了ID,则通过姓名来找你的记录,最后再这个记录最后加上挂失两给字;在注销时,我们把这个卡的记录清空,再把其他的记录写进去,最终实现;详细代码如下
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                Console.WriteLine("欢迎******使用");
                Console.WriteLine("1、办卡   2、充值    3、消费   4、挂失    5、注销");
                string num = Console.ReadLine();
                switch (num)
                {
                    case "1":
                        OpenCard();
                        break;
                    case "2":
                        AddMoney();
                        break;
                    case "3":
                        CostMoney();
                        break;
                    case "4":
                        LostCard();
                        break;
                    case "5":
                        DeleteCard();
                        break;
                }
                System.Threading.Thread.Sleep(2000);
                Console.Clear();
            }
        }

        private static void DeleteCard()
        {
            Console.WriteLine("请输入饭卡ID:");
            string id = Console.ReadLine();
            string[] cards = File.ReadAllLines(path);

            bool mark = false;
            for (int i = 0; i < cards.Length; i++)
            {
                string[] findcard = cards[i].Split('|');
                if (id.Trim() == findcard[0].Trim())
                {
                    cards[i] = "";
                    Console.WriteLine("注销成功!");
                    mark = true;
                    break;
                }
            }

            if (!mark)
            {
                Console.WriteLine("卡号不存在!");
                return;
            }

            File.WriteAllText(path, "");
            foreach (string card in cards)
            {
                if (card != "")
                {
                    File.AppendAllText(path, card + "\r\n");
                }
            }
        }

 

 

        private static void LostCard()
        {
            Console.WriteLine("请输入姓名:");
            string name = Console.ReadLine();
            string[] cards = File.ReadAllLines(path);
            for (int i = 0; i < cards.Length; i++)
            {
                string[] findcard = cards[i].Split('|');
                if (name.Trim() == findcard[1].Trim())
                {
                    cards[i] += "|挂失";
                    Console.WriteLine("挂失成功!");
                    break;
                }
            }
            File.WriteAllLines(path, cards);
        }

 

 

 


        private static void CostMoney()
        {
            Console.WriteLine("请输入饭卡ID:");
            string id = Console.ReadLine();
            string[] cards = File.ReadAllLines(path);

            for (int i = 0; i < cards.Length; i++)
            {
                string[] findcard = cards[i].Split('|');//1|李三|100|挂失
                if (id.Trim() == findcard[0].Trim())
                {
                    if (findcard.Length > 3)
                    {
                        Console.WriteLine("此卡已挂失!");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("请输入消费金额:");
                        float oldaccount = float.Parse(findcard[2]);
                        float newaccount = float.Parse(Console.ReadLine());

                        if (newaccount > oldaccount)
                        {
                            Console.WriteLine("卡余额不足,请充值!");
                            break;
                        }
                        else
                        {
                            cards[i] = "";
                            for (int j = 0; j < findcard.Length; j++)
                            {
                                if (j == 2)
                                {
                                    cards[i] += (oldaccount - newaccount) + "|";
                                }
                                else
                                {
                                    cards[i] += findcard[j] + "|";
                                }
                            }
                            cards[i] = cards[i].TrimEnd('|');
                            Console.WriteLine("消费成功,余额为:{0}", oldaccount - newaccount);
                            break;
                        }
                    }
                }
            }
            File.WriteAllLines(path, cards);
        }

        private static void AddMoney()
        {
            Console.WriteLine("请输入饭卡ID:");
            string id = Console.ReadLine();
            string[] cards = File.ReadAllLines(path);

            for (int i = 0; i < cards.Length; i++)
            {
                string[] findcard = cards[i].Split('|');//1|李三|100|挂失
                if (id.Trim() == findcard[0].Trim())
                {
                    Console.WriteLine("请输入充值金额:");
                    float oldaccount = float.Parse(findcard[2]);
                    float newaccount = float.Parse(Console.ReadLine());

                    cards[i] = "";
                    for (int j = 0; j < findcard.Length; j++)
                    {
                        if (j == 2)
                        {
                            cards[i] += (oldaccount + newaccount) + "|";
                        }
                        else
                        {
                            cards[i] += findcard[j] + "|";
                        }
                    }

                    cards[i] = cards[i].TrimEnd('|');


                    Console.WriteLine("充值成功,余额为:{0}", oldaccount + newaccount);
                    break;
                }
            }
            File.WriteAllLines(path, cards);

        }
        //1|李三|100|挂失
        //
        const string path = "D:/cards.txt";
        private static void OpenCard()
        {
            int cardNumber = 1;
            if (File.Exists(path))
            {
                string[] cards = File.ReadAllLines(path);
                if (cards.Length != 0)
                {
                    string lastcard = cards[cards.Length - 1];
                    string[] subcard = lastcard.Split('|');
                    cardNumber = int.Parse(subcard[0]) + 1;
                }
            }
            Console.WriteLine("请输入姓名:");
            string name = Console.ReadLine();
            Console.WriteLine("请输入充值金额:");
            string money = Console.ReadLine();
            string newcard = cardNumber + "|" + name + "|" + money + "\r\n";
            File.AppendAllText(path, newcard);
            Console.WriteLine("开卡成功!");
        }

 

    }
}