系统介绍
功能简介
通过IO流和字典泛型,实现简单的电话数据写入文本文档,实现了电话本的添加、查找、修改、删除联系人
学习目标:通过本次小程序,能够进一步熟悉灵活运用IO流,字典泛型,包装方法,巩固知识
开发环境
开发工具:VS2015
开发环境:Windows
技术要求
IO流
txt的读入、写入
字典泛型
包装方法
系统设计
利用swich循环构造整体结构,简单调用包装好的方法实现功能
总结:本次小程序遇到的最大的问题就是IO流写入读出TXT,一开始每次运行程序都会清除掉上次保存的联系人,通过代码的修改,实现了覆盖式保存,解决的以前存入的联系人丢失的问题
上代码
主界面:
static void Main(string[] args)
{
Fangfa f = new Fangfa();
while (true)
{
Console.Clear();
Console.WriteLine("----------------------------欢迎光临电话本系统------------------------------");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| 1-查看联系人 2-添加联系人 3-修改联系人 4-删除联系人 5-退出 |");
Console.WriteLine("| |");
Console.WriteLine("| -yjy出品 2.0 |");
Console.WriteLine("----------------------------欢迎光临电话本系统------------------------------");
int x = int.Parse(Console.ReadLine());
switch (x)
{
case 1:
f.Select();
Console.ReadKey();
break;
case 2:
Console.WriteLine(“请输入姓名:”);
string name = Console.ReadLine();
Console.WriteLine(“请输入电话:”);
string telphone = Console.ReadLine();
f.Add(name,telphone);
Console.WriteLine(“添加完成”);
Console.ReadKey();
break;
case 3:
Console.WriteLine(“请输入需要修改的姓名:”);
string name1 = Console.ReadLine();
f.Revise(name1);
Console.WriteLine(“修改完成”);
Console.ReadKey();
break;
case 4:
Console.WriteLine(“请输入需要删除的姓名:”);
string name2 = Console.ReadLine();
f.Remove(name2);
Console.WriteLine(“删除完成”);
Console.ReadKey();
break;
case 5:
Console.WriteLine(“期待您的下次使用”);
return;
default:
Console.WriteLine(“操作有误,重新输入!”);
Console.ReadKey();
break;
}
}
}
构造方法界面
```csharp
//查看方法
public void Select() {
IO i = new IO();
i.Out();
}
//添加方法
public void Add(string name,string telphone) {
IO i = new IO();
i.Write(name,telphone);
}
//修改方法
public void Revise(string name) {
Dictionary<string, string> dc = new Dictionary<string, string>();
IO i = new IO();
dc = i.Out1();
if (dc.ContainsKey(name))
{
dc.Remove(name);
Console.WriteLine("请输入新的联系人手机号:");
string telephone = Console.ReadLine();
dc.Add(name, telephone);
i.In(dc);
Console.WriteLine("信息修改成功!!");
}
else
{
Console.WriteLine("联系人不存在!!");
}
}
//删除方法
public void Remove(string name) {
Dictionary<string, string> dc = new Dictionary<string, string>();
IO i = new IO();
dc = i.Out1();
if (dc.ContainsKey(name))
{
dc.Remove(name);
i.In(dc);
Console.WriteLine("联系人删除成功!!");
}
else
{
Console.WriteLine("联系人不存在,删除失败!!");
}
}
IO流包装界面
//声明字典泛型
Dictionary<string, int> dc = new Dictionary<string, int>();
//覆盖方法
public void In(Dictionary<string ,string> dc) {
string path = @"E:\这是个测试文件夹\Data.txt";
//实例化FileStream类
FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write);
//实例化StreamWriter类
StreamWriter sw = new StreamWriter(file);
//写入
foreach (string key in dc.Keys)
{
string s = String.Format("{0} {1}", key, dc[key]);
sw.WriteLine(s);
}
//释放资源
sw.Dispose();
}
//写入方法
public void Write(string name, string telephone)
{
string path = @"E:\这是个测试文件夹\Data.txt";
//实例化FileStream类
FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
//实例化StreamWriter类
StreamWriter sw = new StreamWriter(fs);
//写入
string str = String.Format("{0} {1}", name, telephone);
sw.WriteLine(str);
Console.WriteLine("联系人添加成功!");
//关闭资源
sw.Dispose();
}
//读出方法,所有
public void Out() {
string path = @"E:\这是个测试文件夹\Data.txt";
//实例化FileStream类
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
//实例化StreamWriter类
StreamReader sr = new StreamReader(file);
//全部读出
string s = sr.ReadToEnd();
Console.WriteLine(s);
//释放资源
sr.Dispose();
}
//写出方法,行
public Dictionary<string, string> Out1()
{
//实例化 字典泛型
Dictionary<string, string> dc = new Dictionary<string, string>();
string path = @"E:\这是个测试文件夹\Data.txt";
//实例化FileStream类
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
//实例化StreamWriter类
StreamReader sr = new StreamReader(file);
//读出
string str;
while ((str = sr.ReadLine()) != null)
{
string[] arr = str.Split(' ');
dc.Add(arr[0], arr[1]);
}
//释放资源
sr.Dispose();
//返回字典泛型值
return dc;
}
如果有人有兴趣希望可以对你有些帮助