Hi Sir,
Good day!
I wanted to ask for a sample code in C# console application, on how could I implement the following scenario.
Sample Scene:
(input came from a text file) (output in a text file also)
I have three animals (CAT, DOG, MOUSE), the user needs to give a name on cat, dog and mouse in a several numbers (unknown numbers), plus they are differentiated on particular colors like BLACK, WHITE, BROWN. And count the number of particular animals based on their names and colors. (eg. class DOG, name: LASSIE, color: BLACK, WHITE and BROWN, Get Count: 5 Black Dogs found with a name of LASSIE, 3 White dogs found with a name of LASSIE, 2 Brown dogs found with a name of LASSIE)
Table example:
-----------------------------------------
ANIMALS NAMES BLACK WHITE BROWN
-----------------------------------------
CAT GARFIELD 2 1 5
CAT BELLA 1 4 3
CAT KITTA 7 9 6
DOG LASSIE 5 3 2
DOG SKITTER 2 1 4
MOUSE MOUSSY 1 3 5
MOUSE RATTY 3 6 9
MOUSE MIGHTY 6 2 2
MOUSE JIMM 1 0 1
-----------------------------------------
TOTAL: 28 26 37
The only fix variables are cat dog and mouse, and the color black white brown,
the unknown of definite value there are the names, the user can give different names and numbers.
Please Sir, I need your help on this.
Thanks in advance!
here's my sample code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace Clustering_Test
{
class Program
{
static void Main(string[] args)
{
string strAnimal = String.Empty;
string catName;
int garfieldBlackCtr = 0, garfieldWhiteCtr = 0, garfieldBrownCtr = 0;
int BellaBlackCtr = 0, BellaWhiteCtr = 0, BellaBrownCtr = 0;
int KittaBlackCtr = 0, KittaWhiteCtr = 0, KittaBrownCtr = 0;
int LassieBlackCtr = 0, LassieWhiteCtr = 0, LassieBrownCtr = 0;
int SkitterBlackCtr = 0, SkitterWhiteCtr = 0, SkitterBrownCtr = 0;
int MoussyBlackCtr = 0, MoussyWhiteCtr = 0, MoussyBrownCtr = 0;
int RattyBlackCtr = 0, RattyWhiteCtr = 0, RattyBrownCtr = 0;
string dogName, dogColorCtr;
string mouseName, mouseColorCtr;
string strGarfield, strBella, strKitta;
string strLassie, strSkitter;
string strMoussy, strRatty;
string dirFile = @"C:\FileDir\Sample_Text.txt";
string recLine;
int recCounter = 0;
StreamReader srTable = new StreamReader(dirFile);
while ((recLine = srTable.ReadLine()) != null)
{
recCounter++;
strAnimal = recLine.Substring(0, 5);
if (strAnimal == "CAT")
{
catName = recLine.Substring(12, 9);
if (catName.Trim() == Cat.GARFIELD)
{
string valCatColor = recLine.Substring(23, 3);
garfieldBlackCtr = Convert.ToInt32(valCatColor.Trim());
Process.intGarfieldBlack += garfieldBlackCtr;
string valCatColor2 = recLine.Substring(35, 3);
garfieldWhiteCtr = Convert.ToInt32(valCatColor.Trim());
Process.intGarfieldWhite += garfieldWhiteCtr;
string valCatColor3 = recLine.Substring(47, 3);
garfieldBrownCtr = Convert.ToInt32(valCatColor.Trim());
Process.intGarfieldBrown += garfieldBrownCtr;
}
}
else if (strAnimal == "DOG")
{
dogName = recLine.Substring(12, 9);
if (dogName.Trim() == Dog.LASSIE)
{
string valDogColor = recLine.Substring(23, 3);
LassieBlackCtr = Convert.ToInt32(valDogColor.Trim());
Process.intLassieBlack += LassieBlackCtr;
string valDogColor2 = recLine.Substring(35, 3);
LassieWhiteCtr = Convert.ToInt32(valDogColor.Trim());
Process.intLassieWhite += LassieWhiteCtr;
string valDogColor3 = recLine.Substring(47, 3);
LassieBrownCtr = Convert.ToInt32(valDogColor.Trim());
Process.intLassieBrown += LassieBrownCtr;
}
else if (strAnimal == "MOUSE")
{
string valMouseColor = recLine.Substring(23, 3);
MoussyBlackCtr = Convert.ToInt32(valMouseColor.Trim());
Process.intMoussyBlack += MoussyBlackCtr;
string valMouseColor2 = recLine.Substring(35, 3);
MoussyWhiteCtr = Convert.ToInt32(valMouseColor.Trim());
Process.intMoussyWhite += MoussyWhiteCtr;
string valMouseColor3 = recLine.Substring(47, 3);
MoussyBrownCtr = Convert.ToInt32(valMouseColor.Trim());
Process.intMoussyBrown += MoussyBrownCtr;
}
}
Process.intTotalBlack = Process.intGarfieldBlack + Process.intBellaBlack + Process.intKittaBlack +
Process.intLassieBlack + Process.intSkitterBlack +
Process.intMoussyBlack + Process.intRattyBlack;
Process.intTotalWhite = Process.intGarfieldWhite + Process.intBellaWhite + Process.intKittaWhite +
Process.intLassieWhite + Process.intSkitterWhite +
Process.intMoussyWhite + Process.intRattyWhite;
Process.intTotalBrown = Process.intGarfieldBrown + Process.intBellaBrown + Process.intKittaBrown +
Process.intLassieBrown + Process.intSkitterBrown +
Process.intMoussyBrown + Process.intRattyBrown;
}
}
}
class Cat
{
public const string GARFIELD = "Garfield";
public const string BELLA = "Bella";
public const string KITTA = "Kitta";
}
class Dog
{
public const string LASSIE = "Lassie";
public const string SKITTER = "Skitter";
}
class Mouse
{
public const string MOUSSY = "Moussy";
public const string RATTY = "Ratty";
public const string MIGHTY = "Mighty";
public const string JIMM = "Jimm";
}
class COLOR
{
public string BLACK = "Black";
public string WHITE = "White";
public string BROWN = "Brown";
}
class Process
{
public static int intGarfieldBlack = 0;
public static int intGarfieldBrown = 0;
public static int intGarfieldWhite = 0;
public static int intBellaBlack = 0;
public static int intBellaBrown = 0;
public static int intBellaWhite = 0;
public static int intKittaBlack = 0;
public static int intKittaBrown = 0;
public static int intKittaWhite = 0;
public static int intLassieBlack = 0;
public static int intLassieBrown = 0;
public static int intLassieWhite = 0;
public static int intSkitterBlack = 0;
public static int intSkitterBrown = 0;
public static int intSkitterWhite = 0;
public static int intMoussyBlack = 0;
public static int intMoussyBrown = 0;
public static int intMoussyWhite = 0;
public static int intRattyBlack = 0;
public static int intRattyBrown = 0;
public static int intRattyWhite = 0;
public static int intTotalBlack = 0;
public static int intTotalBrown = 0;
public static int intTotalWhite = 0;
}
}