using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace WindowsApplication1
{
public class Employee:IComputeProfit
{
string name;
Dictionary<string, Customer> dic = new Dictionary<string, Customer>();
public string Name
{
get { return name; }
set { name = value; }
}
public bool AddCustomer(Customer c)
{
dic.Add(c.Name, c);
return true;
}
public void Print()
{
try
{
FileStream fs = new FileStream(string.Format("{0}.txt", this.Name), FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
writer.WriteLine("{0}的客户列表,客户的个数为:{1}", this.Name, dic.Count);
writer.WriteLine("-------------------------------------------------------");
foreach (Customer c in dic.Values)
{
writer.WriteLine("客户姓名:{0}", c.Name);
writer.WriteLine("客户类型:{0}", c.Leval);
}
writer.Close();
fs.Close();
}
catch
{
}
}
public Customer GetCustomerByName(string customerName)
{
if (dic.ContainsKey(customerName))
return dic[customerName];
else
return null;
}
#region IComputeProfit 成员
public decimal ComputeProfit()
{
decimal d = 115;
d = d + 100 * dic.Count;
return d;
}
#endregion
}
}
---------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
public enum CustomerType
{
黄金,白金,钻石
}
public class Customer:IComputeProfit
{
string name;
CustomerType leval;
public CustomerType Leval
{
get { return leval; }
set { leval = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
#region IComputeProfit 成员
public decimal ComputeProfit()
{
switch (this.Leval)
{
case CustomerType.黄金:
return 125.35;
break;
case CustomerType.白金:
return 175;
break;
case CustomerType.钻石:
return 250;
break;
}
}
#endregion
}
}
----------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
public static class Manager
{
static Dictionary<string, Employee> dic = new
Dictionary<string, Employee>();
public static Dictionary<string, Employee> Dic
{
get { return Manager.dic; }
}
public static bool AddEmployee(Employee e)
{
dic.Add(e.Name, e);
return true;
}
public static bool Service(string employeeName,Customer c)
{
if (dic.ContainsKey(employeeName))
{
dic[employeeName].AddCustomer(c);
return true;
}
else
return false;
}
public static Employee GetEmployeeByName(string employeeName)
{
if (dic.ContainsKey(customerName))
return dic[customerName];
else
return null;
}
}
}
我现在就是不懂到底这个键值对的搜索问题. 尤其是这个employeeName这个键 根本就是没声明过 但是ContainsKey又是如何去查询、? 可能我脑子有点迷糊。 希望有个明白人 帮我详细讲讲泛型集合问题 3Q