コード例 #1
0
        public static ConsoleKeyInfo DisplayMenu(string username)
        {
            Console.Clear();
            Console.Title = "Internal Bank System - Main Menu";

            Console.WriteLine();
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkMagenta;

            Console.WriteLine($"Welcome {username}\n");

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;

            var account = new InternalAccount();

            if (account.IsUserAdmin(username))
            {
                return(DisplayAdminMenu(username));
            }
            return(DisplaySimpleUserMenu(username));
        }
コード例 #2
0
        private static void ViewMyAccount(string username)
        {
            var account = new InternalAccount();

            Console.Title = "Internal Bank System - My Account";
            Console.Clear();
            if (account.IsUserAdmin(username))
            {
                Console.WriteLine($"User:{username}\tYou pressed 1. View Cooperative's internal bank account");
            }
            else
            {
                Console.WriteLine($"User:{username}\tYou pressed 1. View my bank account");
            }

            Console.WriteLine();
            Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine(account.ViewAccount(username));
            Console.WriteLine("------------------------------------------------------------");

            GoToMainMenu();
            return;
        }
コード例 #3
0
        public bool SendTodaysStatements(string username, List <string> memory)
        {
            var    account  = new InternalAccount();
            var    today    = DateTime.Today;
            string filePath = "C:\\Users\\vivi\\Desktop\\";
            string fileName;

            if (account.IsUserAdmin(username))
            {
                fileName = "statement_admin" + today.ToString("_dd_MM_yyyy") + ".txt";
            }
            else
            {
                var userId = account.GetUserId(username);
                fileName = "statement_user_" + userId + today.ToString("_dd_MM_yyyy") + ".txt";
            }

            if (!File.Exists(filePath + fileName))
            {
                memory.Insert(0, $"User: {username}\t | Date: {today:dd/MM/yyyy}\n");
                memory.Insert(1, "");
                memory.Insert(2, "Transaction\t Participants\t Amount\t Transaction Date        \t Account Balance\n");
            }

            try
            {
                using (var sw = new StreamWriter(filePath + fileName, true, Encoding.Unicode))
                {
                    foreach (var item in memory)
                    {
                        sw.WriteLine(item);
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nA file error occurred.");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Press Enter to see Details about the error or any other button to continue..");
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine($"\nError Details: {ex.Message}");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.Write("\nPress any button to continue ..");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.ReadKey();
                }

                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("\nTo go back to Main Menu press Enter. To continue and exit press any key..");
                Console.ForegroundColor = ConsoleColor.White;
                var k = Console.ReadKey();
                if (k.Key != ConsoleKey.Enter)
                {
                    Console.Clear();
                    Console.Title = "Internal Bank System - Exit";
                    Console.WriteLine("\nApplication is closing..");
                    Console.WriteLine($"Goodbye.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.Write("\nPress any button to exit the application ..");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.ReadKey();
                    Environment.Exit(0);
                    Console.ReadKey();
                }
                return(false);
            }
        }
コード例 #4
0
        private static void SendToFile(string username)
        {
            Console.Title = "Internal Bank System - Save File";
            Console.Clear();
            var account = new InternalAccount();

            if (account.IsUserAdmin(username))
            {
                Console.WriteLine($"User:{username}\tYou pressed 5. Send today’s transactions and Exit");
            }
            else
            {
                Console.WriteLine($"User:{username}\tYou pressed 4. Send today’s transactions and Exit");
            }
            Console.WriteLine();

            if (memory.Count == 0)
            {
                Console.WriteLine("There are no transactions to be saved.. Memory is empty.");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("\nPress any button to continue and exit the application ..");
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey();

                Console.Clear();
                Console.Title = "Internal Bank System - Exit";
                Console.WriteLine($"\nGoodbye {username}.");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("\nPress any button to exit the application ..");
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey();
                Environment.Exit(0);
                Console.ReadKey();
            }

            //success
            Console.WriteLine("Saving memory to file..");
            var file  = new FileAccess();
            var valid = file.SendTodaysStatements(username, memory);

            Thread.Sleep(500);
            if (valid)
            {
                Console.WriteLine( );
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Successful Operation!");
                Console.ForegroundColor = ConsoleColor.White;

                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("\nPress any button to continue ..");
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey();

                Console.Clear();
                Console.Title = "Internal Bank System - Exit";
                Console.WriteLine($"\nGoodbye {username}.");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("\nPress any button to exit the application ..");
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey();
                Environment.Exit(0);
                Console.ReadKey();
            }
        }
コード例 #5
0
        private static void WithdrawMenu(string username)
        {
            var message = "";
            var i       = 0;

            while (true)
            {
                Console.Clear();
                Console.Title = "Internal Bank System - Withdraw Menu";
                Console.WriteLine($"User:{username}\tYou pressed 4. Withdraw from account\n");


                //error messages
                if (i > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{message} \nPlease try again."); // You have {tries} tries.\n");
                    Console.ForegroundColor = ConsoleColor.White;
                    //------------------------------------------------------------------------------------------------
                    var key = Esc();
                    if (key.Key == ConsoleKey.Escape)
                    {
                        return;
                    }
                    //------------------------------------------------------------------------------------------------
                }
                i++;

                //available users
                var account = new InternalAccount();
                var users   = account.GetUsernames();

                //withdraw data input
                Console.WriteLine("Please insert the username of the account you want to withdraw from and the amount you want to withdraw.");
                Console.WriteLine("These are the available users:");
                foreach (var item in users)
                {
                    Console.WriteLine($"- {item}");
                }
                Console.WriteLine("To withdraw the same amount to from all accounts type [all] in place of username.");
                Console.Write("Username: "******"Amount: ");
                var amountValidation = Decimal.TryParse(Console.ReadLine(), out decimal amount);

                // if admin from all
                if (account.IsUserAdmin(username) && otherUsername.ToLower() == "all")
                {
                    //Input validation for all
                    Console.WriteLine();
                    if (amount <= 0 || !amountValidation)
                    {
                        message = "Invalid input. Insert a positive number as amount..";
                        continue;
                    }

                    var suffBalance = true;
                    foreach (var unames in users)
                    {
                        if (!account.HasSufficientBalance(unames, amount))
                        {
                            message     = "Insufficient account balance.";
                            suffBalance = false;
                            continue;
                        }
                    }
                    if (!suffBalance)
                    {
                        message = "Insufficient account balance.";
                        continue;
                    }


                    //Successful input
                    Console.WriteLine("\nProcessing your request..");

                    account.WithdrawFromAll(username, amount, out string msg);
                    memory.Add(msg);
                    Thread.Sleep(500);

                    DisplayResults();
                    return;
                }

                //Input validation
                Console.WriteLine();
                if (!account.IsUserValid(otherUsername))
                {
                    message = "User not found.";
                }
                else if (amount <= 0 || !amountValidation)
                {
                    message = "Invalid input. Insert a positive number as amount..";
                }
                else if (username == otherUsername)
                {
                    message = "Cannot withdraw from Cooperative's internal bank account.";
                }
                else if (!account.HasSufficientBalance(otherUsername, amount))
                {
                    message = "Insufficient account balance.";
                }
                else
                {
                    //Successful input
                    Console.WriteLine("\nProcessing your request..");

                    account.Withdraw(otherUsername, amount, out string msg);
                    memory.Add(msg);

                    Thread.Sleep(500);
                    DisplayResults();
                    return;
                }
            }
        }
