コード例 #1
0
        public void AddBorrower(string name, decimal amount, bool shouldSaveToFile = true)
        {
            var isNotExist = true;

            foreach (var checkborrower in Borrowers)
            {
                if (checkborrower.Name == name)
                {
                    isNotExist = false;
                    break;
                }
            }

            if (isNotExist)
            {
                var borrower = new Borrower
                {
                    Name   = name,
                    Amount = amount
                };

                Borrowers.Add(borrower);

                if (shouldSaveToFile)
                {
                    File.AppendAllLines(FileName, new List <string> {
                        borrower.ToString()
                    });
                }
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Taki dłużnik już istnieje!");
                Console.ReadLine();
            }
        }
コード例 #2
0
        public void ChangeBorrower(string name, string calculation, decimal amountToChange, bool shouldSaveToFile = true)
        {
            foreach (var borrower in Borrowers)
            {
                if (borrower.Name == name)
                {
                    BorrowerAmount = borrower.Amount;
                    Borrowers.Remove(borrower);

                    break;
                }
            }

            if (calculation.ToLower() == "d")
            {
                var borrowerToChange = new Borrower
                {
                    Name   = name,
                    Amount = BorrowerAmount + amountToChange
                };

                if (borrowerToChange.Amount == amountToChange)
                {
                    Console.WriteLine();
                    Console.WriteLine("Nie ma takiego dłużnika");
                }
                else
                {
                    Borrowers.Add(borrowerToChange);
                    Console.WriteLine();
                    Console.WriteLine("Kwota długu zmnieniona");
                }
            }
            else
            {
                var borrowerToChange = new Borrower
                {
                    Name   = name,
                    Amount = BorrowerAmount - amountToChange
                };

                if (borrowerToChange.Amount > 0)
                {
                    Borrowers.Add(borrowerToChange);
                    Console.WriteLine();
                    Console.WriteLine("Kwota długu zmnieniona");
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Nie ma takiego dłużnika / Cała kwota została zwrócona - dłużnik usunięty");
                }
            }

            if (shouldSaveToFile)
            {
                var borrowersToSave = new List <string>();

                foreach (var borrower in Borrowers)
                {
                    borrowersToSave.Add(borrower.ToString());
                }

                File.Delete(FileName);
                File.WriteAllLines(FileName, borrowersToSave);
            }
        }