AccountManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AccountManager
{
    class Account
    {
        public Account()
        {
 
        }
        public Account(string name,string account)
        {
            this.Name = name;
            this.AccountNumber = account;
        }
        public string Name { getset; }
        public string AccountNumber { getset; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace AccountManager
{
    //帐号事件声明
    class AccountEventArgs:EventArgs
    {
        public AccountEventArgs()
        {
 
        }
        public AccountEventArgs(string name)
        {
            this.Name = name;
        }
        public string Name { getset; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace AccountManager
{
    class AccountManager
    {
        private List<Account> listAccounts=new List<Account>();
        //使用SingleTon单例模式,
        private static AccountManager instance;
        public static AccountManager Instance
        {
            get 
            {
                if (instance==null)
                {
                    instance = new AccountManager();
                }
                return AccountManager.instance;
            }
            
        }
        //构造函数设为私有,外部无法创建实例,保证在整个应用程序中只有一个AccountManager实例
        private AccountManager()
        {
 
        }
        //帐号事件声明,delegate匿名函数初始化,在调用的时候无需判断该事件是否为空
        public event EventHandler<AccountEventArgs> AccountArise = delegate { };
        public void AddAccount(string name,string number)
        {
            Account account = new Account(name, number);
            this.listAccounts.Add(account);
            AccountArise(thisnew AccountEventArgs(name));
        }
        public void ShowAccounts()
        {
            int count = this.listAccounts.Count;
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("姓名:" + listAccounts[i].Name + "\t帐号:" + listAccounts[i].AccountNumber);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace AccountManager
{
    class Program
    {
        static void Main(string[] args)
        {
            //订阅添加账户事件处理函数
            AccountManager.Instance.AccountArise+=new EventHandler<AccountEventArgs>(Instance_AccountArise);
            AccountManager.Instance.AddAccount("ganquanfu""108253040226");
            AccountManager.Instance.AddAccount("ligang""108253040217");
            AccountManager.Instance.AddAccount("huanghejun""108253040209");
            Console.WriteLine("显示所有的账户信息");
            AccountManager.Instance.ShowAccounts();
            Console.Read();
 
        }
 
        static void  Instance_AccountArise(object sender, AccountEventArgs e)
        {
 	        Console.WriteLine("你刚刚添加的帐号姓名是:");
            string name=e.Name;
            Console.WriteLine(name);
        }
      
    }
    
}


posted @ 2013-04-23 23:07  Predator  阅读(485)  评论(0编辑  收藏  举报