Home > OS >  Matrix representation in c# with classes and List object
Matrix representation in c# with classes and List object

Time:09-17

Here is my code:

 internal class RiskScore
    {

        public void attachCoordinates()
        {


            List<Coordinates> listOfCoords = new List<Coordinates>();

            StringBuilder sb = new StringBuilder();
            sb.Append("Improbable,Acceptable,1,Low$");
            sb.Append("Improbable,Tolerable,4,Medium$");
            sb.Append("Improbable,Undesirable,6,Medium$");
            sb.Append("Improbable,Intolerable,10,High$");
            sb.Append("Possible,Acceptable,2,Low$");
            sb.Append("Possible,Tolerable,5,Medium$");
            sb.Append("Possible,Undesirable,8,High$");
            sb.Append("Possible,Intolerable,11,Extreme$");
            sb.Append("Probable,Acceptable,3,Medium$");
            sb.Append("Probable,Tolerable,7,High$");
            sb.Append("Probable,Undesirable,9,High$");
            sb.Append("Probable,Intolerable,12,Extreme");

            string matrix = sb.ToString();
            string[] EachMatrix = matrix.Split("$");
            foreach (string eachmatrix in EachMatrix)
            {
                string[] EachValue = eachmatrix.Split(",");

                Coordinates coord = new Coordinates
                {
                    coordX = EachValue[0],
                    coordY = EachValue[1],
                    Riskvalue = EachValue[2],
                    RiskRatingKey = EachValue[3]
                };
                listOfCoords.Add(coord);
            }
        }


        public string getRiskProbability(string riskvalue)
        {
            //should return coordX and coordY
            return null;
        }

        public string getRiskValueandRiskRatingKey(string coordX, string coordY)
        {
            //should return Riskvalue and RiskRatingKey
            return null;
        }

    }

    public class Coordinates
    {
        public string coordX;
        public string coordY;
        public string Riskvalue;
        public string RiskRatingKey;
    }

This is to calculate Risk score and Risk Rating. for example If Risk Probablility is Improbable and Acceptable then the risk valueis 1 and the risk rating is Low. This is defined in the stringbuilder(sb).

I have put these values in a List<> Class.

My requirement is: a) If I give an input of 1(riskvalue) I need get the risk probablility which is (Improbable and acceptable) b)If I give the input in reverse ie., Improbable and acceptable I need the riskvalue and risk rating(low, medium, high).

Is this possible with this design?

CodePudding user response:

Well, first thing is that your listOfCoords is always the same you have to define it as class member in order your functions can consult it. Then you have to populate it in the class constructor. There are other approches like that your listOfCoords is always the same it must be readonly, etc, but here I'm just addressing classic OOP.

Then with Linq it's quite easy to achieve what you want. My console test:

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

namespace ConsoleApp
{
    internal class Program
    {

        internal class RiskScore
        {

            private List<Coordinates> listOfCoords = new List<Coordinates>();

            public RiskScore()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Improbable,Acceptable,1,Low$");
                sb.Append("Improbable,Tolerable,4,Medium$");
                sb.Append("Improbable,Undesirable,6,Medium$");
                sb.Append("Improbable,Intolerable,10,High$");
                sb.Append("Possible,Acceptable,2,Low$");
                sb.Append("Possible,Tolerable,5,Medium$");
                sb.Append("Possible,Undesirable,8,High$");
                sb.Append("Possible,Intolerable,11,Extreme$");
                sb.Append("Probable,Acceptable,3,Medium$");
                sb.Append("Probable,Tolerable,7,High$");
                sb.Append("Probable,Undesirable,9,High$");
                sb.Append("Probable,Intolerable,12,Extreme");

                string matrix = sb.ToString();
                string[] EachMatrix = matrix.Split("$");
                foreach (string eachmatrix in EachMatrix)
                {
                    string[] EachValue = eachmatrix.Split(",");

                    Coordinates coord = new Coordinates
                    {
                        coordX = EachValue[0],
                        coordY = EachValue[1],
                        Riskvalue = EachValue[2],
                        RiskRatingKey = EachValue[3]
                    };
                    listOfCoords.Add(coord);
                }
            }


            public string getRiskProbability(string riskvalue)
            {
                var coordinate = listOfCoords.FirstOrDefault(c => c.Riskvalue == riskvalue);
                return (coordinate != null) ? $"{coordinate.coordX}-{coordinate.coordY}" : "??";
            }

            public string getRiskValueandRiskRatingKey(string coordX, string coordY)
            {
                var coordinate = listOfCoords.FirstOrDefault(c => c.coordX == coordX & c.coordY == coordY);
                return (coordinate != null) ? $"{coordinate.Riskvalue}-{coordinate.RiskRatingKey}" : "??";
            }

        }

        public class Coordinates
        {
            public string coordX;
            public string coordY;
            public string Riskvalue;
            public string RiskRatingKey;
        }

