Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- public class FriendsInformation
- {
- private static FriendsInformation friendsInformation;
- private Dictionary<string, Friend> friendsDictionary;
- private BinaryFormatter formatter;
- private const string DATA_FILENAME = "friendsinformation.dat";
- public static FriendsInformation Instance()
- {
- if (friendsInformation == null) {
- friendsInformation = new FriendsInformation();
- } // end if
- return friendsInformation;
- } // end public static FriendsInformation Instance()
- private FriendsInformation()
- {
- // Create a Dictionary to store friends at runtime
- this.friendsDictionary = new Dictionary<string, Friend>();
- this.formatter = new BinaryFormatter();
- } // end private FriendsInformation()
- public void AddFriend(string name, string email)
- {
- // If we already had added a friend with this name
- if (this.friendsDictionary.ContainsKey(name))
- {
- Console.WriteLine("You had already added " + name + " before.");
- }
- // Else if we do not have this friend details
- // in our dictionary
- else
- {
- // Add him in the dictionary
- this.friendsDictionary.Add(name, new Friend(name, email));
- Console.WriteLine("Friend added successfully.");
- } // end if
- } // end public bool AddFriend(string name, string email)
- public void RemoveFriend(string name)
- {
- // If we do not have a friend with this name
- if (!this.friendsDictionary.ContainsKey(name))
- {
- Console.WriteLine(name + " had not been added before.");
- }
- // Else if we have a friend with this name
- else
- {
- if (this.friendsDictionary.Remove(name))
- {
- Console.WriteLine(name + " had been removed successfully.");
- }
- else
- {
- Console.WriteLine("Unable to remove " + name);
- } // end if
- } // end if
- } // end public bool RemoveFriend(string name)
- public void Save()
- {
- // Gain code access to the file that we are going
- // to write to
- try
- {
- // Create a FileStream that will write data to file.
- FileStream writerFileStream =
- new FileStream(DATA_FILENAME, FileMode.Create, FileAccess.Write);
- // Save our dictionary of friends to file
- this.formatter.Serialize(writerFileStream, this.friendsDictionary);
- // Close the writerFileStream when we are done.
- writerFileStream.Close();
- }
- catch (Exception) {
- Console.WriteLine("Unable to save our friends' information");
- } // end try-catch
- } // end public bool Load()
- public void Load()
- {
- // Check if we had previously Save information of our friends
- // previously
- if (File.Exists(DATA_FILENAME))
- {
- try
- {
- // Create a FileStream will gain read access to the
- // data file.
- FileStream readerFileStream = new FileStream(DATA_FILENAME,
- FileMode.Open, FileAccess.Read);
- // Reconstruct information of our friends from file.
- this.friendsDictionary = (Dictionary<String, Friend>)
- this.formatter.Deserialize(readerFileStream);
- // Close the readerFileStream when we are done
- readerFileStream.Close();
- }
- catch (Exception)
- {
- Console.WriteLine("There seems to be a file that contains " +
- "friends information but somehow there is a problem " +
- "with reading it.");
- } // end try-catch
- } // end if
- } // end public bool Load()
- public void Print()
- {
- // If we have saved information about friends
- if (this.friendsDictionary.Count > 0)
- {
- Console.WriteLine("Name, Email");
- foreach (Friend friend in this.friendsDictionary.Values)
- {
- Console.WriteLine(friend.Name + ", " + friend.Email);
- } // end foreach
- }
- else
- {
- Console.WriteLine("There are no saved information about your friends");
- } // end if
- } // end public void Print()
- } // end public class FriendsInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement