名片管理系统
简单架构:
分析名片的特点和作用来设计类,以及用到的成员:
后台:
l 一张名片包含的内容:名称,公司名,地址,联系方式,职位,网址等信息,因此可以考虑把名片做成一个类,名片里的这些包含的内容做为名片类的字段。这些字段里面要限制格式可以考虑用正则表达式。
l 名片的作用:方便联系,联系方式就作为主要的内容了可能有电话,手机,QQ,msn,邮箱等各种联系方式中的一个或者几个,因此可以把联系方式分离出来 做一个泛型集合 实现动态的增删改查。又因为电话手机QQ这些联系方式中有共同的内容 也由自己不同的内容,可以把电话手机等做成一个类来继承联系方式,这里我们把联系方式分为及时通信方式包括MSN,QQ,Email;和电话类:包括手机,固话
l 名片的信息录入要由一个格式的限制,我们会多次限定异常,这里做一个异常类,凡是用到的地方依赖这个异常类。
l 因为我们这里是再做系统可以考虑给名片加一个类型 方便查找,名片的类型可以是名片的一部分但是查找起来不是很方便,也把名片类型作为一个类来处理。
这样我们的后台处理就由CardSet类,Card类,Iconnect接口,Phone抽象类,IM抽象类,Mobile类,Tel类,QQ类,MSN类,Email类组成。
下面介绍一下前台程序的布局和管理:
菜单的布局:1.查询名片 2.管理名片
其中查询又分为:1全部查,2按名称查询,3按分类查询3个子菜单
为了方便操作把名片的查询处理为一个类,类的内部写对名片查询的方法,这个方法最好定义为静态的,因为静态的方法可以直接用类名调用,
管理名片分为:1.添加名片 2.更新名片 3.删除名片
名片的添加和更新,删除都要面临联系人的添加和更新和删除,这里把联系人的管理也处理成一个类,对联系人进行管理,联系人的添加用到了抽象工厂(以后还会学)的思想,个人觉得很巧妙,我是想到不会这样用:
//添加联系方式
public static void AddConnect()
{
#region 添加联系方式
while (true)
{
IConnect connect = null;
//用do..while来控制循环
do
{
Console.WriteLine("添加联系方式:");
Console.WriteLine("1、手机 2、固定电话 3、QQ 4、邮箱地址");
switch (Console.ReadLine())
{
case "1":
connect = new Mobile();//直接把具体的对象用联系方式这个抽象的对象保存起来,就可以对connect的操作就是对所有不同类别的联系方式的操作
break;
case "2":
connect = new Tel();
break;
case "3":
connect = new QQ();
break;
case "4":
connect = new Email();
break;
default:
connect = null;
break;
}
if (connect != null)
{
Console.WriteLine("请输入{0}号码:", connect.Name);
connect.Number = Console.ReadLine();
}
} while (connect == null);//要考虑联系方式是否为空的情况
card.Connects.Add(connect);
Console.WriteLine("继续添加请按Y,否则请按其他键!");
if (Console.ReadLine().ToUpper() != "Y")
{
break;
}
} #endregion
}
MangeConnect:
/// <summary>
/// 联系方式管理
/// </summary>
static class ManageConnect
{
static Card card;
/// <summary>
/// 联系方式管理菜单
/// </summary>
/// <param name="card">名片</param>
public static void Menu(Card card)
{
ManageConnect.card = card;
Console.WriteLine("联系方式修改,请选择操作编号:");
Console.WriteLine("1、添加 2、修改 3、删除 4、退出");
switch (Console.ReadLine())
{
case "1":
AddConnect();
break;
case "2":
ModifyConnect();
break;
case "3":
RemoveConnect();
break;
case "4":
return;
}
}
/// <summary>
/// 添加联系方式
/// </summary>
public static void AddConnect()
{
#region 添加连系方式
while (true)
{
IConnect connect = null;
do
{
Console.WriteLine("添加联系方式:");
Console.WriteLine("1、手机 2、固定电话 3、QQ 4、邮箱地址");
switch (Console.ReadLine())
{
case "1":
connect = new Mobile();
break;
case "2":
connect = new Tel();
break;
case "3":
connect = new QQ();
break;
case "4":
connect = new Email();
break;
default:
connect = null;
break;
}
if (connect != null)
{
Console.WriteLine("请输入{0}号码:", connect.Name);
connect.Number = Console.ReadLine();
}
} while (connect == null);
card.Connects.Add(connect);
Console.WriteLine("继续添加请按Y,否则请按其他键!");
if (Console.ReadLine().ToUpper() != "Y")
{
break;
}
}
#endregion
}
/// <summary>
/// 添加联系方式
/// </summary>
/// <param name="card">名片</param>
public static void AddConnect(Card card)
{
#region 添加连系方式
while (true)
{
IConnect connect = null;
do
{
Console.WriteLine("添加联系方式:");
Console.WriteLine("1、手机 2、固定电话 3、QQ 4、邮箱地址");
switch (Console.ReadLine())
{
case "1":
connect = new Mobile();
break;
case "2":
connect = new Tel();
break;
case "3":
connect = new QQ();
break;
case "4":
connect = new Email();
break;
default:
connect = null;
break;
}
if (connect != null)
{
Console.WriteLine("请输入{0}号码:", connect.Name);
connect.Number = Console.ReadLine();
}
} while (connect == null);
card.Connects.Add(connect);
Console.WriteLine("继续添加请按Y,否则请按其他键!");
if (Console.ReadLine().ToUpper() != "Y")
{
break;
}
}
#endregion
}
/// <summary>
/// 修改联系方式
/// </summary>
public static void ModifyConnect()
{
int conindex = 1;//引入编号,根据编号来修改联系方式
foreach (IConnect connect in card.Connects)
{
Console.WriteLine("{0}、{1}-{2}", conindex++, connect.Name, connect.Number);
}
Console.WriteLine("请选择要修改的联系方式编号:");
int num = int.Parse(Console.ReadLine()) - 1;
IConnect oldconnect = card.Connects[num];
Console.WriteLine("请输入新号码:");
oldconnect.Number = Console.ReadLine();
}
/// <summary>
/// 联系方式
/// </summary>
private static void RemoveConnect()
{
int conindex = 1;
foreach (IConnect connect in card.Connects)
{
Console.WriteLine("{0}、{1}-{2}", conindex++, connect.Name, connect.Number);
}
Console.WriteLine("请选择要修改的联系方式编号:");
int num = int.Parse(Console.ReadLine()) - 1;
IConnect oldconnect = card.Connects[num];
card.Connects.Remove(oldconnect);
}
}
}
MangeCard:
/// <summary>
/// 名片管理
/// </summary>
static class ManageCard
{
/// <summary>
///名片类型
/// </summary>
static List<CardType> cardtypes;
static CardSet cardset;
/// <summary>
/// 管¨¹理¤¨ª名?片?菜?单Ì£¤
/// </summary>
/// <param name="cardset">名?片?册¨¢</param>
/// <param name="cardtypes">名?片?类¤¨¤型¨ª集¡¥合?</param>
public static void Menu(CardSet cardset, List<CardType> cardtypes)
{
ManageCard.cardtypes = cardtypes;
ManageCard.cardset = cardset;
Console.WriteLine("1、¡é添¬¨ª加¨®名?片? 2、¡é修T改?名?片? 3、¡é删¦?除y名?称? 4、¡é退ª?出?名?片?管¨¹理¤¨ª");
switch (Console.ReadLine())
{
case "1":
Add();
break;
case "2":
Update();
break;
case "3":
Delete();
break;
case "4":
return;
}
}
/// <summary>
/// 根¨´据Y姓?名?得Ì?到Ì?新?名?片?集¡¥合?
/// </summary>
/// <param name="name">姓?名?</param>
/// <returns>名?片?集¡¥合?</returns>
static List<Card> GetCardByName(string name)
{
List<Card> newcards = new List<Card>();
foreach (Card oldcard in cardset.Cards)
{
if (oldcard.Name.Contains(name))
{
newcards.Add(oldcard);
}
}
return newcards;
}
/// <summary>
/// 得Ì?到Ì?新?集¡¥合?中D的Ì?名?片?
/// </summary>
/// <param name="newcards">新?名?片?集¡¥合?</param>
/// <returns>名?片?</returns>
static Card GetCard(List<Card> newcards)
{
int index = 1;
foreach (Card oldcard in newcards)
{
Console.WriteLine("{0}、¡é姓?名?:êo{1} 公?司?:êo{2} 职¡ã位?:êo{3} 地Ì?址¡¤:êo{4}", index, oldcard.Name, oldcard.Company, oldcard.Position, oldcard.Address);
index++;
}
Console.WriteLine("请?选?择?修T改?名?片?:êo");
int num = int.Parse(Console.ReadLine()) - 1;
Card card = newcards[num];
Console.WriteLine(card.ToString());
return card;
}
/// <summary>
/// 修T改?名?片?
/// </summary>
/// <param name="cardset">名?片?册¨¢</param>
private static void Update()
{
try
{
Console.WriteLine("请?输º?入¨?要°a修T改?名?片?人¨?的Ì?姓?名?:êo");
string name = Console.ReadLine();
//选?择?名?称?相¨¤同ª?的Ì?名?片?
List<Card> newcards = GetCardByName(name);
//选?择?出?具?体¬?的Ì?名?片?
Card card = GetCard(newcards);
//修T改?各¡Â项?属º?性?
string cardname;
if (IsModify("姓?名?", out cardname))
{
card.Name = cardname;
}
string cardcompany;
if (IsModify("公?司?", out cardcompany))
{
card.Company = cardcompany;
}
string cardposition;
if (IsModify("职¡ã位?", out cardposition))
{
card.Position = cardposition;
}
string cardaddress;
if (IsModify("地Ì?址¡¤", out cardaddress))
{
card.Address = cardaddress;
}
string cardwebsit;
if (IsModify("网ª?址¡¤", out cardwebsit))
{
card.Websit = cardwebsit;
}
int typeid;
if (IsModify("名?片?类¤¨¤型¨ª", out typeid))
{
card.TypeID = typeid;
}
//修T改?联¢a系¦Ì方¤?式º?
ManageConnect.Menu(card);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
/// <summary>
/// 是º?否¤?修T改?
/// </summary>
/// <param name="proname">修T改?属º?性?说¦Ì明¡Â</param>
/// <param name="provalue">修T改?后¨®的Ì?属º?性?值¦Ì</param>
/// <returns>是º?否¤?修T改?</returns>
static bool IsModify(string proname, out string provalue)
{
Console.WriteLine("是º?否¤?要°a修T改?{0},ê?是º?请?按ã¡äY", proname);
if (Console.ReadLine().ToUpper() == "Y")
{
Console.WriteLine("{0}:êo", proname);
provalue = Console.ReadLine();
return true;
}
provalue = null;
return false;
}
/// <summary>
/// 是º?否¤?修T改?
/// </summary>
/// <param name="proname">修T改?属º?性?说¦Ì明¡Â</param>
/// <param name="id">要°a修T改?类¤¨¤型¨ªID</param>
/// <returns>是º?否¤?修T改?</returns>
static bool IsModify(string proname, out int id)
{
Console.WriteLine("是º?否¤?要°a修T改?{0},ê?是º?请?按ã¡äY", proname);
if (Console.ReadLine().ToUpper() == "Y")
{
foreach (CardType cardtype in cardtypes)
{
Console.WriteLine("{0}、¡é{1}", cardtype.ID, cardtype.TypeName);
}
Console.WriteLine("请?选?择?编À¨¤号?:êo");
id = int.Parse(Console.ReadLine());
return true;
}
id = 0;
return false;
}
/// <summary>
/// 删¦?除y名?片?
/// </summary>
/// <param name="cardset">名?片?册¨¢</param>
private static void Delete()
{
try
{
Console.WriteLine("请?输º?入¨?要°a修T改?名?片?人¨?的Ì?姓?名?:êo");
string name = Console.ReadLine();
//选?择?名?称?相¨¤同ª?的Ì?名?片?
List<Card> newcards = GetCardByName(name);
//选?择?出?具?体¬?的Ì?名?片?
Card card = GetCard(newcards);
//删¦?除y名?片?
cardset.RemoveCard(card);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
/// <summary>
/// 添¬¨ª加¨®名?片?
/// </summary>
/// <param name="cardset">名?片?册¨¢</param>
public static void Add()
{
try
{
//创ä¡ä建¡§新?名?片?对?象¨®,ê?并¡é给?各¡Â属º?性?赋3值¦Ì
Card card = new Card();
Console.WriteLine("姓?名?:êo");
card.Name = Console.ReadLine();
Console.WriteLine("公?司?:êo");
card.Company = Console.ReadLine();
Console.WriteLine("职¡ã位?:êo");
card.Position = Console.ReadLine();
Console.WriteLine("地Ì?址¡¤:êo");
card.Address = Console.ReadLine();
Console.WriteLine("网ª?址¡¤:êo");
card.Websit = Console.ReadLine();
foreach (CardType ct in cardtypes)
{
Console.WriteLine("{0}、¡é{1}", ct.ID, ct.TypeName);
}
Console.WriteLine("类¤¨¤型¨ª编À¨¤号?:êo");
card.TypeID = int.Parse(Console.ReadLine());
//添¬¨ª加¨®联¢a系¦Ì方¤?式º?
ManageConnect.AddConnect(card);
//添¬¨ª加¨®名?片?到Ì?名?片?册¨¢
cardset.AddCard(card);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
QueryCard:
/// <summary>
/// 名?片?查¨¦询¡¥
/// </summary>
static class QueryCard
{
/// <summary>
/// 名?片?册¨¢
/// </summary>
static CardSet cardset;
/// <summary>
/// 名?片?类¤¨¤型¨ª
/// </summary>
static List<CardType> cardtypes;
/// <summary>
/// 查¨¦询¡¥名?片?菜?单Ì£¤
/// </summary>
/// <param name="cardset">名?片?册¨¢</param>
/// <param name="cardtypes">名?片?类¤¨¤型¨ª集¡¥合?</param>
public static void Menu(CardSet cardset, List<CardType> cardtypes)
{
QueryCard.cardset = cardset;
QueryCard.cardtypes = cardtypes;
Console.WriteLine("1、¡é全¨?部?查¨¦ 2、¡é按ã¡ä姓?名?查¨¦询¡¥ 3、¡é按ã¡ä分¤?类¤¨¤查¨¦询¡¥ 4、¡é退ª?出?查¨¦询¡¥");
switch (Console.ReadLine())
{
case "1":
QueryAllCard();
break;
case "2":
QueryCardByName();
break;
case "3":
QueryCardByType();
break;
case "4":
return;
}
Console.WriteLine("继¨¬续?请?按ã¡ä任¨?意°a键¨¹!ê?");
Console.ReadLine();
}
/// <summary>
/// 按ã¡ä名?片?类¤¨¤型¨ª查¨¦询¡¥
/// </summary>
private static void QueryCardByType()
{
string typename = "";
foreach (CardType cardtype in cardtypes)
{
Console.WriteLine("{0}、¡é{1}", cardtype.ID, cardtype.TypeName);
}
int typeid = int.Parse(Console.ReadLine());
typename = cardtypes.Single(ct => ct.ID == typeid).TypeName;
Console.WriteLine("类¤¨¤型¨ª:êo{0}", typename);
bool mark = false;//有®D无T数ºy据Y标À¨º志?
foreach (Card oldcard in cardset.Cards)
{
if (oldcard.TypeID == typeid)
{
Console.WriteLine(oldcard.ToString());
mark = true;
}
}
if (!mark)
{
Console.WriteLine("无T数ºy据Y!ê?");
}
}
/// <summary>
/// 按ã¡ä姓?名?查¨¦询¡¥
/// </summary>
private static void QueryCardByName()
{
Console.WriteLine("请输入¦询¥的Ì?姓?名?(模¡ê糊y查¨¦询¡¥):êo");
string name = Console.ReadLine();
bool mark = false;有无数据标志?
foreach (Card oldcard in cardset.Cards)
{
if (oldcard.Name.Contains(name))
{
foreach (CardType ct in cardtypes)
{
if (ct.ID == oldcard.TypeID)
{
Console.Write("类型¨:{0} ", ct.TypeName);
mark = true;
}
}
Console.WriteLine(oldcard.ToString());
}
}
if (!mark)
{
Console.WriteLine("无数据!");
}
}
/// <summary>
/// 查¨¦询¡¥全¨?部?名?片?
/// </summary>
private static void QueryAllCard()
{
bool mark = false;
foreach (Card oldcard in cardset.Cards)
{
foreach (CardType ct in cardtypes)
{
if (ct.ID == oldcard.TypeID)
{
Console.Write("类型¨:{0} ", ct.TypeName);
break;
}
}
mark = true;
Console.WriteLine(oldcard.ToString());
}
if (!mark)
{
Console.WriteLine("无数据!");
}
}
}
}
转载于:https://blog.51cto.com/3333560/624833