Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write in C# , I will Thumbs up in rating!!! Target Platform: Console/.NET Programming Language: C# Description: Create a C#/.NET console application that analyzes

Please write in C# , I will Thumbs up in rating!!!

Target Platform: Console/.NET

Programming Language: C#

Description: Create a C#/.NET console application that analyzes crime data based on a comma-separated-value (CSV) file and generates a report that answers a set of provided questions that is saved in a text file. The path to the CSV file and the path to the report text file are provided to the program as command line arguments. If the user runs the program without supplying the arguments, they should be provided with the syntax for running the program. All exceptions and errors are to be appropriately handled by giving the user feedback and exiting the program. Under no circumstances should the program crash.

Purpose: This application provides experience creating a C#/.NET console application, accepting command line arguments, reading and parsing CSV data, creating data structures, querying data using LINQ, writing a report to a text file, and handling exceptions/errors.

The program is to receive the path to the comma-separated-value (CSV) file as a command line argument. If the CSV file is in the same directory as the program, then the user can just supply the name of the file as the path.

The records in the CSV file are to be read by the program and placed in a collection of structured data. Then, Language INtegrated Query (LINQ) along with general C# code is to be used to determine the answers to a set of questions and a report is to be generated in a text file that is saved. The path of the report file is also to be supplied as a command line argument. If the path of the report file is just the name of the file, then it will be saved in the current directory.

Running the application at the command line in Windows:

CrimeAnalyzer.exe

Running the application at the command line in macOS:

dotnet CrimeAnalyzer.dll

When the application is being run, tested, and debugged in Visual Studio, place the CrimeData.csv file in the same directory as the CrimeAnalyzer.csproj and Program.cs files. When the program runs from within Visual Studio, the project directory is the current directory and where CrimeData.csv will be found, if the name of the file is supplied as the path.

Crime Data CSV File

The data source for the application is CrimeData.csv ( **Data for CrimeData.csv is listed below in BOLD** )

Year,Population,Violent Crime,Murder,Rape,Robbery,Aggravated Assault,Property Crime,Burglary,Theft,Motor Vehicle Theft 1994,260327021,1857670,23326,102216,618949,1113179,12131873,2712774,7879812,1539287 1995,262803276,1798792,21606,97470,580509,1099207,12063935,2593784,7997710,1472441 1996,265228572,1688540,19645,96252,535594,1037049,11805323,2506400,7904685,1394238 1997,267783607,1636096,18208,96153,498534,1023201,11558475,2460526,7743760,1354189 1998,270248003,1533887,16974,93144,447186,976583,10951827,2332735,7376311,1242781 1999,272690813,1426044,15522,89411,409371,911740,10208334,2100739,6955520,1152075 2000,281421906,1425486,15586,90178,408016,911706,10182584,2050992,6971590,1160002 2001,285317559,1439480,16037,90863,423557,909023,10437189,2116531,7092267,1228391 2002,287973924,1423677,16229,95235,420806,891407,10455277,2151252,7057379,1246646 2003,290788976,1383676,16528,93883,414235,859030,10442862,2154834,7026802,1261226 2004,293656842,1360088,16148,95089,401470,847381,10319386,2144446,6937089,1237851 2005,296507061,1390745,16740,94347,417438,862220,10174754,2155448,6783447,1235859 2006,299398484,1435123,17309,94472,449246,874096,10019601,2194993,6626363,1198245 2007,301621157,1422970,17128,92160,447324,866358,9882212,2190198,6591542,1100472 2008,304059724,1394461,16465,90750,443563,843683,9774152,2228887,6586206,959059 2009,307006550,1325896,15399,89241,408742,812514,9337060,2203313,6338095,795652 2010,309330219,1251248,14722,85593,369089,781844,9112625,2168459,6204601,739565 2011,311587816,1206005,14661,84175,354746,752423,9052743,2185140,6151095,716508 2012,313873685,1217057,14856,85141,355051,762009,9001992,2109932,6168874,723186 2013,316128839,1163146,14196,79770,345031,724149,8632512,1928465,6004453,699594

The first line of the CSV file is comprised of the headers that identify the columns. Use the headers to identify the meaning of the data. When your program reads the file, the first line needs to be skipped since it doesn't contain data. Do not delete the first line of the CSV file. Other CSV files provided in the future will have the header, so you need to accept that it is present and handle it.

