Question
Write a C++ program and give output that creates a database based on the characters of Game of Thrones using get and set. The database will consist of two classes: Castle, and Person. You will need to include at least 3 castles and no less than three people per castle.
References for people and castles are:
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=characters%20game%20of%20thrones
And
http://gameofthrones.wikia.com/wiki/Great_Houses
You are asked to write a C# application to create the wESTEROS database (based on the fictional characters of the Game of Thrones by G.G. Martin). The database consists of two classes: Castle, and Person. Classes contain Fields, Properties, and Methods. The database schema is shown below Methads Caste。 p) boel Implement each of the properties, fields and methods of the classes shown above You will include at least three castles and no less than three people per castle. A reference to the Game of Thrones characters follows: aracters%, ne%20of%20thr Another useful reference is: NOTES (1) Feel free to add methods and properties to enhance the model. (2) Find methods must be based on the Dictionary-class. (3) Add tests to prove the functioning of each method supported by the classes (4) Organization, documentation, and niceness is important and will definitely affect your grade The following fragment is an illustration of the work ahead Castle c1 -new castle(House Stark, -Winterfell); Person p1- new Person(Arya Stark, 12); p1.HomeCastle- cl; Console.WriteLine( P1 p1); Person p2 new Person(Jon, Snow, 28, c1); c1.AddPerson (new Person(Sansa Stark, 14)); Console.WriteLine( C1c1); PersonlFirst:Arya, Lest:Stark, Age:12, Castle: House 1 Castle IName: House Stark, Location: Vinterfell Person First:Jon, Last:Snow, Age:20, Castle:Hou Person First:Arya, Last:Stark, lge: 12, Castle:H Person First:Sansa, Lest:Stark, Age:1, Castle:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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#

New Project Recent 4 Installed NET Framework 4.6.1 Sort by: Default Search Installed Templates (Ctrl+E) ρ 、 Type: Visual C# AOnce 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

Program.cs × Source Control Explorer-Disconnected Solution Explorer WESTEROS . I WESTEROS-Program Main(string[] args) , using

Add New ltem - WESTEROS 4 Installed Sort by: Default Search Installed Templates (Ctrl+E) ρ Visual C# Items Type: Visual C#Ite

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************************

namespace WESTEROS 18 references ] public class Person //local property refers to Castle class private Castle homeCastle; //P

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**************************

sing System.Collections.Generic; amespace WESTEROS public class Castle //Private fields that used inside the properties of th//This method used to add the given person to the person list 3 references public bool AddPerson (Person p) try castlePeople.

The UML diagram of the methods and properties added in both the classes are similar to the requirement shown in the question

UML Diagram

WESTEROS.cd Castle.cs Person.cs Program.cs Source Control Explorer- Disconnected Solution Explorer Search Solution Explorer

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****************************

using System; namespace WESTEROS O references class Program O references static void Main (stringC] args) //add a person in p

Now if you build and run the solution the output will be similar as below

Output Screen

WESTEROS.EXE P1 Person [First: Arya, Last: Stark, Age: 12, Castle: House Stark] ******Print Catle C1 has three Persons inside

Add a comment
Know the answer?
Add Answer to:
Write a C++ program and give output that creates a database based on the characters of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT