I have used Visual Studio 2015 (VS2015) to create the project
solution and the language i used is C#, please follow the steps
below to create the project
Open VS2015 and click on the New Project and Choose console
application under Visual C#
Once the console application
created add the classes Person and
Castle, by right click the solution and add new
ite under to whcih you will find Class
And similarly add the other class Castle by
following the same steps above
And now inside the Person and Castle class i have added the
properties and methods as given in the requirement
Please copy the class codes that given below and paste them in
appropriate class, and also please read the inline comment inside
the code which describes more about the code functionality
The comments are highlighted in bolder text
*******************Person.cs**********************************
namespace WESTEROS
{
public class Person
{
//local property refers to Castle class
private Castle homeCastle;
//Person constructor which sets the initail value if the
class properties
public Person()
{
Age = 0;
FirstName = string.Empty;
LastName = string.Empty;
homeCastle = new Castle();
}
//This is another overload constructor which has the
parameters for filling all the properties in the
class
public Person(string firstNameParam, string lastNameParam, int
ageParam, Castle homeCastleParam)
{
Age = ageParam;
FirstName = firstNameParam;
LastName = lastNameParam;
homeCastle = homeCastleParam;
homeCastle.AddPerson(this);
}
//This is also an overload constructor method which sets
the properties
//of firstname, lastname and age by using parameter
values
public Person(string firstNameParam, string lastNameParam, int
ageParam)
{
Age = ageParam;
FirstName = firstNameParam;
LastName = lastNameParam;
homeCastle = new Castle();
}
//This method returns the Title caps of the
person
public string TitleCaps(string strVal)
{
return FirstName + " " + LastName;
}
//This method converts all the property values to
string
//which make ease to display as output
public override string ToString()
{
return "Person [First: " + FirstName + ", Last: " + LastName + ",
Age: " + Age + ", Castle: " + HomeCastle.CastleName + "]";
}
//Below are the four properties of the class
//FirstName, LastName, Age and HomeCastle
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Castle HomeCastle
{
get { return homeCastle; }
set {
homeCastle = value;
homeCastle.AddPerson(this);
}
}
}
}
*************************END************************
And below is the Castle class code, whcih also has the inline
comment that explains the code functionality.
*********************Castle.cs**********************
using System;
using System.Collections.Generic;
namespace WESTEROS
{
public class Castle
{
//Private fields that used inside the properties of the
class
private string castleName;
private string castleLocation;
private List<Person> castlePeople;
private Dictionary<string, Person> peopleDictionary;
//Constructor which initialize or set the default value
to the properties
public Castle()
{
castleName = string.Empty;
castleLocation = string.Empty;
castlePeople = new List<Person>();
}
//This is an overload constructor method which sets the
properties
//of castleName, castleLocation and castlePeople by using parameter
values
public Castle(string castleNameValue, string locationValue)
{
castleName = castleNameValue;
castleLocation = locationValue;
castlePeople = new List<Person>();
}
//This method used to find the person with given first
name and last name
//This uses the dictionary to search the person list
object
public Person FindPerson(string firstNameValue, string
lastNameValue)
{
peopleDictionary = new Dictionary<string, Person>();
foreach (var people in castlePeople)
{
peopleDictionary.Add(people.FirstName+"_"+people.LastName,
people);
}
return peopleDictionary[firstNameValue+"_"+lastNameValue];
}
//This method used to remove the given person from the
person list
public bool RemovePerson(Person p)
{
try
{
castlePeople.Remove(p);
return true;
}
catch
{
return false;
}
}
//This method used to add the given person to the person
list
public bool AddPerson(Person p)
{
try
{
castlePeople.Add(p);
return true;
}
catch
{
return false;
}
}
//This method returns the Title cap of the Castle
object
public string TitleCaps(string strVal)
{
return CastleName + " " + Location;
}
//This method converts all the property values to
string
//which make ease to display as output
public override string ToString()
{
string members = string.Empty;
foreach (var member in FamilyMembers)
{
members = members + Environment.NewLine + "\t" +
member.ToString();
}
return "Castle [Name: " + CastleName + ", Location: " + Location
+ "]" + members;
}
//Below are the three properties of the class
//CastleName, FamilyMembers and Location
public string CastleName
{
get { return castleName; }
set { castleName = value; }
}
public List<Person> FamilyMembers
{
get { return castlePeople; }
set { castlePeople = value; }
}
public string Location
{
get { return castleLocation; }
set { castleLocation = value; }
}
}
}
****************************END**************************
The UML diagram of the methods and properties added in both the
classes are similar to the requirement shown in the question
UML
Diagram
Now inside the main method, i have called few methods from
Castle and Person to test the functionality.
Please read the inline comment to get an idea of
functionality
****************Console Program
Class********************
using System;
namespace WESTEROS
{
class Program
{
static void Main(string[] args)
{
//add a person in person P1 class by calling its
constructor
//similarly add a castle C1 in castle class by calling its
constructor
//then the newly added castle class C1 is assigned to person P1,
whether it adds the person to castle also
Console.WriteLine("**************Print Person P1**************" +
Environment.NewLine);
Castle c1 = new Castle("House Stark", "Winterfall");
Person p1 = new Person("Arya", "Stark", 12);
p1.HomeCastle = c1;
//This statement prints the person object, where it
prints the castle object in it
Console.WriteLine("P1 " + p1.ToString() + Environment.NewLine);
//Now add another person P2 and add this person to the
previously created castle object
Person p2 = new Person("Jon", "Snow", 20, c1);
c1.AddPerson(new Person("Sansa", "Stark", 14));
//If we print the castle object it will return both the
persons P1 and P2 that added before and now
Console.WriteLine("**************Print Catle C1 has three Persons
inside it**************" + Environment.NewLine);
Console.WriteLine("C1 " + c1.ToString() + Environment.NewLine);
//Now find the person P2 and print it
Console.WriteLine("**************Find Person Sansa, Stark and
Print**************" + Environment.NewLine);
Person findPerson = c1.FindPerson("Sansa", "Stark");
Console.WriteLine("P3 " + findPerson.ToString() +
Environment.NewLine);
//Remove the person P2
Console.WriteLine("**************Remove Person Sansa, Stark and
Print Castle**************" + Environment.NewLine);
bool isDeleted = c1.RemovePerson(findPerson);
//The below statement doesn't print the Person P2
because it removed in previous statement
if (isDeleted)
{
Console.WriteLine("C1 " + c1.ToString() +
Environment.NewLine);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
*******************END****************************
Now if you build and run the solution the output will be similar
as below
Output
Screen
New Project Recent 4 Installed NET Framework 4.6.1 Sort by: Default Search Installed Templates (Ctrl+E) ρ 、 Type: Visual C# A proiect for creating a command-line application Windows Forms Application Visual C# Templates ri WPF Application Visual C# Visual C# Windows C# Universal Visual C# Console Application ASP.NET web Application Shared Project Class Library (Package) Console Application (Package) Class Library Class Library (Portable) Silverlight Application Windows 8 Classic Desktop Web Android Cloud Extensibility ioS LightSwitch Office/SharePoint Silverlight Test WCF Workflow Visual C# Visual C# Visual C# Visual C# Visual C# Visual C# Visual Basic Visual C# Visual F# o online and find template D Online Name: Location: Westeros ojects Browse... Solution: Create new solution Create directory for solution Add to source control Solution name: Westeros Cancel
Program.cs × Source Control Explorer-Disconnected Solution Explorer WESTEROS . I WESTEROS-Program Main(string[] args) , using System using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks Search Solution Explorer (Ctrl-:) Solution WESTEROS' (1 project) ESTEROS Build Rebuild Clean View Analyze Properties References App.config Program.cs namespace WESTEROS 0 references class Program e Publish.. 0 references Publish as Azure WebJob... static void Main(string] args) Scope to This New Solution Explorer View Add Manage NuGet Packages Set as Startup Project Debug Source Control Cut Paste New lte... Ctri+Shift+A Existing item New Folder Azure API App Client... Reference... Web Reference... Service Reference... Connected Service.. Analyzer... Windows Form... User Control... Shift+Alt+A Ctrl+X Ctrl+V Del lorer Team Explorer Class View X Remove Rename Project Properties Unload Project Open Folder in File Explorer Properties le WESTEROS.csproj Alt+Enter Class.
Add New ltem - WESTEROS 4 Installed Sort by: Default Search Installed Templates (Ctrl+E) ρ Visual C# Items Type: Visual C#Items An empty class definition Visual C# Items %) Class Code Data General Interface Visual C# Items D Web Windows Form Visual C# Items Windows Forms WPF SQL Server Workflow User Control Visual C# Items Component Class Visual C# Items b Online User Control (WPF) Visual C# Items o About Box Visual C# Items ADO.NET Entity Data Model Application Configuration File Application Manifest File Assembly Information File Bitmap File Visual C# Items Visual C# Items Visual C# Items Visual C# Items Visual C# Items nline and find templat Name: Person.cs Add Cancel
namespace WESTEROS 18 references ] public class Person //local property refers to Castle class private Castle homeCastle; //Person constructor which sets the initail value if the class properties 0 references public Person() Age; FirstName -string.Empty; Lastlame string.Empty homeCastle new Castle(O; //This is another overload constructor which has the parameters for filling all the properties in the class 1 reference public Person (string firstNameParam, string lastNameParam, int ageParam, Castle homeCastleParam) Age-ageParam FirstName firstNameParam; Lastilame lastNameParam homeCastle-homeCastleParam; homeCastle.AddPerson (this); //This is also an overload constructor method which sets the properties //of firstname, lastname and age by using parameter values public Person (string firstNameParam, string lastNameParam, int ageParam) Age ageParam; FirstNamefirstNameParam; LastName 1astNameParam; homeCastlenew stle(O; //This method returns the Title caps of the person O references public string TitleCaps (string strVal) return FirstName " LastName; //This method converts all the property values to string //which make ease to display as output 6 references public override string ToStringO return "Person [First: "FirstName ", Last: LastNameAge:Age + ", Castle:HomeCastle.CastleName "]" //Below are the four properties of the class //FirstName, LastName, Age and HomeCastle 4 references public int Age get; set; 6 references public string FirstName { get; set; 6 references public string LastName get; set; public Castle HomeCastle get return homeCastle; set t homeCastle value; homeCastle.AddPerson(this);
sing System.Collections.Generic; amespace WESTEROS public class Castle //Private fields that used inside the properties of the class private string castleName; private string castleLocation; private List«Person» castlePeople; private Dictionary<string, Person> peopleDictionary //Constructor which initialize or set the default value to the properties 2 references public Castle() castleNamestring.Empty; castleLocation -string.Empty; castlePeople new List< Person>(); //This is an overload constructor method which sets the properties //of castleName, castleLocation and castlePeople by using parameter values 1 reference public Castle(string castleNameValue, string locationValue) castleNamecastleNameValue castleLocation locationValue; cast lePeople new List< Person>(); //This method used to find the person with given first name and last name //This uses the dictionary to search the person list object 1 reference public Person FindPerson (string firstNameValue, string lastNameValue) peopleDictionarynew Dictionary<string, Person>); foreach (var people in castlePeople) peopleDictionary.Add (people.FirstName+"_"+people.LastName, people); return peopleDictionary[firstNameValue+"_"+lastNameValue]; //This method used to remove the given person from the person list 1 reference public bool RemovePerson (Person p try castlePeople.Remove(p) return true; catch return false
//This method used to add the given person to the person list 3 references public bool AddPerson (Person p) try castlePeople.Add(p).; return true; catch return false //This method returns the Title cap of the Castle object O references public string TitleCaps (string strVal) return CastleName + Location; //This method converts all the property values to string //which make ease to display as output 6 references public override string ToStringO string membersstring.Empty; foreach (var member in FamilyMembers) members-membersEnvironment.NewLine + "\t"member.ToString); return "Castle [Name: CastleName", Location: "Location + "]" members; //Below are the three properties of the class //CastleName, FamilyMembers and Location 3 references public string CastleName get f return castleName; 1 set { castleName = value; } 1 reference public List<Person> FamilyMembers get return castlePeople; set castlePeoplevalue; 2 references public string Location get return castleLocation; set { castleLocation value; }
WESTEROS.cd" Castle.cs Person.cs Program.cs Source Control Explorer- Disconnected Solution Explorer Search Solution Explorer (Ctri+) |Castle Person Solution WESTEROS (1 project) [5] WESTEROS D F Properties D References · Fields castlelocation App.config -Properties D Castle.cs castlePeople peopleDictionary c# Person.cs D Program.cs s Age FirstName HomeCastle LastName Properties WESTEROS.cd Castiel larme FamilyMembers Location Methods Person2 overloads) Methods AddPerson Castle (+ 1 overload) ToString FindPerson ⓥ Removeperson TitleCaps ToString Solution Explorer Team Explorer Class View WESTEROS Class Diagram Group By Members Fomat Name Kind
using System; namespace WESTEROS O references class Program O references static void Main (stringC] args) //add a person in person P1 class by calling its constructor //similarly add a castle C1 in castle class by calling its constructor //then the newly added castle class C1 is assigned to person P1, whether it adds the person to castle also Castle c1new Castle("House Stark", "Winterfall"); Person plnew Person("Arya", "Stark", 12); p1.HomeCastle- c1; //This statement prints the person object, where it prints the castle object in İt Console.WriteLine("P1p1.ToString(Environment.NewLine); //Now add another person P2 and add this person to the previously created castle object Person p2new Person("Jon", "Snow", 20, c1); c1.AddPerson(new Person("Sansa", "Stark", 14)); //If we print the castle object it will return both the persons P1 and P2 that added before and now Console.WriteLine("*********Print Catle C1 has three Persons inside it****Environment.NewLine); Console.WriteLine("C1C1.ToString(Environment.NewLine) //Now find the person P2 and print it Person findPersonc1.FindPerson("Sansa", "Stark"); Console.Writeline("P3 " findPerson.ToString) Environment.NewLine); //Remove the person P2 Console.WriteLine("*****Remove Person Sansa, Stark and Print Castle************ bool isDeleted-c1.RemovePerson (findPerson); Environment.NewLine); //The below statement doesn't print the Person P2 because it removed in previous statement if (isDeleted) Console.WriteLine("C1 " + c1.ToString) Environment.NewLine); Console.WriteLine); Console.ReadLineO;
WESTEROS.EXE P1 Person [First: Arya, Last: Stark, Age: 12, Castle: House Stark] ******Print Catle C1 has three Persons inside it************** 1 Castle [Name: House Stark, Location: Winterfall] Person [First: Arya, Last: Stark, Age: 12, Castle: House Stark] Person [First: Jon, Last: Snow, Age: 20, Castle: House Stark] Person [First: Sansa, Last: Stark, Age: 14, Castle: P3 Person [First: Sansa, Last: Stark, Age: 14, Castle: *************Remove Person Sansa, Stark and Print Castle************* 1 Castle [Name: House Stark, Location: Winterfall] Person with name Sansa Stark found Person [First: Arya, Last: Stark, Age: 12, Castle: House Stark] Person [First: Jon, Last: Snow, Age: 20, Castle: House Stark] Sansa Stark Removed from the Person List