コード例 #6
0
        private static void Deposit(string username)
        {
            var message = "";
            var i       = 0;

            while (true)
            {
                Console.Clear();
                Console.Title = "Internal Bank System - Deposit Menu";
                Console.WriteLine($"User:{username}\tYou pressed 3. Deposit to user's bank account\n");

                //error messages
                if (i > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{message} \nPlease try again.");
                    Console.ForegroundColor = ConsoleColor.White;
                    //------------------------------------------------------------------------------------------------
                    var key = Esc();
                    if (key.Key == ConsoleKey.Escape)
                    {
                        return;
                    }
                    //------------------------------------------------------------------------------------------------
                }
                i++;

                //available users
                var account = new InternalAccount();
                var users   = account.GetUsernames();

                //deposit data input
                Console.WriteLine("Please insert the username of the account you want to deposit to and the amount you want to deposit.");
                Console.WriteLine("These are the available users:");
                foreach (var item in users)
                {
                    if (item != username)
                    {
                        Console.WriteLine($"- {item}");
                    }
                }
                if (account.IsUserAdmin(username))
                {
                    Console.WriteLine("To deposit the same amount to all accounts type [all] in place of username.");
                }
                Console.Write("Username: "******"Amount: ");
                var amountValidation = Decimal.TryParse(Console.ReadLine(), out decimal amount);

                //If admin and input all
                if (account.IsUserAdmin(username) && otherUsername.ToLower() == "all")
                {
                    //Input validation for all
                    var adminAmount = (users.Count - 1) * amount;
                    Console.WriteLine();
                    if (amount <= 0 || !amountValidation)
                    {
                        message = "Invalid input. Insert a positive number as amount..";
                        continue;
                    }
                    else if (!account.HasSufficientBalance(username, adminAmount))
                    {
                        message = "Insufficient account balance.";
                        continue;
                    }


                    //Successful input
                    Console.WriteLine("\nProcessing your request..");

                    account.DepositToAll(username, amount, out string msg);
                    memory.Add(msg);
                    Thread.Sleep(500);

                    DisplayResults();

                    return;
                }


                //Input validation for user
                Console.WriteLine();
                if (!account.IsUserValid(otherUsername))
                {
                    message = "User not found.";
                }
                else if (amount <= 0 || !amountValidation)
                {
                    message = "Invalid input. Insert a positive number as amount..";
                }
                else if (username == otherUsername)
                {
                    message = "Cannot deposit to your account.";
                }
                else if (account.IsUserAdmin(otherUsername))
                {
                    message = "Cannot deposit to Cooperative’s internal bank account. \nTo deposit to Cooperative’s internal bank account go back to Main Menu.";
                }
                else if (!account.HasSufficientBalance(username, amount))
                {
                    message = "Insufficient account balance.";
                }
                else
                {
                    //Successful input
                    Console.WriteLine("\nProcessing your request..");

                    account.DepositToAccount(username, otherUsername, amount, out string msg);
                    memory.Add(msg);
                    Thread.Sleep(500);

                    DisplayResults();
                    return;
                }
            }
        }