AmidamaruZXC

Repository

Oct 24th, 2020
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using GeoCoordinatePortable;
  4. using System.Text;
  5.  
  6. namespace ConsoleApp1
  7. {
  8.     public class Repository
  9.     {
  10.         private static List<Order> _orders;
  11.         private static List<Courier> _couriers;
  12.         private static HashSet<Guid> _guids;
  13.  
  14.         public Repository()
  15.         {
  16.             _orders = new List<Order>();
  17.             _couriers = new List<Courier>();
  18.         }
  19.  
  20.         public void AddCourier(Courier courier) =>
  21.             _couriers.Add(courier);
  22.  
  23.         public static void AddOrder(double weight, GeoCoordinate source, GeoCoordinate destination)
  24.         {
  25.             Guid guid = Guid.NewGuid();
  26.             while (_guids.Contains(guid))
  27.                 guid = Guid.NewGuid();
  28.             _guids.Add(guid);
  29.             _orders.Add(new Order(weight, source, destination, guid));
  30.         }
  31.  
  32.         public static bool AssignCourier(Order order)
  33.         {
  34.             double minDist = double.MaxValue;
  35.             foreach (var courier in _couriers)
  36.             {
  37.                 if (courier.MaxAccWeightOfParcel >= order.Weight &&
  38.                 courier.CurrentPosition.GetDistanceTo(order.StartPoint) < minDist)
  39.                     minDist = courier.CurrentPosition.GetDistanceTo(order.StartPoint);
  40.             }
  41.  
  42.             foreach (var courier in _couriers)
  43.             {
  44.                 if (courier.MaxAccWeightOfParcel >= order.Weight &&
  45.                 courier.CurrentPosition.GetDistanceTo(order.StartPoint) == minDist)
  46.                 {
  47.                     order.StartTime = DateTime.Now;
  48.                     courier.IsAvailable = false;
  49.                     order.CourierID = courier.ID;
  50.                     return true;
  51.                 }
  52.             }
  53.             return false;
  54.         }
  55.  
  56.         public static void CompleteOrder(Order order)
  57.         {
  58.             order.EndTime = DateTime.Now;
  59.             foreach (var courier in _couriers)
  60.                 if (courier.ID == order.CourierID)
  61.                 {
  62.                     courier.IsAvailable = true;
  63.                     courier.CurrentPosition = order.EndPoint;
  64.                 }
  65.         }
  66.  
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment