Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using GeoCoordinatePortable;
- using System.Xml.Serialization;
- using System.Text;
- using System.IO;
- namespace ConsoleApp1
- {
- public class Repository
- {
- private static List<Order> _orders;
- private static List<Courier> _couriers;
- private static HashSet<Guid> _guids;
- public Repository()
- {
- _orders = new List<Order>();
- _couriers = new List<Courier>();
- }
- public void AddCourier(Courier courier) =>
- _couriers.Add(courier);
- public static void AddOrder(double weight, GeoCoordinate source, GeoCoordinate destination)
- {
- Guid guid = Guid.NewGuid();
- while (_guids.Contains(guid))
- guid = Guid.NewGuid();
- _guids.Add(guid);
- _orders.Add(new Order(weight, source, destination, guid));
- }
- public static bool AssignCourier(Order order)
- {
- double minDist = double.MaxValue;
- foreach (var courier in _couriers)
- {
- if (courier.MaxAccWeightOfParcel >= order.Weight &&
- courier.CurrentPosition.GetDistanceTo(order.StartPoint) < minDist)
- minDist = courier.CurrentPosition.GetDistanceTo(order.StartPoint);
- }
- foreach (var courier in _couriers)
- {
- if (courier.MaxAccWeightOfParcel >= order.Weight &&
- courier.CurrentPosition.GetDistanceTo(order.StartPoint) == minDist)
- {
- order.StartTime = DateTime.Now;
- courier.IsAvailable = false;
- order.CourierID = courier.ID;
- return true;
- }
- }
- return false;
- }
- public static void CompleteOrder(Order order)
- {
- order.EndTime = DateTime.Now;
- foreach (var courier in _couriers)
- if (courier.ID == order.CourierID)
- {
- courier.IsAvailable = true;
- courier.CurrentPosition = order.EndPoint;
- }
- }
- public static void SaveInfo()
- {
- try
- {
- XmlSerializer xmlFormatter = new XmlSerializer(typeof(List<Order>));
- using (FileStream fs = new FileStream(@"..\..\..\orders.xml", FileMode.OpenOrCreate))
- {
- xmlFormatter.Serialize(fs, _orders);
- }
- xmlFormatter = new XmlSerializer(typeof(List<Courier>));
- using (FileStream fs = new FileStream(@"..\..\..\couriers.xml", FileMode.OpenOrCreate))
- {
- xmlFormatter.Serialize(fs, _couriers);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- public static void LoadInfo()
- {
- try
- {
- XmlSerializer xmlFormatter = new XmlSerializer(typeof(List<Order>));
- using (FileStream fs = new FileStream(@"..\..\..\orders.xml", FileMode.Open))
- {
- _orders = (List<Order>)xmlFormatter.Deserialize(fs);
- }
- xmlFormatter = new XmlSerializer(typeof(List<Courier>));
- using (FileStream fs = new FileStream(@"..\..\..\couriers.xml", FileMode.Open))
- {
- _couriers = (List<Courier>)xmlFormatter.Deserialize(fs);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement