Object Oriented Programming using C#
Case Study
For each message
If the message has expired or if the client has run out of credit then
Set the DaysReamining for that message to zero.
Remove all expired messages.
See code below for this...
public void DailyPurge(ClientBook clients)
{
Client client;
// loop through all current messages and decrement credit and days
foreach( Message m in messages)
{
m.DecrementDays(); // deduct 1 from days remaining for message
try
{
// decrease client credit for this message
client = clients.GetClient(m.ClientID);
client.DecreaseCredit(m.Cost);
}
catch (UnknownClientException uce)
{
MessageBox.Show(“INTERNAL ERROR IN MessageSet.Purge()
\r\nException Details: “ + uce.Source + “ \r\nMessage
details “ + uce.Message + “: \r\n”); }
}
// loop through all current messages
and expire those whose client credit <=0
foreach (Message m in messages)
{
try
{
client = clients.GetClient(m.ClientID);
if (client.Credit <= 0)
{
m.DaysRemaining = 0;
}
}
catch (UnknownClientException)
{
// Do nothing as unknown clients have been reported to
error log already
}
}
// Remove all expired messages
messages.RemoveWhere(ToBeDeleted);
}