Sunteți pe pagina 1din 23

//////////////////////////

//SmartyPhonebook.java
//importing some java package
//////////////////////////
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
/////////////
//SmartyPhonebook extending abstract MIDlet class with CommandListener
/////////////
public class SmartyPhonebook extends MIDlet implements CommandListener
{
//declaring some displayable class
private Display display;
private Alert alert;
private Form mainform, newcontact, viewform, editform;
private List list;

//declaring RMS
private RecordStore recordstore = null;
private RecordEnumeration recordEnumeration = null;
private Filter filter = null;

//Commands
Command exit = new Command("Exit", Command.EXIT, 1);

Command viewList = new Command("View Contacts", Command.SCREEN, 1);


Command newContact = new Command("New Contact", Command.SCREEN, 1);
Command edit = new Command("Edit", Command.SCREEN, 1);
Command delete = new Command("Delete", Command.SCREEN, 1);
Command find = new Command("Find", Command.SCREEN, 1);
Command search = new Command("Search", Command.SCREEN, 1);
Command save = new Command("Save", Command.SCREEN, 1);
Command update = new Command("Update", Command.SCREEN, 1);
Command cancel = new Command("Cancel", Command.BACK, 1);
Command back = new Command("Back", Command.BACK, 1);
//Textfields
TextField fname = new TextField("First Name:", "", 20, TextField.ANY);
TextField lname = new TextField("Last Name:", "", 20, TextField.ANY);
TextField address = new TextField("Address:", "", 70, TextField.ANY);
TextField email = new TextField("Email:", "", 40, TextField.ANY);
TextField phonenum = new TextField("Phone #:", "", 20,
TextField.PHONENUMBER);
TextField firstname, lastname, location, emailadd, phonenumber;

//textbox
TextBox Find = new TextBox("Search Contact:\n", "", 20, TextField.ANY);

int recID, currentTask;


String keyword;

/////////////
//Constructor

/////////////
public SmartyPhonebook()
{
///Main Form
mainform = new Form("Smarty Phonebook");
try
{
Image img = Image.createImage("/headerImage.png");
ImageItem image = new ImageItem("\n\n\n\n", img, Item.LAYOUT_CENTER,
"Smarty Phonebook");
mainform.append(image);
}
catch (Exception e){e.printStackTrace();}
mainform.addCommand(viewList);
mainform.addCommand(exit);
mainform.setCommandListener(this);

//Add New Form


newcontact = new Form("Add New Contact");
newcontact.addCommand(back);
newcontact.addCommand(save);
newcontact.append(fname);
newcontact.append(lname);
newcontact.append(address);
newcontact.append(email);
newcontact.append(phonenum);
newcontact.setCommandListener(this);

}
public void startApp()
{
if (display == null){
display = Display.getDisplay(this);
display.setCurrent(mainform);
}
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}

///////////////////////////////////////////////////////////////////////////
//commandAction which works with commandLister, the command event handling
///////////////////////////////////////////////////////////////////////////
public void commandAction(Command command, Displayable displayable)
{
//trying to open a RecordStore if existing, and if not create it
try
{
recordstore = RecordStore.openRecordStore("phonebookRS", true);
}

catch (Exception error)


{
alert = new Alert("Error Creating",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}

//for exit command, just destroying the applications


if (command == exit)
{
destroyApp(true);
notifyDestroyed();
}

//for back command, setting display to the mainForm


if (command==back)
{
display.setCurrent(list);
}

//for cancel command, setting display to the mainForm


if (command==cancel)
{
display.setCurrent(mainform);
}

//for displaying new contact form


if (command == newContact)
{
lname.setString("");
fname.setString("");
phonenum.setString("");
email.setString("");
address.setString("");
display.setCurrent(newcontact);
}

//for displaying search form


if (command == find)
{
Find.setString("");
Find.addCommand(search);
Find.addCommand(back);
Find.setCommandListener(this);
display.setCurrent(Find);
}

//for searching
if (command == search)
{
keyword=Find.getString();

try
{
list = new List("Smarty Phonebook", List.IMPLICIT);
list.addCommand(newContact);
list.addCommand(find);
list.addCommand(cancel);
list.setCommandListener(this);
currentTask=1;
String inputlname, inputfname, inputphonenum = null;
byte[] byteInputData = new byte[300];
ByteArrayInputStream inputStream = new
ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (recordstore.getNumRecords() > 0)
{
filter = new Filter(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
while (recordEnumeration.hasNextElement())
{
recordstore.getRecord(recordEnumeration.nextRecordId(),byteInputData, 0);
inputlname = inputDataStream.readUTF();
inputfname = inputDataStream.readUTF();
inputphonenum = inputDataStream.readUTF();
list.append(inputlname+", "+inputfname+"
"+inputphonenum,null);
inputStream.reset();

}
}
display.setCurrent(list);
inputStream.close();
}
catch (Exception error)
{
alert = new Alert("Error Reading",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}

//for updating contact entries


if (command == update)
{
try
{
String outputLName=lastname.getString();
String outputFName=firstname.getString();
String outputPhonenum=phonenumber.getString();
String outputemail=emailadd.getString();
String outputaddress=location.getString();
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

DataOutputStream outputDataStream = new


DataOutputStream(outputStream);
outputDataStream.writeUTF(outputLName);
outputDataStream.writeUTF(outputFName);
outputDataStream.writeUTF(outputPhonenum);
outputDataStream.writeUTF(outputemail);
outputDataStream.writeUTF(outputaddress);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.setRecord(recID, outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
display.setCurrent(mainform);
}
catch ( Exception error)
{
alert = new Alert("Error Writing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}

//for saving new contact


if (command == save)
{

try
{
String outputLName=lname.getString();
String outputFName=fname.getString();
String outputPhonenum=phonenum.getString();
String outputemail=email.getString();
String outputaddress=address.getString();
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new
DataOutputStream(outputStream);
outputDataStream.writeUTF(outputLName);
outputDataStream.writeUTF(outputFName);
outputDataStream.writeUTF(outputPhonenum);
outputDataStream.writeUTF(outputemail);
outputDataStream.writeUTF(outputaddress);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
display.setCurrent(mainform);
}
catch ( Exception error)
{

display.setCurrent(alert);
}
}

//for editing contact


if(command == edit)
{
int index = 1+list.getSelectedIndex();
try
{
editform = new Form("Edit Contact Details");
editform.addCommand(back);
editform.addCommand(update);
editform.setCommandListener(this);
byte[] byteInputData = new byte[300];
String inputlname, inputfname, inputphonenum,inputemail, inputaddress =
null;
ByteArrayInputStream inputStream = new
ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if(currentTask==1)
{
filter = new Filter(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
}
if(currentTask==2)
{

Comparator comparator = new Comparator();


recordEnumeration = recordstore.enumerateRecords(null, comparator,
false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++)
{
int datapointer=recordEnumeration.nextRecordId();
if(i==index)
{
recordstore.getRecord(datapointer,byteInputData, 0);
recID=datapointer;
inputlname = inputDataStream.readUTF();
inputfname = inputDataStream.readUTF();
inputphonenum = inputDataStream.readUTF();
inputemail = inputDataStream.readUTF();
inputaddress = inputDataStream.readUTF();
firstname = new TextField("First Name:", inputfname, 20,
TextField.ANY);
lastname = new TextField("Last Name:", inputlname, 20, TextField.ANY);
location = new TextField("Address:", inputaddress, 70, TextField.ANY);
emailadd = new TextField("Email:", inputemail, 40, TextField.ANY);
phonenumber = new TextField("Phone #:", inputphonenum, 20,
TextField.PHONENUMBER);
editform.append(firstname);
editform.append(lastname);
editform.append(location);
editform.append(emailadd);

editform.append(phonenumber);
inputStream.reset();
}
}
display.setCurrent(editform);
inputStream.close();
}
catch (Exception error)
{
alert = new Alert("Error Reading", error.toString(),
null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}

//for viewing each contact using the SELECT_COMMAND


if (command == List.SELECT_COMMAND)
{
int index = 1+list.getSelectedIndex();
try
{
viewform = new Form("Contact Details");
viewform.addCommand(back);
viewform.addCommand(edit);
viewform.addCommand(delete);

viewform.setCommandListener(this);
byte[] byteInputData = new byte[300];
String inputlname, inputfname, inputphonenum,inputemail, inputaddress =
null;
ByteArrayInputStream inputStream = new
ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if(currentTask==1)
{
filter = new Filter(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
}
if(currentTask==2)
{
Comparator comparator = new Comparator();
recordEnumeration = recordstore.enumerateRecords(null, comparator,
false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++)
{
int datapointer=recordEnumeration.nextRecordId();
if(i==index)
{
recordstore.getRecord(datapointer,byteInputData, 0);
recID=datapointer;
inputlname = inputDataStream.readUTF();
inputfname = inputDataStream.readUTF();

inputphonenum = inputDataStream.readUTF();
inputemail = inputDataStream.readUTF();
inputaddress = inputDataStream.readUTF();
StringItem stringfname=new StringItem("Firstname: ", inputfname ,
Item.PLAIN);
StringItem stringlname=new StringItem("Lastname: ", inputlname ,
Item.PLAIN);
StringItem stringphonenum=new StringItem("Phone #: ",
inputphonenum , Item.PLAIN);
StringItem stringemail=new StringItem("Email: ", inputemail ,
Item.PLAIN);
StringItem stringaddress=new StringItem("Address: ", inputaddress ,
Item.PLAIN);
viewform.append(stringfname);
viewform.append(stringlname);
viewform.append(stringphonenum);
viewform.append(stringemail);
viewform.append(stringaddress);
inputStream.reset();
}
}
display.setCurrent(viewform);
inputStream.close();
}
catch (Exception error)
{
alert = new Alert("Error Reading", error.toString(),
null, AlertType.WARNING);

alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (command == delete)
{
int index = 1+list.getSelectedIndex();
try
{
if(currentTask==1)
{
filter = new Filter(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
}
if(currentTask==2)
{
Comparator comparator = new Comparator();
recordEnumeration = recordstore.enumerateRecords(null, comparator,
false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++)
{
int datapointer=recordEnumeration.nextRecordId();
if(i==index)
{
recordstore.deleteRecord(datapointer);
}

}
display.setCurrent(mainform);
}
catch (Exception error)
{
alert = new Alert("Error Reading", error.toString(),
null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (command == viewList)
{
try
{
list = new List("Smarty Phonebook", List.IMPLICIT);
list.addCommand(newContact);
list.addCommand(find);
list.addCommand(cancel);
list.setCommandListener(this);
currentTask=2;
byte[] byteInputData = new byte[300];
String inputlname,inputfname, inputphonenum, inputaddress, inputemail =
null;
ByteArrayInputStream inputStream = new
ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);

Comparator comparator = new Comparator();


recordEnumeration = recordstore.enumerateRecords(null, comparator,
false);
while (recordEnumeration.hasNextElement())
{
recordstore.getRecord(recordEnumeration.nextRecordId(),byteInputData,
0);
inputlname = inputDataStream.readUTF();
inputfname = inputDataStream.readUTF();
inputphonenum = inputDataStream.readUTF();
inputemail = inputDataStream.readUTF();
inputaddress = inputDataStream.readUTF();
list.append(inputfname+" "+inputemail,null);
inputStream.reset();
}
display.setCurrent(list);
inputStream.close();
}
catch (Exception error)
{
alert = new Alert("Error Reading", error.toString(),
null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
try

{
recordstore.closeRecordStore();
}
catch (Exception error)
{
alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}

////////////////////////////////////
//Implementation of RecordComparator
////////////////////////////////////
class Comparator implements RecordComparator
{
//Declaration of variables
private byte[] comparatorInputData = new byte[300];
private ByteArrayInputStream comparatorInputStream = null;
private DataInputStream comparatorInputDataType = null;
public int compare(byte[] record1, byte[] record4)
{
//Declaration of variables
int record1int, record4int;
try

{
comparatorInputStream = new ByteArrayInputStream(record1);
comparatorInputDataType =new DataInputStream(comparatorInputStream);
String data1=comparatorInputDataType.readUTF();
comparatorInputStream = new ByteArrayInputStream(record4);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data2=comparatorInputDataType.readUTF();
int comparison = data1.compareTo(data2);
if (comparison == 0)
{
return RecordComparator.EQUIVALENT;
}
else if (comparison < 0)
{
return RecordComparator.PRECEDES;
}
else
{
return RecordComparator.FOLLOWS;
}
}
catch (Exception error)
{
return RecordComparator.EQUIVALENT;
}
}

public void compareClose()


{
try
{
if (comparatorInputStream!= null)
{
comparatorInputStream.close();
}
if (comparatorInputDataType!= null)
{
comparatorInputDataType.close();
}
}
catch (Exception error)
{
}
}
}

////////////////////////////////
//Implementation of RecordFilter
////////////////////////////////
class Filter implements RecordFilter
{
private String search = null;
private ByteArrayInputStream inputstream = null;

private DataInputStream datainputstream = null;


public Filter(String search)
{
this.search = search.toLowerCase();
}
public boolean matches(byte[] suspect)
{
String string = new String(suspect).toLowerCase();
if (string!= null && string.indexOf(search) != -1)
return true;
else
return false;
}
public void filterClose()
{
try
{
if (inputstream != null)
{
inputstream.close();
}
if (datainputstream != null)
{
datainputstream.close();
}
}

catch ( Exception error)


{
}
}
}

S-ar putea să vă placă și