In: Computer Science
Can someone please provide C# code in Events,Delegates and Reflection for the below problem.
-----------------
Agent-Policy
XYZ Assurance wants to categorize the policies based on the agent who sold the policy and the Insurance type. Given an Agent name, display the policies that were sold by that agent. Similarly, print the policies in the given Insurance type. Write a program to do the same. They have a list of few policies and want to update the list with a few more policies.
Create a class named Agent with the following
attributes
Data Type | Variable Name |
int | _id |
string | _name |
Include appropriate Properties as follows.
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
Include a 2 argument constructor with _id and _name as
arguments.
Create a class named InsuranceType with the
following attributes
Data Type | Variable Name |
int | _id |
string | _type |
Include appropriate properties.
Include a 2 argument constructor with _id and _type as
arguments.
Create a class named Policy with the following
attributes
Data Type | Variable Name |
int | _id |
InsuranceType | _insuranceType |
Agent | _agent |
double | _premiumPayable |
Include appropriate Properties.
Include a 4 argument constructor with _id
,_insuranceType,_agent,_premiumPayable as arguments.
Create a class named PolicyBO and include the
following method,
Method Name | Method Description |
ListPoliciesByAgent(List<Policy> policyList, String name) | The return type of this method is List<Policy>. This method returns a list of policies for given agent |
ListPoliciesByType(List<Policy> policyList, String type) | The return type of this method is List<Policy>. This method returns a list of policies under given insurance type |
Create a class named Program to test the above
classes.
The collection for Policy class is prepopulated with predefined
values, and get n details from the user and add it to the
predefined list.
Get the policy details as a single string separated by " , " in the
order policy id, insuranceType id, insurance type, agent id, agent
name, premium payable. Split the input using Split() method, and
store the policy details in a list of type Policy.
Create a delegate method named
AgentPolicyList(List<Policy> policyList,string
_agentName) with List<Policy> as return type, and
call the ListPoliciesByAgent method using this
delegete method,to get the list of policies for given agent.
Use String.Format("{0, -20}{1, -20}{2:#.00}",
"PolicyId","InsuranceType","PremiumPayable"), to display the
policies for agent.
Create another delegate method named
TypePolicyList(List<Policy> policyList, string
_insuranceType) with List<Policy> as return type,
and call the ListPoliciesByType method using this
delegete method,to get the list of policies for given insurance
type.
String.Format("{0, -20}{1, -20}{2:#.00}",
PolicyId,InsuranceType,PremiumPayable), to display the policies for
insurance type.
Input and Output Format :
First line of the input corresponds to the number of policy
details, n.
Next n lines of the input corresponds to the Policy details
separated by " , ".
(n+1)th line of the input corresponds to the name of the agent
name, to get the policy details.
Last input corresponds to the insurance type, to get the policy
details.
All text in bold corresponds to input and rest corresponds to
output.
The following logs are already present in the Policy list. These
values have to be prefilled before adding new policy details.
PolicyId | InsuranceTypeId | InsuranceType | Agent Id | User | PremiumPayable |
2154 | 3158 | Life Insurance | 154 | Jacob | 25000 |
3401 | 3548 | Health Insurance | 251 | Daniel | 30000 |
2549 | 3154 | Vehicle Insurance | 154 | Jacob | 15000 |
3504 | 3158 | Life Insurance | 251 | Daniel | 20000 |
2501 | 3548 | Health Insurance | 624 | Andrew | 35000 |
2509 | 3154 | Vehicle Insurance | 624 | Andrew | 27000 |
Sample Input and Output :
2
2541,3254,Life Insurance,251,Aiden,20000.50
2571,3846,Health Insurance,154,Joseph,15000
Daniel
Policy List
PolicyId
InsuranceType
PremiumPayable
3401
Health Insurance 30000.00
3504
Life
Insurance
20000.00
Life Insurance
Policy List
PolicyId
Agent
PremiumPayable
2154
Jacob
25000.00
3504
Daniel
20000.00
2541
Aiden
20000.50
--------------------------
Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
delegate List<Policy> Searching(List<Policy> list,string search);
//........Agent class...............
public class Agent
{
public int _id;
public string
_name;
public Agent(int
id,string name)
{
_id=id;
_name=name;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
}
//........Insurance Type
class...............
public class InsuranceType
{
public int _id;
public string
_type;
public InsuranceType(int
id,string type)
{
_id=id;
_type=type;
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
}
public class Policy
{
public int _id;
public double
_premiumPayable;
public InsuranceType
_insuranceType;
public Agent
_agent;
public Policy(int
id,InsuranceType insuranceType,Agent agent,double
premiumPayable)
{
_id=id;
_insuranceType=insuranceType;
_agent=agent;
_premiumPayable=premiumPayable;
}
public double PremiumPayable
{
get { return _premiumPayable; }
set { _premiumPayable = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
}
//..................PolicyBO
Class......................
public class PolicyBO
{
public
List<Policy> ListPoliciesByAgent(List<Policy>
policyList, String name)
{
List<Policy> new1=new List<Policy>();
//Policy p=new Policy();
foreach(Policy p in policyList)
{
if(p._agent.Name==name)
{
new1.Add(p);
}
}
return new1;
}
public
List<Policy> ListPoliciesByType(List<Policy>
policyList, String type)
{
List<Policy> new1=new List<Policy>();
foreach(Policy p in policyList)
{
if(p._insuranceType.Type==type)
{
new1.Add(p);
}
}
return new1;
}
}
public class Program
{
public static void
Main(string[] args)
{
List<Policy> policyList=new List<Policy>();
Agent a;
InsuranceType i_t;
Policy p;
string str;
double pp=25000;
int id;
a=new Agent(154,"jacob");
i_t=new InsuranceType(3158,"life insurance");
p=new Policy(2154,i_t,a,25000);
policyList.Add(p);
a=new Agent(251,"daniel");
i_t=new InsuranceType(3548,"health insurance");
p=new Policy(3401,i_t,a,30000);
policyList.Add(p);
a=new Agent(154,"jacob");
i_t=new InsuranceType(3154,"vehicle insurance");
p=new Policy(2549,i_t,a,15000);
policyList.Add(p);
a=new Agent(251,"daniel");
i_t=new InsuranceType(3158,"life insurance");
p=new Policy(3504,i_t,a,20000);
policyList.Add(p);
a=new Agent(624,"andrew");
i_t=new InsuranceType(3548,"health insyrance");
p=new Policy(2501,i_t,a,35000);
policyList.Add(p);
a=new Agent(624,"andrew");
i_t=new InsuranceType(3154,"vehicle insurance");
p=new Policy(2509,i_t,a,27000);
policyList.Add(p);
/*.......................
.....using delegates.....
.......................*/
PolicyBO pb=new PolicyBO();
List<Policy> found=new List<Policy>();
string search;
//Searching policies by agent name
Searching sc=new Searching(pb.ListPoliciesByAgent);
Console.Write("Enter a name:");
search=Console.ReadLine();
found=sc(policyList,search);
Console.WriteLine("ID\tInsuranceType\tPremiumBayable\n");
foreach(Policy res in found)
{
Console.WriteLine(res.ID+"\t"+res._insuranceType.Type+"\t"+res.PremiumPayable+"\n");
}
//Searching policies by Insurance Type
sc=new Searching(pb.ListPoliciesByType);
Console.Write("Enter Insurance Type:");
search=Console.ReadLine();
found=sc(policyList,search);
Console.WriteLine("ID\tInsuranceType\tPremiumBayable\n");
foreach(Policy res in found)
{
Console.WriteLine(res.ID+"\t"+res._insuranceType.Type+"\t"+res.PremiumPayable+"\n");
}
Console.WriteLine("\nEnd\n");
}
}