лаба 1 готовая

This commit is contained in:
Полина Чубыкина 2024-03-09 14:46:08 +04:00
parent deef66e3fc
commit 47e2bc50b8
7 changed files with 248 additions and 5 deletions

View File

@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectionaryContracts\ConfectioneryContracts.csproj", "{4BC782E2-5108-4C55-9F76-ACE5A56B8735}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{2822ACD8-8B94-4BF2-8C55-478677FDD1FE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{2822ACD8-8B94-4BF2-8C55-478677FDD1FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{89C5A160-FB85-4D56-AFD7-506CC6005080}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{EE30482F-8998-4178-8412-421A02F6A6B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE30482F-8998-4178-8412-421A02F6A6B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE30482F-8998-4178-8412-421A02F6A6B4}.Release|Any CPU.Build.0 = Release|Any CPU
{89C5A160-FB85-4D56-AFD7-506CC6005080}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89C5A160-FB85-4D56-AFD7-506CC6005080}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89C5A160-FB85-4D56-AFD7-506CC6005080}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89C5A160-FB85-4D56-AFD7-506CC6005080}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,60 @@
using System.ComponentModel;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System.Xml.Linq;
namespace ConfectioneryFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ConfectionaryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectionaryDataModels\ConfectioneryDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,61 @@
using ConfectioneryFileImplement.Models;
using System.Xml.Linq;
namespace PastryShopFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string PastryFileName = "Pastry.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Pastry> Pastrys { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SavePastrys() => SaveData(Pastrys, PastryFileName, "Pastrys", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Pastrys = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{
if (File.Exists(filename))
{
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
}
}
}
}

View File

@ -0,0 +1,102 @@
using ConfectioneryDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
namespace ConfectioneryFileImplement.Models
{
public class Pastry : IPastryModel
{
public int Id { get; private set; }
public string PastryName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _PastryComponents =
null;
public Dictionary<int, (IComponentModel, int)> PastryComponents
{
get
{
if (_PastryComponents == null)
{
var source = DataFileSingleton.GetInstance();
_PastryComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
}
return _PastryComponents;
}
}
public static Pastry? Create(PastryBindingModel model)
{
if (model == null)
{
return null;
}
return new Pastry()
{
Id = model.Id,
PastryName = model.PastryName,
Price = model.Price,
Components = model.PastryComponents.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static Pastry? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Pastry()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PastryName = element.Element("PastryName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components =
element.Element("PastryComponents")!.Elements("PastryComponent")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(PastryBindingModel model)
{
if (model == null)
{
return;
}
PastryName = model.PastryName;
Price = model.Price;
Components = model.PastryComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_PastryComponents = null;
}
public PastryViewModel GetViewModel => new()
{
Id = Id,
PastryName = PastryName,
Price = Price,
PastryComponents = PastryComponents
};
public XElement GetXElement => new("Pastry",
new XAttribute("Id", Id),
new XElement("PastryName", PastryName),
new XElement("Price", Price.ToString()),
new XElement("PastryComponents", Components.Select(x =>
new XElement("PastryComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
}
}

View File

@ -44,4 +44,4 @@ namespace ConfectioneryListImplement.Models
};
}
}
}

View File

@ -55,12 +55,12 @@ namespace ConfectioneryView
try
{
int id = Convert.ToInt32(comboBoxPastry.SelectedValue);
var product = _logicP.ReadElement(new PastrySearchModel
var Pastry = _logicP.ReadElement(new PastrySearchModel
{
Id = id
});
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0),
textBoxSum.Text = Math.Round(count * (Pastry?.Price ?? 0),
2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}