Reputation: 57
So I'm making an ATM and the first thing I have to program is the login screen. To define the users I created a User class which is formed by an id, username, password, savings account and checks account. In my windows form I created two buttons, one executes the log in and the other one closes the program. This is the code for my Windows Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ATM_Project
{
public partial class Form1 : Form
{
List<User> users = new List<User>();
int attempts = 3;
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
private void exitbtn_Click(object sender, EventArgs e)
{
this.Close();// this button is used to close the application
}
private void loginbtn_Click(object sender, EventArgs e)
{
verification();
}
public void verification()
{
for (int i = 0; i < users.Count; i++)
{
while (attempts != 0)
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
break;
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts = attempts - 1;
}
}
}
}
}
}
I put my objects inside a list and I use a for loop to traverse the list and I compare whatever the user inputs on the first text box to the username in the ith position and compare whatever the user inputs in the second textbox to the password in the ith position. If they match it should pop a message telling me it's correct. And if it's wrong it should tell me it's wrong and after three attempts it should stop working. I created a public void called verification where I do all that testing and I just call it inside the log in button. However it's no working. When I type something into the text boxes and click the log in button it does nothing. However the exit button does work. Any insight as to why this might be happening? Is there something I could be forgetting?
Upvotes: 0
Views: 115
Reputation: 1885
It looks like you are defining new users but not adding them to the list. So you are looping over 0 user.
You could for example change Form1() to
public Form1()
{
users.add(new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.add(new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.add(new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
Also note that this.Close() only closes the windows, not the application. As explained in Winforms: Application.Exit vs Enviroment.Exit vs Form.Close
I think also the this version of verification might make more sense:
public void verification()
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts -= 1;
}
if(attempts == 0)
{
Environment.Exit();
}
}
Upvotes: 0
Reputation: 406
Looks like you're not adding anything to your users List variable... instead of:
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
try
public Form1()
{
users.Add (new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.Add (new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.Add (new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
Then when you loop through your users List - users.Count will have a value.
Upvotes: 1