Example #1
0
        private void AddNewExpenseButton_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateForm())
            {
                double amount = double.Parse(expenseAmount.Text);
                amount -= amount * 2; // expenses have a negative amount
                Entry newExpense = new Entry(expenseName.Text, amount, expenseCategory.Text, expenseDate.Value, Budget.Id);

                /* Create a expense entry for all existing connections: */
                // temporary expense instance:
                Entry ExpenseEntry = new Entry();
                // edit budget:
                decimal newBalance = Budget.Balance + Convert.ToDecimal(newExpense.Amount);
                Budget.Balance = newBalance;

                // sql and text:
                if (GlobalConfig.sqlConnection && GlobalConfig.textConnection)
                {
                    // sql:
                    ExpenseEntry = GlobalConfig.SQLConnection.SaveEntry(newExpense);
                    GlobalConfig.SQLConnection.EditBudgetBalance(Budget.Id, newBalance);
                    // text: pass the modified expense instance (sql added an unique ID to it) to the text file saver:
                    GlobalConfig.TextFileConnection.SaveEntry(ExpenseEntry);
                    // edit budget balance in the text file:
                    GlobalConfig.TextFileConnection.EditBudgetBalance(Budget.Id, newBalance);
                }
                // sql:
                else if (GlobalConfig.sqlConnection)
                {
                    ExpenseEntry = GlobalConfig.SQLConnection.SaveEntry(newExpense);
                    GlobalConfig.SQLConnection.EditBudgetBalance(Budget.Id, newBalance);
                }
                // text file:
                else if (GlobalConfig.textConnection)
                {
                    newExpense.Id = id;
                    id++;
                    GlobalConfig.TextFileConnection.SaveEntry(newExpense);
                    // edit budget balance in the text file:
                    GlobalConfig.TextFileConnection.EditBudgetBalance(Budget.Id, newBalance);
                }


                Button anotherExpense = (Button)sender;
                if (anotherExpense.Name == "NewExpenseButton")
                {
                    // reset values:
                    expenseAmount.Text           = "0";
                    expenseName.Text             = "My Expense";
                    expenseCategory.SelectedItem = null;
                    expenseDate.Value            = DateTime.Now;
                }
                else
                {
                    BudgetViewer window = new BudgetViewer(Budget);
                    window.Show();
                    this.Close();
                }
            }
        }
        /// <summary>
        /// A method that opens BudgetViewer window provided the user selected a budget from the ComboBox.
        /// </summary>
        private void ViewBudgetButton_Click(object sender, RoutedEventArgs e)
        {
            Budget Budget = new Budget(null, 0);

            if (existingBudgets.SelectedItem == null)
            {
                MessageBox.Show("You haven't selected a budget!");
            }
            else
            {
                Budget = (Budget)existingBudgets.SelectedItem;
                BudgetViewer view = new BudgetViewer(Budget);
                view.Show();
                this.Close();
            }
        }
Example #3
0
        /// <summary>
        /// A method that creates a budget and saves it to a SQL db and a text file provided the form was filled correctly.
        /// </summary>
        private void CreateBudgetButton_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateForm())
            {
                decimal balance              = decimal.Parse(startingBalance.Text);
                Budget  newBudget            = new Budget(budgetName.Text, balance);
                Entry   startingBalanceEntry = new Entry();

                /* Create a budget entry for all existing connections: */

                if (GlobalConfig.sqlConnection && GlobalConfig.textConnection)
                {
                    newBudget = GlobalConfig.SQLConnection.SaveBudget(newBudget);
                    // pass the modified budget instance (sql added an unique ID to it) to the text file saver:
                    GlobalConfig.TextFileConnection.SaveBudget(newBudget);
                    if (newBudget.Balance != 0)
                    {
                        startingBalanceEntry = new Entry("Starting Balance", (Double)newBudget.Balance, "UserInput", DateTime.Now, newBudget.Id);
                        startingBalanceEntry = GlobalConfig.SQLConnection.SaveEntry(startingBalanceEntry);
                        GlobalConfig.TextFileConnection.SaveEntry(startingBalanceEntry);
                    }
                }
                else if (GlobalConfig.sqlConnection)
                {
                    newBudget = GlobalConfig.SQLConnection.SaveBudget(newBudget);
                    if (newBudget.Balance != 0)
                    {
                        startingBalanceEntry = new Entry("Starting Balance", (Double)newBudget.Balance, "UserInput", DateTime.Now, newBudget.Id);
                        startingBalanceEntry = GlobalConfig.SQLConnection.SaveEntry(startingBalanceEntry);
                    }
                }
                else if (GlobalConfig.textConnection)
                {
                    newBudget.Id = id;
                    id++;
                    GlobalConfig.TextFileConnection.SaveBudget(newBudget);
                    if (newBudget.Balance != 0)
                    {
                        startingBalanceEntry    = new Entry("Starting Balance", (Double)newBudget.Balance, "UserInput", DateTime.Now, newBudget.Id);
                        startingBalanceEntry.Id = entry;
                        GlobalConfig.TextFileConnection.SaveEntry(startingBalanceEntry);
                    }
                }

                // reset values:
                startingBalance.Text = "0";
                budgetName.Text      = "My Budget";

                if (addExpense)
                {
                    AddExpenses window = new AddExpenses(newBudget);
                    window.Show();
                    this.Close();
                }
                else
                {
                    BudgetViewer view = new BudgetViewer(newBudget);
                    view.Show();
                    this.Close();
                }
            }
        }