A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.
This class is defined in the System.Collections.Generic namespace, so you should import or using System.Collections.Generic namespace.
Dictionary<TKey,TValue>
TKey - The type of the keys in the dictionary.
TValue - The type of the values in the dictionary.
Input Occupancy String format:
Room_Adult_Children_Age : 1_2_1_10 [room always in string 1]
1_2_1_10 ; 1_2_1_8 or 1_2_1_10
; 1_2_1_8 ; 1_2_1_5
Output: 2_1_2_10_11
Input: 1_1_0_-1; 1_1_0_-1
Output: 2_1_0_-1
Input: 1_2_2_6_8 ; 1_2_2_4_6 ; 1_2_2_6_9
If you want to search for 2 single
room with 2 adult and 0 child you pass the occupancy like that:
Input: 1_2_0_-1 ; 1_2_0_-1
If 2 or 3
room then Input Occupancy string format:
Scenario 1: Search for 1
Adult
If you want to search for 1
single room with 1 adult and 0 child you pass the occupancy like that: Input: 1_1_0_-1
Where -1 stands for an age of
an adult which is not required so we pass it -1.
Scenario 2: Search for 1
Adult and 2 Children
If you want to search for 1
single room with 1 adult and 2 child you pass the occupancy like that:
Input: 1_1_1_10;1_1_1_11
where
10 stand for the first age of the first child and 11 stands for the second age
of the child.
Scenario 3: Search for 2
rooms one for 1 Adult and 0 Children and the Second for 2 Adults
If you want to search for 2
rooms with 1 adult and 0 children and another room for 2 adults and 0 children
you pass them like that:
Input: 1_1_0_-1;1_2_0_-1
Output: 1_1_0_-1;1_2_0_-1
Scenario 4: Search for 2
rooms for the same 1 Adult and 0 Children.
It will be like that
Scenario 5: Search for 3
rooms for the same 2 Adult and 2 Children.
It will be
like that
Output: 3_2_2_6_8_4_6_6_9
Scenario 6: Search for 2
Adult 0 Children
Output: 2_2_0_-1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string GroupOccupancy(string occupancy)
{
string abc = "";
string[] arrOccupancy = occupancy.Split(';');
List<string> objage = new List<string>();
/*The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings.*/
Dictionary<object, int> DifferentCarcategory = new Dictionary<object, int>();
foreach (Object obj in arrOccupancy)
{
/*ContainsKey method. It returns true if the key was found.*/
string sssdd = Convert.ToString(obj).Substring(0, 5);
//sssdd = Regex.Replace( "@"+sssdd , "@\\s*[\\d]+?_", "");
if (!DifferentCarcategory.ContainsKey(sssdd))
{
int totallength = Convert.ToString(obj).Length;
String age = Convert.ToString(obj).Substring(5, totallength - 5);
objage.Add(age);
DifferentCarcategory.Add(sssdd, 1);
}
else
{
DifferentCarcategory[sssdd]++;
int totallength = Convert.ToString(obj).Length;
String age = Convert.ToString(obj).Substring(5, totallength - 5);
objage.Add(age);
}
}
/* use foreach syntax and KeyValuePair generics in the foreach loop. With collections like Dictionary,
* we must always know the value types. With each KeyValuePair, there is a Key member and Value member.*/
foreach (string kvp in DifferentCarcategory.Keys)
{
string strkv = kvp;
if (Convert.ToInt32(DifferentCarcategory[kvp]) == 1)
{
abc = abc + ";" + kvp;
}
else
{
/*Regex.Replace static method with a string replacement.The "\d" metacharacter matches digit characters. */
abc = abc + ";" + Regex.Replace(kvp, "^[\\d]*?_", Convert.ToString(DifferentCarcategory[kvp]) + "_");
}
}
int spiltindex = 0;
abc = abc.Trim().Trim(';');
String sssnew = "";
string xxxx = "";
foreach (string kvp in objage)
{
if (!abc.Contains(";"))
{
abc = abc + kvp;
}
else
{
sssnew = sssnew + abc.Split(';')[spiltindex] + kvp + ";";
}
spiltindex++;
}
//this condition for no child case in this case only pass (-1) one time in string.
xxxx = Convert.ToString(abc).Substring(3, 2);
if (xxxx == "_0")
abc = Convert.ToString(abc).Substring(0, 8);
else
abc = String.IsNullOrWhiteSpace(sssnew) ? abc : sssnew;
abc = abc.Trim().Trim(';');
return abc.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = GroupOccupancy(textBox1.Text);
}
}
}