Re: Static methods
Woot4Moo@gmail.com said:
Hi guys I am working on a program and I cant seem to figure out where
it is I am going wrong.
It is involving Bank accounts and PINs. I have a Bank class and a
BankAccount class.
In my BankAccount class I have a variable declared as private static
int pin.
as well as:
public static int checkPin()
{
return pin;
}
In my Bank class I have
public double getBalance(String nameOfOwner, int finalPin)
{
int pin = BankAccount.checkPin();
if(pin != finalPin) return -1;
// rest of code to get balance, which works properly.
}
You already have gotten advise on some other aspects, but I'd like to
comment on your naming of things.
When you look at the "checkPin" method, does it really check a given pin?
No; what it does is that it gets the pin. Actually you check the pin in your
Bank.getBalance method (the "pin != finalPin" expression).
Rather, you could change the method to really check a pin, by changing
the return value into boolean - like
public boolean checkPin(int pinInput)
{
return (pin == pinInput);
}
.... which you would then use like
public double getBalance(String nameOfOwner, int finalPin)
{
BankAccount account;
// somehow fetch the account for the owner, then check the pin
if(account.checkPin(finalPin)) return -1;
// rest of code to get balance, which works properly.
}
Of course, to continue with this trend, I'm not certain whether
checking the pin belongs into a method called getBalance.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)