Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.30 KB | None | 0 0
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. using System;
  143. using System.Collections.Generic;
  144. using System.IO;
  145. using System.Runtime.Serialization;
  146. using System.Runtime.Serialization.Formatters.Binary;
  147. using System.Text;
  148.  
  149. public class FriendsInformation
  150. {
  151.  
  152.     private static FriendsInformation friendsInformation;
  153.  
  154.     private Dictionary<string, Friend> friendsDictionary;
  155.     private BinaryFormatter formatter;
  156.  
  157.     private const string DATA_FILENAME = "friendsinformation.dat";
  158.  
  159.     public static FriendsInformation Instance()
  160.     {
  161.         if (friendsInformation == null) {
  162.             friendsInformation = new FriendsInformation();
  163.         } // end if
  164.  
  165.         return friendsInformation;
  166.     } // end public static FriendsInformation Instance()
  167.  
  168.     private FriendsInformation()
  169.     {
  170.         // Create a Dictionary to store friends at runtime
  171.         this.friendsDictionary = new Dictionary<string, Friend>();
  172.         this.formatter = new BinaryFormatter();
  173.     } // end private FriendsInformation()
  174.  
  175.     public void AddFriend(string name, string email)
  176.     {
  177.         // If we already had added a friend with this name
  178.         if (this.friendsDictionary.ContainsKey(name))
  179.         {
  180.             Console.WriteLine("You had already added " + name + " before.");
  181.         }
  182.         // Else if we do not have this friend details
  183.         // in our dictionary
  184.         else
  185.         {
  186.             // Add him in the dictionary
  187.             this.friendsDictionary.Add(name, new Friend(name, email));
  188.             Console.WriteLine("Friend added successfully.");
  189.         } // end if
  190.     } // end public bool AddFriend(string name, string email)
  191.  
  192.     public void RemoveFriend(string name)
  193.     {
  194.         // If we do not have a friend with this name
  195.         if (!this.friendsDictionary.ContainsKey(name))
  196.         {
  197.             Console.WriteLine(name + " had not been added before.");
  198.         }
  199.         // Else if we have a friend with this name
  200.         else
  201.         {
  202.             if (this.friendsDictionary.Remove(name))
  203.             {
  204.                 Console.WriteLine(name + " had been removed successfully.");
  205.             }
  206.             else
  207.             {
  208.                 Console.WriteLine("Unable to remove " + name);
  209.             } // end if
  210.         } // end if
  211.     } // end public bool RemoveFriend(string name)
  212.  
  213.     public void Save()
  214.     {
  215.         // Gain code access to the file that we are going
  216.         // to write to
  217.         try
  218.         {
  219.             // Create a FileStream that will write data to file.
  220.             FileStream writerFileStream =
  221.                 new FileStream(DATA_FILENAME, FileMode.Create, FileAccess.Write);
  222.             // Save our dictionary of friends to file
  223.             this.formatter.Serialize(writerFileStream, this.friendsDictionary);
  224.  
  225.             // Close the writerFileStream when we are done.
  226.             writerFileStream.Close();
  227.         }
  228.         catch (Exception) {
  229.             Console.WriteLine("Unable to save our friends' information");
  230.         } // end try-catch
  231.     } // end public bool Load()
  232.              
  233.     public void Load()
  234.     {
  235.      
  236.         // Check if we had previously Save information of our friends
  237.         // previously
  238.         if (File.Exists(DATA_FILENAME))
  239.         {
  240.  
  241.             try
  242.             {
  243.                 // Create a FileStream will gain read access to the
  244.                 // data file.
  245.                 FileStream readerFileStream = new FileStream(DATA_FILENAME,
  246.                     FileMode.Open, FileAccess.Read);
  247.                 // Reconstruct information of our friends from file.
  248.                 this.friendsDictionary = (Dictionary<String, Friend>)
  249.                     this.formatter.Deserialize(readerFileStream);
  250.                 // Close the readerFileStream when we are done
  251.                 readerFileStream.Close();
  252.  
  253.             }
  254.             catch (Exception)
  255.             {
  256.                 Console.WriteLine("There seems to be a file that contains " +
  257.                     "friends information but somehow there is a problem " +
  258.                     "with reading it.");
  259.             } // end try-catch
  260.  
  261.         } // end if
  262.          
  263.     } // end public bool Load()
  264.  
  265.     public void Print()
  266.     {
  267.         // If we have saved information about friends
  268.         if (this.friendsDictionary.Count > 0)
  269.         {
  270.             Console.WriteLine("Name, Email");
  271.             foreach (Friend friend in this.friendsDictionary.Values)
  272.             {
  273.                 Console.WriteLine(friend.Name + ", " + friend.Email);
  274.             } // end foreach
  275.         }
  276.         else
  277.         {
  278.             Console.WriteLine("There are no saved information about your friends");
  279.         } // end if
  280.     } // end public void Print()
  281.  
  282. } // end public class FriendsInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement