C# OOP Basic Exam Prep 11 July 2017 - Need For Speed
Два дена се мъчих над BuisnessLogic частта на тази задача и постояно получавах 40/100 (тестове 2, 4 и 5 ми даваха грешен отговор).
Проблема беше, че Open метода съм го декларирал така:
public void Open(int id, string type, int length, string route, int prizePool,string extra = null)
{
var race = RaceFactory.Create(type, length, route, prizePool,extra);
races[id] = race;
}
Сега получавам 100/100 със следната тъпотия:
public void Open(int id, string type, int length, string route, int prizePool)
{
var race = RaceFactory.Create(type, length, route, prizePool);
races[id] = race;
}
public void Open(int id, string type, int length, string route, int prizePool, string extra)
{
races[id] = RaceFactory.Create(type, length, route, prizePool, extra);
}
Това ми е RaceFactory класа:
public static class RaceFactory
{
public static Race Create(string type, int length, string route, int prizePool, string extra = null)
{
switch (type)
{
case "Casual":
return new CasualRace(length, route, prizePool);
case "Drag":
return new DragRace(length, route, prizePool);
case "Drift":
return new DriftRace(length, route, prizePool);
case "TimeLimit":
int goldTime = int.Parse(extra);
return new TimeLimitRace(length, route, prizePool, goldTime);
case "Circuit":
int laps = int.Parse(extra);
return new CircuitRace(length, route, prizePool, laps);
default:
throw new System.ArgumentOutOfRangeException(
nameof(type),
$"Race type must be “Casual”, “Drag”, “Drift”, “TimeLimit” or “Circuit”!");
}
}
}
Та въпроса ми е защо едното е грешно, а другото не?