Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A user would like to query a data file containing demographic information about

ID: 3599776 • Letter: A

Question

A user would like to query a data file containing demographic information about the fifty US states. You are to write a C# Windows form program to perform and display the query results.

The US state demographics information is in a CSV file named stateDemographics.csv. This file, which also contains the png pictures file for each state, is located on Blackboard with the file name of stateFlagPictures.zip. Download and upzip the files.

The format of the stateDemographics.csv consists of 51 lines of text, with the first line of text describing the name of each field in the text line, separate by commas.

You are to read this file, parse each text line into the individual fields, stored the fields into a StateData object container, and then store each StateData object into a List.

The first line in the file contains the names for each header.

'ID,'Name,'Abbreviation,'DateStatehood,'Capital,'LandArea,'Population,'BlackPopulation,'FlagName

1,Alabama,AL,12/14/1819,Montgomery,155.40,4858979,1329356,alabama.png

2,Alaska,AK,1/3/1959,Juneau,2716.70,738432,39863,alaska.png

3,Arizona,AZ,2/14/1912,Phoenix,474.90,6828065,388585,arizona.png

Perform queries on the data in the List to answer the following questions. Display the results to a richTextBox.

Display all information about each state.

Display all states by population size.

Display all states by black population size.

Calculate and display the percentage of black people in each state.

Calculate and display the number of people per square mile in each state.

Calculate and display how long each state has been a member of the United States of America.

Display a header before display each query.

Separate each query with three blank lines

Have one line of text describing the query text

Have one line of dashes next

Then display the query results.

Example

Query for Population > 1,000,000 && Population < 2,000,000

--------------------------------------------------------------------------------------

Idaho, 1,654,930

Hawaii 1,431,603

Maine 1,329,328

Create two classes. One class, called StateData to hold your data and another class to process all the queries.

Explanation / Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

// BingAds.AdIntelligence is the application-defined namespace that
// this example uses for the Ad Intelligence service reference.

using GetDemographics.BingAds.AdIntelligence;


namespace GetDemographics
{
class Program
{
private static AdIntelligenceServiceClient service = null;

// Specify your credentials.

private static string m_password = "<passwordgoeshere>";
private static string m_username = "<usernamegoeshere>";
private static string m_token = "<devtokengoeshere>";

// Specify the advertiser's account ID.

private static long m_accountId = <accountidgoeshere>;


static void Main()
{
KeywordDemographicResult[] demographics = null;

try
{
service = new AdIntelligenceServiceClient();

demographics = GetDemographics(m_accountId);

if (demographics != null)
{
foreach (KeywordDemographicResult keyword in demographics)
{
Console.WriteLine(keyword.Keyword);

if (null == keyword.Device)
{
Console.WriteLine(" There is no demographic data available for the keyword. ");
}
else
{
Console.WriteLine(" " + keyword.Device);
Console.WriteLine(" Female: {0:n0}", keyword.KeywordDemographics.Female);
Console.WriteLine(" Male: {0:n0}", keyword.KeywordDemographics.Male);
Console.WriteLine(" Ages 18 to 24: {0:n0}", keyword.KeywordDemographics.Age18_24);
Console.WriteLine(" Ages 25 to 34: {0:n0}", keyword.KeywordDemographics.Age25_34);
Console.WriteLine(" Ages 35 to 49: {0:n0}", keyword.KeywordDemographics.Age35_49);
Console.WriteLine(" Ages 50 to 64: {0:n0}", keyword.KeywordDemographics.Age50_64);
Console.WriteLine(" Over 64: {0:n0}", keyword.KeywordDemographics.Age65Plus);
}
}
}

service.Close();
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);

if (null != e.InnerException)
{
Console.WriteLine(" " + e.InnerException.Message);
}

if (service != null)
{
service.Abort();
}
}
catch (TimeoutException e)
{
Console.WriteLine(e.Message);

if (service != null)
{
service.Abort();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);

if (service != null)
{
service.Abort();
}
}
}


// Get the age and gender of users who have searched for the specified keywords.
// The demographic information is broken out by device type.

static KeywordDemographicResult[] GetDemographics(long accountId)
{
GetKeywordDemographicsRequest request = new GetKeywordDemographicsRequest();
GetKeywordDemographicsResponse response = null;

// Set the header information.

request.CustomerAccountId = accountId.ToString();
request.DeveloperToken = m_token;
request.UserName = m_username;
request.Password = m_password;

// Set the request information.

request.Device = new string[] { "Smartphones", "Computers", "NonSmartphones" };
request.Keywords = new[] { "flower delivery" };
request.Language = "English";
request.PublisherCountry = "US";

try
{
response = service.GetKeywordDemographics(request);
}
catch (FaultException<AdApiFaultDetail> fault)
{
// Log this fault.

Console.WriteLine("GetKeywordDemographics failed with the following faults: ");

foreach (AdApiError error in fault.Detail.Errors)
{
if (105 == error.Code) // InvalidCredentials
{
Console.WriteLine("The specified credentials are not valid or the account is inactive.");
}
else
{
Console.WriteLine("Error code: {0} ({1}) Message: {2} Detail: {3} ",
error.ErrorCode, error.Code, error.Message, error.Detail);
}
}

}
catch (FaultException<ApiFaultDetail> fault)
{
// Log this fault.

Console.WriteLine("GetKeywordDemographics failed with the following faults: ");

foreach (OperationError error in fault.Detail.OperationErrors)
{
switch (error.Code)
{
case 106: // UserIsNotAuthorized
Console.WriteLine("The user is not authorized to call this operation.");
break;

case 1257: // CampaignServiceMissingLanguage
Console.WriteLine("The language cannot be null or empty.");
break;

case 1505: // CampaignServiceKeywordsArrayShouldNotBeNullOrEmpty
Console.WriteLine("The list of keywords cannot be null or empty.");
break;

case 3408: // CampaignServiceLanguageAndCountryNotSupported
Console.WriteLine("The specified publisher country is not allowed for the specified language.");
break;

case 3412: // CampaignServiceInvalidLanguage
Console.WriteLine("The specified language is not valid.");
break;

case 3413: // CampaignServiceInvalidPublisherCountry
Console.WriteLine("The specified publisher country is not valid.");
break;

case 3417: //CampaignServiceInvalidDevice
Console.WriteLine("One or more of the specified devices is not a valid device type.");
break;

default:
Console.WriteLine("Error code: {0} ({1}) Message: {2} Detail: {3} ",
error.ErrorCode, error.Code, error.Message, error.Details);
break;
}
}

foreach (BatchError error in fault.Detail.BatchErrors)
{
Console.WriteLine("Error code: {0} ({1}) Index: {2} Message: {3} Detail: {4} ",
error.ErrorCode, error.Code, error.Index, error.Message, error.Details);
}

}

return (null == response) ? null : response.KeywordDemographicResult;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote