public MachineryObject(string catalogueNumber, string manufacturer, string model, string description, int quantity, Branch category, decimal price, Dimensions dimensions, double weight) : base(catalogueNumber, manufacturer, model, description, quantity,category, price) { this.dimensions = dimensions; this.weight = weight; }
public override void LoadStore() { try { //Loading products from file var productReader = new StreamReader(SolutionPath + @"\ProductList.txt"); using (productReader) { string productToken = productReader.ReadLine(); while (productToken != null) { //Split by new line to products var splitedProductProperties = productToken.Split(new string[] { " ~obj~ " }, StringSplitOptions.RemoveEmptyEntries); Type classType = Type.GetType("WarehouseSystem.StoreObjects." + splitedProductProperties[0].Trim(), true); var currentSettings = Activator.CreateInstance(classType); //Split by symbol ~|~ to every property of the product splitedProductProperties = splitedProductProperties[1].Split(new string[] { " ~|~ " }, StringSplitOptions.RemoveEmptyEntries); foreach (var productProperty in splitedProductProperties) { //Split by symbols ~~ to class name(product category) and properties var propertyInfos = productProperty.Split(new string[] { "~~" }, StringSplitOptions.RemoveEmptyEntries); var property = classType.GetProperty(propertyInfos[0]); var type = property.PropertyType; //Parsing values using correct type from reflection dynamic newValue; if (type.Name == "Branch") { newValue = Enum.Parse(typeof(Branch), propertyInfos[1]); } else if (type.Name == "Material") { newValue = Enum.Parse(typeof(Material), propertyInfos[1]); } else if (type.Name == "Color") { newValue = Enum.Parse(typeof(Color), propertyInfos[1]); } else if (type.Name == "Dimensions") { var dimensions = propertyInfos[1].Split(new string[] { "x" }, StringSplitOptions.RemoveEmptyEntries); newValue = new Dimensions(double.Parse(dimensions[0]), double.Parse(dimensions[1])); } else { newValue = Convert.ChangeType(propertyInfos[1], type); } property.SetValue(currentSettings, newValue); } //Adding the new created product to the list of products this.AddProduct(currentSettings as StoreObject); productToken = productReader.ReadLine(); } } } catch (Exception ex) { System.Windows.MessageBox.Show("Failed to load products!"); } }