Object Oriented Programming using C#

(backadmin) #1

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


The Main method used to test this dictionary is given below :-


static void Main(string[] args)
{
Bank b = new Bank();
Account a;
int an; // account number;
a = new Account(“Bert”, “06/12/1963”);
b.AddAccount(1,a);
// try to create and add a duplicate account-this shouldn’t work
a = new Account(“Bert”, “06/12/1963”);
b.AddAccount(1, a);
a = new Account(“Alice”, “14/08/1990”);
b.AddAccount(2, a);
a = new Account(“Claire”, “1/1/2000”);
b.AddAccount(3, a);
// Display the accounts
foreach (KeyValuePair<int, Account> kvp in b.Accounts)
{
an = kvp.Key;
a = kvp.Value;
Console.WriteLine(“Account number: “ + an +
“\nAccount details: “ + a.ToString());
}
a = b.GetAccount(1);
a.DepositMoney(100);
foreach (KeyValuePair<int, Account> kvp in b.Accounts)
{
an = kvp.Key;
a = kvp.Value;
Console.WriteLine(“Account number: “ + an +
“\nAccount details: “ + a.ToString());
}
Console.ReadLine();
}

Functionally the ‘Main’ method above is virtually identical to the Main method used to test the Set example.


The code above performs the following operations :-
• Several accounts are created, including one attempt to create a duplicate,
• The collection of accounts is displayed, in this case including an account number,
• an account is retrieved and amended and then
• the collection is displayed again.


To keep this example short we have not protected against the possibility that the account being retrieved cannot be found.

Free download pdf