protected void btnSubmit_Click(object sender, EventArgs e) { Expense expense = new Expense() { DateOfExpense = DateTime.Parse(dateTxtBox.Text), ExpenseType = expenseTypeTxtBox.Text, Cost = double.Parse(sumTxtBox.Text) }; using (StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["FilePath"], true)) { writer.WriteLine(expense); } }
private static void ReadDataJsonDict() { var pathToDataSet = AppData.settings.PathToAppData; if (File.Exists(pathToDataSet)) { var sr = new StreamReader(pathToDataSet); string jsonString = sr.ReadToEnd(); sr.Close(); var dataSet = JsonSerializer.Deserialize <Dictionary <string, object> >(jsonString); // categories var categories = JsonSerializer.Deserialize <List <object> >(dataSet[categoriesKey].ToString()); foreach (var cat in categories) { var category = new ExpenseCategory() { name = cat.ToString() }; AppData.expenseCategories.Add(category); } // billing periods var billingPeriods = JsonSerializer.Deserialize <List <object> >(dataSet[billingPeriodsKey].ToString()); foreach (var bp in billingPeriods) { var billingPeriodFromJson = JsonSerializer.Deserialize <Dictionary <string, object> >(bp.ToString()); var startDate = DateTime.Parse(billingPeriodFromJson[billingPeriodStartDateKey].ToString()); var endDate = DateTime.Parse(billingPeriodFromJson[billingPeriodEndDateKey].ToString()); var plannedSavings = Decimal.Parse(billingPeriodFromJson[billingPeriodPlannedSavingsKey].ToString(), CultureInfo.InvariantCulture); var billingPeriod = new BillingPeriod() { startDate = startDate, endDate = endDate, plannedSavings = plannedSavings }; // new incomes if (billingPeriodFromJson.Keys.Contains(billingPeriodIncomesKey)) { var incomes = JsonSerializer.Deserialize <List <object> >(billingPeriodFromJson[billingPeriodIncomesKey].ToString()); foreach (var inc in incomes) { var incomeFromJson = JsonSerializer.Deserialize <Dictionary <string, string> >(inc.ToString()); var value = Decimal.Parse(incomeFromJson[incomeValueKey].ToString(), CultureInfo.InvariantCulture); if (value == 0) { continue; } var typeString = incomeFromJson[incomeTypeKey]; var type = typeString.Equals(billingPeriodIncomeTypeSalary) ? Income.IncomeType.Salary : Income.IncomeType.Additional; var comment = incomeFromJson.Keys.Contains(incomeCommentKey) ? incomeFromJson[incomeCommentKey] : ""; var income = new Income() { value = value, type = type, comment = comment }; billingPeriod.incomes.Add(income); } } else { if (billingPeriodFromJson.Keys.Contains(billingPeriodNetIncomeKey)) { var income = new Income() { value = Decimal.Parse(billingPeriodFromJson[billingPeriodNetIncomeKey].ToString(), CultureInfo.InvariantCulture), type = Income.IncomeType.Salary }; if (income.value > decimal.Zero) { billingPeriod.incomes.Add(income); } } if (billingPeriodFromJson.Keys.Contains(billingPeriodAdditionalIncomeKey)) { var income = new Income() { value = Decimal.Parse(billingPeriodFromJson[billingPeriodAdditionalIncomeKey].ToString(), CultureInfo.InvariantCulture), type = Income.IncomeType.Additional }; if (income.value > decimal.Zero) { billingPeriod.incomes.Add(income); } } } //expenses var expenses = JsonSerializer.Deserialize <List <object> >(billingPeriodFromJson[billingPeriodExpensesKey].ToString()); foreach (var exp in expenses) { var expenseFromJson = JsonSerializer.Deserialize <Dictionary <string, string> >(exp.ToString()); var value = Decimal.Parse(expenseFromJson[expenseValueKey].ToString(), CultureInfo.InvariantCulture); var date = DateTime.Parse(expenseFromJson[expenseDateKey].ToString()); var comment = expenseFromJson[expenseCommentKey].ToString(); var monthlyExpense = expenseFromJson[expenseMonthlyExpenseKey].ToString().Equals("True") ? true : false; var categoryName = expenseFromJson[expenseCategoryKey].ToString(); var category = new ExpenseCategory() { name = categoryName }; foreach (var cat in AppData.expenseCategories) { if (cat.Equals(category)) { category = cat; break; } } var expense = new Expense() { value = value, date = date, comment = comment, monthlyExpense = monthlyExpense, category = category }; billingPeriod.expenses.Add(expense); } AppData.billingPeriods.Add(billingPeriod); } } }