Вторая усложненная

This commit is contained in:
VictoriaPresnyakova 2023-06-16 21:37:53 +04:00
parent 3c45143f01
commit 7e33506003
11 changed files with 365 additions and 67 deletions

View File

@ -121,7 +121,7 @@ namespace JewelryStore
AdressTextBox.Text = model.StoreAdress;
OpeningDatePicker.Text = Convert.ToString(model.OpeningDate);
DataGridView.Rows.Clear();
foreach (var el in model.Jewels.Values)
foreach (var el in model.StoreJewels.Values)
{
DataGridView.Rows.Add(new object[] { el.Item1.JewelName, el.Item1.Price, el.Item2 });
}

View File

@ -22,80 +22,121 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
//«Выполняется»).
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
private readonly IStoreLogic _storeLogic;
private readonly IJewelStorage _jewelStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IJewelStorage jewelStorage, IStoreLogic storeLogic)
{
_logger = logger;
_orderStorage = orderStorage;
_storeLogic = storeLogic;
_jewelStorage = jewelStorage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен) return false;
if (model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Insert operation failed. Order status incorrect.");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newStatus)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
return false;
}
model.Status = newStatus;
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
var jewel = _jewelStorage.GetElement(new() { Id = viewModel.JewelId });
if (jewel == null)
{
throw new ArgumentNullException(nameof(jewel));
}
if (!_storeLogic.AddJewel(jewel, viewModel.Count))
{
throw new Exception($"AddJewel operation failed. Store is full.");
}
}
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется);
}
public bool DeliveryOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Готов) return false;
model.Status = OrderStatus.Выдан;
model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
return StatusUpdate(model, OrderStatus.Готов);
}
public bool FinishOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Выполняется) return false;
model.Status = OrderStatus.Готов;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
return StatusUpdate(model, OrderStatus.Выдан);
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
_logger.LogInformation("Order. OrderId:{Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Принят) return false;
model.Status = OrderStatus.Выполняется;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
@ -110,12 +151,12 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
if (model.JewelId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор драгоценности", nameof(model.JewelId));
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.JewelId));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество драгоценностей в заказе должно быть больше 0", nameof(model.Count));
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
}
if (model.Sum <= 0)
@ -125,6 +166,5 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. JewelId: { JewelId}", model.Id, model.Sum, model.JewelId);
}
}
}

View File

@ -31,29 +31,34 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
if (quantity <= 0)
{
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
throw new ArgumentException("Количество изделий должно быть больше 0", nameof(quantity));
}
_logger.LogInformation("AddJewelInStore. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
_logger.LogInformation("AddJewel. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
var element = _storeStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddJewelInStore element not found");
_logger.LogWarning("AddJewel element not found");
return false;
}
_logger.LogInformation("AddJewelInStore find. Id:{Id}", element.Id);
if (element.Jewels.TryGetValue(jewel.Id, out var pair))
if (element.JewelMaxCount - element.StoreJewels.Select(x => x.Value.Item2).Sum() < quantity)
{
element.Jewels[jewel.Id] = (jewel, quantity + pair.Item2);
_logger.LogInformation("AddJewelInStore. Has been added {quantity} {jewel} in {StoreName}", quantity, jewel.JewelName, element.StoreName);
throw new ArgumentNullException("Магазин переполнен", nameof(quantity));
}
_logger.LogInformation("AddJewel find. Id:{Id}", element.Id);
if (element.StoreJewels.TryGetValue(jewel.Id, out var pair))
{
element.StoreJewels[jewel.Id] = (jewel, quantity + pair.Item2);
_logger.LogInformation("AddJewel. Added {quantity} {jewel} to '{StoreName}' store", quantity, jewel.JewelName, element.StoreName);
}
else
{
element.Jewels[jewel.Id] = (jewel, quantity);
_logger.LogInformation("AddPastryInShop. Has been added {quantity} new Jewel {jewel} in {StoreName}", quantity, jewel.JewelName, element.StoreName);
element.StoreJewels[jewel.Id] = (jewel, quantity);
_logger.LogInformation("AddJewel. Added {quantity} new jewel {jewel} to '{StoreName}' store", quantity, jewel.JewelName, element.StoreName);
}
_storeStorage.Update(new()
@ -62,15 +67,64 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
StoreAdress = element.StoreAdress,
StoreName = element.StoreName,
OpeningDate = element.OpeningDate,
Jewels = element.Jewels
JewelMaxCount = element.JewelMaxCount,
StoreJewels = element.StoreJewels,
});
return true;
}
public bool AddJewel(IJewelModel jewel, int quantity)
{
if (jewel == null)
{
throw new ArgumentNullException(nameof(jewel));
}
if (quantity <= 0)
{
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
}
var freePlaces = _storeStorage.GetFullList()
.Select(x => x.JewelMaxCount - x.StoreJewels
.Select(p => p.Value.Item2).Sum()).Sum() - quantity;
if (freePlaces < 0)
{
_logger.LogInformation("AddJewel. Failed to add jewel to store. It's full.");
return false;
}
foreach (var store in _storeStorage.GetFullList())
{
var temp = Math.Min(quantity, store.JewelMaxCount - store.StoreJewels.Select(x => x.Value.Item2).Sum());
if (temp <= 0)
{
continue;
}
if (!AddJewel(new() { Id = store.Id }, jewel, temp))
{
_logger.LogWarning("An error occurred while adding jewel to stores");
return false;
}
quantity -= temp;
if (quantity == 0)
{
return true;
}
}
return true;
}
public bool Create(StoreBindingModel model)
{
CheckModel(model);
model.Jewels = new();
model.StoreJewels = new();
if (_storeStorage.Insert(model) == null)
{
@ -131,6 +185,11 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
return list;
}
public bool SellJewel(IJewelModel jewel, int quantity)
{
return _storeStorage.SellJewel(jewel, quantity);
}
public bool Update(StoreBindingModel model)
{
CheckModel(model, false);
@ -165,7 +224,13 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
throw new ArgumentNullException("Нет названия магазина", nameof(model.StoreName));
}
if (model.JewelMaxCount < 0)
{
throw new ArgumentException("Максимальное количество изделий в магазине не может быть меньше нуля", nameof(model.JewelMaxCount));
}
_logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAdress, model.Id);
var element = _storeStorage.GetElement(new StoreSearchModel
{
StoreName = model.StoreName

View File

@ -14,10 +14,10 @@ namespace JewelryStoreContracts.BindingModels
public DateTime OpeningDate { get; set; } = DateTime.Now;
public Dictionary<int, (IJewelModel, int)> Jewels { get; set; } = new();
public Dictionary<int, (IJewelModel, int)> StoreJewels { get; set; } = new();
public int Id { get; set; }
public int PackageMaxCount { get; set; }
public int JewelMaxCount { get; set; }
}
}

