Please help me fix this code
Please help me in this area of code.
i used three record store for each form
I want expenses button to display all the expenses for the selected trip.
i want to use image in the background of the forms in this code
i want to sum all expenses amount for a particular trip selected.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
/**
* @author Oluwadawen
*/
public class MXpence extends MIDlet implements CommandListener {
String[] strTempIDTrip;
Display mDisplay;
Form basicDetails, tripDetails, search, expensesDetails;
TextField staffId, staffName, staffDepartment, tripPurpose, tripDate, listPurpose;
TextField amountExpenses, detailsExpenses, addComments;
TextBox welcome, confirma;
RecordStore rs, rsa, rss;
Alert mCfmAlert, confirmed, Check, Confirm, Check2, Confirm2, delete;
Alert error, errora, Alerta;
DateField dtfDOT, timeExpenses;
List mList, sList, dList;
Ticker ticker;
StringItem constraint, constraints, dis;
Vector mvector;
ChoiceGroup typeExpenses;
public MXpence() throws IOException {
Display mDisplay = Display.getDisplay(this);
//****************************************This Command for Welcome Screen************************************************************************
//welcome = new TextBox("Welcome (Set your trip expenses)", "Please fill all required field \n \n * This signify required field(s)", 200, TextField.UNEDITABLE);
//*********************************************This code section is for List All Trip Expenses Screen***********************************
mList = new List("All Trip Details", List.EXCLUSIVE);
mList.addCommand(new Command("OK", Command.OK, 0));
mList.addCommand(new Command("Delete", Command.SCREEN, 1));
mList.addCommand(new Command("Add Expenses", Command.SCREEN, 2));
mList.addCommand(new Command("Delete All", Command.SCREEN, 3));
mList.setCommandListener(this);
mvector = new Vector();
///*********************************************This code section is for Searching for a Trip to know its Expenses***********************
sList = new List("Search for a Trip", List.EXCLUSIVE);
sList.addCommand(new Command("Ok", Command.OK, 1));
sList.addCommand(new Command("Expenses", Command.SCREEN, 0));
//sList.addCommand(new Command("Look Back", Command.BACK, 0));
sList.setCommandListener(this);
//*********************************************This code section is for Displaying a Trip Expenses**************************************
dList = new List("Your Trip Expenses", List.EXCLUSIVE);
dList.addCommand(new Command("Finish", Command.OK, 0));
dList.addCommand(new Command("Delete Expenses", Command.SCREEN, 1));
dList.addCommand(new Command("Sum Expenses", Command.SCREEN, 2));
dList.setCommandListener(this);
//**************************************************This Codes Section for Basic Details Screen***************************************************
basicDetails = new Form("MXpence Application");
ticker = new Ticker("Welcome User! Make your trip expenses easier");
basicDetails.setTicker(ticker);
constraint = new StringItem("* This signify required field(s)", "", StringItem.PLAIN);
staffId = new TextField("*Staff Id:", "", 100, TextField.ANY);
staffName = new TextField("*Staff Name:", "", 200, TextField.ANY);
staffDepartment = new TextField("Staff Department(optional):", "", 100, TextField.ANY);
staffId.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
staffName.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
staffDepartment.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
basicDetails.append(constraint);
basicDetails.append(staffId);
basicDetails.append(staffName);
basicDetails.append(staffDepartment);
basicDetails.addCommand(new Command("Exit", Command.EXIT, 0));
basicDetails.addCommand(new Command("Next", Command.SCREEN, 0));
basicDetails.setCommandListener(this);
mCfmAlert = new Alert("", "", Image.createImage("/confirm.jpg"), null);
mCfmAlert.setTimeout(Alert.FOREVER);
mCfmAlert.addCommand(new Command("Confirm", Command.SCREEN, 0));
mCfmAlert.addCommand(new Command("Cancel", Command.BACK, 0));
mCfmAlert.setCommandListener(this);
try {
confirmed = new Alert("Data Saved! \n ", "", null, AlertType.CONFIRMATION);
confirmed.setTimeout(5000);
} catch (Exception e) {
}
try {
error = new Alert("", "One of the fiels is Empty", Image.createImage("/alert.jpg"), null);
error.setTimeout(Alert.FOREVER);
} catch (Exception e) {
}
//*************************************************This code section is for Trip Details Screen**************************************************
tripDetails = new Form("Enter your trip details");
tripPurpose = new TextField("*Purpose of the Trip:", "", 1000, TextField.ANY);
dtfDOT = new DateField("*Date of the Trip:", DateField.DATE);
dtfDOT.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
tripPurpose.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
dtfDOT.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
Check = new Alert("Confirmation!", "", null, AlertType.CONFIRMATION);
Check.setTimeout(Alert.FOREVER);
Check.addCommand(new Command("Back", Command.BACK, 0));
Check.addCommand(new Command("Add", Command.SCREEN, 0));
Check.setCommandListener(this);
try {
Confirm = new Alert("Data Saved! \n ", "", null, AlertType.CONFIRMATION);
Confirm.setTimeout(Alert.FOREVER);
} catch (Exception e) {
}
try {
delete = new Alert("Confirmation!", "", null, AlertType.CONFIRMATION);
delete.setTimeout(Alert.FOREVER);
} catch (Exception e) {
}
tripDetails.append(tripPurpose);
tripDetails.append(dtfDOT);
tripDetails.addCommand(new Command("Save", Command.SCREEN, 0));
tripDetails.addCommand(new Command("Close", Command.SCREEN, 1));
tripDetails.addCommand(new Command("Search for trip", Command.SCREEN, 2));
tripDetails.addCommand(new Command("List All Trip", Command.SCREEN, 3));
tripDetails.setCommandListener(this);
//********************************************This code section is for Search for Trip Expenses Screen*****************************************************
search = new Form("Search for Expenses");
listPurpose = new TextField("Enter Trip Purpose:", "", 1000, TextField.ANY);
listPurpose.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
search.append(listPurpose);
search.addCommand(new Command("Back", Command.BACK, 0));
search.addCommand(new Command("Search", Command.SCREEN, 0));
search.setCommandListener(this);
//*********************************************This code section is for Delete Alert****************************************************
try {
Alerta = new Alert("Deleted! \n ", "", null, AlertType.ERROR);
Alerta.setTimeout(1000);
} catch (Exception e) {
}
//**************************This is for adding expenses details***************************************************************************
expensesDetails = new Form("Enter details of expenses");
String tExp[]={"Select...","Food","Transport","Others"};
typeExpenses=new ChoiceGroup("Type of expenses",ChoiceGroup.POPUP,tExp,null);
//typeExpenses = new TextField("*Type of Expenses:", "", 1000, TextField.ANY);
amountExpenses = new TextField("*Amount of Expenses:", "", 100, TextField.NUMERIC);
detailsExpenses = new TextField("*Details of Expenses:", "", 1000, TextField.ANY);
addComments = new TextField("Additional Comments(optional):", "", 1000, TextField.ANY);
timeExpenses = new DateField("*Time of Expenses:", DateField.TIME);
constraints = new StringItem("* This signify required field(s)", "", StringItem.PLAIN);
dis = new StringItem("", "", StringItem.PLAIN);
timeExpenses.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
typeExpenses.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
amountExpenses.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
detailsExpenses.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
//timeExpenses = new TextField("*Type of Expenses:", "", 1000, TextField.ANY);
addComments.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_BOTTOM);
expensesDetails.append(dis);
expensesDetails.append(constraints);
expensesDetails.append(typeExpenses);
expensesDetails.append(amountExpenses);
expensesDetails.append(detailsExpenses);
expensesDetails.append(timeExpenses);
expensesDetails.append(addComments);
expensesDetails.addCommand(new Command("Back", Command.BACK, 0));
expensesDetails.addCommand(new Command("Insert", Command.SCREEN, 0));
expensesDetails.setCommandListener(this);
Check2 = new Alert("Confirmation!", "", null, AlertType.CONFIRMATION);
Check2.setTimeout(Alert.FOREVER);
Check2.addCommand(new Command("Get Back", Command.BACK, 0));
Check2.addCommand(new Command("Correct", Command.BACK, 0));
Check2.setCommandListener(this);
try {
Confirm2 = new Alert("Data Saved! \n ", "One of the fiels is Empty", null, AlertType.CONFIRMATION);
Confirm2.setTimeout(5000);
} catch (Exception e) {
}
try {
errora = new Alert("Error! \n ", "One of the fiels is Empty", Image.createImage("/alert.jpg"), null);
errora.setTimeout(Alert.FOREVER);
} catch (Exception e) {
}
}
public void startApp() {
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(basicDetails);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
//***********************This Commands is for Exiting Welcome Screen***********************************************
if (c.getLabel().equals("Exit")) {
notifyDestroyed();
}
//***********************This Commands is for initializing Basic Details Screen**********************************************************
if (c.getLabel().equals("Next")) {
checkBasicDetails();
}
if (c.getLabel().equals("Confirm")) {
addBasicDetails();
}
if (c.getLabel().equals("Cancel")) {
mDisplay.setCurrent(basicDetails);
}
//***********************This Commands is for Saving Trip Details*************************************************************************
if (c.getLabel().equals("Save")) {
checkTripDetails();
}
if (c.getLabel().equals("Add")) {
addTripDetails();
}
if (c.getLabel().equals("Back")) {
mDisplay.setCurrent(tripDetails);
}
if (c.getLabel().equals("Close")) {
staffId.setString("");
staffName.setString("");
staffDepartment.setString("");
mDisplay.setCurrent(basicDetails);
}
//***********************This Commands is for Listing all trip to either add Expenses or delete a trip***********************************************
if (c.getLabel().equals("List All Trip")) {
listAllTrips();
}
if (c.getLabel().equals("OK")) {
mDisplay.setCurrent(tripDetails);
}
//*************************************This command is to delete a specific trip out of all trips***************************************
if (c.getLabel().equals("Delete")) {
deleteSingleTrip();
}
//****************************************This is command is for Adding Expenses for a particular trip selected***************************
if (c.getLabel().equals("Add Expenses")) {
mDisplay.setCurrent(expensesDetails);
}
if (c.getLabel().equals("Insert")) {
checkTripExpenses();
}
if (c.getLabel().equals("Correct")) {
addTripExpenses();
}
if (c.getLabel().equals("Get Back")) {
mDisplay.setCurrent(expensesDetails);
}
if (c.getLabel().equals("Previous")) {
mDisplay.setCurrent(tripDetails);
}
//***********************This Commands is for Searching for a Trip to add Expenses*******************************************************
if (c.getLabel().equals("Search for trip")) {
listPurpose.setString("");
mDisplay.setCurrent(search);
}
if (c.getLabel().equals("Search")) {
searchForTrip();
}
if (c.getLabel().equals("Back")) {
mDisplay.setCurrent(tripDetails);
}
if (c.getLabel().equals("Ok")) {
mDisplay.setCurrent(search);
}
//***********************This Commands is for Displaying Expenses for a particular trip***********************************************
if (c.getLabel().equals("Expenses")) {
displayExpenses();
}
if (c.getLabel().equals("Sum Expenses")) {
sumAllExpensesForTrip();
}
if (c.getLabel().equals("Finish")) {
mDisplay.setCurrent(sList);
}
if (c.getLabel().equals("Delete Expenses")) {
deleteExpenses();
}
//***********************This Commands is for Deleting All the Trips Records***********************************************
if (c.getLabel().equals("Delete All")) {
deleteAllTrip();
}
}
private void checkBasicDetails() {
String stId = staffId.getString();
String stName = staffName.getString();
boolean checking = true;
if (stId == null || stId.trim().equals("")) {
error.setString(stId);
error.setString("Staff Id cannot be empty");
mDisplay.setCurrent(error);
checking = false;
} else if (stName == null || stName.trim().equals("")) {
error.setString(stName);
error.setString("Staff Name cannot be empty");
mDisplay.setCurrent(error);
checking = false;
}
if (checking) {
String stIds = staffId.getString();
String stNames = staffName.getString();
String stDepts = staffDepartment.getString();
// display the details to the user
mCfmAlert.setString("Details Entered \n Staff Id:" + stIds + "\nStaff Name:" + stNames + "\nStaff Department:" + stDepts);
mDisplay.setCurrent(mCfmAlert);
}
}
private void addBasicDetails() {
try {
if (rs == null) {
rs = RecordStore.openRecordStore("DataManagement", true);
}
String dataToadd = staffId.getString() + "," + staffName.getString() + "," + staffDepartment.getString();
//String dataToadd = staffId.getString() + ";" + staffName.getString()+ ";" + staffDepartment.getString();
byte dataInByte[] = dataToadd.getBytes();
rs.addRecord(dataInByte, 0, dataInByte.length);
confirmed.setString("Data is saved. \n Total number of record is " + String.valueOf(rs.getNumRecords()));
mDisplay.setCurrent(confirmed, tripDetails);
rs.closeRecordStore();
rs = null;
//mDisplay.setCurrent();
} catch (RecordStoreException ex) {
}
}
private void checkTripDetails() {
String tpp = tripPurpose.getString();
String dot = dtfDOT.getDate().toString();
boolean checking = true;
if (tpp == null || tpp.trim().equals("")) {
error.setString(tpp);
error.setString("Purpose of the Trip cannot be empty");
mDisplay.setCurrent(error);
checking = false;
} else if (dot == null || dot.trim().equals("")) {
error.setString(dot);
error.setString("Date of trip cannot be empty");
mDisplay.setCurrent(error);
checking = false;
}
if (checking) {
String tpps = tripPurpose.getString();
String dots = dtfDOT.getDate().toString();
// display the details to the user
Check.setString("Details Entered \nPurpose of the trip:" + tpps + "\nDate of the trip:" + dots);
mDisplay.setCurrent(Check);
}
}
private void addTripDetails() {
try {
if (rsa == null) {
rsa = RecordStore.openRecordStore("DataTripManagement", true);
}
String dataToadd = tripPurpose.getString() + "," + dtfDOT.getDate().toString();
byte dataInByte[] = dataToadd.getBytes();
rsa.addRecord(dataInByte, 0, dataInByte.length);
Confirm.setString("Data is saved. \n Total number of record is " + String.valueOf(rsa.getNumRecords()));
mDisplay.setCurrent(Confirm, tripDetails);
rsa.closeRecordStore();
rsa = null;
} catch (RecordStoreException ex) {
}
tripPurpose.setString("");
dtfDOT.setDate(null);
}
//i mean i did not use the mvector to save the id of the trip
private void listAllTrips() {
mList.deleteAll();
try {
if (rsa == null) {
rsa = RecordStore.openRecordStore("DataTripManagement", true);
}
RecordEnumeration re = rsa.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
int id = re.nextRecordId();
byte[] inByteFormat = rsa.getRecord(id);
String record = new String(inByteFormat);
mList.append("no." + id + "\n" + record, null);
}
rsa.closeRecordStore();
rsa = null;
mDisplay.setCurrent(mList);
} catch (Exception e) {
}
}
private void deleteSingleTrip() {
try {
if (rsa == null) {
rsa = RecordStore.openRecordStore("DataTripManagement", true);
}
int selectedIndex = mList.getSelectedIndex();
int ID = ((Integer) mvector.elementAt(selectedIndex)).intValue();
rsa.deleteRecord(ID);
mList.delete(selectedIndex);
Alert delAlert = new Alert("Data has been removed");
mDisplay.setCurrent(delAlert, mList);
} catch (Exception e) {
e.printStackTrace();
}
}
private void deleteAllTrip() {
try {
RecordStore.deleteRecordStore("DataTripManagement");
Alerta.setString("All trip details deleted");
mDisplay.setCurrent(Alerta, tripDetails);
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkTripExpenses() {
int cindex = typeExpenses.getSelectedIndex();
String tpE = typeExpenses.getString(cindex);
String amtE = amountExpenses.getString();
String tmE = timeExpenses.getDate().toString();
boolean checking = true;
if (tpE == null || tpE.trim().equals("Select...")) {
error.setString(tpE);
error.setString("Type of expenses cannot be Select...");
mDisplay.setCurrent(error);
checking = false;
} if (amtE == null || amtE.trim().equals("")) {
error.setString(amtE);
error.setString("Amount of expenses cannot be empty");
mDisplay.setCurrent(error);
checking = false;
} else if (tmE == null || tmE.trim().equals("")) {
error.setString(tmE);
error.setString("Time of expenses cannot be empty");
mDisplay.setCurrent(error);
checking = false;
}
if (checking) {
String tpEs = typeExpenses.getString(cindex);
String amtEs = amountExpenses.getString();
String dtEs = detailsExpenses.getString();
String tmEs = timeExpenses.getDate().toString();
String adCs = addComments.getString();
// display the details to the user
Check2.setString("Details Entered \nType of Expenses: " + tpEs + "\nAmount of Expenses: " + amtEs + "\nDetails of Expenses: " + dtEs + "\nTime of Expenses: " + tmEs + "\nAdditional Comments: " + adCs);
mDisplay.setCurrent(Check2);
}
}
private void addTripExpenses() {
try {
if (rss == null) {
rss = RecordStore.openRecordStore("DataExpensesManagement", true);
}
int tyname = typeExpenses.getSelectedIndex();
String dataToadd = typeExpenses.getString(tyname) + "," + amountExpenses.getString() + "," + detailsExpenses.getString() + "," + timeExpenses.getDate().toString() + "," + addComments.getString();
byte dataInByte[] = dataToadd.getBytes();
rss.addRecord(dataInByte, 0, dataInByte.length);
Confirm2.setString("Data is saved. \n Total number of record is " + String.valueOf(rss.getNumRecords()));
rss.closeRecordStore();
rss = null;
mDisplay.setCurrent(Confirm2, mList);
} catch (RecordStoreException ex) {
}
//typeExpenses.setString("");
amountExpenses.setString("");
detailsExpenses.setString("");
timeExpenses.setDate(null);
addComments.setString("");
}
private void searchForTrip() {
try {
if (rsa == null) {
rsa = RecordStore.openRecordStore("DataTripManagement", true);
}
RecordEnumeration re = rsa.enumerateRecords(null, null, true);
int n = re.numRecords();
strTempIDTrip = new String[n];
sList.deleteAll();
int i = 0;
while (re.hasNextElement()) {
int ID = re.nextRecordId();
String data = new String(rsa.getRecord(ID));
String productN = data.substring(0, data.indexOf(","));
if (productN.indexOf(listPurpose.getString()) != -1) {
sList.append("no." + ID + "\n" + data, null);
strTempIDTrip[i] = ID+"";
i++;
}
}
mDisplay.setCurrent(sList);
rsa.closeRecordStore();
rsa = null;
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
private void displayExpenses() {
dList.deleteAll();
try {
int selectedIndex = sList.getSelectedIndex();
int ID = Integer.parseInt(strTempIDTrip[selectedIndex]);
String idd = String.valueOf(ID);
if (rss == null) {
rss = RecordStore.openRecordStore("DataExpensesManagement", true);
}
RecordEnumeration re = rss.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
int id = re.nextRecordId();
String data = new String(rss.getRecord(id));
//String expensesN = data.substring(0, data.indexOf(","));
if (data.indexOf(idd) != -1) {
//mvector.addElement(new Integer(id));
dList.append(data, null);
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
} finally {
try {
rss.closeRecordStore();
rss = null;
} catch (RecordStoreException recordStoreException) {
}
}
this.mDisplay.setCurrent(dList);
}
private void deleteExpenses() {
try {
if (rss == null) {
rss = RecordStore.openRecordStore("DataExpensesManagement", true);
}
int selectedIndex = dList.getSelectedIndex();
int ID = ((Integer) mvector.elementAt(selectedIndex)).intValue();
rss.deleteRecord(ID);
dList.delete(selectedIndex);
Alert deleAlert = new Alert("Data has been removed");
mDisplay.setCurrent(deleAlert, dList);
} catch (Exception e) {
e.printStackTrace();
}
}
private void sumAllExpensesForTrip() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
View Answers