Object Oriented Programming using C#

(backadmin) #1

Object Oriented Programming using C#
Generic Collections and how to Serialize them


Finally the Main method used to demonstrate this is shown below...


static void Main(string[] args)
{
Bank b = new Bank();
Account a;
a = new Account(“Bert”, “06/12/1963”);
b.AddAccount(a);
// try to create and add a duplicate account-this shouldn’t work
a = new Account(“Bert”, “06/12/1963”);
b.AddAccount(a);
a = new Account(“Alice”, “14/08/1990”);
b.AddAccount(a);
a = new Account(“Claire”, “1/1/2000”);
b.AddAccount(a);

// Display the accounts
foreach (Account acc in b.Accounts)
{
Console.WriteLine(acc.ToString());
}
Account a1 = b.GetAccount(“Bert”, “06/12/1963”);
a1.DepositMoney(100);
// Display the accounts again
foreach (Account acc in b.Accounts)
{
Console.WriteLine(acc.ToString());
}
Console.ReadLine();
}

This Main method is very similar to the Main method used to test lists. Several accounts are created, displayed, an account
is retrieved and after a deposit into this account has been made the list is displayed again.


Two changes should be noted. First an attempt is made to create a duplicate account – this should not work. The list of
accounts has been created in a non alphabetical order.


By looking at the program output, shown below, we can see that :-
a) no duplicate account was created, so overriding Equals() and GetHashCode() worked.
b) the accounts are listed in the order created but not in an meaningful order. Entries in sets are not stored in
any order and therefore we cannot be sure which order they will be retrieved in.

Free download pdf