laba2Complicated #11
@ -14,9 +14,11 @@ namespace AutomobilePlantFileImplement
|
||||
private readonly string ComponentFileName = "Component.xml";
|
||||
private readonly string OrderFileName = "Order.xml";
|
||||
private readonly string CarFileName = "Car.xml";
|
||||
private readonly string ShopFileName = "Shop.xml";
|
||||
public List<Component> Components { get; private set; }
|
||||
public List<Order> Orders { get; private set; }
|
||||
public List<Car> Cars { get; private set; }
|
||||
public List<Shop> Shops { get; private set; }
|
||||
public static DataFileSingleton GetInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
@ -28,11 +30,13 @@ namespace AutomobilePlantFileImplement
|
||||
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
|
||||
public void SaveCars() => SaveData(Cars, CarFileName, "Cars", x => x.GetXElement);
|
||||
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
|
||||
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
|
||||
private DataFileSingleton()
|
||||
{
|
||||
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
|
||||
Cars = LoadData(CarFileName, "Car", x => Car.Create(x)!)!;
|
||||
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
|
||||
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
|
||||
}
|
||||
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
|
||||
{
|
||||
|
@ -0,0 +1,147 @@
|
||||
using AutomobilePlantContracts.BindingModels;
|
||||
using AutomobilePlantContracts.SearchModels;
|
||||
using AutomobilePlantContracts.StoragesContracts;
|
||||
using AutomobilePlantContracts.ViewModels;
|
||||
using AutomobilePlantDataModels.Models;
|
||||
using AutomobilePlantFileImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AutomobilePlantFileImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
private readonly DataFileSingleton source;
|
||||
public ShopStorage()
|
||||
{
|
||||
source = DataFileSingleton.GetInstance();
|
||||
}
|
||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
return source.Shops
|
||||
.Select(x => x.GetViewModel)
|
||||
.Where(x => x.Name.Contains(model.Name ?? string.Empty))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
return source.Shops.Select(shop => shop.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1;
|
||||
var newShop = Shop.Create(model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
source.Shops.Add(newShop);
|
||||
source.SaveShops();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
shop.Update(model);
|
||||
source.SaveShops();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
source.Shops.Remove(shop);
|
||||
source.SaveShops();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
|
||||
public bool SellCar(ICarModel model, int count)
|
||||
{
|
||||
var car = source.Cars.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (car == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var countStore = count;
|
||||
|
||||
var shopCars = source.Shops.SelectMany(shop => shop.ShopCars.Where(c => c.Value.Item1.Id == car.Id));
|
||||
|
||||
foreach (var c in shopCars)
|
||||
{
|
||||
count -= c.Value.Item2;
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
count = countStore;
|
||||
|
||||
foreach (var shop in source.Shops)
|
||||
{
|
||||
var cars = shop.ShopCars;
|
||||
|
||||
foreach (var c in cars.Where(x => x.Value.Item1.Id == car.Id))
|
||||
{
|
||||
var min = Math.Min(c.Value.Item2, count);
|
||||
cars[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min);
|
||||
count -= min;
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
shop.Update(new ShopBindingModel
|
||||
{
|
||||
Id = shop.Id,
|
||||
Name = shop.Name,
|
||||
Address = shop.Address,
|
||||
OpeningDate = shop.OpeningDate,
|
||||
ShopCars = cars
|
||||
});
|
||||
|
||||
source.SaveShops();
|
||||
|
||||
if (count <= 0) break;
|
||||
}
|
||||
|
||||
return count <= 0;
|
||||
}
|
||||
}
|
||||
}
|
110
AutomobilePlant/AutomobilePlantFileImplement/Models/Shop.cs
Normal file
110
AutomobilePlant/AutomobilePlantFileImplement/Models/Shop.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using AutomobilePlantContracts.BindingModels;
|
||||
using AutomobilePlantContracts.ViewModels;
|
||||
using AutomobilePlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace AutomobilePlantFileImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
public string Address { get; private set; } = string.Empty;
|
||||
public DateTime OpeningDate { get; private set; }
|
||||
public Dictionary<int, int> Cars { get; private set; } = new();
|
||||
private Dictionary<int, (ICarModel, int)>? _shopCars = null;
|
||||
|
||||
public Dictionary<int, (ICarModel, int)> ShopCars
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopCars == null)
|
||||
{
|
||||
var source = DataFileSingleton.GetInstance();
|
||||
_shopCars = Cars.ToDictionary(
|
||||
x => x.Key,
|
||||
y => ((source.Cars.FirstOrDefault(z => z.Id == y.Key) as ICarModel)!, y.Value)
|
||||
);
|
||||
}
|
||||
return _shopCars;
|
||||
}
|
||||
}
|
||||
|
||||
public static Shop? Create(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Address = model.Address,
|
||||
OpeningDate = model.OpeningDate,
|
||||
Cars = model.ShopCars.ToDictionary(x => x.Key, x => x.Value.Item2)
|
||||
};
|
||||
}
|
||||
|
||||
public static Shop? Create(XElement element)
|
||||
{
|
||||
if (element == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Shop()
|
||||
{
|
||||
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
||||
Name = element.Element("Name")!.Value,
|
||||
Address = element.Element("Address")!.Value,
|
||||
OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
|
||||
Cars = element.Element("ShopCars")!.Elements("ShopCar").ToDictionary(
|
||||
x => Convert.ToInt32(x.Element("Key")?.Value),
|
||||
x => Convert.ToInt32(x.Element("Value")?.Value)
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public void Update(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
Address = model.Address;
|
||||
OpeningDate = model.OpeningDate;
|
||||
if (model.ShopCars.Count > 0)
|
||||
{
|
||||
Cars = model.ShopCars.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||||
_shopCars = null;
|
||||
}
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Address = Address,
|
||||
OpeningDate = OpeningDate,
|
||||
ShopCars = ShopCars,
|
||||
};
|
||||
|
||||
public XElement GetXElement => new(
|
||||
"Shop",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("Name", Name),
|
||||
new XElement("Address", Address),
|
||||
new XElement("OpeningDate", OpeningDate.ToString()),
|
||||
new XElement("ShopCars", Cars.Select(x =>
|
||||
new XElement("ShopCar",
|
||||
new XElement("Key", x.Key),
|
||||
new XElement("Value", x.Value)))
|
||||
.ToArray()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user