예제 #1
0
 public void AddNewDebt()
 {
     AddDebt addDebt = new AddDebt(this);
 }
예제 #2
0
        private static void AddNewDebt(ExpenseManager inEM)
        {
            ExpenseManager em = inEM;
            Debt debt = new Debt();
            int dExpenseId = 0;
            string dPersonOwing = string.Empty;
            double dAmountOutstanding = 0;
            bool dIsPaid = false;
            DateTime dDatePaid = new DateTime(9999, 01, 01);
            bool isValid = false;
            string input = string.Empty;
            string invalid = "\t\tInvalid Value.";

            List<int> expenseIdList = new List<int>();
            List<GeneralExpense> expenseList = em.GetListGeneralExpense();
            List<Bill> billList = em.GetListBill();

            // Get Expenses
            Console.WriteLine("Expenses:");
            foreach (GeneralExpense expense in expenseList)
            {
                Console.WriteLine(expense.GetShortDetails());
                expenseIdList.Add(expense.Id);
            }

            // Get Bills
            Console.WriteLine();
            Console.WriteLine("Bills:");
            foreach (Bill bill in billList)
            {
                Console.WriteLine(bill.GetShortDetails());
                expenseIdList.Add(bill.Id);
            }
            Console.WriteLine();

            // Get Expense Id
            while (!isValid)
            {
                Console.Write("Expense Id: ");
                input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine(invalid);
                    Console.ReadLine();
                }
                else
                {
                    if (input.Equals(_cancelChar))
                        return;
                    if (int.TryParse(input, out dExpenseId))
                    {
                        if (expenseIdList.Contains(dExpenseId))
                        {
                            isValid = true;
                        }
                        else
                        {
                            Console.WriteLine("\t\tNot a valid expense id.");
                            Console.ReadLine();
                        }
                    }
                    else
                    {
                        Console.WriteLine(invalid);
                        Console.ReadLine();
                    }
                }
            }

            isValid = false;

            // Get Person Owing
            while (!isValid)
            {
                Console.Write("Person Owing: ");
                dPersonOwing = Console.ReadLine();
                if (string.IsNullOrEmpty(dPersonOwing))
                {
                    Console.WriteLine(invalid);
                    Console.ReadLine();
                }
                else
                {
                    if (dPersonOwing.Equals(_cancelChar))
                        return;
                    isValid = true;
                }
            }

            isValid = false;
            input = string.Empty;

            // Get Amount
            while (!isValid)
            {
                Console.Write("Amount: ");
                input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine(invalid);
                    Console.ReadLine();
                }
                else
                {
                    if (input.Equals(_cancelChar))
                        return;
                    if (double.TryParse(input, out dAmountOutstanding))
                    {
                        isValid = true;
                    }
                    else
                    {
                        Console.WriteLine(invalid);
                        Console.ReadLine();
                    }
                }
            }

            isValid = false;
            input = string.Empty;

            // Is Paid
            while (!isValid)
            {
                Console.Write("Is Debt Paid (Y/N): ");
                input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine(invalid);
                    Console.ReadLine();
                }
                else
                {
                    switch (input.ToUpper())
                    {
                        case "Y":
                            if (input.Equals(_cancelChar))
                                return;
                            isValid = true;
                            dIsPaid = true;
                            break;
                        case "N":
                            if (input.Equals(_cancelChar))
                                return;
                            isValid = true;
                            dIsPaid = false;
                            break;
                        default:
                            Console.WriteLine(invalid);
                            Console.ReadLine();
                            break;
                    }
                }
            }

            isValid = false;
            input = string.Empty;

            if (dIsPaid)
            {
                // Date paid
                while (!isValid)
                {
                    Console.Write("Date Paid: ");
                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        Console.WriteLine(invalid);
                        Console.ReadLine();
                    }
                    else
                    {
                        if (input.Equals(_cancelChar))
                            return;
                        if (DateTime.TryParse(input, out dDatePaid))
                        {
                            isValid = true;
                        }
                        else
                        {
                            Console.WriteLine(invalid);
                            Console.ReadLine();
                        }
                    }
                }
            }
            else
            {
                dDatePaid = new DateTime(9999, 01, 01);
            }

            debt.ExpenseId = dExpenseId;
            debt.PersonOwing = dPersonOwing;
            debt.AmountOutstanding = dAmountOutstanding;
            debt.IsPaid = dIsPaid;
            debt.DatePaid = dDatePaid;

            AddDebt addDebt = new AddDebt(em);
            addDebt.InsertValues(debt);
        }