public void NetworkSerializeAndDeserialize() { var savedNetwork = new PandaSocialNetwork(); var ivo = new Panda("Ivo", "*****@*****.**", GenderType.Male); var rado = new Panda("Rado", "*****@*****.**", GenderType.Male); var tony = new Panda("Tony", "*****@*****.**", GenderType.Female); savedNetwork.AddPanda(ivo); savedNetwork.AddPanda(rado); savedNetwork.AddPanda(tony); savedNetwork.MakeFriends(ivo, rado); savedNetwork.MakeFriends(rado, tony); var pandaSerializer = new PandaSocialNetworkBinarySerializer("unitTest.dat"); pandaSerializer.Save(savedNetwork); var loadedNetwork = pandaSerializer.Load(); Assert.IsTrue( savedNetwork.HasPanda(ivo) == loadedNetwork.HasPanda(ivo) && savedNetwork.HasPanda(rado) == loadedNetwork.HasPanda(rado) && savedNetwork.HasPanda(tony) == loadedNetwork.HasPanda(tony) && savedNetwork.HasPanda(new Panda("a", "*****@*****.**", GenderType.Male)) == loadedNetwork.HasPanda(new Panda("a", "*****@*****.**", GenderType.Male)) && savedNetwork.ConnectionLevel(ivo, rado) == loadedNetwork.ConnectionLevel(ivo, rado) && savedNetwork.ConnectionLevel(ivo, tony) == loadedNetwork.ConnectionLevel(ivo, tony) && savedNetwork.HowManyGenderInNetwork(2, ivo, GenderType.Female) == loadedNetwork.HowManyGenderInNetwork(2, ivo, GenderType.Female) ); }
public void IfThereIsPanda() { var thereIsPanda = new PandaSocialNetwork(); var panda = new Panda("Kostadin", "*****@*****.**", GenderType.Male); thereIsPanda.AddPanda(panda); thereIsPanda.AddPanda(panda); }
public void IfAlreadyFriends() { var alreadyFriends = new PandaSocialNetwork(); var panda1 = new Panda("Orhan", "*****@*****.**", GenderType.Male); var panda2 = new Panda("Mumun", "*****@*****.**", GenderType.Male); alreadyFriends.MakeFriends(panda1, panda2); alreadyFriends.MakeFriends(panda1, panda2); }
public void AddPanda(Panda panda) { if (pandas.ContainsKey(panda)) { throw new PandaAlreadyThereException(); } else { pandas.Add(panda, new Node(panda)); } }
public void hasPandaInTheNetwork() { var panda = new Panda("Stefan", "*****@*****.**", GenderType.Male); var network = new PandaSocialNetwork(); bool hasPanda = false; network.AddPanda(panda); if (network.HasPanda(panda)) { hasPanda = true; } Assert.IsTrue(hasPanda); }
public void MakeFriends(Panda panda1, Panda panda2) { Node panda1Node, panda2Node; bool notInNetwork = false; if (!HasPanda(panda1)) { panda1Node = new Node(panda1); pandas.Add(panda1, panda1Node); notInNetwork = true; } else { panda1Node = pandas[panda1]; } if (!HasPanda(panda2)) { panda2Node = new Node(panda2); pandas.Add(panda2, panda2Node); notInNetwork = true; } else { panda2Node = pandas[panda2]; } if (notInNetwork) { panda1Node.Friends.Add(panda2Node); panda2Node.Friends.Add(panda1Node); } else { if (panda1Node.Friends.Contains(panda2Node)) { throw new PandasAlreadyFriendsException(); } else { panda1Node.Friends.Add(panda2Node); panda2Node.Friends.Add(panda1Node); } } }
public bool AreFriends(Panda panda1, Panda panda2) { if (HasPanda(panda1) && HasPanda(panda2)) { Node panda1Node = pandas[panda1]; Node panda2Node = pandas[panda2]; if (panda1Node.Friends.Contains(panda2Node)) { return true; } else { return false; } } return false; }
public static void Main() { Console.WriteLine("WELCOME TO THE PANDA SOCIAL NETWORK!"); PandaSocialNetwork pandaSocialNetwork = new PandaSocialNetwork(); List<string> help = new List<string>(); help.Add("<--------------!!!----------------->"); help.Add("help: display the useful commands"); help.Add("addPanda: creata a panda and add it to the social network"); help.Add("hasPanda: check if the panda is in the social network"); help.Add("makeFriends: make pandas friends "); help.Add("areFriends: check if pandas are friends"); help.Add("friendsOf: display the given panda's friends"); help.Add("connectionLevel: display the connection level between pandas"); help.Add("areConnected: check if pandas are connected"); help.Add("how many gender in network: display the number of gender pandas"); help.Add("<--------------!!!----------------->"); Console.WriteLine("Type help to begin!"); do { string input = Console.ReadLine(); string[] commands = input.Split(' '); switch (commands[0]) { case "help": Console.WriteLine(); foreach (var helpCommand in help) { Console.WriteLine(helpCommand); } Console.WriteLine(); break; case "addPanda": { Panda panda = null; if (commands[3].ToLower() == "male") { panda = new Panda(commands[1], commands[2], GenderType.Male); } else if (commands[3].ToLower() == "female") { panda = new Panda(commands[1], commands[2], GenderType.Female); } else { throw new ArgumentException("gender must be male or female"); } pandaSocialNetwork.AddPanda(panda); break; } case "hasPanda": { Panda panda = null; if (commands[3].ToLower() == "male") { panda = new Panda(commands[1], commands[2], GenderType.Male); } else if (commands[3].ToLower() == "female") { panda = new Panda(commands[1], commands[2], GenderType.Female); } else { throw new ArgumentException("gender must be male or female"); } Console.WriteLine(pandaSocialNetwork.HasPanda(panda)); break; } default: Console.WriteLine("ERROR: Invalid command!"); break; } } while (true); }
public bool HasPanda(Panda panda) { return pandas.ContainsKey(panda); }
public Node(Panda panda) { Panda = panda; Friends = new HashSet<Node>(); }
public int HowManyGenderInNetwork(int level, Panda panda, GenderType gender) { Node pandaNode; if (HasPanda(panda)) { pandaNode = pandas[panda]; } else { return -1; } HashSet<Node> visited = new HashSet<Node>(); Queue<Node> nodesQueue = new Queue<Node>(); Queue<int> levelQueue = new Queue<int>(); int genderCount = 0; nodesQueue.Enqueue(pandaNode); levelQueue.Enqueue(0); while (nodesQueue.Count > 0) { Node curNode = nodesQueue.Dequeue(); int curLevel = levelQueue.Dequeue(); if (curLevel <= level) { switch (gender) { case GenderType.Male: { if (curNode.Panda.IsMale) ++genderCount; break; } case GenderType.Female: { if (curNode.Panda.IsFemale) ++genderCount; break; } default: break; } } if (curLevel == 2) { continue; } foreach (var friend in curNode.Friends) { if (!visited.Contains(friend)) { visited.Add(friend); nodesQueue.Enqueue(friend); levelQueue.Enqueue(curLevel + 1); } } } return genderCount; }
public bool AreConnected(Panda panda1, Panda panda2) { return ConnectionLevel(panda1, panda2) != -1; }
public int ConnectionLevel(Panda panda1, Panda panda2) { Node panda1Node, panda2Node; if (HasPanda(panda1) && HasPanda(panda2)) { panda1Node = pandas[panda1]; panda2Node = pandas[panda2]; } else { return -1; } HashSet<Node> visited = new HashSet<Node>(); Queue<Node> nodesQueue = new Queue<Node>(); Queue<int> levelQueue = new Queue<int>(); nodesQueue.Enqueue(panda1Node); levelQueue.Enqueue(0); while (nodesQueue.Count > 0) { Node curNode = nodesQueue.Dequeue(); int curLevel = levelQueue.Dequeue(); if (curNode.Equals(panda2Node)) { return curLevel; } foreach (var friend in curNode.Friends) { if (!visited.Contains(friend)) { visited.Add(friend); nodesQueue.Enqueue(friend); levelQueue.Enqueue(curLevel + 1); } } } return -1; }
public List<Panda> FriendsOf(Panda panda) { if (HasPanda(panda)) { return pandas[panda].Friends.Select(x => x.Panda).ToList(); } else { return new List<Panda>(); } }
public void IfValidName() { var validName = new Panda("Pesho", "*****@*****.**", GenderType.Male); Assert.AreEqual("Pesho", validName.PandaName); }
public void InvalidEmail() { var invalidEmail = new Panda("Goshko","goshkomail.com", GenderType.Male); Assert.AreEqual(invalidEmail.PandaEmail, "goshkomail.com"); }