View File

@ -10,7 +10,7 @@ namespace JewelryStoreContracts.ViewModels
{
public class StoreViewModel : IStoreModel
{
public Dictionary<int, (IJewelModel, int)> Jewels { get; set; } = new();
public Dictionary<int, (IJewelModel, int)> StoreJewels { get; set; } = new();
public int Id { get; set; }
[DisplayName("Название магазина")]
@ -21,6 +21,6 @@ namespace JewelryStoreContracts.ViewModels
public DateTime OpeningDate { get; set; } = DateTime.Now;
[DisplayName("Вместимость магазина")]
public int PackageMaxCount { get; set; }
public int JewelMaxCount { get; set; }
}
}

View File

@ -11,6 +11,7 @@ namespace JewelryStoreDataModels.Models
public string StoreName { get; }
public string StoreAdress { get; }
DateTime OpeningDate { get; }
Dictionary<int, (IJewelModel, int)> Jewels { get; }
Dictionary<int, (IJewelModel, int)> StoreJewels { get; }
public int JewelMaxCount { get; }
}
}

View File

@ -14,9 +14,12 @@ namespace JewelryStoreFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string JewelFileName = "Jewel.xml";
private readonly string StoreFileName = "Store.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Jewel> Jewels { get; private set; }
public List<Store> Stores { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -28,11 +31,15 @@ namespace JewelryStoreFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName,"Components", x => x.GetXElement);
public void SaveJewels() => SaveData(Jewels, JewelFileName, "Jewels", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveStores() => SaveData(Stores, StoreFileName, "Stores", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x =>Component.Create(x)!)!;
Jewels = LoadData(JewelFileName, "Jewel", x => Jewel.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Stores = LoadData(StoreFileName, "Store", x => Store.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{

View File

@ -2,6 +2,8 @@
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDataModels.Models;
using JewelryStoreFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
@ -12,34 +14,101 @@ namespace JewelryStoreFileImplement.Implements
{
public class StoreStorage : IStoreStorage
{
private readonly DataFileSingleton source;
public StoreStorage()
{
source = DataFileSingleton.GetInstance();
}
public StoreViewModel? Delete(StoreBindingModel model)
{
throw new NotImplementedException();
var element = source.Stores.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Stores.Remove(element);
source.SaveStores();
return element.GetViewModel;
}
return null;
}
public StoreViewModel? GetElement(StoreSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue)
{
return null;
}
return source.Stores.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.StoreName) && x.StoreName ==
model.StoreName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.StoreName))
{
return new();
}
return source.Stores.Where(x =>
x.StoreName.Contains(model.StoreName)).Select(x => x.GetViewModel).ToList();
}
public List<StoreViewModel> GetFullList()
{
throw new NotImplementedException();
return source.Stores.Select(x => x.GetViewModel).ToList();
}
public StoreViewModel? Insert(StoreBindingModel model)
{
throw new NotImplementedException();
model.Id = source.Stores.Count > 0 ? source.Stores.Max(x => x.Id) + 1 : 1;
var newStore = Store.Create(model);
if (newStore == null)
{
return null;
}
source.Stores.Add(newStore);
source.SaveStores();
return newStore.GetViewModel;
}
public bool SellJewel(IJewelModel model, int quantity)
{
if (source.Stores.Select(x => x.StoreJewels.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < quantity)
{
return false;
}
foreach (var store in source.Stores.Where(x => x.StoreJewels.ContainsKey(model.Id)))
{
int QuantityInCurrentShop = store.StoreJewels[model.Id].Item2;
if (QuantityInCurrentShop <= quantity)
{
store.StoreJewels.Remove(model.Id);
quantity -= QuantityInCurrentShop;
}
else
{
store.StoreJewels[model.Id] = (store.StoreJewels[model.Id].Item1, QuantityInCurrentShop - quantity);
quantity = 0;
}
if (quantity == 0)
{
return true;
}
}
return false;
}
public StoreViewModel? Update(StoreBindingModel model)
{
throw new NotImplementedException();
var store = source.Stores.FirstOrDefault(x => x.Id == model.Id);
if (store == null)
{
return null;
}
store.Update(model);
source.SaveStores();
return store.GetViewModel;
}
}
}

