PDA

View Full Version : creating unique bank account numbers


sunnny
10-15-2005, 01:50 AM
Hi,

I am trying to create a Account Number class with a createAccountNumber method. A unique account number is created everytime the method runs. The account number must adhere by these conditions to make sure it is unique.

1). All account numbers should be 6 digits long.
2) The first digit cannot be zero,
3) (first_digit +last_digit )%10 should be zero.

I don't know how to fulfill all the requirements and my code is not working. If someone could take a look and guide me how to do this it will be really, really helpful :D. Here is my code:

public class AccountNumberMaker{

private static int nextAvailableAccountNumber = 100009;
private int accountNumber;
public int nextAccountNumber(){

String stringNextAvailableAccountNumber = Integer.toString (nextAvailableAccountNumber);

int first_digit = nextAvailableAccountNumber / 100000;
int last_digit= nextAvailableAccountNumber % 100000;
while(true){

if(stringNextAvailableAccountNumber.length() == 6){
if((first_digit +last_digit )%10 == 0){

return nextAvailableAccountNumber;
}
}
nextAvailableAccountNumber++;
}
}

} //end class AccountNumberMaker


Thanks!!!!!!!
Sunny

suryad
10-15-2005, 10:38 PM
What do you mean when you say your code is not working? Could you be more specific? And also next time please as a request when you post code, use the code tag and that way it is a lot easier to read! Thanks!

suryad
10-15-2005, 10:41 PM
I dont know how you code on your comp, but also when you type code for legibility purposes make you sure you follow the Java coding conventions...makes life a lot easier! :thumbsup:

suryad
10-15-2005, 10:48 PM
Have you tried adding in a main method so you can actually run the program? And also you do realize that you have an infinite loop going on in the case of the while loop? Is that on purpose? Hope this helps.

sunnny
10-16-2005, 03:34 PM
Thanks for the reply. Yes the infinite loop is on purpose. I want the loop to keep going until the functions returns something and the function returns something when all the conditions are true. What I mean by doesn't working is that it prints the same number eveytime I call this method in my testing, within a main method, in my case the nmber printed is 100009. I don't know why the number is not incremented. Here is my code with a cleaner syntax :D . I don't know when I code in JCreator I have all the indentation, but when I pasted the code in here I lost all my indentation.

public class AccountNumberMaker{

private static int nextAvailableAccountNumber = 100009;
private int accountNumber;

public int nextAccountNumber(){

String stringNextAvailableAccountNumber = Integer.toString (nextAvailableAccountNumber);
int first_digit = nextAvailableAccountNumber / 100000;
int last_digit= nextAvailableAccountNumber % 100000;

while(true){

if(stringNextAvailableAccountNumber.length() == 6){
if((first_digit +last_digit )%10 == 0){

return nextAvailableAccountNumber;
}
}
nextAvailableAccountNumber++;
}
}

} //end class AccountNumberMaker

Here is the code that I am using to test this function:

public class tester{

public static void main(String [] args){

AccountNumberMaker account = new AccountNumberMaker();

System.out.println(account.nextAccountNumber());
System.out.println(account.nextAccountNumber());
System.out.println(account.nextAccountNumber());
System.out.println(account.nextAccountNumber());
System.out.println(account.nextAccountNumber());
System.out.println(account.nextAccountNumber());
System.out.println(account.nextAccountNumber());

}

} //end class AccountNumberMaker