Basically I am trying to set up a list of recent users, so as not to type out a name time after time. In the newUsr method, I add a user entered name to the database, unless the name already exists in the database. If the database already has 5 recent users names, I selct a name by random. It would be much better if I could select it by the date of the name that was hasnt been used the longest.
I have thought about reading all elements into a vector of strings, working with the vector. removing all elements in the database, and then once done adding all elements from the vector to the database.
any comments on cleaning my code up and critisms will be welcome too if guid me on how to better my programming style.
public void writeToRecentUsersDB(String ruName){
byte[] bstringArray = ruName.getBytes();
try {
dbRecentUsers.addRecord(bstringArray, 0, bstringArray.length);
} catch (RecordStoreException ex) {
}
}
public void newUser(){
boolean playON = true;
int xFontPos = 50;
long lCurrTick = 0; // current system time in milliseconds;
int iKey = 0;
boolean option3isRunning = true;
Random rand = new Random();
boolean toAdd = true;
//int rand5 = rand.nextInt(5);
while(option3isRunning){
lCurrTick = System.currentTimeMillis();
iKey = getKeyStates();
checkKeys(iKey, lCurrTick);
updatePrompt(lCurrTick);
//if(isDown[fireKey]){
if(keyPressedCode == TOP_LEFT_KEY){//acknowledge
name = prText;
String str;
int rndIndex = 0;
cleanScreen();
openRecentUsersDB(RECENT_USERS);
try {
int testrms = dbRecentUsers.getNumRecords();
if( testrms > 0){
for(int index =1; index < testrms+1 ; index++){//added one as rms databases work from 1 to ...
str = new String(dbRecentUsers.getRecord(index));
if (str.equals(name)){
rndIndex = index;
toAdd = false; // or rather not to write to record
}//if
}//for
}//if
//dbRecentUsers.deleteRecord(rndIndex);
// insert name into recent names database - max of 5
testrms = dbRecentUsers.getNumRecords();
int testrms2 = dbRecentUsers.getSize();
if(testrms == 5){
try {//delte a random record.
//if name is not being used
rndIndex = rand.nextInt(5)+ 1;
if(rndIndex == 6)
rndIndex = 1;
dbRecentUsers.deleteRecord(rndIndex); // for now. actaully want a date recent played.
} catch (RecordStoreException ex) {
}
}
testrms = dbRecentUsers.getNumRecords();
if(testrms < 5){
if(toAdd){
writeToRecentUsersDB(name);
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
closeRecentUsersDB();
option3isRunning = false;
}

New Topic/Question
Reply


MultiQuote



|