0

I am using C#, Visual Studio 2015.

How do I add these two accounts (same person) from the form load?

Example Data to add:

Bob Smith 1/1/1970 (account):123456 (balance):$5000 and (account):222222 and (balance):$3000?

Here is part of the BankAccount class:

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

and this is some of the Wallet class:

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

QUESTION: How do I create two accounts (from same person) to already be there?

6
  • I'd suggest you revise your question, it's difficult to follow. Commented May 2, 2016 at 3:44
  • Alright... I did, though not sure it helped... Commented May 2, 2016 at 3:59
  • First you should know HOW and WHERE to store your data. It can be a database, file, etc. Next, you should know WHEN to load your data. Commented May 2, 2016 at 4:03
  • 1
    This line makes no sense: this.AccountNumber = Convert.ToInt16(accounts.AccountList); Commented May 2, 2016 at 4:06
  • Evan: Could you be more specific? Commented May 2, 2016 at 4:12

1 Answer 1

1

The two classes in your question not only create a reference with each other (bank account knows wallet and wallet knows bank account), but also have no public way to set up the data as per your requirements: you can't create a BankAccount and define both person and balance.

Instead, try something like this - obviously violating some of your other requirements, but solving your question around creating the correct instances:

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

The Wallet class should not reference the bank accounts! If you need to know what bank accounts are owned by who just check on them rather than keeping references:

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

And this is how you set up you sample scenario. As the account number is random you cannot specify it unless you change your code.

Wallet account = new Wallet("Bob", "Smith", ...);
BankAccount account1 = new BankAccount(account);
account1.TransferMoney(5000);
BankAccount account2 = new BankAccount(account);
account2.TransferMoney(3000);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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