- Dec 10, 2009
- 324
- 7
- 0
Would anyone with more advanced programming knowledge then myself be able to help me out on a few questions?
This is the code.
Questions
1. The system should not allow the creation of any acounts with names which are empty strings. Describe how the
programmer has accomplished this in the code above. Add code to prevent the creation of accounts with an
initial balance value which is less than 0 or greater than 10000.
Give the amended C# code.
[10 marks]
2. The ValidateName method in the Account class has been made static. Describe the effect of this.what
it means for users of the Account class and why it is a good idea to do this.
[10 marks]
3. There is a need for an additional constructor which would allow instances of the Account class to be created
without providing an initial balance value. In this situation the balance should be set to 0.
Write the C# code for this constructor.
[10 marks]
4. Describe how you could use the ToString method in the classes above to allow the class content to be
printed out. Create code to implement a ToString behaviour for the BabyAccount class. Ensure that it
uses behaviour in the parent Account class as appropriate.
Write the C# code for the ToString method.
[10 marks]
5. There is a need for a method called TestBank that wil serves as a test harness for the bank. It will create
instances of the account classes and perform tests on the management of the account balance that they provide.
The tests should be used to make sure that when a user pays in and attempts to withdraw funds the system
always behaves correctly. The following tests have been suggested:
Create an account and make sure the initial balance is zero
Create an account with an empty name string and make sure it is rejected
Create an account with an initial balance value of -50 and make sure it is rejected
Create an account with a balance of zero. Pay in 50 pounds and make sure that the balance is now 50
pounds.
Write the C# code for the TestBank method.
[10 marks]
6. At the moment a BabyAccount holder cannot withdraw more than 10 pounds at once. The bank has asked
you to change the program so that the amount that can be withdrawn from a BabyAccount can be set for
each account between 1 and 15 pounds. Add a data member maxBabyWithDraw to the BabyAccount and
create a method called SetMaxBabyWithdraw which will provide this behaviour. Create two tests for the
new method which make sure that the maximum withdrawal amount cannot be set above 15 or below 1 using
your code.
Give the complete C# code for each of the methods that you have modified.
This is the code.
Spoiler for Code:
public interface IAccount
{
bool PayInFunds ( decimal amount );
bool WithdrawFunds ( decimal amount );
decimal GetBalance ();
string RudeLetterString();
string GetName ();
bool SetName ( string inName);
}
public abstract class Account : IAccount
{
private decimal balance = 0;
public abstract string RudeLetterString();
public virtual bool WithdrawFunds ( decimal amount )
{
if ( balance < amount )
{
return false ;
}
this.balance = this.balance - amount ;
return true;
}
public virtual decimal GetBalance ()
{
return this.balance;
}
public bool PayInFunds ( decimal amount )
{
if ( (balance + amount) < balance )
{
return false;
}
this.balance = this.balance + amount ;
return true;
}
private string name;
public string GetName()
{
return this.name;
}
public static string ValidateName ( string name )
{
string trimmedName = name.Trim();
if ( trimmedName.Length == 0 )
{
return "No text in the name";
}
return "";
}
public bool SetName ( string inName )
09/07/2015 5
{
string reply ;
reply = ValidateName(inName);
if ( reply.Length > 0 )
{
return false;
}
this.name = inName.Trim();
return true;
}
public Account ( string inName, decimal inBalance )
{
if ( !SetName(inName) )
{
throw new Exception("Invalid name");
}
this.balance = inBalance;
}
}
public class CustomerAccount : Account
{
public override string RudeLetterString()
{
return "You are overdrawn" ;
}
public CustomerAccount ( string inName, decimal inBalance ) :
base (inName, inBalance)
{
}
}
public class BabyAccount : Account
{
public override bool WithdrawFunds ( decimal amount )
{
if (amount > 10)
{
return false ;
}
return base.WithdrawFunds(amount);
}
public override string RudeLetterString()
{
return "Tell daddy you are overdrawn";
}
public BabyAccount ( string inName, decimal inBalance ) :
base (inName, inBalance)
{
}
}
public class Test
{
public static void Main ()
{
CustomerAccount test = new CustomerAccount ( "Fred", 25);
Console.WriteLine ( test.GetName() );
}
}
{
bool PayInFunds ( decimal amount );
bool WithdrawFunds ( decimal amount );
decimal GetBalance ();
string RudeLetterString();
string GetName ();
bool SetName ( string inName);
}
public abstract class Account : IAccount
{
private decimal balance = 0;
public abstract string RudeLetterString();
public virtual bool WithdrawFunds ( decimal amount )
{
if ( balance < amount )
{
return false ;
}
this.balance = this.balance - amount ;
return true;
}
public virtual decimal GetBalance ()
{
return this.balance;
}
public bool PayInFunds ( decimal amount )
{
if ( (balance + amount) < balance )
{
return false;
}
this.balance = this.balance + amount ;
return true;
}
private string name;
public string GetName()
{
return this.name;
}
public static string ValidateName ( string name )
{
string trimmedName = name.Trim();
if ( trimmedName.Length == 0 )
{
return "No text in the name";
}
return "";
}
public bool SetName ( string inName )
09/07/2015 5
{
string reply ;
reply = ValidateName(inName);
if ( reply.Length > 0 )
{
return false;
}
this.name = inName.Trim();
return true;
}
public Account ( string inName, decimal inBalance )
{
if ( !SetName(inName) )
{
throw new Exception("Invalid name");
}
this.balance = inBalance;
}
}
public class CustomerAccount : Account
{
public override string RudeLetterString()
{
return "You are overdrawn" ;
}
public CustomerAccount ( string inName, decimal inBalance ) :
base (inName, inBalance)
{
}
}
public class BabyAccount : Account
{
public override bool WithdrawFunds ( decimal amount )
{
if (amount > 10)
{
return false ;
}
return base.WithdrawFunds(amount);
}
public override string RudeLetterString()
{
return "Tell daddy you are overdrawn";
}
public BabyAccount ( string inName, decimal inBalance ) :
base (inName, inBalance)
{
}
}
public class Test
{
public static void Main ()
{
CustomerAccount test = new CustomerAccount ( "Fred", 25);
Console.WriteLine ( test.GetName() );
}
}
Questions
1. The system should not allow the creation of any acounts with names which are empty strings. Describe how the
programmer has accomplished this in the code above. Add code to prevent the creation of accounts with an
initial balance value which is less than 0 or greater than 10000.
Give the amended C# code.
[10 marks]
2. The ValidateName method in the Account class has been made static. Describe the effect of this.what
it means for users of the Account class and why it is a good idea to do this.
[10 marks]
3. There is a need for an additional constructor which would allow instances of the Account class to be created
without providing an initial balance value. In this situation the balance should be set to 0.
Write the C# code for this constructor.
[10 marks]
4. Describe how you could use the ToString method in the classes above to allow the class content to be
printed out. Create code to implement a ToString behaviour for the BabyAccount class. Ensure that it
uses behaviour in the parent Account class as appropriate.
Write the C# code for the ToString method.
[10 marks]
5. There is a need for a method called TestBank that wil serves as a test harness for the bank. It will create
instances of the account classes and perform tests on the management of the account balance that they provide.
The tests should be used to make sure that when a user pays in and attempts to withdraw funds the system
always behaves correctly. The following tests have been suggested:
Create an account and make sure the initial balance is zero
Create an account with an empty name string and make sure it is rejected
Create an account with an initial balance value of -50 and make sure it is rejected
Create an account with a balance of zero. Pay in 50 pounds and make sure that the balance is now 50
pounds.
Write the C# code for the TestBank method.
[10 marks]
6. At the moment a BabyAccount holder cannot withdraw more than 10 pounds at once. The bank has asked
you to change the program so that the amount that can be withdrawn from a BabyAccount can be set for
each account between 1 and 15 pounds. Add a data member maxBabyWithDraw to the BabyAccount and
create a method called SetMaxBabyWithdraw which will provide this behaviour. Create two tests for the
new method which make sure that the maximum withdrawal amount cannot be set above 15 or below 1 using
your code.
Give the complete C# code for each of the methods that you have modified.