public void MakeFriends(Panda panda1, Panda panda2) { if (pandaFriends.ContainsKey(panda1) && pandaFriends.ContainsKey(panda2) == false) { AddPanda(panda2); } else if (pandaFriends.ContainsKey(panda1) == false && pandaFriends.ContainsKey(panda2)) { AddPanda(panda1); } else if (pandaFriends.ContainsKey(panda1) == false && pandaFriends.ContainsKey(panda2) == false) { AddPanda(panda1); AddPanda(panda2); } if (pandaFriends[panda1].Contains(panda2) == false) { pandaFriends[panda1].Add(panda2); } else { throw new PandaException("This panda exist"); } if (pandaFriends[panda2].Contains(panda1) == false) { pandaFriends[panda2].Add(panda1); } else { throw new PandaException("Existing Panda"); } }
public bool AreConnected(Panda panda1, Panda panda2) { if (!HasPanda(panda1) || !HasPanda(panda2)) { return(false); } var visited = new List <Panda>(); var queue = new Queue <Panda>(); queue.Enqueue(panda1); while (queue.Count > 0) { var pandaContainer = queue.Dequeue(); visited.Add(pandaContainer); if (pandaFriends[pandaContainer].Contains(panda2)) { return(true); } foreach (var neighbor in pandaFriends[pandaContainer]) { if (!visited.Contains(neighbor)) { queue.Enqueue(neighbor); } } } return(false); }
public void AddPanda(Panda panda) { if (this.HasPanda(panda)) { throw new PandaException.PandaAlreadyThereException("The panda is already in network!"); } else { this.pandasAndFriends.Add(panda, new List<Panda>()); } }
public void AddPanda(Panda panda) { if (!pandaFriends.ContainsKey(panda)) { // not null!!! pandaFriends.Add(panda, new List <Panda>()); } else { throw new PandaException("This panda exist in the friends network"); } }
public bool HasPanda(Panda panda) { if (this.pandaFriends.ContainsKey(panda)) { return(true); } else { return(false); } }
public bool IsFemale(Panda panda) { if (panda.IsMale(panda)) { return(false); } else { return(true); } }
public bool IsMale(Panda panda) { if (panda.Gender.Equals("Male")) { return(true); } else { return(false); } }
public bool AreFriends(Panda panda1, Panda panda2) { if (pandaFriends[panda1].Contains(panda2) && pandaFriends[panda2].Contains(panda1)) { return(true); } else { return(false); } }
public List <Panda> FriendsOf(Panda panda) { if (this.HasPanda(panda)) { return(this.pandaFriends[panda].ToList()); } else { throw new PandaException("Panda not existing in the SocialNetwork"); } }
public bool AreFriends(Panda firstPanda, Panda secondPanda) { if (this.HasPanda(firstPanda) && this.HasPanda(secondPanda)) { if (this.pandasAndFriends[firstPanda].Contains(secondPanda) && this.pandasAndFriends[secondPanda].Contains(firstPanda)) { return true; } } return false; }
public int ConnectionLevel(Panda firstPanda, Panda secondPanda) { if (this.HasPanda(firstPanda) && this.HasPanda(secondPanda)) { if (AreFriends(firstPanda, secondPanda)) { return 1; } else { List<Panda> friends = this.FriendsOf(firstPanda); int level = 1; while (true) { if (friends.Contains(secondPanda)) { break; } int friendsCount = friends.Count; for (int i = 0; i < friends.Count; i++) { var friendList = this.FriendsOf(friends[i]); for (int j = 0; j < friendList.Count; j++) { if (!friends.Contains(friendList[j]) && !friendList[j].Equals(firstPanda)) { friends.Add(friendList[j]); i++; } } } if (friendsCount == friends.Count) { return -1; } level++; } return level; } } throw new PandaException.PandaIsNotOnNetworkException(); }
public static void Main() { //var network = new PandaSocialNetwork(); var ivo = new Panda("Ivo", "*****@*****.**", GenderType.Male); var rado = new Panda("Rado", "*****@*****.**", GenderType.Male); var tony = new Panda("Tony", "*****@*****.**", GenderType.Female); var tonyz = new Panda("Tonyz", "*****@*****.**", GenderType.Female); // Adding pandas to network: //network.AddPanda(ivo); //network.AddPanda(rado); //network.AddPanda(tony); //network.AddPanda(tonyz); // Make some of them to be friends: //network.MakeFriends(ivo, rado); //network.MakeFriends(rado, tony); IPandaSocialNetworkStorageProvider provider = new PandaSocialNetworkStorageProvider(); IPandaSocialNetworkStorageProvider dataProvider = new PandaSocialNetworkAdoNetStorageProvider(); // provider.Save(network, "MyNetwork"); // Make save of network // var network = provider.Load("MyNetwork"); // network is loaded from save file. string connectionString = ConfigurationManager.ConnectionStrings["PandaSocialNetowrk"].ConnectionString; var network = dataProvider.Load(connectionString); // dataProvider.Save(network, connectionString); // Save network to DB // Check some methods: Console.WriteLine(network.ConnectionLevel(ivo, rado)); // 1 Console.WriteLine(network.ConnectionLevel(ivo, tony)); // 2 Console.WriteLine(network.ConnectionLevel(ivo, tonyz)); // -1 Console.WriteLine(network.AreConnected(ivo, tonyz)); network.MakeFriends(tony, tonyz); foreach (var friend in network.FriendsOf(rado)) { Console.WriteLine(friend.ToString()); } Console.WriteLine(network.HowManyGenderInNetwork(7, rado, GenderType.Female)); // 2 }
public bool AreConnected(Panda firstPanda, Panda secondPanda) { if (this.HasPanda(firstPanda) && this.HasPanda(secondPanda)) { if (this.ConnectionLevel(firstPanda, secondPanda) > 0) { return true; } else { return false; } } throw new PandaException.PandaIsNotOnNetworkException(); }
public PandaSocialNetwork Load(string connectionString) { using (var connection = new SqlConnection(connectionString)) { connection.Open(); var network = new PandaSocialNetwork(); string getAllPandas = "SELECT * FROM Panda"; SqlCommand getAllPandasSQL = new SqlCommand(getAllPandas, connection); var reader = getAllPandasSQL.ExecuteReader(); var pandaList = new List<Panda>(); while (reader.Read()) { var panda = new Panda((string)reader["Name"], (string)reader["Email"], (GenderType)reader["Gender"]); panda.Id = (int)reader["Id"]; pandaList.Add(panda); } reader.Close(); foreach (var panda in pandaList) { if (!network.HasPanda(panda)) { network.AddPanda(panda); } string getFiendsCommand = "SELECT pf.Friend_Id FROM PandaSocialNetowrk.dbo.PandaFriends pf JOIN PandaSocialNetowrk.dbo.Panda p ON p.Id = pf.Panda_Id WHERE pf.Panda_Id = @pandaId"; SqlCommand getFriendsSQL = new SqlCommand(getFiendsCommand, connection); getFriendsSQL.Parameters.AddWithValue("@pandaId", panda.Id); var idReader = getFriendsSQL.ExecuteReader(); while (idReader.Read()) { int id = (int)idReader["Friend_Id"]; var friend = pandaList.Where(p => p.Id == id).LastOrDefault(); if (!network.AreFriends(panda,friend)) { network.MakeFriends(panda, friend); } } idReader.Close(); } return network; } }
public int HowManyGenderInNetwork(int level, Panda panda, GenderType gender) { var visited = new List <Panda>(); var queue = new Queue <ConnectionLevelNode>(); int genderResult = 0; queue.Enqueue(new ConnectionLevelNode() { Node = panda, Level = 0 }); while (queue.Count > 0) { var pandaContainer = queue.Dequeue(); visited.Add(pandaContainer.Node); if (pandaContainer.Level <= level) { if (pandaContainer.Node.Gender == gender) { genderResult = genderResult + 1; } } else { break; } foreach (var neighbor in pandaFriends[pandaContainer.Node]) { if (!visited.Contains(neighbor)) { queue.Enqueue(new ConnectionLevelNode() { Node = neighbor, Level = pandaContainer.Level + 1 }); } } } return(genderResult); }
static void Main(string[] args) { Panda panda1 = new Panda(name: "Peter", email: "*****@*****.**", gender: GenderType.Male); Panda panda2 = new Panda(name: "Maria", email: "*****@*****.**", gender: GenderType.Female); Panda panda3 = new Panda(name: "Jim", email: "*****@*****.**", gender: GenderType.Male); Panda panda4 = new Panda(name: "Jennifer", email: "*****@*****.**", gender: GenderType.Female); Panda panda5 = new Panda(name: "Joe", email: "*****@*****.**", gender: GenderType.Male); Panda panda6 = new Panda(name: "Tim", email: "*****@*****.**", gender: GenderType.Male); Panda panda7 = new Panda(name: "Bob", email: "*****@*****.**", gender: GenderType.Male); Panda panda8 = new Panda(name: "Victoria", email: "*****@*****.**", gender: GenderType.Female); Panda panda9 = new Panda(name: "Julie", email: "*****@*****.**", gender: GenderType.Female); Panda panda10 = new Panda(name: "Jerry", email: "jerryhackbg.com", gender: GenderType.Male); SocialNetwork network = new SocialNetwork(); network.AddPanda(panda1); network.AddPanda(panda2); network.AddPanda(panda3); network.AddPanda(panda4); network.AddPanda(panda5); network.AddPanda(panda6); network.AddPanda(panda7); network.AddPanda(panda8); network.AddPanda(panda9); network.MakeFriends(panda1, panda2); network.MakeFriends(panda1, panda3); network.MakeFriends(panda2, panda4); network.MakeFriends(panda2, panda5); network.MakeFriends(panda3, panda6); network.MakeFriends(panda3, panda7); network.MakeFriends(panda4, panda8); network.MakeFriends(panda4, panda9); Console.WriteLine(network.HowManyGenderInNetwork(3, panda1, GenderType.Female)); Console.WriteLine(network.ConnectionLevel(panda1, panda9)); Console.WriteLine(network.AreFriends(panda1, panda3)); Console.WriteLine(network.AreConnected(panda2, panda10)); }
public int ConnectionLevel(Panda panda1, Panda panda2) { var visited = new List <Panda>(); var queue = new Queue <ConnectionLevelNode>(); queue.Enqueue(new ConnectionLevelNode() { Node = panda1, Level = 0 }); while (queue.Count > 0) { var nodeLevel = queue.Dequeue(); visited.Add(nodeLevel.Node); if (pandaFriends[nodeLevel.Node].Contains(panda2)) { return(nodeLevel.Level + 1); } foreach (var neighbor in pandaFriends[nodeLevel.Node]) { if (!visited.Contains(neighbor)) { queue.Enqueue(new ConnectionLevelNode() { Node = neighbor, Level = nodeLevel.Level + 1 }); } } } return(-1); }
public void TestInitialize() { network = new PandaSocialNetwork(); ivo = new Panda("Ivo", "*****@*****.**", GenderType.Male); rado = new Panda("Rado", "*****@*****.**", GenderType.Male); tony = new Panda("Tony", "*****@*****.**", GenderType.Female); pesho = new Panda("Pesho", "*****@*****.**", GenderType.Male); }
public void Save(PandaSocialNetwork network, string connectionString) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand deleteCommand = new SqlCommand("DELETE FROM Panda", connection); SqlCommand deleteFriend = new SqlCommand("DELETE FROM PandaFriends", connection); SqlCommand reseed = new SqlCommand("DBCC CHECKIDENT (Panda, RESEED, 0)", connection); deleteFriend.ExecuteNonQuery(); deleteCommand.ExecuteNonQuery(); reseed.ExecuteNonQuery(); string addPandaToDbCommand = "INSERT INTO Panda (Name,Email,Gender) VALUES (@name,@email,@gender)"; string addFirendCommand = "INSERT INTO PandaFriends (Panda_Id,Friend_Id) VALUES (@pandaId,@friendId)"; foreach (var panda in network.AllPandasAndFriends) { SqlCommand addPandaToDbSQLCommand = new SqlCommand(addPandaToDbCommand, connection); addPandaToDbSQLCommand.Parameters.AddWithValue("@name", panda.Key.Name); addPandaToDbSQLCommand.Parameters.AddWithValue("@email", panda.Key.Email); addPandaToDbSQLCommand.Parameters.AddWithValue("@gender", panda.Key.Gender); addPandaToDbSQLCommand.ExecuteNonQuery(); } string getAllPandas = "SELECT * FROM Panda"; SqlCommand getAllPandasSQL = new SqlCommand(getAllPandas, connection); var reader = getAllPandasSQL.ExecuteReader(); var pandaList = new List<Panda>(); while (reader.Read()) { var panda = new Panda((string)reader["Name"], (string)reader["Email"], (GenderType)reader["Gender"]); panda.Id = (int)reader["Id"]; pandaList.Add(panda); } reader.Close(); foreach (var panda in pandaList) { var friends = network.FriendsOf(panda); foreach (var friend in friends) { foreach (var equalFriend in pandaList) { if (friend.Equals(equalFriend)) { friend.Id = equalFriend.Id; } } } foreach (var friend in friends) { SqlCommand addFriendCommandSQL = new SqlCommand(addFirendCommand, connection); addFriendCommandSQL.Parameters.AddWithValue("@pandaId", panda.Id); addFriendCommandSQL.Parameters.AddWithValue("@friendId", friend.Id); addFriendCommandSQL.ExecuteNonQuery(); } } } }
public List<Panda> FriendsOf(Panda panda) { if (this.HasPanda(panda)) { return this.pandasAndFriends[panda].ToList(); } throw new PandaException.PandaIsNotOnNetworkException(); }
public bool HasPanda(Panda panda) { if (this.pandasAndFriends.ContainsKey(panda)) { return true; } return false; }
public int HowManyGenderInNetwork(int level, Panda panda, GenderType gender) { if (!this.HasPanda(panda)) { throw new PandaException.PandaIsNotOnNetworkException(); } List<Panda> friends = this.FriendsOf(panda); int countLevel = 0; int genderCount = 0; while (true) { countLevel++; int friendsCount = friends.Count; for (int i = 0; i < friends.Count; i++) { var friendList = this.FriendsOf(friends[i]); for (int j = 0; j < friendList.Count; j++) { if (!friends.Contains(friendList[j]) && !friendList[j].Equals(panda)) { friends.Add(friendList[j]); i++; } } } if (countLevel == level) { genderCount = friends.Where(x => x.Gender == gender).Count(); break; } else if (friendsCount == friends.Count) { genderCount = friends.Where(x => x.Gender == gender).Count(); break; } } return genderCount; }
public void MakeFriends(Panda firstPanda, Panda secondPanda) { if (this.AreFriends(firstPanda, secondPanda)) { throw new PandaException.PandasAlreadyFriendsException("This pandas are already friends!"); } if (!this.HasPanda(firstPanda)) { this.AddPanda(firstPanda); } if (!this.HasPanda(secondPanda)) { this.AddPanda(secondPanda); } this.pandasAndFriends[firstPanda].Add(secondPanda); this.pandasAndFriends[secondPanda].Add(firstPanda); }
public void CheckFriendsOfMethodWhenPandaIsNotInNetwork() { var panda = new Panda("Panda", "*****@*****.**", GenderType.Male); network.FriendsOf(panda); }