How do I code this (quite easy I guess)?

A

Alamannianus

Until now the class is sweet and dandy. Now I want to implement a
function, so that when the user fills in his emailaddress and password,
the system generates a unique id for this user. How can I implement
such a function? Please help.


######################################


public class user {

private String email; //user fills in his email
private String passwort; //user choses a password
private String id; // HERE AN ID SHOULD BE GENERATED BY THE
SYSTEM


public user(String emailAddress, String Userpassword) {

email = emailAddress;
passwort = Userpassword;

}

public void releasedateindtabase () {
System.out.println(("User with ID: " + id ));
System.out.println(("and email Address " + email));
System.out.println(("is registered in the system"));

}


}



###########################################################


// this "superclass" is the one where all users are stored

import java.util.ArrayList;

public class Database {

private Arraylist user;

public Database() {

user = new Arraylist();

}

public void getuser(user theuser) {

user.add(theuser);
}


}
 
S

sameergn

If you are using an Oracle DB as the backend then you can use a
sequence and use the nextval method on that sequence to generate the
ID.

If you do not have DB as a backend then you can store a counter in
file, load it when class is loaded (using static initialization), store
it in static variable, write a synchronized method that will increment
it and return the next value, write it back to file when your app/class
is done

both the methods will generate a predictable id i.e. If I know my ID, I
can guess others IDs too.
 
A

Alun Harford

Until now the class is sweet and dandy. Now I want to implement a
function, so that when the user fills in his emailaddress and password,
the system generates a unique id for this user. How can I implement
such a function? Please help.

Firstly, I think I should point out that storing passwords as plaintext is
fine for a toy, but I wouldn't store my password in it!
######################################


public class user {

private String email; //user fills in his email
private String passwort; //user choses a password
private String id; // HERE AN ID SHOULD BE GENERATED BY THE
SYSTEM private static int maxID=0;


public user(String emailAddress, String Userpassword) {

email = emailAddress;
passwort = Userpassword; id=maxID++;

}
....

Alternatively, if you didn't want the ID to be easily guessed,
public class user {

private String email; //user fills in his email
private String passwort; //user choses a password
private String id; // HERE AN ID SHOULD BE GENERATED BY THE
SYSTEM
private static java.util.HashSet ids = new java.util.HashTable();
private java.security.SecureRandom random = new
java.security.SecureRandom();
public user(String emailAddress, String Userpassword) throws Exception{

email = emailAddress;
passwort = Userpassword;
boolean finished=false;
int i;
for(i=0;i<1024 && !finished;i++) {
int randomNumber = random.next(32);
if(!ids.contains(new Integer(randomNumber))) {
finished=true;
id=randomNumber;
ids.add(new Integer(randomNumber));
}
}
if(finished==false) throw new Exception(); //Failed to add it
1024 times in a row. HashSet is too full (2^32 users)
....

Alun Harford
 
A

Alun Harford

Alun Harford said:
Alternatively, if you didn't want the ID to be easily guessed,

private static java.util.HashSet ids = new java.util.HashTable();

that obviously should be:
private static java.util.HashSet ids = new java.util.HashSet();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top