// Обновить информацию о банковском счете public void updateBankAccount(BankAccount account) { // Добавила Convert.ToInt32 для состояния счета var sql = String.Format("UPDATE bankAccounts SET client='{0}',amount='{1}',isBlocked='{2}' WHERE id='{3}'", account.ClientId, account.Amount, Convert.ToInt32(account.IsBlocked), account.Id); var cmd = new MySqlCommand(sql, mySqlConnection); cmd.ExecuteNonQuery(); }
public BankAccount[] getBankAccountsByClient(int clientId) { var sql = String.Format("SELECT * FROM bankAccounts WHERE client='{0}'", clientId); var cmd = new MySqlCommand(sql, mySqlConnection); MySqlDataReader reader; try { reader = cmd.ExecuteReader(); } catch (Exception ex) { throw new Exception(ex.Message, ex); } var list = new List<BankAccount>(); while (reader.Read()) { var account = new BankAccount(Convert.ToInt32(reader["id"])) { // Считала ClientId ClientId = Convert.ToInt32(reader["client"]), Amount = Convert.ToInt32(reader["amount"]), IsBlocked = Convert.ToBoolean(reader["isBlocked"]) }; list.Add(account); } reader.Close(); return list.ToArray(); }
// Получить банковский счет public BankAccount getBankAccount(int id) { var sql = String.Format("SELECT * FROM bankAccounts WHERE id='{0}' LIMIT 1", id); var cmd = new MySqlCommand(sql, mySqlConnection); MySqlDataReader reader; try { reader = cmd.ExecuteReader(); } catch (Exception ex) { throw new Exception(ex.Message, ex); } if (reader.Read()) { var account = new BankAccount(Convert.ToInt32(reader["id"])) { ClientId = Convert.ToInt32(reader["client"]), Amount = Convert.ToInt32(reader["amount"]), IsBlocked = Convert.ToBoolean(reader["isBlocked"]) }; reader.Close(); return account; } reader.Close(); throw new ArgumentException("Can't read bank account from database. Invalid id?"); }
// перевести деньги private void transMoney(double money, BankAccount baRecip) { baRecip.Amount += money; if (baRecip.Amount > 0) baRecip.IsBlocked = false; DatabaseManager.Instance.updateBankAccount(baRecip); }
// событие на нажатие кнопки private void button4_Click(object sender, EventArgs e) { switch (button4.Text) { case "": break; case "5000 руб.": if (isGive) GiveMoney(5000, Acc); else transMoney(5000, baRecip); break; case "Перевести деньги": mainWindow.Visible = false; mainLb.Visible = true; mainLb.Items.Clear(); backButton(); LoadBankAccounts(); button4.Text = "Выбрать"; isGive = false; break; case "Выбрать": if (mainLb.SelectedIndex == -1) { button4.Text = "Перевести деньги"; button4_Click(sender, e); break; } Acc = mainLb.SelectedIndex; mainLb.Items.Clear(); mainLb.Visible = false; mainWindow.Visible = true; mainWindow.Text = "Выберите получателя:"; backButton(); button2.Text = "Свой счет"; button3.Text = "Чужой счет"; break; case "Ввести": try { baRecip = DatabaseManager.Instance.getBankAccount(Convert.ToInt32(TbLogin.Text)); } catch(Exception exp) { if (exp.Message == "Can't read bank account from database. Invalid id?") { mainWindow.Text = "Данный счет не существует, повторите попытку"; TbLogin.Text = ""; } else { mainWindow.Text = "Неверный ввод, повторите попытку"; TbLogin.Text = ""; } } mainLb.Items.Clear(); mainLb.Visible = false; mainWindow.Visible = true; Recip = DatabaseManager.Instance.getClientById(baRecip.ClientId); mainWindow.Text = "Получатель: "+Recip.LastName+" "+Recip.Name+" "+Recip.MiddleName+".\nВведите нужную сумму:"; label1.Text = "Cумма:"; label1.Visible = true; TbLogin.Visible = true; TbLogin.Text = ""; backButton(); button4.Text = "Перевести"; break; case "Перевести": try { if(ba[Acc].Amount > Convert.ToDouble(TbLogin.Text)) { ba[Acc].Amount -= Convert.ToDouble(TbLogin.Text); DatabaseManager.Instance.updateBankAccount(ba[Acc]); transMoney(Convert.ToDouble(TbLogin.Text), baRecip); } else { mainWindow.Text = "Недостаточно средств"; TbLogin.Text = ""; break; } mainLb.Items.Clear(); mainLb.Visible = false; mainWindow.Visible = true; mainWindow.Text = "Операция произошла успешно"; backButton(); label1.Visible = false; TbLogin.Visible = false; } catch (Exception exp) { TbLogin.Text = ""; break; } break; } }
// событие на нажатие кнопки private void button2_Click(object sender, EventArgs e) { switch (button2.Text) { case "": break; case "Выдать деньги": ClearButtons(); mainWindow.Visible = false; mainLb.Items.Clear(); mainLb.Visible = true; backButton(); LoadBankAccounts(); button2.Text = "Выбрать"; isGive = true; break; case "Выбрать": if (mainLb.SelectedIndex == -1) { button2.Text = "Выдать деньги"; button2_Click(sender, e); break; } if (isGive) { Acc = mainLb.SelectedIndex; } else { baRecip = ba[mainLb.SelectedIndex]; } ClearButtons(); mainWindow.Visible = true; mainWindow.Text = "Выберите сумму:"; mainLb.Visible = false; mainLb.Items.Clear(); backButton(); LoadBankAccounts(); summButton(); button2.Text = "Ввести сумму"; break; case "Ввести сумму": mainWindow.Text = "Введите нужную сумму:"; label1.Text = "Cумма:"; label1.Visible = true; TbLogin.Visible = true; TbLogin.Text = ""; backButton(); button2.Text = "Ввести"; break; case "Ввести": try { if (isGive) { GiveMoney(Convert.ToDouble(TbLogin.Text), Acc); label1.Visible = false; TbLogin.Visible = false; } else transMoney(Convert.ToDouble(TbLogin.TextAlign), baRecip); } catch(Exception exp) { button2.Text = "Ввести сумму"; button2_Click(sender, e); break; } break; case "Свой счет": mainWindow.Visible = false; mainLb.Visible = true; backButton(); LoadBankAccounts(); button2.Text = "Выбрать"; break; } }