示例#1
0
        private void IngredientChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            // IngrеdientModel property names important for calculation and validation
            string isConcentrationUnknownName = nameof(IngredientModel.IsConcentrationUnknown);
            string isQuantityUnknownName      = nameof(IngredientModel.IsQuantityUnknown);
            string quantityName      = nameof(IngredientModel.Quantity);
            string concentrationName = nameof(IngredientModel.Concentration);

            // ValidateSystemOfEquations
            if (e.PropertyName == isConcentrationUnknownName || e.PropertyName == isQuantityUnknownName)
            {
                ValidateSystemOfEquations();
            }

            // Calculate if data are changed
            IngredientModel senderIngredientModel = sender as IngredientModel;

            if (senderIngredientModel != null &&
                (
                    e.PropertyName == isConcentrationUnknownName ||
                    e.PropertyName == isQuantityUnknownName ||
                    (e.PropertyName == quantityName && !senderIngredientModel.IsQuantityUnknown) ||
                    (e.PropertyName == concentrationName && !senderIngredientModel.IsConcentrationUnknown)
                )
                )
            {
                CalculateIfValid();
            }
        }
示例#2
0
        /// <summary>
        /// Add new ingredient
        /// </summary>
        /// <param name="parameter">Parameter of the command handler</param>
        private void AddIngredientCommandHandler(object parameter)
        {
            IngredientModel ingredient = new IngredientModel(_view, string.Empty, 0, 0, IngredientChangedEventHandler);

            Ingredients.Add(ingredient);
            ValidateSystemOfEquations();
            CalculateIfValid();
        }
示例#3
0
        /// <summary>
        /// Delete the ingredient
        /// </summary>
        /// <param name="parameter">Parameter of the command handler</param>
        private void DeleteIngredientCommandHandler(object parameter)
        {
            IngredientModel itemToRemove = parameter as IngredientModel;

            if (itemToRemove != null)
            {
                int itemIndex = Ingredients.IndexOf(itemToRemove);

                // Rmove the item from the list
                Ingredients.RemoveAt(itemIndex);
                ValidateSystemOfEquations();
                CalculateIfValid();
            }
        }
示例#4
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="view">Model view</param>
        public MainWindowModel(MainWindow view)
        {
            _view = view;

            // Try to load initial data configuration from a file
            List <Ingredient> fileItems = Deserialize(@".\load.xml");

            if (fileItems != null && fileItems.Count > 2)
            {// Try to initialize from file
                Ingredient total = fileItems[0];
                IEnumerable <IngredientModel> ingredients = fileItems
                                                            .GetRange(1, fileItems.Count - 1)
                                                            .Select(i => new IngredientModel(view, i, IngredientChangedEventHandler));

                Total       = new IngredientModel(view, total, IngredientChangedEventHandler);
                Ingredients = new ObservableCollection <IngredientModel>(ingredients);
            }
            else
            {// default initialization
                string totalStr = (string)_view.Resources["Total"];
                // Total
                Total = new IngredientModel(view, totalStr, 0, 45, IngredientChangedEventHandler, true, false);

                // Ingredients
                string          waterStr         = (string)_view.Resources["Water"];
                IngredientModel ingredient1      = new IngredientModel(view, waterStr, 0, 0, IngredientChangedEventHandler, true, false);
                string          strongAlcoholStr = (string)_view.Resources["StrongAlcohol"];
                IngredientModel ingredient2      = new IngredientModel(view, strongAlcoholStr, 30, 60, IngredientChangedEventHandler, false, false);
                Ingredients = new ObservableCollection <IngredientModel>(new[] { ingredient1, ingredient2 });
            }

            // Commands
            CalculateCommand        = new Command(CalculateCommandHandler, CalculateCommandCanExecute);
            LoadDataCommand         = new Command(LoadDataCommandHandler, LoadDataCommandCanExecute);
            SaveDataCommand         = new Command(SaveDataCommandHandler, SaveDataCommandCanExecute);
            AddIngredientCommand    = new Command(AddIngredientCommandHandler, AddIngredientCommandCanExecute);
            DeleteIngredientCommand = new Command(DeleteIngredientCommandHandler, DeleteMappingItemCommandCanExecute);

            // Validate
            ValidateSystemOfEquations();

            // Calculate
            CalculateIfValid();
        }
示例#5
0
        /// <summary>
        /// Set NaN to all unknown values selected for calculation
        /// </summary>
        /// <param name="ingredients">Ingredients</param>
        /// <param name="total">Total</param>
        public static void ClearDataForCalculation(IEnumerable <IngredientModel> ingredients, IngredientModel total)
        {
            foreach (IngredientModel ingredient in ingredients)
            {
                if (ingredient.IsQuantityUnknown)
                {
                    ingredient.Quantity = double.NaN;
                }

                if (ingredient.IsConcentrationUnknown)
                {
                    ingredient.Concentration = double.NaN;
                }
            }

            if (total.IsQuantityUnknown)
            {
                total.Quantity = double.NaN;
            }

            if (total.IsConcentrationUnknown)
            {
                total.Concentration = double.NaN;
            }
        }