All pastes #1279066 Raw Edit

Unnamed

public java v1 · immutable
#1279066 ·published 2008-12-07 20:59 UTC
rendered paste body
/*Does not compile, just a selction of code that you can use.Code not tested!http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.htmlUse the sun java docs to understand how to use the arraylist methods and such!*///arraylist declarationpublic ArrayList<User> listProfiles = new ArrayList<User>();public ArrayList<String> ListProfileID = new ArrayList<String>();public Object objProfileLock = new Object();/*synchronized private method, which returns nothing.  It is thread safe. Meaning that only one thread at a time can use it.synchronized an object called objProfileLockThis object acts as a lock, to add to the array lists, first the calling thread has to gain control of this lock. Once gained we can do some stuff!** Assumed that the profile ID has already been created from something*/private synchronized void addUser(String profileID, String strName, String strEmail){	synchronized(objProfileLock){	//Create an object of User and set soem data!		User objUser = new User();		objUser.addID(profileID);		objUser.addName(strName);		objUser.addEmail(strEmail);	//Add the object to the profile list.  Then add the profile ID to the other list.		listProfiles.add(objUser);		ListProfileID.add(profileID);	}}/*  NEW ONETo get a user just pass in a profile id.At the moment returns nothing.  You work out what you want to return*/private synchronized void getUser(String profileID){	synchronized(objProfileLock){	//lets fine the profile!		int intLocation = ListProfileID.indexOf(profileID);		if (intLocation == -1){		//if intLocation == -1, then the profile is not in the list, therefore does not exist!		} else {		//if it does exist we can do some more stuff.			String strNewString = listProfiles.get(intLocation).getName();			/*			No casting should be required, because you are using Arraylists, which already have the class types.			You could even do something similar when adding objects.				You could create an empty object, add it.  Then access the object methods like above				Very useful when you want to update an object ;)			You only need to cast when returning or sending Objects between methods and servers!			I think so anyway!			*/		}	}}/*Old ignore the this once, use the one above*//* OLD ONETo get a user just pass in a profile id.At the moment returns nothing.  You work out what you want to return*/private synchronized void getUser(String profileID){	synchronized(objProfileLock){	//lets fine the profile!		int intLocation = ListProfileID.indexOf(profileID);		if (intLocation == -1){		//if intLocation == -1, then the profile is not in the list, therefore does not exist!		} else {		//if it does exist we can do some more stuff.		/*			Since we know the profile, we know the object should also exist!			Create an empty object of the User class		*/			User objEmpty = new User();		/*				now lets get that profile out of the list!			Once the objects have been added to a list, they become "lifeless" they do not belong ot any class			We need to make that object useable by casting it its correct Class type.			Since an empty object has been created, we now must give it some data			We do that by retriving the object out of the arary list and casting			its the bracket part which does the casting			Remember the object you just created and the object you expect must be the same class.		*/			objEmpty = (User)listProfiles.get(intLocation);						//the object is now useable!			String strName = objEmpty.getName();		}	}	}/*The class file time!IT MUST HAVE THE FOLLOWING import java.io.Serializable; and the class must have implements SerializableIt should allow the object to be moved around.*/import java.io.*;import java.util.*;import java.io.Serializable; public class User implements Serializable{	//Where data will be stored!	public String ProfileID	public String Name	public String Email	//object constructor,	user(){};		public void addID(String strID){		ProfileID = strID;	}		public String getID(){		return ProfileID;	}		public void addName(String strName){		Name = strName;	}		public String getName(){		return Name ;	}		public void addEmail(String strEmail){		Email = strEmail;	}		public String getEmail(){		return strEmail ;	}}