简体   繁体   中英

Loop is not storing data in array correctly C#

Hey guys I'm currently working on a bank program for a class project. The idea the user will need to make an account if not done so already but if they already do they can just login using account number and pin. However. instead of my program constantly adding data to an array that's size is 100 it just replaces the data in slot [0] just wondering why.

public partial class Form1 : MetroForm
{
    //For Creating new account
    string newAccountType;
    Accounts[] customers = new Accounts[99999];
    int temp;
    string VerifyPin = ("");
    private void openAccount_Click(object sender, EventArgs e)
    {

        for (int index=0;index < customers.Length; ++index)
        {
            var R1 = new Random();
            var R2 = new Random();



            customers[index] = new Accounts();
            customers[index].Name = newName.Text;
            customers[index].accountType = newAccountType;
            customers[index].accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000));
            customers[index].accountPin = createPin.Text;
            customers[index].accountBalance = 100.00;
            temp = index;


        }

        MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
        metroTabControl1.SelectedTab = metroTabPage2;

    }

        private void checkBalance_Click(object sender, EventArgs e)
        {
        int veri=0;
        bool isfound = false;
            for (int count = 0; count < customers.Length; ++count)
                {
            if (Convert.ToInt32(userName.Text) == customers[count].accountNumber)
            {
                veri = count;
                isfound = true;

            }
            else
                isfound = false;
            accountnotfound.Text = "Account Not Found";


        }
        if (isfound && (customers[veri].accountPin == pinText.Text))
        {
            MetroMessageBox.Show(this, "account found", "account found");
        }
        else
        {
            MetroMessageBox.Show(this, "account not found or wrong pin", "account not found");
            pinText.Text = "";
        }

        accountBalance.Visible = true;
        userWithdraw.Visible = true;
        userDeposite.Visible = true;
        accountBalance.Text = "Welcome, "+customers[veri].Name+"\nYour current balance is: "+customers[veri].accountBalance;


    }

public class Accounts
{
    private string name, AccountType, AccountPin;
    private int AccountNumber;
    private double AccountBalance;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public int accountNumber
    {
        get
        {
            return AccountNumber;
        }
        set
        {
            AccountNumber = value;
        }
    }
    public string accountPin
    {
        get
        {
            return AccountPin;
        }
        set
        {
            AccountPin = value;
        }
    }
    public string accountType
    {
        get
        {
            return AccountType;
        }
        set
        {
            AccountType = value;
        }
    }
    public double accountBalance
    {
        get
        {
            return AccountBalance;
        }
        set
        {
            AccountBalance = value;
        }
    }
}
for (int index=0;index < customers.Length; ++index)

customers[index] = new Accounts();

Index will be 0 every time your array loops, thus replacing customers[0] every time you openAccount_Click is called.

You don't really need to loop to add. You just need to make sure you are writing the next possible index. Maybe use your Temp variable and increment it + 1 for every user added and then without a loop do like do.

customers[Temp] = new Accounts();
customers[Temp].Name = newName.Text;
customers[Temp].accountType = newAccountType;
customers[Temp].accountNumber = (R1.Next(1000000,9000000))+   (R2.Next(100,9000));
customers[Temp].accountPin = createPin.Text;
customers[Temp].accountBalance = 100.00;
Temp += 1;

You would be better off using a List of Accounts for your customers global instead.

List<Accounts> customers = new List<Accounts>();

Then inside openAccount_Click you can add a new Accounts like so.

customers.Add(new Accounts {
Name = newName.Text, 
accountType = newAccountType,
accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000)),
accountPin = createPin.Text,
accountBalance = 100.00
});

I think you just need to keep track of which element in the array is available (ie has a null value). Once no elements in the array are null, this means that the customers array is full, so you can't add any more customers at that point.

Make these following changes. In the part of the code just where the code starts the loop:

bool added = false;

for (int index=0;index < customers.Length; ++index)
{
    if (customers[index] != null) continue;
    ...

Also, after you actually add a customer, do this (right after the loop ends):

if (!added)
{
    // show error message here!
}
else
{
    MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber",     MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    metroTabControl1.SelectedTab = metroTabPage2;
}

bool added = false; for (int x = 0; x <= temp; x++)

        {

            if (customers[temp] != null) continue;
                {
                for (int indexies = 0; indexies < customers.Length; indexies++)
                {



                        var R1 = new Random();
                        var R2 = new Random();
                        Convert.ToInt32(createPin.Text);


                        customers[temp] = new Accounts();
                        customers[temp].Name = newName.Text;
                        customers[temp].accountType = newAccountType;
                    customers[temp].accountNumber = 1;//(R1.Next(1000000, 9000000)) + (R2.Next(100, 9000));
                        customers[temp].accountPin = Convert.ToInt32(createPin.Text);
                        customers[temp].accountBalance = 100.00;
                        added = true;


                }


                }


            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM