Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
4.2 kB
2
Indexable
Never
class Checkpoint
{
    public string Name { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double? Penalty { get; set; }
}
abstract class CheckpointBuilder
{
    public abstract void SetName(string name);
    public abstract void SetCoordinates(double latitude, double longitude);
    public abstract void SetPenalty(double? penalty);
    public abstract Checkpoint Build();
}
class CheckpointConcreteBuilder : CheckpointBuilder
{
    private string _name;
    private double _latitude;
    private double _longitude;
    private double? _penalty;

    public override void SetName(string name)
    {
        _name = name;
    }

    public override void SetCoordinates(double latitude, double longitude)
    {
        if (latitude < -90 || latitude > 90)
        {
            throw new ArgumentOutOfRangeException(nameof(latitude), "Широта должна быть в диапазоне от -90 до 90.");
        }

        if (longitude < -180 || longitude > 180)
        {
            throw new ArgumentOutOfRangeException(nameof(longitude), "Долгота должна быть в диапазоне от -180 до 180.");
        }

        _latitude = latitude;
        _longitude = longitude;
    }

    public override void SetPenalty(double? penalty)
    {
        _penalty = penalty;
    }

    public override Checkpoint Build()
    {
        return new Checkpoint
        {
            Name = _name,
            Latitude = _latitude,
            Longitude = _longitude,
            Penalty = _penalty
        };
    }
}
class TotalPenaltyCheckpointBuilder : CheckpointBuilder
{
    private double totalPenalty = 0.0;

    public override void SetName(string name)
    {
    }

    public override void SetCoordinates(double latitude, double longitude)
    {
    }

    public override void SetPenalty(double? penalty)
    {
        if (penalty != null)
        {
            totalPenalty += penalty.Value;
        }
    }

    public override Checkpoint Build()
    {
        return null;
    }

    public double GetTotalPenalty()
    {
        return totalPenalty;
    }
}
class Trip
{
    private List<Checkpoint> checkpoints = new List<Checkpoint>();

    public void AddCheckpoint(CheckpointBuilder builder)
    {
        var checkpoint = builder.Build();
        checkpoints.Add(checkpoint);
    }

    public void ProcessCheckpoints()
    {
        foreach (var checkpoint in checkpoints)
        {
            Console.WriteLine($"Имя: {checkpoint.Name}");
            Console.WriteLine($"Координаты: ({checkpoint.Latitude}, {checkpoint.Longitude})");
            if (checkpoint.Penalty == null)
            {
                Console.WriteLine("Штраф: незачёт СУ");
            }
            else
            {
                Console.WriteLine($"Штраф: {checkpoint.Penalty} ч");
            }
        }

        var totalPenaltyBuilder = new TotalPenaltyCheckpointBuilder();
        foreach (var checkpoint in checkpoints)
        {
            totalPenaltyBuilder.SetPenalty(checkpoint.Penalty);
        }
        var totalPenalty = totalPenaltyBuilder.GetTotalPenalty();
        Console.WriteLine($"Суммарный штраф: {totalPenalty} ч");
    }
}

class Program3
{
    public static void program3()
    {
        var trip = new Trip();
        var checkpointBuilder = new CheckpointConcreteBuilder();

        checkpointBuilder.SetName("Обязательный КП 1");
        checkpointBuilder.SetCoordinates(55.1234, 95.6789);
        checkpointBuilder.SetPenalty(null);
        trip.AddCheckpoint(checkpointBuilder);

        checkpointBuilder.SetName("Необязательный КП 2");
        checkpointBuilder.SetCoordinates(-55.1234, -95.6789);
        checkpointBuilder.SetPenalty(2.5);
        trip.AddCheckpoint(checkpointBuilder);
        
        checkpointBuilder.SetName("Необязательный КП 3");
        checkpointBuilder.SetCoordinates(12.3456, 32.6177);
        checkpointBuilder.SetPenalty(5);
        trip.AddCheckpoint(checkpointBuilder);

        trip.ProcessCheckpoints();
    }
}