实例进入两个类的列表

时间:2016-05-02 03:36:47

标签: c# visual-studio-2015

我正在使用C#,Visual Studio 2015。

如何从表单加载中添加这两个帐户(同一个人)?

要添加的示例数据:

鲍勃史密斯1970年1月1日 (账户):123456(余额):5000美元和(账户):222222和(余额):3000美元?

以下是BankAccount类的一部分:

        public int AccountNumber { get; private set; }
        public double Balance { get; private set; }

        public BankAccount()
        {
            this.AccountNumber = generateAccountNumber();
            this.Balance = 0;
        }


        public BankAccount(Wallet accounts)
        {
            this.AccountNumber = generateAccountNumber();
            this.Balance = 0;
            this.AccountNumber = Convert.ToInt16(accounts.AccountList);     
        }

        public BankAccount(double beginningBalance)
        {
            Balance = beginningBalance;
        }

        private int generateAccountNumber()
        {
            Random numGenerator = new Random();
            //Requirement: 6 digit account number, cannot start with zero
            return numGenerator.Next(100000, 999999);
        }

这是一些Wallet类:

private string firstName;
        private string lastName;
        private DateTime birthDate;
        private List<BankAccount> accountList = new List<BankAccount>();

        public Wallet(string firstName, string lastName, DateTime birth, List<BankAccount> Account)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.birthDate = birth;
            this.accountList = Account;
        }

        public List<BankAccount> AccountList
        {
            get { return accountList; }
            set { accountList = value; }
        }

问题:如何创建两个帐户(来自同一个人)已经存在?

1 个答案:

答案 0 :(得分:1)

您问题中的两个类不仅相互创建引用(银行帐户知道钱包和钱包知道银行帐户),而且还没有public方式根据您的要求设置数据:您无法创建BankAccount并定义人与平衡。

相反,尝试这样的事情 - 显然违反了你的一些其他要求,但解决了围绕创建正确实例的问题:

public BankAccount
{
    public Wallet Account { get; private set; } // reference to the person
    public int AccountNumber { get; private set; }
    public decimal Balance { get; private set; }

    public BankAccount(Wallet account)
    {
        this.AccountNumber = GenerateAccountNumber();
        this.Balance = 0; // always zero to start with
        this.Account = account; // check to be NOT null or bank account may have no account
    }

    public void TransferMoney(decimal amount)
    {
        this.Balance += amount;
    }

    private int GenerateAccountNumber()
    {
        return new Random().Next(100000, 999999);
    }
}

Wallet课程不应该引用银行帐户!如果你需要知道谁只是检查他们所拥有的银行账户而不是保留参考资料:

public class Wallet
{
    private string firstName;
    private string lastName;
    private DateTime birthDate;
    // don't keep a reference here. Bank accounts already know their accounts
    // private List<BankAccount> accountList = new List<BankAccount>();

    public Wallet(string firstName, string lastName, DateTime birth)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birth;
    }
}

这就是你设置示例场景的方法。由于帐号是随机的,除非您更改代码,否则无法指定帐号。

Wallet account = new Wallet("Bob", "Smith", ...);
BankAccount account1 = new BankAccount(account);
account1.TransferMoney(5000);
BankAccount account2 = new BankAccount(account);
account2.TransferMoney(3000);