Cookies, Stacks and Lists

I can’t get into too much detail on this assignment however in general – a user would access a simple Java web server.  The user would make a request of the server and some text would be displayed in their browser.

There was to be a collection of text items which would be sent back to the user in random order – and no duplicates until the entire collection had been run thru.

So – a list of strings, some way of randomizing the list and then a way to keep track of what was being sent to the user were parts of the core requirements.

My solution was to start with a Java LinkedHashMap – using integers for the Keys, and then the text would be the Value part of the pair.   And no, I would not be doing some kind of random loading of the list – the providing random text comes from another piece.  An array would work here as well – both have  O(1) look-up, there isn’t any iteration nor adding/deleting from the list.

Oh, and one other requirement – the user needed to add a nickname so as to personalize the return.  So my method looked something like this:

/**
*  Takes a String name, places it into the line of text and then return.
*
*  @param name String containing a nickname supplied by a user.
*  @return LinkedHashMap containing collection of text.
*
*/

static  LinkedHashMap  getrt(String name){

LinkedHashMap lhm = new LinkedHashMap();

String rt = “A. ” + name + “, what goes 99 click? A centipede with a wooden leg.”;
lhm.put(1, rt);
rt = “B. ” +  name + “, how many seconds are in a year? There are 12 seconds in a year. January 2nd, February 2nd, March 2nd…”;
lhm.put(2, rt);
rt = “C. ” +  name + “, here is one for you.  Little four-year-old Julie was looking at her new baby brother for the first time. He was fast asleep. After staring at her tiny, motionless baby brother for a few minutes, Julie looked up at her mother and asked why didn’t he come with batteries?”;
lhm.put(3, rt);
rt = “D. ” +  name + “, you can sure pity the dad with three kids in college. He tells his wife that they are getting poorer by degrees.”;
lhm.put(4, rt);
rt = “E. You know ” + name + “, when I die I want to go peacefully — like my grandfather did — in his sleep. Not screaming like the passengers in his car.”;
lhm.put(5, rt);
return lhm;
}

Next part was create a random Queue – this gets passed back to the user as part of their cookie, and gets used to deliver the text from the LinkedHashMap:

/**
*  Takes an int count of a list and returns a randomized queue
*
*  @param listSize int value containing size of a list.
*  @return Queue containing a randomized List of ints.
*
*/
static Queue outputQueue(int listSize){
Queue queue = new LinkedList();
Random rnd = new Random(System.currentTimeMillis());

for(int cntr = 0; cntr < listSize; cntr++){

int i = Math.abs(rnd.nextInt() % 75);
// Want a random list of unique integers no greater than the size of the text list.
while(i <= 0 || i > listSize || queue.contains(i) ){
i = Math.abs(rnd.nextInt() % 75);
}
queue.add(i);
}
return queue;
}

And no, the user isn’t getting the queue passed back to them – the value of the queue gets added to an empty string and that gets returned to the user:

// No more values – need to requeue
out.println(name + “, I told you everything – restarting the list.”);
TextServer.textQ = outputQueue(lhm.size());
s = TextServer.textQ.poll().toString();
int qSize = TextServer.textQ.size();
for(int i = 1; i <= qSize; i++){
s += TextServer.textQ.poll().toString();
}
jIndex = s;

The first value in the string gets ‘popped’ off, as what would happen in a queue – the string value gets used to find a corresponding Key in the LinkedHashMap – and the user gets served a line of text.  The string is returned to the user as part of their cookie.  Once all the values from the string are ‘popped’ off – in other words the string is empty – another call is placed to the outputQueue method and a fresh string is sent back to the user.  Here is the code for that method:

/**
*  Gets a nickname from client and returns text using the name to output stream.
*
*  @param lhm LinkedHashMap containing text.
*  @param name String containing a nickname supplied by a user.
*  @param jokeIndex String holding the list of text yet to be told.
*  @param out PrintStream sent to client containing text with the supplied nickname.
*  @exception IllegalArgumentException Thrown to indicate that a method has been passed an illegal or inappropriate argument.
*
*/

static String printText(LinkedHashMap lhm, String name, String textIndex, PrintStream out){
String jIndex = textIndex;

try{
int qnum = 0;
// If starting from scratch – get a random queue and set the values into the textIndex string
if(Integer.parseInt(jIndex) <= 0){
out.println(name + “, I’m starting the text list.”);
out.flush();
TextServer.textQ = outputQueue(lhm.size());
jIndex = TextServer.textQ.poll().toString();
int qSize = TextServer.textQ.size();
for(int i = 1; i <= qSize; i++){
jIndex += TextServer.textQ.poll().toString();
}
}

// Using the string textIndex as a cheap queue grab the first value and use it.
String s = jIndex.substring(0, 1);
qnum = Integer.parseInt(s);
out.println(lhm.get(qnum));
out.flush();

if(jIndex.length() >= 2){
  // Here I’m ‘popping’ the first value off the string
s = jIndex.substring(1);
jIndex = s;
} else {
// No more values – need to requeue
out.println(name + “, I told you everything – restarting the list.”);
TextServer.textQ = outputQueue(lhm.size());
s = TextServer.textQ.poll().toString();
int qSize = TextServer.textQ.size();
for(int i = 1; i <= qSize; i++){
s += TextServer.textQ.poll().toString();
}
jIndex = s;
}
} catch (IllegalArgumentException ex){
out.println(“Failed in attempt serve a text to ” + name);
}

return jIndex;
}

Advertisements

About Lee Greffin
Just another programmer...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: