-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.cs
More file actions
187 lines (166 loc) · 6.71 KB
/
Copy pathtransfer.cs
File metadata and controls
187 lines (166 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ATMuto
{
// Represents the transfer form in the ATMuto application.
public partial class transfer : Form
{
// Constructor for the transfer form.
public transfer()
{
InitializeComponent(); // Initializes form components.
}
// SQL connection to the database.
SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\krist\Documents\ATMDb.mdf;Integrated Security=True;Connect Timeout=30");
// Stores the account number of the logged-in user.
string Acc = Login.AccNumber;
// Logs a transaction into the TransactionTbl.
private void addtransaction(string accNumber, string type, decimal amount, string recipientAcc = null)
{
try
{
// SQL query to insert a transaction record.
string query = "INSERT INTO TransactionTbl (AccNum, Type, Amount, TDate, RecipientAccNum) VALUES (@AccNum, @Type, @Amount, @TDate, @RecipientAccNum)";
using (SqlCommand cmd = new SqlCommand(query, Con))
{
cmd.Parameters.AddWithValue("@AccNum", accNumber);
cmd.Parameters.AddWithValue("@Type", type);
cmd.Parameters.AddWithValue("@Amount", amount);
cmd.Parameters.AddWithValue("@TDate", DateTime.Now);
cmd.Parameters.AddWithValue("@RecipientAccNum", recipientAcc ?? (object)DBNull.Value);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show($"Error logging transaction: {ex.Message}");
}
}
// Current balance of the logged-in user.
int bal;
// New balance after a transaction.
int newbalance;
// Handles the "Transfer" button click event.
private void trans_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(amount.Text) || string.IsNullOrWhiteSpace(accountNumberTo.Text))
{
MessageBox.Show("Missing Information");
return;
}
if (Convert.ToDecimal(amount.Text) <= 0)
{
MessageBox.Show("Enter a Valid Amount");
return;
}
if (Convert.ToDecimal(amount.Text) > bal)
{
MessageBox.Show("Insufficient Balance");
return;
}
if (!IsRecipientAccountValid(accountNumberTo.Text))
{
MessageBox.Show("Recipient account does not exist");
return;
}
newbalance = bal - Convert.ToInt32(amount.Text);
decimal transferAmount = Convert.ToDecimal(amount.Text);
try
{
Con.Open();
// Update sender's balance.
string query = "UPDATE AccountTbl SET Balance = @newbalance WHERE Accnum = @senderAcc";
using (SqlCommand cmd = new SqlCommand(query, Con))
{
cmd.Parameters.AddWithValue("@newbalance", newbalance);
cmd.Parameters.AddWithValue("@senderAcc", Acc);
cmd.ExecuteNonQuery();
}
// Update recipient's balance.
string query2 = "UPDATE AccountTbl SET Balance = Balance + @transferAmount WHERE Accnum = @recipientAcc";
using (SqlCommand cmd2 = new SqlCommand(query2, Con))
{
cmd2.Parameters.AddWithValue("@transferAmount", transferAmount);
cmd2.Parameters.AddWithValue("@recipientAcc", accountNumberTo.Text);
cmd2.ExecuteNonQuery();
}
// Log the transactions.
addtransaction(Acc, "Transfer", -transferAmount, accountNumberTo.Text); // Log for the sender.
addtransaction(accountNumberTo.Text, "Transfer", transferAmount, Acc); // Log for the recipient.
MessageBox.Show("Transfer Successful");
HOME home = new HOME();
home.Show();
this.Hide();
}
catch (Exception ex)
{
MessageBox.Show($"Error during transfer: {ex.Message}");
}
finally
{
Con.Close();
}
}
// Retrieves the current balance of the logged-in user.
private void getbalance()
{
Con.Open();
string query = "SELECT Balance FROM AccountTbl WHERE AccNum = @AccNum";
using (SqlCommand cmd = new SqlCommand(query, Con))
{
cmd.Parameters.AddWithValue("@AccNum", Acc);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
balancetbl.Text = dt.Rows[0][0].ToString() + " pesos balance ";
bal = Convert.ToInt32(dt.Rows[0][0].ToString());
}
Con.Close();
}
// Validates if the recipient account exists in the database.
private bool IsRecipientAccountValid(string recipientAcc)
{
try
{
Con.Open();
string query = "SELECT COUNT(*) FROM AccountTbl WHERE AccNum = @RecipientAcc";
using (SqlCommand cmd = new SqlCommand(query, Con))
{
cmd.Parameters.AddWithValue("@RecipientAcc", recipientAcc);
int count = Convert.ToInt32(cmd.ExecuteScalar());
return count > 0;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error validating recipient account: {ex.Message}");
return false;
}
finally
{
Con.Close();
}
}
// Event handler for the form's Load event.
private void transfer_Load(object sender, EventArgs e)
{
getbalance(); // Fetch and display the current balance on form load.
}
// Event handler for the "Back" button click event.
private void back_Click(object sender, EventArgs e)
{
HOME home = new HOME(); // Navigate to the HOME form.
home.Show();
this.Hide(); // Hide the current form.
}
}
}