Reputation: 21
My goal is to create a system that can create an endless amount of accounts and store them in a list, the system will then check this list to see if the account exists and if it does it well let the user login. Ultimately I want to create a method which can create accounts however for now I am sticking with 2. The way I expect my code to function is that it creates 2 separate accounts, each with its own object properties, and then let the user login with either account. What ends up happening is both accounts appear in the list however the 2nd account seems to overwrite all the properties of the first. I'm still new to c# and I'm not quite sure what caused this to happen/how I can fix it.
I have tried to separate the initialization of the 2 accounts however I'm not sure I've done it correctly or if that is even the right term.
The Main section of my code starts by creating a list, and then creating those accounts within the list using predetermined functions, It then prints out the account details so that they can clearly be seen.
static void Main(string[] args)
{
//This List is used to store Accounts
AccountInteractions accountInteractions = new AccountInteractions();
//First Account
Account account = new Account();
account.ReadAccount();
accountInteractions.AddAccount(account);
Console.WriteLine();
//Second Account
account = new Account();
account.ReadAccount();
accountInteractions.AddAccount(account);
//Listing accounts and account variables
foreach (var item in accountInteractions.AccountList)
{
Console.WriteLine("Name: {0}, Email: {1}, Password: ${2}", account.Username, account.UserEmail, account.UserPassword);
}
}
The methods below show how the users input is used to create the accounts, also shown is how the system checks for whether the account matches that within the list.
public void ReadAccount()
{
Console.Write("Username: ");
Username = Console.ReadLine();
Console.Write("Email: ");
UserEmail = Console.ReadLine();
Console.Write("Password: ");
UserPassword = Console.ReadLine();
Login = false;
}
public void LogInToAccount(Account account)
{
//Setup User Input
Console.WriteLine();
Console.Write("Email: ");
userEmailCheck = Console.ReadLine();
Console.Write("Password: ");
userPasswordCheck = Console.ReadLine();
//Verifying Details to check if account exists
if (account.userEmailCheck == account.UserEmail & account.userPasswordCheck == account.UserPassword)
{
account.Login = true;
Console.WriteLine("Login Successful");
}
else
{
Console.WriteLine("Invalid credentials, please try again.");
account.LogInToAccount(account);
}
}
This picture shows the output using basic details entered
As can be seen despite entering separate details both accounts end up having the same properties. Ultimately both accounts should be able to login using the LogInToAccount() method, and both should have their own unique properties.
Upvotes: 1
Views: 68
Reputation: 460340
You are using the wrong variable account
instead of the loop variable item
here:
foreach (var item in accountInteractions.AccountList)
{
Console.WriteLine("Name: {0}, Email: {1}, Password: ${2}", account.Username, account.UserEmail, account.UserPassword);
}
account
refers to the last initialized account. The reason for such problems is that you do too much in the same scope. Use methods, for example one to output existing accounts.
Upvotes: 1