Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Lab1/IRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Itmo.ObjectOrientedProgramming.Lab1.Models;

namespace Itmo.ObjectOrientedProgramming.Lab1;

public interface IRoute
{
Success Traverse(ITrain train);
}
12 changes: 12 additions & 0 deletions src/Lab1/Models/ITrain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Models;

public interface ITrain
{
double Speed { get; }

Success ApplyForce(double force);

Success TravelDistance(double distance);

void ResetAcceleration();
}
7 changes: 7 additions & 0 deletions src/Lab1/Models/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Models;

public record Success(double Time, string? ErrorMessage = null)
{
public bool IsSuccess => ErrorMessage == null;
public bool IsFailure => ErrorMessage != null;
}
75 changes: 75 additions & 0 deletions src/Lab1/Models/Train.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Models;

public class Train : ITrain
{
private readonly TrainSpecifications specs;
private double speed;
private double acceleration;

public Train(TrainSpecifications specifications)
{
this.specs = specifications;
speed = 0;
acceleration = 0;
}

public double Mass => specs.Mass;

public double MaxForce => specs.MaxForce;

public double Precision => specs.Precision;

public double Speed => speed;

public double Acceleration => acceleration;

public Success ApplyForce(double force)
{
if (Math.Abs(force) > MaxForce)
{
return new Success(0, "Приложенная сила превышает максимально допустимую");
}

acceleration = force / Mass;
return new Success(0);
}

public Success TravelDistance(double distance)
{
if (distance <= 0)
{
return new Success(0);
}

double distanceRemaining = distance;
double elapsedTime = 0;

while (distanceRemaining > 0)
{
if (speed == 0 && acceleration == 0)
{
return new Success(0, "Поезд не может пройти расстояние без скорости и ускорения");
}

double newSpeed = speed + (acceleration * Precision);

if (newSpeed < 0)
{
return new Success(0, "Скорость поезда стала отрицательной");
}

double distanceTraveled = newSpeed * Precision;

distanceRemaining -= distanceTraveled;
speed = newSpeed;
elapsedTime += Precision;
}

return new Success(elapsedTime);
}

public void ResetAcceleration()
{
acceleration = 0;
}
}
26 changes: 26 additions & 0 deletions src/Lab1/Models/TrainSpecifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Models;

public class TrainSpecifications
{
public double Mass { get; }

public double MaxForce { get; }

public double Precision { get; }

public TrainSpecifications(double mass, double maxForce, double precision)
{
if (mass <= 0)
throw new ArgumentException("Масса должна быть положительной", nameof(mass));

if (maxForce <= 0)
throw new ArgumentException("Максимальная сила должна быть положительной", nameof(maxForce));

if (precision <= 0)
throw new ArgumentException("Точность должна быть положительной", nameof(precision));

Mass = mass;
MaxForce = maxForce;
Precision = precision;
}
}
40 changes: 40 additions & 0 deletions src/Lab1/Route.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Itmo.ObjectOrientedProgramming.Lab1.Models;
using Itmo.ObjectOrientedProgramming.Lab1.RouteSegments;

namespace Itmo.ObjectOrientedProgramming.Lab1;

public class Route : IRoute
{
private readonly IReadOnlyCollection<IRouteSegment> segments;
private readonly double speedLimit;

public Route(IReadOnlyCollection<IRouteSegment> segments, double speedLimit)
{
this.segments = segments;
this.speedLimit = speedLimit;
}

public Success Traverse(ITrain train)
{
double totalElapsedTime = 0;

foreach (IRouteSegment segment in segments)
{
Success segmentResult = segment.Traverse(train);

if (segmentResult.IsFailure)
{
return segmentResult;
}

totalElapsedTime += segmentResult.Time;
}

if (train.Speed > speedLimit)
{
return new Success(totalElapsedTime, $"Скорость поезда ({train.Speed}) превышает лимит маршрута ({speedLimit})");
}

return new Success(totalElapsedTime);
}
}
30 changes: 30 additions & 0 deletions src/Lab1/RouteSegments/EnergyPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Itmo.ObjectOrientedProgramming.Lab1.Models;

namespace Itmo.ObjectOrientedProgramming.Lab1.RouteSegments;

