RMSTutorial

Contents


1. Introduction to RMS:

Small computing devices do not have a robust file system and therefore are unable to store information in the manner that we are accustomed to when working with a PC, server, and other traditional computing devices. The Record Management System (RMS) provides a file system–like environment that is used to store and maintain persistence in a small computing device. We can insert records, read records, search for particular records, and sort records stored by the RMS.

RMS stores information in a record store. A record store is a collection of records organized as rows (records) and columns (fields). RMS automatically assigns to each row a unique integer that identifies the row in the record store, which is called the record ID.

2. Aim:

To create a simple application in J2ME that will provide facility for storing expenses. The application will provide options for storing the reason for expens and the amount spent using an easy interface.


3. Tools Needed:

  1. NetBeans IDE 6.0/ 6.1
  2. Mobility Pack.

4. Creating the basic classes:

Create New MIDP application. Name it as ExpensesApp.

I. A HelloMIDlet.java file will be created in the src\hello\ package.

II. Create a new java file: Expenses.java.

III. Paste the following code there:


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
/**
* This class is the base for handling the storage part of our
* Expenses application
*
* It handles all the writing and reading of the data from
* record store.
*
* An instance of this class will be created in the main Midlet
*
* @author jay
*/
public class Expenses {

/**
* The reason for which the expense was incurred
*/
String Reason = null;

/**
* The Amount of money that spent for the particular expense
*/
int Amount ;

/**
* Method to write the st to disk
*/
void writeToDisk() throws IOException {
try {
RecordStore ExpenseRecord = RecordStore.openRecordStore("ExpRecord", true);
//Creating output streams
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
//Writing value to be saved to output stream
os.writeUTF(this.Reason);
os.writeInt(this.Amount);

//close stream
os.close();

byte[] data = baos.toByteArray();

//Write the record to the record store
int id = ExpenseRecord.addRecord(data, 0, data.length);
//Close Record store
ExpenseRecord.closeRecordStore();
} catch (RecordStoreFullException ex) {
ex.printStackTrace();
} catch (RecordStoreNotFoundException ex) {
ex.printStackTrace();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}

/**
*Method to read record from disk and
* initialize values of member variables
* @param RecordID the id of the record to be read
*/
int readFromDisk(int RecordID) throws IOException {
boolean flag = true;
while (flag) {
try {
RecordStore ExpenseRecord = RecordStore.openRecordStore("ExpRecord",false);
//read get the record, specified by RecordID in a byte array
byte[] data = ExpenseRecord.getRecord(RecordID);


//If record is read, RecordID is valid and flag
//needs to be reset
flag = false;

DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));

//retrieve

this.Reason = is.readUTF();
this.Amount = is.readInt();


//close stream
is.close();
ExpenseRecord.closeRecordStore();
} catch (RecordStoreFullException ex) {
ex.printStackTrace();
} catch (RecordStoreNotFoundException ex) {

ex.printStackTrace();
} catch (InvalidRecordIDException ex) {
//If invalid recordID, it means that the
//the record was deleted, and we need to search
//in next id for a proper record.
RecordID++;
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
//Return the ID of next record
return (++RecordID);
}
}

You may want to add functionality to store date, place etc. Make the corresponding entries here.

IV. Create a new java file: ManageExpenses.java.

package hello;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

/**
* This class is the base for handling the storage part of our
* Expenses application
*
* An instance of this class will be created in the main Midlet
*
* @author jay
*/

public class ManageExpenses {

/**
* The array corresponding that will store myExpense objects
*/

Expenses myExpense[] = null;


/**
* count: To indicate the total number of records
* added: To indicate number of records added since the midlet
* was last opened
*/
int count;

int added;


/**
* Constructor.
*/

ManageExpenses(){
myExpense = new Expenses[20];
count = 0;
added = 0;
};

/**
* Method to write the current state of Expense object
* to a record store.
* This is similar to Serialization of an object
*/
void writeToDisk() {
for (int i = count-added; i < count; i++) {
try {
myExpense[I].writeToDisk();
} catch (IOException ex) {

}
}
}


/**
* Method to read from disk and initialize Expense object
* It is like Deserialization
*/
void readFromDisk() {
try {

//Open Record Store
RecordStore SubjectRecord = RecordStore.openRecordStore("ExpRecord", false);

//Get the length of Record Store in a Length member variable
int Length = SubjectRecord.getNumRecords();

//Start From RecordID 1
int RecordID = 1;

for (int i = 0; i < Length; i++) {
myExpense[I] = new Expenses();
RecordID = myExpense[I].readFromDisk(RecordID);
count++;

}
//Close Record Store
SubjectRecord.closeRecordStore();

} catch (IOException ex) {
ex.printStackTrace();
} catch (RecordStoreFullException ex) {
ex.printStackTrace();
} catch (RecordStoreNotFoundException ex) {
ex.printStackTrace();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}

/**

* @param r

* @param a

* @param d

*

* To add a Expense entry in the list

*/

void addExpense(String r, String a)

{

Expenses e = new Expenses();

//e.id = 0;

e.Reason = r;

e.Amount = Integer.parseInt(a);

myExpense[Count++] = e;

added++;

}

}

5. Creating the basic display containers:

I. Open the HelloMIDlet.java and click on the Flow tab you will find something like this:

File:StartPage_RMSTutorial.JPG


II. Now go to the palette window and select the list option. Add it to the file by clicking anywhere on the Flow tab as shown:

File:AddList_RMSTutorial.JPG


Rename the List name as well as the instance name as menu. This can be done by right clicking on the list and selecting properties option. This list will be used to display the main menu.

III. Now create an exitCommand for the Menu List Item. This can be done by:

RightClick >> New/Add >> exitCommand as shown:

File:AddCommand_RMSTutorial.JPG

IV. Similarly add two List Elements to Menu and rename them as Add Expense and View Expenses.

V. Add another List item and rename it as viewExpense. Add backCommand to it. Rename it as backCommandView.

Go to Screen view.

Right-click and add two text-fields.

Set their instance names and Labels to “Reason” and “Amount”.

File:ScreenView_RMSTutorial.JPG

VI. Now go to the Palette tab and add a new Form.

Rename it as addExpenseForm.

Give the title as Add Expense.

Add cancelCommand and okCommand to the form in similar way as described before.

Rename them as cancelCommandAddForm and okCommandAddForm.

VII. Add a splashScreen from the palette.

Right click splashScreen >> Select Edit.

There set the message to be displayed as “Expense added successfully!”

VIII. Finally delete the form that was previously present.

The layout will be as follows:

File:View1_RMSTutorial.JPG

6. Linking the display items:

Now we come to the most interesting and easy part of creating the application. We will link the display items that we created. This is an easy drag and drop process.

Left click on the event and drag it to the item you want to be displayed on occurrence of that event. The display item will be automatically linked with the event.

Example: On the Started event of Mobile Device item, we want the menu to be displayed.

So, right-click on Started button and drag it to menu.

Create following links in a similar fashion:

Event Display Item
addExpenseForm: cancelCommandaddForm() menu
Mobile Device:Started()
menu
menu:exitCommandMenu()
Mobile Device
menu: Add Expense() addExpenseForm
addExpenseForm: okCommandAddForm() splashScreen
splashScreen: DISMISS_COMMAND Menu
menu: View Expenses() viewExpenses
viewExpenses: backCommandView() Menu


The window will look like as follows:

(Note: To get the items arranged in a systematic fashion, click on the button highlighted in red circle in figure)

File:View2_RMSTutorial.JPG

7. Adding RMS functionality in the event handlers using the classes created.

I. Open the Source Tab.

II. Add private variables :

//To indicate that application has started for first time

private boolean flag = false;

//Array of expenses

private ManageExpenses myexp = new ManageExpenses();

III. In the exitMIDlet() function add the following code:

try {
myexp.writeToDisk();
} catch (Exception ex) {
ex.printStackTrace();
}

IV. Go to Generate Method: menuAction.

Place the code below as shown in next figure


if(!flag)
{
myexp.readFromDisk();
flag = true;
}
for (int i = viewExpense.size();  i < myexp.count;i++)
{
viewExpense.append(myexp.myExpense[[I | i]].Reason+" : "+myexp.myExpense[I].Amount , null);
}

File:Code1_RMSTutorial.JPG

V. Similarly expand Generate Method: CommandAction for Displayables.

Add following code as shown.

myexp.addExpense(Reason.getString(),Amount.getString() ); Amount.setString(""); Reason.setString("");

File:Code2_RMSTutorial.JPG

8. Final Step: Compile and run the project.

You can see following output when you launch the Midlet.

File:Output1_RMSTutorial.JPG

9. Future scope:

The functionality can be extended so as to store expenses according to date, give useful information from the stored expenses data like expenses for a particular month etc.

Contributed by: Jay Mahadeokar

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo