0

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?

Sebastian
  • 57
  • 1
  • 2
  • 8
  • 1
    You should remember to upvote/accept your previous questions before making new ones :) – DavidG Nov 28 '15 at 01:11
  • @DavidG - The OP seems to have a good record of accepting answers. – Enigmativity Nov 28 '15 at 01:13
  • @Enigmativity That's true, didn't look at the entire history, just the previous question asked a short time ago. Anyway Sebastian, you haven't added the 3 users to your list so the `for` loop is never entered – DavidG Nov 28 '15 at 01:14
  • I forgot to accept answers on the last one. Just did. – Sebastian Nov 28 '15 at 01:17
  • That previous question actually holds the answer to this one too. Please read the answers carefully and you should see what is missing here. – DavidG Nov 28 '15 at 01:18
  • @Sebastian You forgot to add the `User()` to the list. – Tony Wu Nov 28 '15 at 01:30

2 Answers2

1

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.

Robert Pilcher
  • 376
  • 1
  • 10
  • Ok it works. Thank you very much. Now another question. Whenever I enter a wrong username/password it sends the error alert three times and then it stops working. How could I fix that? I now it has to something to do with how I stated my while loop. – Sebastian Nov 28 '15 at 01:21
  • Now that I think about it. I might just put an if inside an if instead of using a while loop. – Sebastian Nov 28 '15 at 01:22
  • Ok I modified it already. Removed the while loop and used a mix of ifs and breaks and now it works fine. – Sebastian Nov 28 '15 at 01:41
  • @Sebastian - You shouldn't ask another question in your comment. Stack Overflow is all about creating a repository of questions and answers. If you ask questions inside question then no-one will be able to to find them. You should ask a brand new question. – Enigmativity Nov 28 '15 at 03:24
0

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();
            }
}
Community
  • 1
  • 1
TTT
  • 1,848
  • 2
  • 30
  • 60