-6

I've got a short question. I have a class Bank that needs to have a property with different amounts of constructors.

class Program
{
    static void Main(string[] args)
    {
        Bank ing = new Bank("ing");
        int AccountNrName1 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode");
        int AccountNrName2 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode", Balance, Minimum);
}

The constructor "OpenAccount" has to return both cases (AccountNrName1, AccountNrName2). Here is what I have so far:

public class Bank
{
    private string name, lastname, housenumber, postalcode;
    private int openaccount;
    private int balance;
    private int min;

    public int OpenAccount(string Name, string LastName, string HouseNumber, string PostalCode, int Balance, int Min)
    {
        name = Name;
        lastname = LastName;
        housenumber = HouseNumber;
        postalcode = PostalCode;
        balance = Balance;
        min = Min;

        return openaccount;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Methods can have optional parameters, but, you can also overload - so, while you seem to want a constructor (because you're creating objects, but, you could make it methods.. but you do need to make the object first then!):

potentially you could have 2 constructors to allow for those conditions :

public OpenAccount (String name, String LastName, String Housenumber, String Postcode)
{ do stuff }
public OpenAccount (String name, String LastName, String Housenumber, String Postcode, int balance, int min)
{ do stuff }

Or you could do

public OpenAccount (String name, String LastName, String Housenumber, String Postcode, int balance=0, int min=0)
{do stuff}

which allows your to auto set those values if none are provided. I didnt check it works for constructors, but it should.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

You need a class to hold on to the account information.

public class Account {

    public Account(int accountNumber) { AccountNumber = accountNumber; }

    public int AccountNumber { get; private set; }
    public string Name { get;set; } 
    public string LastName { get;set; } 
    public string HouseNumber { get;set; }
    public string PostalCode { get;set; }
    public int Balance { get;set; }
    public int Min { get;set; }
}

The Bank will hold a collection of Accounts and allow you to open accounts

public class Bank {

    string name;
    int accountIds = 0;
    List<Account> accounts = new List<Account>();

    public Bank(string bankName) {
        name = bankName;
    }

    //Open an account and return the account number
    //Using optional parameters to allow
    //OpenAccount("Name", "LastName", "HouseNumber", "PostalCode");
    //OpenAccount("Name", "LastName", "HouseNumber", "PostalCode", Balance, Minimum);
    public Account OpenAccount(string Name, string LastName, string HouseNumber, string PostalCode, int Balance = 0, int Min = 0) {
        var newAccount = new Account(++accountIds) {
            Name = Name;
            LastName = LastName;
            HouseNumber = HouseNumber;
            PostalCode = PostalCode;
            Balance = Balance;
            Min = Min;
        };
        accounts.Add(newAccount);
        return newAccount;    
    }
}

So now when you call the Bank to open an Account

class Program {
    static void Main(string[] args) {
        Bank ing = new Bank("ing");
        Account AccountNrName1 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode");
        Account AccountNrName2 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode", Balance, Minimum);
    }
}

You should get back the opened account with all the properties populated.

Nkosi
  • 235,767
  • 35
  • 427
  • 472