无法从列表中读取帐号

时间:2019-12-01 14:53:56

标签: c#

我正在尝试从文本文件中读取AccountNumber,但似乎没有从文本文件中读取AccountNumber,因此我可以确认文本文件位置字符串正确。另外,请注意,当前仅将用户设置为访问BasicAccountTestRepository类,但是我正在尝试获取LoadAccounts方法,以便能够从FileAccountRepository.cs调用GetAccounts()。根据我的指示,我应该能够从此类中的文本文件中进行读取。我在LoadAccounts方法上收到NullReferenceException,说“对象引用未设置为对象的实例。”

Accounts.txt

AccountNumber,Name,Balance,Type
10001,Free Account,100,F
20002,Basic Account,500,B
30003,Premium Account,1000,P

FileAccountRepository.cs

public class FileAccountRepository : IAccountRepository
{
    List<Account> accounts = new List<Account>();
    public Account _account = new Account();

    public void GetUsers() {
        string path = @".\Accounts.txt";
        string[] rows = File.ReadAllLines(path);


        for (int i = 1; i < rows.Length; i++)
        {
            string[] columns = rows[i].Split(',');

            //Account account = new Account();
            _account.AccountNumber = columns[0];
            _account.Name = columns[1];
            _account.Balance = Decimal.Parse(columns[2]);
            if (columns[3] == "F")
            {
                _account.Type = AccountType.Free;
            }
            else if (columns[3] == "B")
            {
                _account.Type = AccountType.Basic;
            }
            else if (columns[3] == "P")
            {
                _account.Type = AccountType.Premium;
            }

            accounts.Add(_account);
        }
    }


    public Account LoadAccount(string AccountNumber)
    {            
        if (accounts.Any(x => x.AccountNumber == AccountNumber))
        {
            return _account;
        }

        return null;
    }

    public void SaveAccount(Account account)
    {
        _account = account;
    }        
}

AccountManager.cs

...
public AccountLookupResponse LookupAccount(string accountNumber)
    {
        AccountLookupResponse response = new AccountLookupResponse();
        FileAccountRepository fileAccount = new FileAccountRepository(); //NullReferenceException

        fileAccount.GetUsers();
        response.Account = _accountRepository.LoadAccount(accountNumber);

        if(response.Account == null)
        {
            response.Success = false;
            response.Message = $"{accountNumber} is not a valid account.";
        }
        else
        {
            response.Success = true;
        }

        return response;
    }
...

1 个答案:

答案 0 :(得分:0)

您需要修复GetUsers中的代码。此代码是错误的,因为正在将相同的实例添加到您的列表中。这将以列表中的许多条目结尾,但是对于相同的Account实例,所有这些条目都显示相同的数据。在最后一个循环中分配给变量_account的数据。

您需要在循环内声明和初始化类型为Account的变量。这样,列表中的每个条目都是具有不同数据的不同实例

public class FileAccountRepository : IAccountRepository
{
    List<Account> accounts = new List<Account>();

    public void GetUsers() {
        string path = @".\Accounts.txt";
        string[] rows = File.ReadAllLines(path);


        for (int i = 1; i < rows.Length; i++)
        {
            Account data = new Account();
            string[] columns = rows[i].Split(',');

            data.AccountNumber = columns[0];
            data.Name = columns[1];
            data.Balance = Decimal.Parse(columns[2]);
            if (columns[3] == "F")
            {
                data.Type = AccountType.Free;
            }
            else if (columns[3] == "B")
            {
                data.Type = AccountType.Basic;
            }
            else if (columns[3] == "P")
            {
                data.Type = AccountType.Premium;
            }

            accounts.Add(data);
        }
    }

现在,当您需要按AccountNumber查找帐户时,可以调用LoadAccount方法从列表中搜索该帐户

public Account LoadAccount(string AccountNumber)
{            
    return accounts.FirstOrDefault(x => x.AccountNumber == AccountNumber);
}

FirstOrDefault将根据您的lambda表达式在列表中搜索匹配的值,并返回找到的帐户或为空。