0

I'm currently learning c#, and i'm trying to make a script that create a bank account and then find it back and add money on it.

This method is used to create a new account :

static void CreateNewAccount()
    {
        Console.WriteLine("Enter a name for a new account.");
        string bname = Console.ReadLine();
        Console.WriteLine("Creating a new account for : {0}", bname);
        List<BankAccount> account = new List<BankAccount>() // not sure about it
        {
            new BankAccount { name = bname } // creating a new account
    };

        Console.WriteLine(account.Exists(x => x.name == bname));
        var useraccount = account.Find(x => x.name == bname); // Trying to find the account that i've created earlier
        useraccount.Deposit(100); // trying to add money on it
        useraccount.CheckBalance();
        Console.WriteLine("test");

    }

And here is my class :

class BankAccount
{
    private double _balance=0;
    public string name;
    public BankAccount()
    {
        Console.WriteLine("You succesfuly created a new account.");
    }
    public double CheckBalance()
    {
        return _balance;
    }
    public void Deposit(double n)
    {
        _balance += n;
    }
    public void WithDraw(double n)
    {
        _balance -= n;
    }
}

I'm not sure at all about how to use List and how to use Find. I writed this because i've it found on a similar script.

Do you know a easy way to make it ? I'm a beginner.

Thanks

4
  • The object is added to the list guys!! This code is correct syntax wise but what you want to do and whats wrong with it? Commented Oct 16, 2016 at 12:16
  • You don't need a list to do it so why are you using a list? All you need to do is to create a single object of the class bank account. You can use a for loop instead of a list if you are doing it for many bank accounts. What is your application doing anyway? Why does it need to use lists? Commented Oct 16, 2016 at 12:35
  • You need a class Bank which contains a List<BankAccount>(). Or add to the BankAccount class a public static List<BankAccount> accounts = new List<BankAccount>(); Commented Oct 16, 2016 at 13:25
  • @jdweng ok, i'm going to try this solution, can you explain me how I can create this list inside Bank class and how access it please ? Commented Oct 16, 2016 at 13:34

2 Answers 2

1

You could use LINQ to find a certain object in a list.

var query = account.Where(a => a.name == "A NAME" );

Then to use this

foreach(var account in query.ToList())
{
    //do work
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {



        }
        static void CreateNewAccount()
        {
            Bank bank = new Bank();
            Console.WriteLine("Enter a name for a new account.");
            string bname = Console.ReadLine();
            Console.WriteLine("Creating a new account for : {0}", bname);
            BankAccount account = new BankAccount(bname, 0);

            Console.WriteLine(bank.GetAccounts().Exists(x => x.name == bname));
            var useraccount = bank.GetAccount(bname); // Trying to find the account that i've created earlier
            useraccount.Deposit(100); // trying to add money on it
            useraccount.CheckBalance();
            Console.WriteLine("test");

        }
    }
    class Bank
    {
        private List<BankAccount> accounts = new List<BankAccount>();
        public List<BankAccount> GetAccounts()
        {
            return accounts;
        }
        public BankAccount GetAccount(string name)
        {
            return accounts.Where(x => x.name == name).FirstOrDefault();
        }
    }
    class BankAccount
    {
        private double _balance = 0;
        public string name;
        public BankAccount(string name, double balance)
        {
            this.name = name;
            this._balance = balance;
            Console.WriteLine("You succesfuly created a new account.");
        }
        public double CheckBalance()
        {
            return _balance;
        }
        public void Deposit(double n)
        {
            _balance += n;
        }
        public void WithDraw(double n)
        {
            _balance -= n;
        }
    }
}

4 Comments

Thanks ! It look like what i was currently trying to do ! I took your code but I have an System.NullReferenceException at line 26 (the line useraccount.Deposit(100);), i will try to fix it. Thanks again.
Get account could return a null if the account doesn't exist. So you must check for null before executing line 26. You can't deposit money into an account that doesn't exist.
If i create a account named john (bname = john) and then i'm looking for a account named john (bname = john) Console.WriteLine(bank.GetAccounts().Exists(x => x.name == bname)); It return false.
Yes, but you get the exception two lines later. useraccount is null so you can't deposit the money. The real issue is you are missing the following : bank.accounts.Add(account);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.