使用用户输入指定的名称创建对象

时间:2016-10-16 12:07:28

标签: c# object input

我目前正在学习c#,我正在尝试制作一个创建银行帐户的脚本,然后找回它并在其上加钱。

此方法用于创建新帐户:

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");

    }

这是我的班级:

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;
    }
}

我根本不确定如何使用List以及如何使用Find。我写这个是因为我发现它是在类似的剧本上发现的。

你知道一个简单的方法吗?我是初学者。

由于

2 个答案:

答案 0 :(得分:1)

您可以使用LINQ在列表中查找某个对象。

stepi

然后使用此

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

答案 1 :(得分:0)

请尝试以下操作:

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;
        }
    }
}