public void guitien() { Program.currentLoggedInAccount = bankmodel.FindByAccountbyUsername(Program.currentLoggedInAccount.Username); if (Program.currentLoggedInAccount != null) { Console.WriteLine("Nhập số tiền cần gửi: "); var amount = double.Parse(Console.ReadLine()); // lấy thông tin tài khoản mới nhất trước khi kiểm tra số dư. var transactionHistory = new TransactionNganhang() { Id = Guid.NewGuid().ToString(), SenderId = Program.currentLoggedInAccount.Accountid, ReceiverId = Program.currentLoggedInAccount.Accountid, Type = TransactionNganhang.TrasactionType.Deposit, Message = "Tiến hành gửi tiền tại ATM với số tiền: " + amount, Amount = amount, CreatedAtMls = DateTime.Now.Ticks, UpdatedAtMls = DateTime.Now.Ticks, Status = 1 }; if (bankmodel.updatebalance(Program.currentLoggedInAccount, transactionHistory)) { Console.WriteLine("Giao dịch thành công."); } } else { Console.WriteLine("Vui lòng đăng nhập để sử dụng chức năng này."); } }
public void giaodich() { Console.WriteLine("Vui lòng nhập số tài khoản chuyển tiền: "); var accountNumber = Console.ReadLine(); var receiverAccount = bankmodel.FindByAccountbyAccountid(accountNumber); if (receiverAccount == null) { Console.WriteLine("Tài khoản nhận tiền không tồn tại hoặc đã bị khoá."); return; } Console.WriteLine("Tài khoản nhận tiền: " + accountNumber); Console.WriteLine("Chủ tài khoản: " + receiverAccount.Username); Console.WriteLine("Nhập số tiền chuyển khoản: "); var amount = double.Parse(Console.ReadLine()); Program.currentLoggedInAccount = bankmodel.FindByAccountbyUsername(Program.currentLoggedInAccount.Username); if (amount > Program.currentLoggedInAccount.Balance) { Console.WriteLine("Số dư tài khoản không đủ thực hiện giao dịch."); return; } // Console.WriteLine("Nhập nội dung giao dịch: "); // var content = Console.ReadLine(); var transactionHistory = new TransactionNganhang() { Id = Guid.NewGuid().ToString(), Type = TransactionNganhang.TrasactionType.Stranfer, Amount = amount, Message = "", SenderId = Program.currentLoggedInAccount.Accountid, ReceiverId = accountNumber }; if (Program.currentLoggedInAccount.Accountid == accountNumber) { return; } if (bankmodel.Transfer(Program.currentLoggedInAccount, transactionHistory)) { Console.WriteLine("Giao dịch thành công."); } else { Console.WriteLine("Giao dịch thất bại, vui lòng thử lại."); } }
public void ruttien() { Program.currentLoggedInAccount = bankmodel.FindByAccountbyUsername(Program.currentLoggedInAccount.Username); if (Program.currentLoggedInAccount != null) { Console.Clear(); Console.WriteLine("Tiến hành rút tiền tại hệ thống SHB."); Console.WriteLine("Vui lòng nhập số tiền cần rút."); var amount = double.Parse(Console.ReadLine()); if (amount <= 0) { Console.WriteLine("Số lượng không hợp lệ, vui lòng thử lại."); return; } var transaction = new TransactionNganhang() { Id = Guid.NewGuid().ToString(), SenderId = Program.currentLoggedInAccount.Accountid, ReceiverId = Program.currentLoggedInAccount.Accountid, Type = TransactionNganhang.TrasactionType.Withdaw, Message = "Tiến hành rút tiền tại ATM với số tiền: " + amount, Amount = amount, CreatedAtMls = DateTime.Now.Ticks, UpdatedAtMls = DateTime.Now.Ticks, Status = 1 }; if (bankmodel.updatebalance(Program.currentLoggedInAccount, transaction) ) { Console.WriteLine("Giao dịch thành công."); } else { Console.WriteLine("Giao dịch thất bại, vui lòng thử lại."); } } else { Console.WriteLine("Vui lòng đăng nhập để sử dụng chức năng này."); } }
public bool updatebalance(NganhangAccount account, TransactionNganhang transactionNganhang) { var tran = ConnectionHepper.GetConnection().BeginTransaction(); try { var cmd = new MySqlCommand("select Balance from AccountNganhang where Accountid = @Accountid", ConnectionHepper.GetConnection()); cmd.Parameters.AddWithValue("@Accountid", account.Accountid); var reader = cmd.ExecuteReader(); double currentAccountBalance = 0; if (reader.Read()) { currentAccountBalance = reader.GetDouble("Balance"); } reader.Close(); if (currentAccountBalance < 0) { Console.WriteLine("Không đủ tiền trong tài khoản."); return(false); } if (transactionNganhang.Type == (TransactionNganhang.TrasactionType) 1) { if (currentAccountBalance < transactionNganhang.Amount) { Console.WriteLine("Khong du tien thuc hien giao dich"); return(false); } currentAccountBalance -= transactionNganhang.Amount; } else if (transactionNganhang.Type == (TransactionNganhang.TrasactionType) 2) { currentAccountBalance += transactionNganhang.Amount; } var updateQuery = "update AccountNganhang set `Balance` = @balance where Accountid = @Accountid"; var sqlCmd = new MySqlCommand(updateQuery, ConnectionHepper.GetConnection()); sqlCmd.Parameters.AddWithValue("@Balance", currentAccountBalance); sqlCmd.Parameters.AddWithValue("@Accountid", account.Accountid); var updateResult = sqlCmd.ExecuteNonQuery(); var historyTransactionQuery = "insert into TransactionNganhang (Id, Type, Senderid, ReceiverId, Amount, Message) " + "values (@Id, @Type, @Senderid, @ReceiverId, @Amount, @Message)"; var historyTransactionCmd = new MySqlCommand(historyTransactionQuery, ConnectionHepper.GetConnection()); historyTransactionCmd.Parameters.AddWithValue("@Id", transactionNganhang.Id); historyTransactionCmd.Parameters.AddWithValue("@Amount", transactionNganhang.Amount); historyTransactionCmd.Parameters.AddWithValue("@Type", transactionNganhang.Type); historyTransactionCmd.Parameters.AddWithValue("@Message", transactionNganhang.Message); historyTransactionCmd.Parameters.AddWithValue("@Senderid", transactionNganhang.SenderId); historyTransactionCmd.Parameters.AddWithValue("@Receiverid", transactionNganhang.ReceiverId); var historyResult = historyTransactionCmd.ExecuteNonQuery(); if (updateResult != 1 || historyResult != 1) { throw new Exception("Không thể thêm giao dịch hoặc update tài khoản."); } tran.Commit(); } catch (Exception e) { Console.WriteLine(e.Message); tran.Rollback(); // lưu giao dịch vào. return(false); } ConnectionHepper.CloseConnection(); return(true); }
public bool Transfer(NganhangAccount currentLoggedInAccount, TransactionNganhang transactionHistory) { var mySqlTransaction = ConnectionHepper.GetConnection().BeginTransaction(); try { // Kiểm tra số dư tài khoản. var selectBalance = "select Balance from AccountNganhang where Accountid = @Accountid"; var cmdSelect = new MySqlCommand(selectBalance, ConnectionHepper.GetConnection()); cmdSelect.Parameters.AddWithValue("@Accountid", currentLoggedInAccount.Accountid); var reader = cmdSelect.ExecuteReader(); double currentAccountBalance = 0; if (reader.Read()) { currentAccountBalance = reader.GetDouble("Balance"); } reader.Close(); // important. if (currentAccountBalance < transactionHistory.Amount) { throw new Exception("Không đủ tiền trong tài khoản."); } currentAccountBalance -= transactionHistory.Amount; // Update tài khoản. var updateQuery = "update `AccountNganhang` set `Balance` = @Balance where Accountid = @Accountid "; var sqlCmd = new MySqlCommand(updateQuery, ConnectionHepper.GetConnection()); sqlCmd.Parameters.AddWithValue("@Balance", currentAccountBalance); sqlCmd.Parameters.AddWithValue("@Accountid", currentLoggedInAccount.Accountid); var updateResult = sqlCmd.ExecuteNonQuery(); // Kiểm tra số dư tài khoản. var selectBalanceReceiver = "select Balance from `AccountNganhang` where Accountid = @Accountid "; var cmdSelectReceiver = new MySqlCommand(selectBalanceReceiver, ConnectionHepper.GetConnection()); cmdSelectReceiver.Parameters.AddWithValue("@Accountid", transactionHistory.ReceiverId); var readerReceiver = cmdSelectReceiver.ExecuteReader() ?? throw new ArgumentNullException("cmdSelectReceiver.ExecuteReader()"); double receiverBalance = 0; if (readerReceiver.Read()) { receiverBalance = readerReceiver.GetDouble("Balance"); } readerReceiver.Close(); // important. receiverBalance += transactionHistory.Amount; // Update tài khoản. var updateQueryReceiver = "update `AccountNganhang` set `Balance` = @Balance where Accountid = @Accountid"; var sqlCmdReceiver = new MySqlCommand(updateQueryReceiver, ConnectionHepper.GetConnection()); sqlCmdReceiver.Parameters.AddWithValue("@Balance", receiverBalance); sqlCmdReceiver.Parameters.AddWithValue("@Accountid", transactionHistory.ReceiverId); var updateResultReceiver = sqlCmdReceiver.ExecuteNonQuery(); // Lưu lịch sử giao dịch. var historyTransactionQuery = "insert into TransactionNganhang (Id, Type, Senderid, ReceiverId, Amount, Message) " + "values (@Id, @Type, @Senderid, @ReceiverId, @Amount, @Message)"; var historyTransactionCmd = new MySqlCommand(historyTransactionQuery, ConnectionHepper.GetConnection()); historyTransactionCmd.Parameters.AddWithValue("@Id", transactionHistory.Id); historyTransactionCmd.Parameters.AddWithValue("@Amount", transactionHistory.Amount); historyTransactionCmd.Parameters.AddWithValue("@Type", transactionHistory.Type); historyTransactionCmd.Parameters.AddWithValue("@Message", transactionHistory.Message); historyTransactionCmd.Parameters.AddWithValue("@Senderid", transactionHistory.SenderId); historyTransactionCmd.Parameters.AddWithValue("@Receiverid", transactionHistory.ReceiverId); var historyResult = historyTransactionCmd.ExecuteNonQuery(); if (updateResult != 1 || historyResult != 1 || updateResultReceiver != 1) { throw new Exception("Không thể thêm giao dịch hoặc update tài khoản."); } mySqlTransaction.Commit(); return(true); } catch (Exception e) { Console.WriteLine(e.StackTrace); mySqlTransaction.Rollback(); return(false); } finally { ConnectionHepper.CloseConnection(); } }