妙用goto语句实现模拟ATM交易系统的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Banking
{
    class Program
    {
        static void Main(string[] args)
        {
            string Continue="";//用来保存是否还需要交易标志
            Account account1 = new Account();
            account1.Balance = 1000;//初始化账户余额

            Start:
            Console.Clear();//清屏
            Console.WriteLine("请选择你需要进行的交易:");
            Console.WriteLine("A、存款  B、取款 C、退卡");
            string deal = Console.ReadLine().Trim().ToUpper();
            switch (deal)
            {
                case "A":
                    goto Deposit;
                    break;
                case "B":
                    goto WithDraw;
                    break;
                default:
                    goto Finished;
                    break;
            }

          
        WithDraw:
            Console.Clear();
            Console.WriteLine("温馨提示:目前你正在进行取款交易!");
            Console.WriteLine("目前你账户余额为:" + account1.Balance+"元");          
            Console.WriteLine("请输入你取款的数目:");
             WithDrawInput:int draw = int.Parse(Console.ReadLine());
            if (!account1.WithDraw(draw))
            {
                goto WithDrawInput;
            }
            Console.WriteLine("取款交易成功后,你目前的账户余额为:" + account1.Balance+"元");
            Console.WriteLine("是否还需要继续交易:Y:继续取款交易,N:返回");
            Continue = Console.ReadLine().Trim().ToUpper(); ;
            if (Continue.Equals("Y"))
            {
                goto Deposit;
            }
            else
            {
                goto Start;
            }          

        Deposit:
            Console.Clear();
            Console.WriteLine("温馨提示:目前你正在进行存款交易!");
            Console.WriteLine("目前你账户余额为:" + account1.Balance + "元");        
            Console.WriteLine("请输入你存款数目:");
            DepositInput: int deposit = int.Parse(Console.ReadLine());
            if (!account1.Deposit(deposit))
            {
                goto DepositInput;
            }
            Console.WriteLine("存款交易成功后,你目前的账户余额为:" + account1.Balance+"元");
            Console.WriteLine("是否还需要继续交易:Y:继续存款交易,N:返回");
            Continue= Console.ReadLine().Trim().ToUpper();
            if (Continue.Equals("Y"))
            {
                goto Deposit;
            }
            else
            {
                goto Start;
            }
            Finished:
            Console.Clear();      
            Console.WriteLine("谢谢合作,欢迎再次使用!");
           
            Console.Read();
        }
    }
}

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Banking
{
    class Account
    {
        private int balance;

        public int Balance
        {
            get { return balance; }
            set            
            {
                if (value<=0)
                {
                    this.balance = 0;
                }
                else
                {
                    this.balance = value;
                }
            }
        }

        public bool Deposit(int deposit)
        {

            if (deposit<=0)
            {
                Console.WriteLine("输入的存款数目不能为负数,请重新输入:");
                return false ;
            }
            Mutex mutex = new Mutex(true, "Mutex1");
            mutex.WaitOne();
            this.balance += deposit;
            mutex.Close();
            Console.WriteLine("系统正在处理,请耐心等待...");
            Thread.Sleep(3000);
            Console.WriteLine("存款交易成功!");
            Thread.Sleep(2000);
            return true;
        }

        public bool WithDraw(int draw)
        {
            if (draw<0)
            {
                Console.WriteLine("输入的取款数目不能为负数,请重新输入:");
                return false;
            }
            if (draw>this.balance)
            {
                Console.WriteLine("你的余额不足以支付你的申请金额,请重新输入:");
                return false;
            }
            Mutex mutex = new Mutex(true, "Mutex2");
            mutex.WaitOne();
            this.balance -= draw;
            mutex.Close();
            Console.WriteLine("系统正在处理,请耐心等待...");
            Thread.Sleep(3000);
            Console.WriteLine("取款交易成功!");
            Thread.Sleep(2000);
            return true;
        }


    }
}

 

posted @ 2013-04-16 18:29  Predator  阅读(187)  评论(0编辑  收藏  举报