private void PredictWeeklySales(object sender, EventArgs e) { List <Sale> WeeklySalesList; double[] WeeklySales = new double[4] { 0, 0, 0, 0 }; double[] TotalCost = new double[4] { 0, 0, 0, 0 }; ProductTypeList = _PHPRepo.GetProductByType(_productType); //Check for error in input if (!(ProductTypeList.Any())) { MessageBox.Show("Invalid Product Type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { ProductTypeTextBox.Text = _productType; PredictionPeriod.Text = DateTime.Now.ToString() + " - " + DateTime.Now.AddDays(7).ToString(); } //Data from previous 2 weeks for (int i = 0; i < 2; i++) { //Fill first 2 eleement of array with data from previous 2 weeks WeeklySalesList = _PHPRepo.GetSaleByDate(DateTime.Now.AddDays(-7 - i * 7), DateTime.Now.AddDays(-i * 7)); foreach (Product p in ProductTypeList) { foreach (Sale s in WeeklySalesList) { foreach (ProductSale ps in s.ProductSales) { if (ps.Product == p) { TotalCost[i] += ps.Product.Price * ps.Quantity; WeeklySales[i] = WeeklySales[i] + ps.Quantity; } } } } } for (int i = 2; i < 4; i++) { WeeklySalesList = _PHPRepo.GetSaleByDate(DateTime.Now.AddYears(1 - i).AddDays(-7), DateTime.Now.AddYears(1 - i)); foreach (Product p in ProductTypeList) { foreach (Sale s in WeeklySalesList) { foreach (ProductSale ps in s.ProductSales) { if (ps.Product == p) { TotalCost[i] += ps.Product.Price * ps.Quantity; WeeklySales[i] = WeeklySales[i] + ps.Quantity; } } } } } PredictedSales.Text = PredictSales(WeeklySales).ToString(); TotalCostText.Text = "$" + CostPrediction(TotalCost).ToString(); }