Exemple #1
0
        private void AddExpenseBtn_Click(object sender, EventArgs e)
        {
            string  expenseTitle    = ExpenseTitleTbx.Text;
            string  expenseNotes    = ExpenseNotesRtbx.Text;
            decimal expensePrice    = ExpensePriceNum.Value;
            int     expenseQuantity = Convert.ToInt32(ExpenseQuantityNum.Value);

            if (string.IsNullOrEmpty(ExpenseTitleTbx.Text))
            {
                throw new ApplicationException("Please, specify a title.");
            }

            if (expensePrice <= 0)
            {
                throw new ApplicationException("Please, specify a positive price.");
            }

            Expense expense = Expense.Create(expenseTitle, expensePrice, expenseQuantity,
                                             string.IsNullOrWhiteSpace(expenseNotes) ? null : expenseNotes);

            // Share expeses with all users by default
            foreach (User user in User.Enumerate())
            {
                if (user != Server.CurrentSession.Info)
                {
                    expense.AddParticipant(user.Id);
                }
            }

            AddExpenseToUI(expense);
        }
Exemple #2
0
        public void CalculateBill()
        {
            Participant p1 = new Participant();

            p1.AddCharge(10.00);
            p1.AddCharge(20.00);

            Participant p2 = new Participant();

            p2.AddCharge(15.00);
            p2.AddCharge(15.01);
            p2.AddCharge(3.00);
            p2.AddCharge(3.01);

            Participant p3 = new Participant();

            p3.AddCharge(5.00);
            p3.AddCharge(9.00);
            p3.AddCharge(4.00);

            Expense e = new Expense();

            e.AddParticipant(p1);
            e.AddParticipant(p2);
            e.AddParticipant(p3);

            List <Expense> eList = new List <Expense> {
                e
            };

            ICalculator c = new ExpenseCalculator(eList);

            c.Calculate();

            Assert.AreEqual(Math.Round(eList[0].Participants[0].Balance, 2), -1.99);
            Assert.AreEqual(Math.Round(eList[0].Participants[1].Balance, 2), -8.01);
            Assert.AreEqual(Math.Round(eList[0].Participants[2].Balance, 2), 10.01);
        }
Exemple #3
0
        private void ProcessExpense()
        {
            try
            {
                //Check if token is correct
                if (_lookaheadFirst.TokenType == TokenType.Quantity)
                {
                    //Get quantity of participants
                    int participantsQtt = int.Parse(_lookaheadFirst.Value);
                    //Add new expense
                    Expense expense = new Expense();
                    _expenses.Add(expense);
                    //Discart current token
                    DiscardToken();

                    //Process participants
                    for (int i = 0; i < participantsQtt; i++)
                    {
                        Participant p = new Participant();
                        ProcessParticipant(p);
                        expense.AddParticipant(p);
                    }

                    //Try to process new expense
                    ProcessExpense();
                }
                else if (_lookaheadFirst.TokenType == TokenType.Zero)
                {
                    //End of file
                    return;
                }
                else if (_tokenSequence.Count == 0)
                {
                    throw new InvalidEndOfFileException();
                }
                else
                {
                    throw new InvalidTokenException(_line, _lookaheadFirst.Value, "Number of participants or 0 to end file");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void AddExpenseBtn_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(ExpenseTitleTbx.Text))
            {
                MessageBox.Show("Please enter expense title");
                return;
            }

            string expenseTitle = ExpenseTitleTbx.Text;
            string expenseNotes = ExpenseNotesRtbx.Text;

            decimal expensePrice    = 0;
            int     expenseQuantity = 1;

            try
            {
                expensePrice    = ExpensePriceNum.Value;
                expenseQuantity = Convert.ToInt32(ExpenseQuantityNum.Value);
            }
            catch (Exception)
            {
                MessageBox.Show("Please enter a correct number");
                return;
            }

            Expense expense = Expense.Create(expenseTitle, expensePrice, expenseQuantity, expenseNotes);

            // Share expeses with all users by default
            foreach (User user in User.Enumerate())
            {
                if (user != Server.CurrentSession.Info)
                {
                    expense.AddParticipant(user.Id);
                }
            }

            CalculateAndPopulateExpenses();
            MessageBox.Show("You successfully created the expense!");
        }