SRP – resolve

  • CricketRatingSource
  • CricketDeserializer
  • Logger

          OCP is still violated , which we will resolve it in next chapters.

				
					// See https://aka.ms/new-console-template for more information

using Newtonsoft.Json;
using System.Xml.Serialization;

public class CricketDeserializer
{
    public List<CricketRating> cricketRatingDeserializeJson(string sourceJson)
    {
        return JsonConvert.DeserializeObject<List<CricketRating>>(sourceJson);
    }

    public List<CricketRating> cricketRatingDeserializeXml(string sourceXml)
    {
        return DeserializeToObject<List<CricketRating>>(sourceXml);
    }

    public T DeserializeToObject<T>(string filepath) where T : class
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));

        using (StreamReader sr = new StreamReader(filepath))
        {
            return (T)ser.Deserialize(sr);
        }
    }

    public T SerializeXml<T>(string filepath, T result) where T : class
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

        using (FileStream stream = File.Create(filepath))
        {
            xmlSerializer.Serialize(stream, result);
        }
        return result;
    }
}
				
			
				
					namespace SRP
{
    public class CricketRatingSource
    {
        const string jsonpath = "C:\\Users\\611184113\\source\\repos\\SRP\\SRP\\cricket.json";
        string xmlpath = Path.Combine(Environment.CurrentDirectory, "cricket.xml");

        public string CricketRatingFromJson()
        {
            return File.ReadAllText(jsonpath);
        }
        public string CricketRatingFromXML()
        {
            return xmlpath;
        }
    }
}

				
			
				
					// See https://aka.ms/new-console-template for more information

using SRP;

internal class CricketSpecialistInAFormat
{
    public Logger Logger { get; set; } = new Logger();
    public CricketRatingSource sourceObj = new ();
    public CricketDeserializer cricketDeserializerObj = new();
    public int OverAllRating { get; set; }

    internal void OverAllRate(Source source)
    {
        List<CricketRating> ratingObject = new();
        if(Source.JSON == source)
        {
            var ratingJsonStr = sourceObj.CricketRatingFromJson();
            ratingObject = cricketDeserializerObj.cricketRatingDeserializeJson(ratingJsonStr); ;

        }
        else if (Source.XML == source)
        {
            var ratingXMLStr = sourceObj.CricketRatingFromXML();
            ratingObject = cricketDeserializerObj.cricketRatingDeserializeXml(ratingXMLStr);
        }
        
        foreach (var item in ratingObject)
        {
            Logger.Log("-----------------------------");
            Enum.TryParse(item.Format, out CricketFormat format);
            switch (format)
            {
                case CricketFormat.T20:
                    Logger.Log("T20 Format");

                    if (item.StrikeRate > 150 && item.Average > 40)
                    {
                        Logger.Log($"star player:: {item.Name}");
                        Logger.Log("Applicable for special bonus");
                    }
                    else
                    {
                        Logger.Log($"Average player:: {item.Name}");
                        Logger.Log("player will be given next 10 chances to prove");
                        Logger.Log("Not applicable for bonus");
                    }
                    break;
                case CricketFormat.ODI:
                    Logger.Log("ODI Format");
                    if (item.StrikeRate > 95)
                    {
                        Logger.Log($"star player:: {item.Name}");
                        Logger.Log("Applicable for special bonus");
                    }
                    else
                    {
                        Logger.Log($"Average player:: {item.Name}");
                        Logger.Log("Not applicable for bonus");
                    }
                    break;
                default:
                    Logger.Log("unknown format!!");
                    break;
            } 
        }
    }
}
				
			

Leave a Comment