Answer the question
In order to leave comments, you need to log in
Is the API implementation correct: new Car().Run(100)?
Hello! I have a task: I have a car and a bike, the car has a max. the speed is 300 km/h, the bicycle has 40 km/h, you need to implement the appropriate classes and take into account these restrictions. Suggested API: new Car().Run(100)
I solved this problem like this:
public class Trans
{
public Trans()
{
Vmax = 0;
}
public int Vmax { get; set; }
public string Run(int v)
{
if (v <= Vmax) return "Скорость движения транспорта в пределах нормы\n";
else return "Скорость движения транспорта выше максимальной\n";
}
}
public class Car : Trans
{
public Car()
{
Vmax = 300;
}
}
public class Byke : Trans
{
public Byke()
{
Vmax = 40;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write(new Car().Run(100));
Console.Write(new Byke().Run(100));
Console.ReadKey();
}
}
Answer the question
In order to leave comments, you need to log in
I would rewrite this code like this:
public abstract class Transport {
public int MaxSpeed { get; private set; }
public Transport(int maxSpeed) {
MaxSpeed = maxSpeed;
}
public string Run(int speed)
=> $"Скорость движения транспорта {(speed <= MaxSpeed ? "в пределах нормы" : "выше максимальной")}";
}
public class Car : Transport {
public Car() : base(300) {}
}
public class Bike : Transport {
public Bike() : base(40) {}
}
Well, what's the question? There is no right or wrong. It works or not. And if it works, then how effectively (efficiency criteria are not attached).
If this is the whole task, then the unwinding of classes is an overhead in code.
enum Transport
{
Car,
Byke
}
static void Main(string[] args)
{
var maxSpeed = new Dictionary<Transport, int>()
{
{ Transport.Car, 300 },
{ Transport.Byke, 40 }
};
int testSpeed = 100;
string test = maxSpeed[Transport.Car] <= testSpeed ? "Скорость движения транспорта в пределах нормы\n" : "Скорость движения транспорта выше максимальной\n";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question