OCP -1
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; }
public void OverAllRate(Source source)
{
List 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:
var raterT20 = new T20RatingPlayer(this.Logger);
raterT20.Rate(item);
break;
case CricketFormat.ODI:
var raterOdi = new ODIRatingPlayer(this.Logger);
raterOdi.Rate(item);
break;
default:
Logger.Log("unknown format!!");
break;
}
}
}
}
T20RatingPlaer & ODIRatingPlayer
public interface IRating
{
public void Rate(CricketRating item);
}
public class T20RatingPlayer : IRating
{
private Logger _logger;
public T20RatingPlayer(Logger logger)
{
_logger=logger;
}
public void Rate(CricketRating item)
{
_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");
}
}
}
public class ODIRatingPlayer :IRating
{
private Logger _logger;
public ODIRatingPlayer(Logger logger)
{
_logger=logger;
}
public void Rate(CricketRating item)
{
_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");
}
}
}