Reputation:
I'm writing ATM software in order to train object-oriented programming and I have got Pin class with a VerifyPin method.
class Pin
{
private int _pin;
public void ChangePin (int newPin)
{
if (newPin.ToString().Length != 4)
{
throw new ArgumentException("PIN must be exactly 4 digits long");
}
_pin = newPin;
}
public bool VerifyPin (int pin)
{
return (_pin == pin);
}
}
I'm trying to call this method inside other classes in order to verify PIN which is stored inside Card, as well as Account.
So far I came up with an idea to do something like this, but I'm sure there is a better way
class Card
{
private Pin _pin;
public Card(Pin pin)
{
this._pin = pin;
public bool VerifyPin (int pin)
{
return this._pin.VerifyPin(pin);
}
}
Any sources to learn more about subject will be welcomed as well.
Upvotes: 1
Views: 100
Reputation: 5895
I would say that the pin should be encapsulated in an Account, but not a Card. A card would be associated with an account. So to verify a pin when given a card you would call the following code:
bool verified = myCard.Account.VerifyPin(pin);
(A static method doesn't work because it can't access the field _pin
).
Upvotes: 0
Reputation:
This method can be made static. This way it could be accessed and used from any class in the same project, But seems what you would like to create a pin object and then call that object method : VerifyPin that will take the pin you want to validate as parameter
Upvotes: 2
Reputation: 1537
If you make the VerifyPin method static you may call it without creating an instance of the Pin class.
public static bool VerifyPin(int pin){}
Pin.VerifyPin(pin);
Upvotes: 0