使用用户输入给出的名称创建对象

本文关键字:创建对象 用户 输入 | 更新日期: 2023-09-27 18:13:19

我目前正在学习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;
    }
}

我不确定如何使用列表和如何使用查找。我写这个是因为我在一个类似的脚本中发现了它。

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

谢谢

使用用户输入给出的名称创建对象

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

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

然后使用

foreach(var account in query.ToList())
{
    //do work
}

尝试如下:

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