Advertisement
AmidamaruZXC

Repository

Oct 24th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using GeoCoordinatePortable;
  4. using System.Xml.Serialization;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     public class Repository
  11.     {
  12.         private static List<Order> _orders;
  13.         private static List<Courier> _couriers;
  14.         private static HashSet<Guid> _guids;
  15.  
  16.         public Repository()
  17.         {
  18.             _orders = new List<Order>();
  19.             _couriers = new List<Courier>();
  20.         }
  21.  
  22.         public void AddCourier(Courier courier) =>
  23.             _couriers.Add(courier);
  24.  
  25.         public static void AddOrder(double weight, GeoCoordinate source, GeoCoordinate destination)
  26.         {
  27.             Guid guid = Guid.NewGuid();
  28.             while (_guids.Contains(guid))
  29.                 guid = Guid.NewGuid();
  30.             _guids.Add(guid);
  31.             _orders.Add(new Order(weight, source, destination, guid));
  32.         }
  33.  
  34.         public static bool AssignCourier(Order order)
  35.         {
  36.             double minDist = double.MaxValue;
  37.             foreach (var courier in _couriers)
  38.             {
  39.                 if (courier.MaxAccWeightOfParcel >= order.Weight &&
  40.                 courier.CurrentPosition.GetDistanceTo(order.StartPoint) < minDist)
  41.                     minDist = courier.CurrentPosition.GetDistanceTo(order.StartPoint);
  42.             }
  43.  
  44.             foreach (var courier in _couriers)
  45.             {
  46.                 if (courier.MaxAccWeightOfParcel >= order.Weight &&
  47.                 courier.CurrentPosition.GetDistanceTo(order.StartPoint) == minDist)
  48.                 {
  49.                     order.StartTime = DateTime.Now;
  50.                     courier.IsAvailable = false;
  51.                     order.CourierID = courier.ID;
  52.                     return true;
  53.                 }
  54.             }
  55.             return false;
  56.         }
  57.  
  58.         public static void CompleteOrder(Order order)
  59.         {
  60.             order.EndTime = DateTime.Now;
  61.             foreach (var courier in _couriers)
  62.                 if (courier.ID == order.CourierID)
  63.                 {
  64.                     courier.IsAvailable = true;
  65.                     courier.CurrentPosition = order.EndPoint;
  66.                 }
  67.         }
  68.  
  69.         public static void SaveInfo()
  70.         {
  71.             try
  72.             {
  73.                 XmlSerializer xmlFormatter = new XmlSerializer(typeof(List<Order>));
  74.  
  75.                 using (FileStream fs = new FileStream(@"..\..\..\orders.xml", FileMode.OpenOrCreate))
  76.                 {
  77.                     xmlFormatter.Serialize(fs, _orders);
  78.                 }
  79.  
  80.                 xmlFormatter = new XmlSerializer(typeof(List<Courier>));
  81.  
  82.                 using (FileStream fs = new FileStream(@"..\..\..\couriers.xml", FileMode.OpenOrCreate))
  83.                 {
  84.                     xmlFormatter.Serialize(fs, _couriers);
  85.                 }
  86.             }
  87.             catch (Exception ex)
  88.             {
  89.                 Console.WriteLine(ex.Message);
  90.             }
  91.         }
  92.  
  93.         public static void LoadInfo()
  94.         {
  95.             try
  96.             {
  97.                 XmlSerializer xmlFormatter = new XmlSerializer(typeof(List<Order>));
  98.  
  99.                 using (FileStream fs = new FileStream(@"..\..\..\orders.xml", FileMode.Open))
  100.                 {
  101.                     _orders = (List<Order>)xmlFormatter.Deserialize(fs);
  102.                 }
  103.  
  104.                 xmlFormatter = new XmlSerializer(typeof(List<Courier>));
  105.  
  106.                 using (FileStream fs = new FileStream(@"..\..\..\couriers.xml", FileMode.Open))
  107.                 {
  108.                     _couriers = (List<Courier>)xmlFormatter.Deserialize(fs);
  109.                 }
  110.             }
  111.             catch (Exception ex)
  112.             {
  113.                 Console.WriteLine(ex.Message);
  114.             }
  115.         }
  116.  
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement