Can someone please provide C# code in exception handling for below problem.- ---
ID: 3744826 • Letter: C
Question
Can someone please provide C# code in exception handling for below problem.-
--------------------------------------------------
Invalid Agent
XYZ Insurance company issues an online customer portal wherein the customer submits a claim request to avail any claim. This portal checks for the following,
1. Policy renewal date must be ahead of the policy start date.
2. Agent id corresponding to the agent must be registered with the company.
Write a program to validate the above details and display an error message if it is not in the given format.
Create classes InvalidDateException and InvalidAgentException which extends Exception class.
Create Policy class with the following attributes
Create a 4 argument constructor with the following arguments _policyId, _brokerId, _policyStartDate, _policyRenewalDate
Override the ToString() method and return the policy details in the given format( refer sample input and output).
Create Agent class with the following attributes
Create a 2 argument constructor with the following arguments _agentId, _agentName
Create PolicyBO class with the following method
Create Program class to test the above classes.
Prepopulate the agent list and get the policy details and call the ValidateData() method. To handle both the exceptions surround the method call with try catch block.
Prepopulate the following data in the agent List
Input and Output Format:
The input is policy details as a single string separated by " , ". The order of the input must be policy ID, broker ID, policy start date, policy renewal date.
The dates must be in the (dd-mm-yyyy) format.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input/Output 1:
LI1001,1208,11-04-2015,11-05-2015
Exception : Agent not found!
Sample Input/Output 2:
LI1001,1201,11-4-2015,11-05-2015
Exception : Invalid date!
Sample Input/Output 3:
LI1001,1201,11-04-2015,11-05-2015
Policy ID : LI1001
Broker ID : 1201
Policy Start Date : 11-04-2015
Policy Renewal Date : 11-05-2015
-------------------------------
Thanks
Data Type Variable Name string _policyId string _brokerId DateTime _policyStartDate DateTime _policyRenewalDateExplanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Policy_Test
{
//Exception Class
public class InvalidDateException:Exception
{
//Overide the Exception Message
public override string Message
{
get
{
return "Exception : Invalid date!";
}
}
}
public class InvalidAgentException: Exception
{
public InvalidAgentException(string msg):base(msg)
{
}
//Overide the Exception Message
}
public class Policy
{
private string _policyId;
private string _brokerId;
private DateTime _policyStartDate;
private DateTime _policyRenewalDate;
public Policy(string PolicyId, string BrokerId, DateTime PolicyStartDate, DateTime PolicyRenewalDate)
{
this._brokerId = BrokerId;
this._policyId = PolicyId;
this._policyStartDate = PolicyStartDate;
this._policyRenewalDate = PolicyRenewalDate;
}
public override string ToString()
{
string[] arr = this._policyStartDate.ToShortDateString().ToString().Split('/');
string[] arr1 = this._policyRenewalDate.ToShortDateString().ToString().Split('/');
//Overiding ToString and generating comma separetged value
return "Policy ID:" + this._policyId + " " + "Broker ID : " + this._brokerId + " " + "Policy Start Date :" + string.Join("-", arr) + " " + "Policy Renewal Date :" + string.Join("-", arr1);
}
}
public class Agent
{
public string _agentId;
private string _agentName;
public Agent(string agentId, string agentName)
{
_agentId = agentId;
_agentName = agentName;
}
}
public class PolicyBO
{
public Policy ValidateData(string _policyDetails, List<Agent> agentList)
{
Policy p =null;
string[] arr = _policyDetails.Split(',');
if (arr[2].Length < 10 || arr[3].Length < 10)
throw new InvalidDateException();
else
{
//Polici Start Date
DateTime strDT = DateTime.Parse(arr[2]);
//Policy Renewel Date
DateTime rnDT = DateTime.Parse(arr[3]);
//Find agent with specified ID
Agent ag = agentList.FirstOrDefault(a => a._agentId == arr[1]);
try
{
if (ag == null)
throw new InvalidAgentException("Exception : Agent not found!");
else
p = new Policy(arr[0], arr[1], strDT, rnDT);
}
catch(InvalidAgentException ia)
{
Console.WriteLine(ia);
}
}
return p;
}
}
class Program
{
static void Main(string[] args)
{
//Create Prepopulate the agent List
List<Agent> agentList = new List<Agent>()
{
new Agent("1201","Abraham"),
new Agent("1202","Adam"),
new Agent("1203","Albert"),
new Agent("1204","Blake")
};
//Now read the policy data from screen
string s = Console.ReadLine();
Policy p = new PolicyBO().ValidateData(s, agentList);
if (p != null) {
Console.WriteLine(p);
Console.WriteLine("-------------------------------");
}
Console.WriteLine("Thanks");
}
}
}