Program to Dynamically Allocate Bank Accounts To the Agents/Tellecaller in c#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 595
The below program will do so -

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//step 1 - program starts

//Data to be filled on a Case By Case Basis
List<string> lstCustomerBankAccounts = new List<string>(); //Step 2

//Data to be filled on a Case By Case Basis
List<string> lstAgents = new List<string>(); //Step 3

var countCustomerBankAccounts = lstCustomerBankAccounts.Count; //Step 4

var countAgents = lstAgents.Count; //Step 5

var repeatCount = countCustomerBankAccounts / countAgents + 1; //Step 6

var numberOfAgentsGenerated = Enumerable.Repeat(lstAgents, repeatCount).SelectMany(m => m); //Step 7

var customerBankAccountToAgentMapping =
numberOfAgentsGenerated
.Zip(lstCustomerBankAccounts,
(agent, account) => new { Agent = agent, CustomerBankAccounts = account }
); //Step 8

var groupcustomerBankAccountByAgent = customerBankAccountToAgentMapping.GroupBy(a => a.Agent); //Step 9

var result = groupcustomerBankAccountByAgent
.Select(g => new
{
Agent = g.Key
,
Accounts = g.Select(x => x.CustomerBankAccounts).ToList()
});
foreach (var r in result)
{
Console.WriteLine("Agent Name: " + r.Agent);
foreach (var cba in r.Accounts)
{
Console.WriteLine(" Is holding " + cba);
}
Console.WriteLine(Environment.NewLine);
}//Step 10

Console.ReadKey();//Step 11
}
}
}


The above program works on the below algorithm

Step 1: Start
Step 2: Get the Customer Bank Accounts list.
Step 3: Get the Agents list.
Step 4: Count of Step 2
Step 5: Count of Step 3

Step 6:
Case 1 : If Count(Step 4) < Count(Step 5) Then Go To Step 7.
Case 2 : If Count(Step 4) = Count(Step 5) Then Go To Step 7.
Case 3 : If Count(Step 4) > Count(Step 5) Then Go To Step 7.

Step 7:
a) Divide Count(Step 4) / Count(Step 5) + 1
b) Number of Agents generated = Repeat of Agents in a Round Robin way till it exhausts Count(Step 5) * (a)

Step 8: Map in a sequential way Step 6 and Step 2 until the smaller of them exhausts.
Step 9: Group Customer Bank Accounts obtained in Step 7 by their Agents.
Step 10: Display.
Step 11: Stop.

Comments or Responses

Posted by: Sheonarayan on: 1/5/2016 Level:HonoraryPlatinum | Status: [Administrator] | Points: 10
Good chunk of code snippets, however little bit of explanations would have added a lot of value to this post.
Posted by: Rajnilari2015 on: 1/6/2016 Level:Platinum | Status: [Member] [Microsoft_MVP] [MVP] | Points: 10
@Sheonarayan Sir, updated the post as per your suggestion

Login to post response