public class EnergyPath : IRouteSegment
{
private readonly double distance;
private readonly double force;

public EnergyPath(double distance, double force)
{
this.distance = distance;
this.force = force;
}

public Success Traverse(ITrain train)
{
Success forceApplicationResult = train.ApplyForce(force);
if (forceApplicationResult.IsFailure)
{
return forceApplicationResult;
}

Success distanceTravelResult = train.TravelDistance(distance);

train.ResetAcceleration();

return distanceTravelResult;
}
}
8 changes: 8 additions & 0 deletions src/Lab1/RouteSegments/IRouteSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Itmo.ObjectOrientedProgramming.Lab1.Models;

namespace Itmo.ObjectOrientedProgramming.Lab1.RouteSegments;

public interface IRouteSegment
{
Success Traverse(ITrain train);
}
18 changes: 18 additions & 0 deletions src/Lab1/RouteSegments/RegularPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Itmo.ObjectOrientedProgramming.Lab1.Models;

namespace Itmo.ObjectOrientedProgramming.Lab1.RouteSegments;

public class RegularPath : IRouteSegment
{
private readonly double distance;

public RegularPath(double distance)
{
this.distance = distance;
}

public Success Traverse(ITrain train)
{
return train.TravelDistance(distance);
}
}
27 changes: 27 additions & 0 deletions src/Lab1/RouteSegments/Station.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Itmo.ObjectOrientedProgramming.Lab1.Models;

namespace Itmo.ObjectOrientedProgramming.Lab1.RouteSegments;

