要求:展示全部联系人,查找联系人,添加联系人,删除联系人,更改联系人信息,,并且实现本地存储

使用Dictionary<T,T>

Program类:主要是实现整体框架结构

Font类:实现数据存储

Hash类:实现各种操作的方法;

test。txt:用来存储的记事本

功能很简单:主要思路是,在程序的开始获取本地记录,赋值给字典,然后对这些数据进行各种增删改查操作,运行结束期间将所有更改后的数据传到本地文件,并覆盖之前内容

(该程序总体功能都能实现,在存储的时候会有一些小bug,比如如果文件中没有内容,但是有一个回车或者空格,会报错,多多提提意见)

 

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace 电话本
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             //定义字典,用来接收文本文件里的内容
16             Dictionary<string, string> ht = new Dictionary<string, string>();
17             //实例化存储数据的类
18             Font f = new Font();
19             //调用读取方法,把值传给ht
20             ht = f.IoDu();
21             //实例化完成各种操作的类的对象
22             Hash hash = new Hash();
23             //将ht的值通过属性传给Hash类里的ddd
24             hash.DDD = ht;
25             for (;;)
26             {
27                 //调取Hash类中的头部样式模板
28                 hash.Title();
29                 int x = int.Parse(Console.ReadLine());
30                 if (x == 1)
31                 {
32                     //调用添加联系人的方法
33                     hash.DictionaryAdd();               
34                 }
35                 else if (x == 2)
36                 {
37                     //调用查询联系人的方法
38                     hash.DictionaryCha();
39                 }
40                 else if (x == 3)
41                 {
42                     //调用修改联系人的方法
43                     hash.DictionaryGai();                     
44                 }
45                 else if (x==4)
46                 {
47                     //调用删除联系人的方法
48                     hash.DictionaryRemove();
49                 }
50                 else if (x == 5)
51                 {
52                     //调用退出程序的方法
53                     hash.Cun();
54                     break;
55                 }
56 
57             }
58             
59         }
60     }
61 }
Program
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 电话本
 9 {
10     class Font
11     {
12         //实例化Hash类,
13         Hash hash = new Hash();        
14         public void IoAdd(Dictionary<string, string> dic)
15         {//添加联系人的方法            
16             FileStream fs = new FileStream(@"E:\VS2015\yangyong\电话本\电话本\test.txt", FileMode.Create, FileAccess.Write);
17             StreamWriter sw = new StreamWriter(fs);
18             foreach (string c in dic.Keys) {
19                 sw.WriteLine(c + "," + dic[c]);
20             }           
21             sw.Close();
22             fs.Close();
23         }
24         public Dictionary<string,string> IoDu() {//读取联系人
25             //Hash ha = new Hash();
26             Dictionary<string, string> dic = new Dictionary<string, string>();
27             FileStream ff = new FileStream(@"E:\VS2015\yangyong\电话本\电话本\test.txt",FileMode.Open,FileAccess.Read);
28             StreamReader rm = new StreamReader(ff);
29             string str;
30             if ((str = rm.ReadLine()) != null)
31             {
32                 Console.WriteLine("删除成功!");
33                 string[] strarr = str.Split(',');
34                 dic.Add(strarr[0], strarr[1]);
35             }             
36             rm.Close();
37             ff.Close();
38             return dic;
39         }
40 
41 
42     }
43 }
Font
  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 
  9 namespace 电话本
 10 {
 11     
 12     public class Hash
 13     {
 14 
 15         //定义一个字典类型字段,
 16         private Dictionary<string, string> ddd = new Dictionary<string, string>();
 17         public Dictionary<string, string> DDD {
 18             get { return ddd; }
 19             set { ddd = value; }
 20         }
 21         Program p = new Program();        
 22         public void Title() {//头部页面
 23             Console.Clear();
 24             Console.WriteLine("-----------------------欢迎光临电话本小程序-----------------------");
 25             Console.WriteLine("1.添加联系人   2.显示联系人   3.修改联系人   4.删除联系人   5.退出");
 26             Console.WriteLine("-----------------------欢迎光临电话本小程序-----------------------");
 27         }       
 28         public void DictionaryAdd() {//添加联系人            
 29             Console.WriteLine("姓名:");
 30             string name = Console.ReadLine();
 31             Console.WriteLine("联系方式:");
 32             string age = Console.ReadLine();
 33             ddd.Add(name, age);
 34             Console.WriteLine("添加成功!");
 35             Console.WriteLine("按任意键继续....");
 36             Console.ReadKey();           
 37         }
 38         public void DictionaryCha() {//查找联系人
 39             for (;;)
 40             {
 41                 Console.Clear();
 42                 Console.WriteLine("A.快速查找   B.显示全部   Q.退出查找");
 43                 char y = char.Parse(Console.ReadLine().ToUpper());
 44                 if (y == 'A')
 45                 {
 46                     Console.WriteLine("请输入联系人姓名:");
 47                     string name = Console.ReadLine();
 48                     if (ddd.ContainsKey(name))
 49                     {
 50                         Console.WriteLine("姓名:{0};联系方式:{1};", name, ddd[name]);
 51                         Console.WriteLine("按任意键继续....");
 52                         Console.ReadKey();
 53                         continue;
 54                     }
 55                     else
 56                     {
 57                         Console.WriteLine("查不到当前联系人!!");
 58                         Console.WriteLine("按任意键继续....");
 59                         Console.ReadKey();
 60                         continue;
 61                     }                                       
 62                 }
 63                 else if (y == 'B')
 64                 {
 65                     Console.WriteLine("-----------------------全部联系人-----------------------");
 66                     foreach (string c in ddd.Keys)
 67                     {
 68                         Console.WriteLine("姓名:{0};联系方式:{1};", c, ddd[c]);
 69                     }
 70                     Console.WriteLine("按任意键继续....");
 71                     Console.ReadKey();
 72                 }
 73                 else if (y == 'Q')
 74                 {
 75                     break;
 76 
 77                 }
 78                 else {
 79                     Console.WriteLine("输入有误!!!!");
 80                     Console.WriteLine("按任意键继续....");
 81                     Console.ReadKey();
 82                 }
 83             }
 84         }
 85         public void DictionaryGai() {//修改联系人
 86             Console.WriteLine("请输入要修改的联系人姓名:");
 87             string name = Console.ReadLine();
 88             Console.WriteLine("请输入要修改的内容:");
 89             string age = Console.ReadLine();
 90             if (ddd.ContainsKey(name)) {
 91                 ddd[name] = age;
 92                 Console.WriteLine("修改成功!");
 93                 Console.WriteLine("按任意键继续....");
 94                 Console.ReadKey();
 95             }
 96             else
 97             {
 98                 Console.WriteLine("没有该联系人,是否新建并保存当前联系人?Y/N");
 99                 char x = char.Parse(Console.ReadLine().ToUpper());
100                 if (x == 'Y')
101                 {
102                     ddd.Add(name, age);
103                     Console.WriteLine("添加成功!");
104                     Console.WriteLine("按任意键继续....");
105                     Console.ReadKey();
106                 }
107                 else {
108                     Console.WriteLine("按任意键继续....");
109                     Console.ReadKey();
110                 }
111             }                                 
112         }
113         public void DictionaryRemove()//删除联系人
114         {
115             Console.WriteLine("请输入要删除的联系人姓名:");
116             string name = Console.ReadLine();
117             Console.WriteLine("确认删除?Y/N");
118             char x = char.Parse(Console.ReadLine().ToUpper());
119             switch (x)
120             {
121                 case 'Y':
122                     ddd.Remove(name);
123                     Console.WriteLine("删除成功!");
124                     Console.WriteLine("按任意键继续....");
125                     Console.ReadKey();
126                     break;
127                 case 'N':
128                     Console.WriteLine("按任意键继续....");
129                     Console.ReadKey();
130                     break;
131                 default:
132                     Console.WriteLine("输入有误!!!!");
133                     Console.WriteLine("按任意键继续....");
134                     Console.ReadKey();
135                     break;
136             }            
137         }
138         //退出程序前先将数据遍历存入IO流
139         public void Cun() {
140             Font f = new Font();
141             f.IoAdd(ddd);
142         }
143     }
144 }
Hash