In: Computer Science
USE C#
Maximum used policy
At the moment, AmSure Insurance Agency distributes exciting Life
Insurance Products, Health Insurance Products and Motor Insurance
Products. The company wants to find the policy type that has
maximum number of users. Given a CSV file with all the policy
details that are availed through Amsure, Write a program to find
the policy type which has maximum users using LINQ
statements.
Create a class Policy which include following
attributes,
Data Type | Variable Name |
int | _policyNumber |
string | _insuranceType |
DateTime | _startDate |
DateTime | _renewalDate |
Include appropriate Properties as follows.
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
Create the policy.csv file which contain the policy details (ie)
policy number, insurance type, policy start date and policy renewal
date which is separated by comma.
Create a program class. Read the policy.csv file and store it into
the policy list. Write a LINQ query to find the policy type under
which maximum policies are there.
Input and Output Format:
Input consists of policy.csv file.
Output should display the policy type under which maximum policies
are there.
Sample input and output :
Input file policy.csv contain the details as
follows,
6435,Home Insurance,01-03-2016,10-07-2017
5234,Health Insurance,12-12-2014,06-01-2019
5614,Health Insurance,02-09-2015,03-11-2019
8575,Motor Insurance,07-11-2011,03-12-2017
8541,Health Insurance,01-11-2015,07-12-2016
4526,Motor Insurance,05-01-2017,13-09-2018
Output :
Health Insurance
Updated policy.csv file path.
Also update date time format if required, It Is dd-MM-yyyy for now according to the test data provided.
Please find code in bold.
// Program.cs
using System;
using System.IO;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void
Main(string[] args)
{
List
// Read all lines from file in string array
string[] lines =
File.ReadAllLines(@"C:\WorkSpace\Chegg\policy.csv");
foreach (var line in lines)
{
string[] values = line.Split(',');
Policy policy = new Policy();
policy.PolicyNumber = Convert.ToInt32(values[0]);
policy.InsuranceType = values[1];
policy.StartDate = DateTime.ParseExact(values[2], "dd-MM-yyyy",
CultureInfo.InvariantCulture);
policy.RenewalDate = DateTime.ParseExact(values[3], "dd-MM-yyyy",
CultureInfo.InvariantCulture);
policyList.Add(policy);
}
// LinQ statement to get most insured policy type
var pl = policyList
.GroupBy(p => p.InsuranceType)
.OrderByDescending(x => x.Count())
.FirstOrDefault();
Console.WriteLine(pl.Key);
Console.Read();
}
}
}
// Policy.cs
using System;
namespace ConsoleApp
{
public class Policy
{
private int
_policyNumber;
private string
_insuranceType;
private DateTime
_startDate;
private DateTime
_renewalDate;
public int
PolicyNumber
{
get { return _policyNumber; }
set { _policyNumber = value; }
}
public string
InsuranceType
{
get { return _insuranceType; }
set { _insuranceType = value; }
}
public DateTime
StartDate
{
get { return _startDate; }
set { _startDate = value; }
}
public DateTime
RenewalDate
{
get { return _renewalDate; }
set { _renewalDate = value; }
}
}
}