When a line is read from the file, it can be split on the commas so that the pieces of data are put in an array. The order of the data elements in the array will be the same order as they are listed in a record in a line.

var line = reader.ReadLine(); var values = line.Split(',');

Make sure the number of values obtained from a line is the expected number of values.

Handline Errors

A well-written program should not crash regardless of the input or data provided to it. You should handle all exceptions and error conditions that arise when the program is run. If an exception or error occurs, it should be reported to the user and the program should exit.

If the user runs the program without providing the two command line parameters, then the program is to display a message to the user indicating how the program is to be run.

CrimeAnalyzer

The following are some of the errors that could occur that need to be handled. Note, there is no implication that these are all the possible errors. They are being provided to give you a sense of error conditions that could arise that need to be handled.

CSV file can't be opened.

Error occurs reading lines from CSV file.

Record doesn't contain the correct number of data elements.

Record contains data that is not of the right type. Note: All the data is numerical.

Report file can't be opened or written to.

When you display an error message to the user, be as specific as possible about the problem and where it occurred. For example, if a record is malformed the line number of the record in the file should be included in the message to the user along with any other info that could be helpful in finding and correcting the problem.

The following is an example of an error message to the user when the wrong number of values is encountered in a record.

$"Row {lineNumber} contains {values.Length} values. It should contain {NumItemsInRow}."

Using Language INtegrated Query (LINQ)

LINQ is very helpful in answering the questions provided below. To illustrate how LINQ works to help with this program, a LINQ statement is provided below to answer the question: What years is the number of rapes less than 85000?

var years = from crimeStats in crimeStatsList where crimeStats.Rape < 85000 select crimeStats.Year;

After the above line executes, years is a collection (IEnumerable) of the years that were found using the LINQ statement.

In the above LINQ, crimeStatsList is a variable referencing a List of instances of a class (in my case CrimeStats) that was created in the C# code before this LINQ statement. The instances of the class in the List holds the pieces of data from a row in CSV data for Population, ViolentCrime, Murder, Rape, etc. crimeStats in from crimeStats in crimeStatsList is a variable in the LINQ that refers to individual instances from the list of instances. So, instances of crimeStats are going to be gotten out of (from) crimeStatsList. The name crimeStats was chosen by me to indicate an instance from the list. The name can be any valid variable name. The where condition establishes which instances of crimeStats are to be included. In the above example, crimeStats.Rape < 85000 indicates instances of crimeStats for which the number of rapes is less than 85000 are to be included. Rape is a property of the CrimeStats class. crimeStats.Rape is the quantity of rapes for a given instance of CrimeStats. The select near the end determines what information is returned. select crimeStats.Year indicates that from the instances of CrimeStats, the Year property value is to be chosen. Since there is possibly more than one instance of CrimeStats that could meet the condition of the where clause, the result is an IEnumerable which means we can use foreach to iterate through the results collection.

.

The following is the way to iterate through an IEnumerable.

foreach (var year in years) { Console.WriteLine(year); }

.

.

Questions

The questions to be answered by the application that are to be used as the basis of a report that the application generates are the following.

What is the range of years included in the data?

How many years of data are included?

What years is the number of murders per year less than 15000?

What are the years and associated robberies per year for years where the number of robberies is greater than 500000?

What is the violent crime per capita rate for 2010? Per capita rate is the number of violent crimes in a year divided by the size of the population that year.

What is the average number of murders per year across all years?

What is the average number of murders per year for 1994 to 1997?

What is the average number of murders per year for 2010 to 2013?

What is the minimum number of thefts per year for 1999 to 2004?

What is the maximum number of thefts per year for 1999 to 2004?

What year had the highest number of motor vehicle thefts?

.

Example Report

Crime Analyzer Report

Period: 1994-2013 (20 years)

Years murders per year < 15000: 2010, 2011, 2012, 2013

Robberies per year > 500000: 1994 = 618949, 1995 = 580509, 1996 = 535594

Violent crime per capita rate (2010): 0.0040450234834638

Average murder per year (all years): 16864.25

Average murder per year (1994-1997): 20696.25

Average murder per year (2010-2014): 14608.75

Minimum thefts per year (1999-2004): 6937089

Maximum thefts per year (1999-2004): 7092267

Year of highest number of motor vehicle thefts: 1994

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions