PROBLEM STATEMENT
Create an application to perform the following operations on a persistent database
1. Adding Records
2. Deleting Records
3. Updating Records
CODE
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class RemoveRecordDemo extends MIDlet implements ItemStateListener, CommandListener
{
private Display display;
private Form frmMain;
private FormAdd frmAdd;
private List list = new List("All records ",List.IMPLICIT);
private Command cmdAdd, cmdExit, cmdDelete, cmdBack1,
cmdUpdate;
private Vector vecOP;
private ChoiceGroup chgOP;
protected DisplayManager displayMgr;
private RecordStore rs;
private static final String strREC_STORE_OP = "OPList";
private boolean flagSortByPriority = false,
flagShowPriority = true;
private ByteArrayInputStream istrmBytes = null;
private DataInputStream istrmDataType = null;
String strOP;
int strRecordId;
byte[] recData = new byte[250];
ByteArrayOutputStream ostrmBytes = null;
DataOutputStream ostrmDataType = null;
private RecordEnumeration e = null;
private ComparatorInt comp = null;
public RemoveRecordDemo()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
//initializing Form’s object
frmMain = new Form("");
frmAdd = new FormAdd("Add record", this);
//Initializing choice group
chgOP = new ChoiceGroup("", Choice.MULTIPLE);
vecOP = new Vector();
//Initailizing Command button
cmdAdd = new Command("Add record", Command.SCREEN, 2);
cmdDelete = new Command("Delete record", Command.SCREEN, 2);
cmdExit = new Command("Exit", Command.EXIT, 1);
cmdBack1 = new Command("back", Command.BACK, 1);
cmdUpdate = new Command("Update record", Command.SCREEN, 2);
frmMain.addCommand(cmdBack1);
//Adding list to form
list.addCommand(cmdAdd);
list.addCommand(cmdUpdate);
list.addCommand(cmdDelete);
list.addCommand(cmdExit);
list.setCommandListener(this);
frmMain.append(chgOP);
// Adding Command Listner
frmMain.setCommandListener(this);
frmMain.setItemStateListener(this);
displayMgr = new DisplayManager(display, list);
rs = openRecStore(strREC_STORE_OP);
initInputStreams();
initOutputStreams();
initEnumeration();
writeRMS2Vector();
rebuildOPList();
}
public void startApp()
{
display.setCurrent(list);
}
public void destroyApp(boolean unconditional)
{
if (comp != null)
comp.compareIntClose();
if (e != null)
e.destroy();
try
{
if (istrmDataType != null)
istrmDataType.close();
if (istrmBytes != null)
istrmBytes.close();
if (ostrmDataType != null)
ostrmDataType.close();
if (ostrmBytes != null)
ostrmBytes.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
closeRecStore(rs);
}
public void pauseApp(){}
private void initInputStreams()
{
istrmBytes = new ByteArrayInputStream(recData);
istrmDataType = new DataInputStream(istrmBytes);
}
private void initOutputStreams()
{
ostrmBytes = new ByteArrayOutputStream();
ostrmDataType = new DataOutputStream(ostrmBytes);
}
private void initEnumeration()
{
if (flagSortByPriority)
comp = new ComparatorInt();
else
comp = null;
try
{
e = rs.enumerateRecords(null, comp, false);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
private RecordStore openRecStore(String name)
{
try
{
return RecordStore.openRecordStore(name, true);
}
catch (Exception e)
{
System.out.println(e.toString());
return null;
}
}
private void closeRecStore(RecordStore rs)
{
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
private void deleteRecStore(String name)
{
try
{
RecordStore.deleteRecordStore(name);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
protected void addOPItem(String Name, String Address, String phonenum)
{
if (strOP == "A")
{
try
{
ostrmBytes.reset();
ostrmDataType.writeUTF(Name);
ostrmDataType.writeUTF(Address);
ostrmDataType.writeUTF(phonenum);
ostrmDataType.flush();
byte[] record = ostrmBytes.toByteArray();
int recordId = rs.addRecord
(record, 0, record.length);
OPItem item = new OPItem
(Name, Address, phonenum, recordId);
vecOP.addElement(item);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (strOP == "U")
{
try
{
ostrmBytes.reset();
ostrmDataType.writeUTF(Name);
ostrmDataType.writeUTF(Address);
ostrmDataType.writeUTF(phonenum);
ostrmDataType.flush();
byte[] record = ostrmBytes.toByteArray();
rs.setRecord(strRecordId, record, 0, record.length);
OPItem item = new OPItem
(Name, Address, phonenum, strRecordId);
vecOP.addElement(item);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
writeRMS2Vector();
rebuildOPList();
}
private void writeRMS2Vector()
{
vecOP.removeAllElements();
try
{
e.rebuild();
while (e.hasNextElement())
{
istrmBytes.reset();
int id = e.nextRecordId();
rs.getRecord(id, recData, 0);
OPItem item = new OPItem(istrmDataType.readUTF(),
istrmDataType.readUTF(),
istrmDataType.readUTF(), id);
vecOP.addElement(item);
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
protected void rebuildOPList()
{
for (int i = chgOP.size(); i > 0; i--)
chgOP.delete(i - 1);
list.deleteAll();
OPItem item;
String phonenum;
String Name;
String Address;
StringBuffer strb;
for (int i = 0; i < vecOP.size(); i++)
{
item = (OPItem)vecOP.elementAt(i);
phonenum = item.getphonenum();
Name = item.getName();
Address = item.getAddress();
strb = new StringBuffer();
strb.append(Name + " " + Address + " " + phonenum);
list.append(strb.toString(), null);
chgOP.append(strb.toString(), null);
}
}
public void itemStateChanged(Item item)
{
ChoiceGroup cg;
cg = (ChoiceGroup)item;
boolean selected[] = new boolean[cg.size()];
cg.getSelectedFlags(selected);
for (int i = 0; i < cg.size(); i++)
{
if (selected[i])
{
OPItem opitem = (OPItem)vecOP.elementAt(i);
if (strOP == "D")
{
try
{
rs.deleteRecord(opitem.getRecordId());
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
if (strOP == "U")
{
displayMgr.pushDisplayable(frmAdd);
frmAdd.setTitle("Update record");
try
{
frmAdd.tfName.setString(opitem.getName()); frmAdd.tfAddress.setString(opitem.getAddress());
frmAdd.tfPhonenum.setString(opitem.getphonenum());
strRecordId = opitem.getRecordId();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
break;
}
}
writeRMS2Vector();
rebuildOPList();
}
public void commandAction(Command c, Displayable d)
{
if (c == cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
else
{
if (c == cmdAdd)
{
strOP = "A";
frmAdd.setTitle("Add record");
frmAdd.tfName.setString("");
frmAdd.tfAddress.setString("");
frmAdd.tfPhonenum.setString("");
displayMgr.pushDisplayable(frmAdd);
}
else if (c == cmdDelete)
{
strOP = "D";
frmMain.setTitle("Delete record");
displayMgr.pushDisplayable(frmMain);
}
else if (c == cmdUpdate)
{
strOP = "U";
frmMain.setTitle("Update record");
displayMgr.pushDisplayable(frmMain);
}
else if (c == cmdBack1)
{
displayMgr.popDisplayable();
}
}
}
}
class DisplayManager extends Stack
{
// Reference to Display object
private Display display;
// Main displayable for MIDlet
private Displayable mainDisplayable;
private Alert alStackError; // Alert for error conditions
/* Display manager constructor */
public DisplayManager(Display display, Displayable mainDisplayable)
{
// Only one display object per midlet, this is it
this.display = display;
this.mainDisplayable = mainDisplayable;
// Create an alert displayed when an error occurs
alStackError = new Alert("Displayable Stack Error");
alStackError.setTimeout(Alert.FOREVER); // Modal
}
public void pushDisplayable(Displayable newDisplayable)
{
//System.out.println("pushDisplayable");
push(display.getCurrent());
display.setCurrent(newDisplayable);
}
/*Return to the main displayable object of MIDlet*/
public void home()
{
while (elementCount > 1)
pop();
display.setCurrent(mainDisplayable);
}
/* Pop displayable from stack and set as active */
public void popDisplayable()
{
// If the stack is not empty, pop next displayable
if (empty() == false)
display.setCurrent((Displayable)pop());
else
// On error show an alert
// Once acknowldeged, set 'mainDisplayable' as active
display.setCurrent(alStackError, mainDisplayable);
}
}
class FormAdd extends Form implements CommandListener
{
private Command cmBack,cmSave;
protected TextField tfName;
protected TextField tfAddress;
protected TextField tfPhonenum;
//protected ChoiceGroup cgPriority;
private RemoveRecordDemo midlet;
public FormAdd(String title, RemoveRecordDemo midlet)
{
// Call the Form constructor
super(title);
// Save reference to MIDlet so we can access
// the display manager class and rms
this.midlet = midlet;
//Initailizing Command buttons
cmSave = new Command("Save", Command.SCREEN, 1);
cmBack = new Command("Back", Command.BACK, 2);
// Create textfield for entering todo items
tfName = new TextField
("Name", null, 15, TextField.ANY);
tfAddress = new TextField
("Address", null, 250, TextField.ANY);
tfPhonenum = new TextField
("Phonenum", null, 15, TextField.PHONENUMBER);
// Create choicegroup and append options (no images)
//cgPriority = new ChoiceGroup("Priority", Choice.EXCLUSIVE);
// Add buttons to form
addCommand(cmSave);
addCommand(cmBack);
append(tfName);
append(tfAddress);
append(tfPhonenum);
setCommandListener(this);
}
public void commandAction(Command c, Displayable s)
{
if (c == cmSave)
{
// Add a new todo item
/* Notice we bump priority by 1. This is because the choicegroup entries start at zero. We would like the records in the rms to store priorities starting at 1. Thus, if a user requests to display priorities on the todo list, the highest priority is 1 (not zero)*/
midlet.addOPItem(tfName.getString(), tfAddress.getString(), tfPhonenum.getString());
}
// Any other event and we go back to the main form...
// Pop the last displayable off the stack
midlet.displayMgr.popDisplayable();
}
}
class ComparatorInt implements RecordComparator
{
// Read from a specified byte array
private byte[] record = new byte[10];
// Read Java data types from the above byte array
private ByteArrayInputStream strmBytes = null;
private DataInputStream strmDataType = null;
public void compareIntClose()
{
try
{
if (strmBytes != null)
strmBytes.close();
if (strmDataType != null)
strmDataType.close();
}
catch (Exception e)
{}
}
public int compare(byte[] rec1, byte[] rec2)
{
int x1, x2;
try
{
//If either record is larger than our buffer, reallocate
int maxsize = Math.max(rec1.length, rec2.length);
if (maxsize > record.length)
record = new byte[maxsize];
// Read record #1
// We want the priority which is first "field"
strmBytes = new ByteArrayInputStream(rec1);
strmDataType = new DataInputStream(strmBytes);
x1 = strmDataType.readInt();
// Read record #2
strmBytes = new ByteArrayInputStream(rec2);
strmDataType = new DataInputStream(strmBytes);
x2 = strmDataType.readInt();
// Compare record #1 and #2
if (x1 == x2)
return RecordComparator.EQUIVALENT;
else if (x1 < x2)
return RecordComparator.PRECEDES;
else
return RecordComparator.FOLLOWS;
}
catch (Exception e)
{
return RecordComparator.EQUIVALENT;
}
}
}
class OPItem
{
private String phonenum;
private String Name;
private String Address;
private int recordId;
public OPItem(String Name,String Address, String phonenum, int recordId)
{
this.Name = Name;
this.Address = Address;
this.phonenum = phonenum;
this.recordId = recordId;
}
public String getphonenum()
{
return phonenum;
}
public void setphonenum(String phonenum)
{
// setphonenum
this.phonenum = phonenum;
}
public String getAddress()
{
//getAddress
return Address;
}
public void setAddress(String Address)
{
// setAddress
this.Address = Address;
}
public String getName()
{
// getName
return Name;
}
public void setName(String Name)
{
// setName
this.Name = Name;
}
public int getRecordId()
{
// getRecordId
return recordId;
}
}
No comments:
Post a Comment