View File

@ -0,0 +1,107 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace JewelryStoreFileImplement.Models
{
public class Store : IStoreModel
{
public string StoreName { get; private set; } = string.Empty;
public string StoreAdress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
public Dictionary<int, int> Jewels { get; private set; } = new();
public Dictionary<int, (IJewelModel, int)> _storeJewels = null;
public Dictionary<int, (IJewelModel, int)> StoreJewels
{
get
{
if (_storeJewels == null)
{
var source = DataFileSingleton.GetInstance();
_storeJewels = Jewels.ToDictionary(x => x.Key, y => ((source.Jewels.FirstOrDefault(z => z.Id == y.Key) as IJewelModel)!, y.Value));
}
return _storeJewels;
}
}
public int Id { get; private set; }
public int JewelMaxCount { get; private set; }
public static Store? Create(StoreBindingModel? model)
{
if (model == null)
{
return null;
}
return new Store()
{
Id = model.Id,
StoreName = model.StoreName,
StoreAdress = model.StoreAdress,
JewelMaxCount = model.JewelMaxCount,
OpeningDate = model.OpeningDate,
Jewels = model.StoreJewels.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Store? Create(XElement element)
{
if (element == null)
{
return null;
}
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
StoreName = element.Element("StoreName")!.Value,
StoreAdress = element.Element("StoreAdress")!.Value,
OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
JewelMaxCount = Convert.ToInt32(element.Element("JewelMaxCount")!.Value),
Jewels = element.Element("StoreJewels")!.Elements("Jewel").ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(StoreBindingModel? model)
{
if (model == null)
{
return;
}
StoreName = model.StoreName;
StoreAdress = model.StoreAdress;
OpeningDate = model.OpeningDate;
JewelMaxCount = model.JewelMaxCount;
Jewels = model.StoreJewels.ToDictionary(x => x.Key, x => x.Value.Item2);
_storeJewels = null;
}
public StoreViewModel GetViewModel => new()
{
Id = Id,
StoreName = StoreName,
StoreAdress = StoreAdress,
StoreJewels = StoreJewels,
OpeningDate = OpeningDate,
JewelMaxCount = JewelMaxCount,
};
public XElement GetXElement => new("Store",
new XAttribute("Id", Id),
new XElement("StoreName", StoreName),
new XElement("StoreAdress", StoreAdress),
new XElement("OpeningDate", OpeningDate),
new XElement("JewelMaxCount", JewelMaxCount),
new XElement("StoreJewels", Jewels
.Select(x => new XElement("Jewel",
new XElement("Key", x.Key),
new XElement("Value", x.Value))
).ToArray()));
}
}

View File

@ -2,6 +2,7 @@
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDataModels.Models;
using JewelryStoreListImplement.Models;
using System;
using System.Collections.Generic;
@ -106,6 +107,11 @@ namespace JewelryStoreListImplement.Implements
return newStore.GetViewModel;
}
public bool SellJewel(IJewelModel model, int quantity)
{
throw new NotImplementedException();
}
public StoreViewModel? Update(StoreBindingModel model)
{
foreach (var store in _source.Stores)

View File

@ -16,7 +16,7 @@ namespace JewelryStoreListImplement.Models
public DateTime OpeningDate { get; private set; }
public Dictionary<int, (IJewelModel, int)> Jewels { get; private set; } = new();
public Dictionary<int, (IJewelModel, int)> StoreJewels { get; private set; } = new();
public int Id { get; private set; }
@ -32,7 +32,7 @@ namespace JewelryStoreListImplement.Models
StoreName = model.StoreName,
StoreAdress = model.StoreAdress,
OpeningDate = model.OpeningDate,
Jewels = new()
StoreJewels = new()
};
}
@ -45,6 +45,7 @@ namespace JewelryStoreListImplement.Models
StoreName = model.StoreName;
StoreAdress = model.StoreAdress;
OpeningDate = model.OpeningDate;
StoreJewels = model.StoreJewels;
}
public StoreViewModel GetViewModel => new()
@ -53,7 +54,9 @@ namespace JewelryStoreListImplement.Models
StoreName = StoreName,
StoreAdress = StoreAdress,
OpeningDate = OpeningDate,
Jewels = Jewels
StoreJewels = StoreJewels
};
public int JewelMaxCount => throw new NotImplementedException();
}
}