        static void Main(string[] args)
        {
            var r = new RiskScore();
            Console.WriteLine(r.getRiskProbability("1"));
            Console.WriteLine(r.getRiskProbability("1123"));
            Console.WriteLine(r.getRiskValueandRiskRatingKey("Improbable", "Acceptable"));
            Console.WriteLine(r.getRiskValueandRiskRatingKey("Word1", "Word2"));
        }
    }
}

I've added a check for not existing values. But what I would do is define all your data as Enums, or whatever you want, in order you cannot call the functions with non existing values. Also the functions would return the Coordinate and you do what ever you want with it, but both things are another story.

CodePudding user response:

Not as your code currently stands. Assuming your listOfCoords is your data set it's not accessible for your getRiskProbability and your getRiskValueandRiskRatingKey methods, so they won't be able to return anything from it.

Secondly, if you're dealing with Coordinates you should return that, and at the point you need a particular string use that required property.

I would be tempted to strip out the methods you want to perform on the list of coordinates into extension methods and leave the RiskScore like this:

public static class RiskScore
{
    // Probably better to pass this into RiskScore rather than create it like this.
    public static string GetMatrixData()
    {
        var sb = new StringBuilder();
        sb.Append("Improbable,Acceptable,1,Low$");
        sb.Append("Improbable,Tolerable,4,Medium$");
        sb.Append("Improbable,Undesirable,6,Medium$");
        sb.Append("Improbable,Intolerable,10,High$");
        sb.Append("Possible,Acceptable,2,Low$");
        sb.Append("Possible,Tolerable,5,Medium$");
        sb.Append("Possible,Undesirable,8,High$");
        sb.Append("Possible,Intolerable,11,Extreme$");
        sb.Append("Probable,Acceptable,3,Medium$");
        sb.Append("Probable,Tolerable,7,High$");
        sb.Append("Probable,Undesirable,9,High$");
        sb.Append("Probable,Intolerable,12,Extreme");

        return sb.ToString();
    }

    public static List<Coordinates> GetCoordinates(string matrixData)
    {
        var listOfCoords = new List<Coordinates>();
        var matrices = matrixData.Split("$");
        foreach (var eachmatrix in matrices)
        {
            var eachValue = eachmatrix.Split(",");

            Coordinates coord = new Coordinates
            {
                coordX = eachValue[0],
                coordY = eachValue[1],
                Riskvalue = eachValue[2],
                RiskRatingKey = eachValue[3]
            };
            listOfCoords.Add(coord);
        }

        return listOfCoords;
    }
}

Then create extension methods like this:

public static class CoordinatesExtensions
{
    public static Coordinates GetCoordinateFromRiskValue(this IList<Coordinates> coordinates, string riskvalue)
    {
        if (string.IsNullOrEmpty(riskvalue))
            return null; // Or throw error

        // No riskvalues found (ignores case and culture) or coordinates is empty
        if (coordinates.Count() < 1 || !coordinates.Any(coord => String.Equals(coord.Riskvalue, riskvalue, StringComparison.CurrentCultureIgnoreCase)))
            return null;

        return coordinates.First(c => String.Equals(c.Riskvalue, riskvalue, StringComparison.CurrentCultureIgnoreCase));
    }

    public static Coordinates GetCoordinateFromCoordinates(this IList<Coordinates> coordinates, string coordX, string coordY)
    {
        if (string.IsNullOrEmpty(coordX) || string.IsNullOrEmpty(coordY))
            return null; // Or throw error

        // No coordinates found (ignores case and culture) or coordinates is empty
        if (coordinates.Count() < 1 || !coordinates.Any(coord => String.Equals(coord.coordX, coordX, StringComparison.CurrentCultureIgnoreCase) &&
                                                       String.Equals(coord.coordY, coordY, StringComparison.CurrentCultureIgnoreCase)))
            return null;

        return coordinates.First(c => String.Equals(c.coordX, coordX, StringComparison.CurrentCultureIgnoreCase) &&
                                 String.Equals(c.coordY, coordY, StringComparison.CurrentCultureIgnoreCase));
    }
}

Then you can use it like this:

    static void Main(string[] args)
    {
        var data = RiskScore.GetMatrixData();
        var coordinates = RiskScore.GetCoordinates(data);

        var coordinateFromRiskValue = coordinates.GetCoordinateFromRiskValue("1");
        var coordinateFromCoordinates = coordinates.GetCoordinateFromCoordinates("Improbable", "acceptable");

        Console.WriteLine($"Using RiskValue: x = {coordinateFromRiskValue.coordX}, y = {coordinateFromRiskValue.coordY}");
        Console.WriteLine($"Using Coordinates: riskValue = {coordinateFromCoordinates.Riskvalue}, riskRatingKey = {coordinateFromCoordinates.RiskRatingKey}");
    }
  •  Tags:  
  • c#
  • Related