I am trying to solve Car kata problem. I want to calculate the time between, when user select the option to Start the car and when he decide to accelerate (considering its been done to warm engine).
On the basis of time it is idle(engine is running but not accelerated) how can I calculate the time and use that time to show the CONTINUOUS DROP in fuel level over console.
For eg: If initially my fuel level is 20L, in idle state fuel is getting consumed at 0.002Lit/sec, then I till the time user is waiting on the screen to press Accelerate, he should see the CONTINUOUS DROP in fuel level from 20L Assume he took 10 sec to accelerate, for every sec 0.002*1=0.002L is getting used up for 1st sec Fuel level: 20L for 2nd sec Fuel Level: 20L-0.002L= 19.998L and so on.
Any leads would be really helpful. Thanks in advance
I am not asking how get time difference between two DateTime objects. I want to calculate the real time fuel consumption.
while(true)
{
Console.WriteLine("Press 1: Start Car");
Console.WriteLine("Press 2: Accelerate Car");
Console.WriteLine("Press 3: Apply Brakes Car");
Console.WriteLine("Press 4: Stop Car");
Console.WriteLine("Press 5: Refuel Car");
//Stopwatch sw = new Stopwatch();
//sw.Start();
string input = Console.ReadLine();
switch (input)
{
case "1":
Console.WriteLine("Starting the Car...");
c.EngineStart();
//Console.WriteLine(sw.ElapsedMilliseconds * 1000);
//var total = sw.ElapsedMilliseconds * 1000;
break;
case "2":
Console.WriteLine("Accelerating...");
break;
default:
break;
}
//sw.Stop();
}
public void EngineStart()
{
fuel.Consume(0.0003);
Console.WriteLine("Fuel Level " fuel.getFuelLevel());
Console.WriteLine("Speed " "0 km/hr");
}
CodePudding user response:
You can declare two DateTime
variables just just both the Console.ReadLine
and assign them DateTime.Now
. And then take difference of these two variables
CodePudding user response:
Use stopwatch to capture the time between two user entered inputs
var watch = new Stopwatch();
watch.Start();
//Code between 1st and 2nd input
watch.Stop();