public class Station : IRouteSegment
{
private readonly double speedLimit;
private readonly double passengerLoadTime;

public Station(double speedLimit, double passengerLoadTime)
{
this.speedLimit = speedLimit;
this.passengerLoadTime = passengerLoadTime;
}

public Success Traverse(ITrain train)
{
if (train.Speed > speedLimit)
{
return new Success(0, $"Скорость поезда ({train.Speed}) превышает лимит станции ({speedLimit})");
}

double totalElapsedTime = passengerLoadTime;

return new Success(totalElapsedTime);
}
}
135 changes: 135 additions & 0 deletions src/Lab1/bin/Debug/net9.0/Lab1.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v9.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v9.0": {
"Lab1/1.0.0": {
"dependencies": {
"Itmo.Dev.Editorconfig": "1.0.12",
"SourceKit.Analyzers.Collections": "1.0.50",
"SourceKit.Analyzers.Enumerable": "1.0.50",
"SourceKit.Analyzers.MemberAccessibility": "1.0.31",
"SourceKit.Analyzers.Nullable": "1.0.50",
"SourceKit.Analyzers.Properties": "1.0.50",
"StyleCop.Analyzers": "1.2.0-beta.507"
},
"runtime": {
"Lab1.dll": {}
}
},
"Itmo.Dev.Editorconfig/1.0.12": {
"runtime": {
"lib/netstandard2.0/Itmo.Dev.Editorconfig.dll": {
"assemblyVersion": "1.0.12.0",
"fileVersion": "1.0.12.0"
}
}
},
"SourceKit/1.1.50": {},
"SourceKit.Analyzers.Collections/1.0.50": {
"dependencies": {
"SourceKit": "1.1.50"
}
},
"SourceKit.Analyzers.Enumerable/1.0.50": {
"dependencies": {
"SourceKit": "1.1.50"
}
},
"SourceKit.Analyzers.MemberAccessibility/1.0.31": {
"dependencies": {
"SourceKit": "1.1.50"
}
},
"SourceKit.Analyzers.Nullable/1.0.50": {
"dependencies": {
"SourceKit": "1.1.50"
}
},
"SourceKit.Analyzers.Properties/1.0.50": {
"dependencies": {
"SourceKit": "1.1.50"
}
},
"StyleCop.Analyzers/1.2.0-beta.507": {
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"StyleCop.Analyzers.Unstable/1.2.0.507": {}
}
},
"libraries": {
"Lab1/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Itmo.Dev.Editorconfig/1.0.12": {
"type": "package",
"serviceable": true,
"sha512": "sha512-+JwouEfM+SsBCCmP2h2escdpPF1Mx+kTms+lI2kjUaZyBOzR1Xt/LEhrwTQ2pd7cQ9Qpr/ul2MZehGIgUmgRxQ==",
"path": "itmo.dev.editorconfig/1.0.12",
"hashPath": "itmo.dev.editorconfig.1.0.12.nupkg.sha512"
},
"SourceKit/1.1.50": {
"type": "package",
"serviceable": true,
"sha512": "sha512-dxG3n6AN1gO3w0cX8ebfH21K+Ga2W9ro+RN1vzJ6NAF8YSzq2HeEnYGVkVHnR8t9M2jklQnHBetbOnB6vaViQA==",
"path": "sourcekit/1.1.50",
"hashPath": "sourcekit.1.1.50.nupkg.sha512"
},
"SourceKit.Analyzers.Collections/1.0.50": {
"type": "package",
"serviceable": true,
"sha512": "sha512-YfTg4iXGB4q6OqEacV3zXnRLHsAjP0wqh1wQ1rTEJgZDdClnoeY2Za3xI6XEdWTclws4+vf5uIi7dnC/ZImHqw==",
"path": "sourcekit.analyzers.collections/1.0.50",
"hashPath": "sourcekit.analyzers.collections.1.0.50.nupkg.sha512"
},
"SourceKit.Analyzers.Enumerable/1.0.50": {
"type": "package",
"serviceable": true,
"sha512": "sha512-XKlKz51FfLYkmg7om3fI8Owt/EeXqzuuaJ8AjFdbo4Qkfr5UfjGTELkYOMPsZfGgZskMiVE88PXJHzcYd5ilHw==",
"path": "sourcekit.analyzers.enumerable/1.0.50",
"hashPath": "sourcekit.analyzers.enumerable.1.0.50.nupkg.sha512"
},
"SourceKit.Analyzers.MemberAccessibility/1.0.31": {
"type": "package",
"serviceable": true,
"sha512": "sha512-Ri9M3YxJcTO8O64rjm1dXUAxM4otnaNFSSsXaPqrE7+coA02mq2qUY1hRFk0B95/CSkUQUvJR2sr+x2Vz0cDMw==",
"path": "sourcekit.analyzers.memberaccessibility/1.0.31",
"hashPath": "sourcekit.analyzers.memberaccessibility.1.0.31.nupkg.sha512"
},
"SourceKit.Analyzers.Nullable/1.0.50": {
"type": "package",
"serviceable": true,
"sha512": "sha512-HgOw15lWBhu3NwwXsX48U8TZqCUgyVLwzESUKk8+seb7taDEdq7v1Hc7VOP9oWEvCbj/6DmCfyI4qyS0v8MrHg==",
"path": "sourcekit.analyzers.nullable/1.0.50",
"hashPath": "sourcekit.analyzers.nullable.1.0.50.nupkg.sha512"
},
"SourceKit.Analyzers.Properties/1.0.50": {
"type": "package",
"serviceable": true,
"sha512": "sha512-yScfY0yhCnkdLb0weAG3N/XXsUGBta8bphvv+rWwInx/DiPMi8wGBHstqRJygmrhzeAp7b91+Gx+aIQ8Cr3pUg==",
"path": "sourcekit.analyzers.properties/1.0.50",
"hashPath": "sourcekit.analyzers.properties.1.0.50.nupkg.sha512"
},
"StyleCop.Analyzers/1.2.0-beta.507": {
"type": "package",
"serviceable": true,
"sha512": "sha512-/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"path": "stylecop.analyzers/1.2.0-beta.507",
"hashPath": "stylecop.analyzers.1.2.0-beta.507.nupkg.sha512"
},
"StyleCop.Analyzers.Unstable/1.2.0.507": {
"type": "package",
"serviceable": true,
"sha512": "sha512-gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw==",
"path": "stylecop.analyzers.unstable/1.2.0.507",
"hashPath": "stylecop.analyzers.unstable.1.2.0.507.nupkg.sha512"
}
}
}
Binary file added src/Lab1/bin/Debug/net9.0/Lab1.dll
Binary file not shown.
Binary file added src/Lab1/bin/Debug/net9.0/Lab1.pdb
Binary file not shown.
Loading