コード例 #1
0
        /*
         * Tiến hành chuyển khoản, mặc định là trong ngân hàng.
         * 1. Yêu cầu nhập số tài khoản cần chuyển.(số tài khoản của người nhận.)
         *     1.1. Xác minh thông tin tài khoản và hiển thị tên người cần chuyển.
         * 2. Nhập số tiền cần chuyển.
         *     2.1. Kiểm tra số dư tài khoản.
         * 3. Nhập nội dung chuyển tiền.
         *     3.1 Xác nhận nội dung chuyển tiền.
         * 4. Thực hiện chuyển tiền.
         *     4.1. Mở transaction. Mở block try catch.
         *     4.2. Trừ tiền người gửi.
         *         4.2.1. Lấy thông tin tài khoản gửi tiền một lần nữa. Đảm bảo thông tin là mới nhất.
         *         4.2.2. Kiểm tra lại một lần nữa số dư xem có đủ tiền để chuyển không.
         *             4.2.2.1. Nếu không đủ thì rollback.
         *             4.2.2.2. Nếu đủ thì trừ tiền và update vào bảng `accounts`.
         *     4.3. Cộng tiền người nhận.
         *         4.3.1. Lấy thông tin tài khoản nhận, đảm bảo tài khoản không bị khoá hoặc inactive.
         *         4.3.1.1. Nếu ok thì update số tiền cho người nhận.
         *         4.3.1.2. Nếu không ok thì rollback.
         *     4.4. Lưu lịch sử giao dịch.
         *     4.5. Kiểm tra lại trạng thái của 3 câu lệnh trên.
         *         4.5.1. Nếu cả 3 cùng thành công thì commit transaction.
         *         4.5.2. Nếu bất kỳ một câu lệnh nào bị lỗi thì rollback.
         *     4.x. Đóng, commit transaction.
         */
        public void Transfer()
        {
            Console.WriteLine("Nhập số tài khoản cần chuyển: ");
            var       accountNumber = Console.ReadLine();
            YYAccount account       = model.GetByAccountNumber(accountNumber);

            if (account == null)
            {
                Console.WriteLine("Invalid account information");
                return;
            }
            Console.WriteLine("Tên người cần chuyển: " + account.Name);
            Console.WriteLine("Nhập số tiền cần chuyển: ");
            var amount = Utillty.GetDecimalNumber();

            Console.WriteLine("Please enter message content: ");
            var content = Console.ReadLine();
//            Program.currentLoggedIn = model.GetAccountByUserName(Program.currentLoggedIn.Username);
            var historyTransaction = new YYTransaction()
            {
                Id                    = Guid.NewGuid().ToString(),
                Type                  = YYTransaction.TransactionType.TRANSFER,
                Amount                = amount,
                Content               = content,
                SenderAccountNumber   = Program.currentLoggedInYyAccount.AccountNumber,
                ReceiverAccountNumber = accountNumber,
                Status                = YYTransaction.ActiveStatus.DONE
            };

            if (model.Tranfer(Program.currentLoggedInYyAccount, historyTransaction))
            {
                Console.WriteLine("Transaction success!");
            }
            else
            {
                Console.WriteLine("Transaction fails, please try again!");
            }
            Program.currentLoggedInYyAccount = model.Login(Program.currentLoggedInYyAccount.Username);
            Console.WriteLine("Current balance: " + Program.currentLoggedInYyAccount.Balance);
            Console.WriteLine("Press enter to continue!");
            Console.ReadLine();
        }