Hi!
I am tryig to write a simple j2me MIDlet program.
Using that program, a user has to fill the required information to make an air reservation. And he also can see the resersation list.
I used the Recordstore to write and read again that reservation list. But my program does not show any list. I think It even does not write the list in the recordstore.
Where is the error?
Here is my program.
//ZSecondReservationMIDlet.java
package zsecondreservation;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ZSecondReservationMIDlet extends MIDlet {
static ZSecondReservationMIDlet zreservationmidlet;
ZSecondReservationMainform zreservationmainform=new ZSecondReservationMainform("login From");
Display display=Display.getDisplay(this);
public ZSecondReservationMIDlet() {
ZSecondReservationMIDlet.zreservationmidlet=this;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(zreservationmainform);
// TODO Auto-generated method stub
}
}
/////ReservationForm.java
package zsecondreservation;
import javax.microedition.lcdui.*;
//import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
public class ReservationForm extends Form{
TextField tfName=new TextField("Name","",7,TextField.ANY);
ChoiceGroup cgFrom=new ChoiceGroup("From",ChoiceGroup.POPUP);
ChoiceGroup cgTo=new ChoiceGroup("To",ChoiceGroup.POPUP);
DateField cgDate=new DateField("Date",DateField.DATE_TIME);
ChoiceGroup cgTime=new ChoiceGroup("Time",ChoiceGroup.EXCLUSIVE);
ChoiceGroup cgFood=new ChoiceGroup("Food",ChoiceGroup.EXCLUSIVE);
TextField tfMobile=new TextField("Mobile no","",9,TextField.PHONENUMBER);
Command cmdLogout=new Command("Logout",Command.SCREEN,0);
Command cmdBook=new Command("Book",Command.SCREEN,0);
Command cmdDisplayBookingList=new Command("DisplayBookingList",Command.SCREEN,0);
public ReservationForm(String s) {
super(s);
append(tfName);
cgFrom.append("Singapore",null);
cgFrom.append("Bangkok",null);
append(cgFrom);
cgTo.append("Tokyo",null);
cgTo.append("Seoul",null);
append(cgTo);
append(cgDate);
cgTime.append("Morning", null);
cgTime.append("Evening", null);
append(cgTime);
cgFood.append("Veg",null);
cgFood.append("non-veg",null);
append(cgFood);
append(tfMobile);
addCommand(cmdLogout);
addCommand(cmdBook);
addCommand(cmdDisplayBookingList);
setCommandListener(new ReservationCommandHandler());
}
}
//////ReservationCommandHandler.java
package zsecondreservation;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
public class ReservationCommandHandler implements CommandListener {
public ReservationCommandHandler() {
// TODO Auto-generated constructor stub
}
public void commandAction(Command c, Displayable d) {
ReservationForm reservationform=(ReservationForm)d;
//ZSecondReservationMainform zreservationmainform=new ZSecondReservationMainform("login From");
RecordStore recordstore=null;
RecordEnumeration recordenumeration=null;
if(c==reservationform.cmdLogout)
{
ZSecondReservationMainform zreservationmainform=new ZSecondReservationMainform("login From");
Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
display.setCurrent(zreservationmainform);
}
if(c==reservationform.cmdBook)
{
try {
recordstore=RecordStore.openRecordStore("reservation list",true);
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Alert alert=new Alert("Error Creating",e.toString(),null,AlertType.ERROR);
Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
display.setCurrent(alert);
}
byte[] OutputRecord;
ByteArrayOutputStream outputstream=new ByteArrayOutputStream();
DataOutputStream outputdatastream=new DataOutputStream(outputstream);
try {
outputdatastream.writeUTF(reservationform.tfName.getString());
outputdatastream.writeInt(reservationform.cgFrom.getSelectedIndex());
outputdatastream.writeInt(reservationform.cgTo.getSelectedIndex());
outputdatastream.flush();
OutputRecord=outputstream.toByteArray();
recordstore.addRecord(OutputRecord, 0, OutputRecord.length);
Alert alert=new Alert("Reservation completed","Have a nice trip!",null,AlertType.INFO);
Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
display.setCurrent(alert);
outputstream.reset();
outputstream.close();
outputdatastream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Alert alert=new Alert("Error writing",e.toString(),null,AlertType.ERROR);
Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
display.setCurrent(alert);
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(c==reservationform.cmdDisplayBookingList)
{
Alert alert=new Alert("*******","****",null,AlertType.INFO);////////////////
Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);///////////
display.setCurrent(alert);/////////////
try {
StringBuffer buffer=new StringBuffer();
recordstore=RecordStore.openRecordStore("reservation list",false);
recordenumeration=recordstore.enumerateRecords(null, null, true);
byte[] byteinputdata=new byte[300];
ByteArrayInputStream inputstream=new ByteArrayInputStream(byteinputdata);
DataInputStream datainputstream=new DataInputStream(inputstream);
while(recordenumeration.hasNextElement())
{
recordstore.getRecord(recordenumeration.nextRecordId(),byteinputdata, 0);
try {
buffer.append(datainputstream.readUTF());
buffer.append("\n");
buffer.append(datainputstream.readInt());
buffer.append("\n");
buffer.append(datainputstream.readInt());
buffer.append("\n");
alert=new Alert("Reservatin List",buffer.toString(),null,AlertType.INFO);
display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
display.setCurrent(alert);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
////ReservationMainForm.java
package zsecondreservation;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
public class ZSecondReservationMainform extends Form{
TextField txtname=new TextField("Name","",7,TextField.ANY);
TextField txtpassword=new TextField("Password","",7,TextField.PASSWORD);
Command cmdlogin=new Command("Login",Command.SCREEN,1);
Command cmdexit=new Command("Exit",Command.EXIT,0);
public ZSecondReservationMainform(String title) {
super(title);
append(txtname);
append(txtpassword);
addCommand(cmdlogin);
addCommand(cmdexit);
setCommandListener(new LoginCommandHandlar());
// TODO Auto-generated constructor stub
}
}
///////LoginCommandHandlar.java
package zsecondreservation;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
public class LoginCommandHandlar implements CommandListener {
//Alert alert;
//Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
public LoginCommandHandlar() {
// TODO Auto-generated constructor stub
}
public void commandAction(Command c, Displayable d) {
ZSecondReservationMainform zloginform =(ZSecondReservationMainform)d;
if(c==zloginform.cmdexit)
{
ZSecondReservationMIDlet.zreservationmidlet.notifyDestroyed();
}
// TODO Auto-generated method stub
if(c==zloginform.cmdlogin)
{
Display display=Display.getDisplay(ZSecondReservationMIDlet.zreservationmidlet);
Alert alert;
if(zloginform.txtname.getString().equals("name")&&zloginform.txtpassword.getString().equals("password"))
{
ReservationForm reservationform= new ReservationForm("Reservation Form");
alert=new Alert("Correct","Login is successful",null,AlertType.INFO);
display.setCurrent(alert,reservationform);
}
else
{
alert=new Alert("Incorrect","Try again",null,AlertType.ERROR);
zloginform.txtname.setString("");
zloginform.txtpassword.setString("");
display.setCurrent(alert);
}
//display.setCurrent(alert);
}
}
}