Home > Software engineering >  How do I obtain a parameter of all objects from a class constructor in c#
How do I obtain a parameter of all objects from a class constructor in c#

Time:09-23

I am making a very basic sort of online ATM app using c#.NET. Bascically i have made a class called account and have used a constructor to store the accounts of customers, making them objects of the class. These customers are to enter a pin to login. The customer's pin is one of the parameter of the customer object and i have been trying to make a loop that will loop through all the individual customers and identify which account the pin belongs to.

 class Account
 {
    public string customerName;
    public string pin;
    public int balance;

    public Account(string accCustomerName, string accPin, int accBalance)
    {
        customerName = accCustomerName;
        pin = accPin;
        balance = accBalance;
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    Account acc1 = new Account("Henry_Adobe", "2316", 10000);
    Account acc2 = new Account("Patrick_Boka", "3198", 15000);
    Account acc3 = new Account("Fred_Simp", "8768", 20000);
    Account acc4 = new Account("Derek_Black", "7645", 25000);

    if (txtInput.Text == acc1.pin)
    {
        MessageBox.Show("Login Succesful!");
    }
}

I have managed to access the pin by stating the name of a specific name of each object(acc1.pin) but this is not efficient as it requires me to enter the name of very object. Is there a way in which i could easily access the parameters of all the accounts?

CodePudding user response:

I'll take a you a step further, since an ATM app that forgets the last balance each time it runs isn't very useful.

Let's start with Account class:

class Account
{
    public string CustomerName {get;set;};
    public string PIN {get;set;};
    public decimal Balance {get;set;};

    public Account(string customerName, string Pin, decimal balance)
    {
        CustomerName = customerName;
        PIN = Pin;
        Balance = balance;
    }

    public static IList<Account> Accounts {get;private set;} = new List<Account>();

    public static void ReadAccounts(string fileName)
    {
        using var rdr = new Microsoft.VisualBasic.FileIO.TextFieldParser(AccountsFileName))
        {
            rdr.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
            rdr.Delimiters = new string[] {','};
            Accounts.Clear();
            string[] row = null;
            while( (row = rdr.ReadFields()) != null)
            {
                Accounts.Add(new Account(row[3], row[0], Decimal.Parse(row[1])));
            }
        }
    }

    public static void WriteAccounts(string fileName)
    {
        using (var writer = new StreamWriter(fileName, false))
        {
            writer.WriteLine("PIN,Balance,CustomerName");
            foreach(var acct in Accounts)
            {
                writer.WriteLine($"{acct.PIN},{acct.Balance},\"{acct.CustomerName}\"");
            }
        }
    }
}

We updated the fields to become properties with meaningful data types, added a static place to the keep the list of accounts, and added methods to read and write from a file. Later on, you'll replace the csv file with a database of some kind, but this will get you started.

Now let's move on to loading the form:

private string AccountsFileName = "AccountsFile.csv";

private void Form1_Load(object sender, EventArgs e)
{
    Account.ReadFile(AccountsFileName);
}

But we need to stop there. At the point where a form loads, the user hasn't had a chance to enter any text yet! You need a button, in addition to the textbox, where the user can click to login after they type the pin:

private Account currentAccount = null;

private void btnLogin_Click(object sender, EventArgs e)
{
    foreach(var account in Account.Accounts)
    {
        if (account.PIN == txtInput.Text)
        {
            currentAccont = account;
            MessageBox.Show("Login successful!");
            return;
        }
    }
    MessageBox.Show("Login failed");
}   

And, finally, you need a way to save the file when the program ends. For now I'll just use the Form1_Closing event, but later you'll probably need something different:

private void Form1_Closing(object sender, EventArgs e)
{
    Account.WriteAccounts(AccountsFileName);
}

What you're still missing is the ability to show users their balance and allow them to make deposits and withdrawals. Additionally, any ATM worth using will need to show individual transactions, typically stored as two off-setting ledger entries.

  • Related