Skip to main content

Using Textbox to compute sample operation...

6 replies [Last post]
amuche
Offline
Joined: 2011-09-05

please i want to try adding numbers into two textboxes using lwuit 1.5 textboxes. what i want to do is input a number in the first textbox, and anoda in the second

textbox while keeping a counter of the number of times i do that and click on the next buton which resets the buttons and adds mor numbers in both textboxes that should be summed up individualy..

E.g a a textbox keeps prices of cars and another keeps qty

user enters price and qty and click on next to enter mor price and qty

finaly, user adds up all prices entered thus far and all qty entered thus far by clicking on the compute button which shows result of such computations...

I could do this with lcdui but havng that chalenge with lwuit

Please do not read without a reply..any rply would be appreciated...thanking u all..amuche

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
amuche
Offline
Joined: 2011-09-05

10xs obina...bt hw du i mek results of calculations or anyfin retain its information after the app. loads again? if results of calculations are viewed in a label

and i hav anoda buton that can alow user view last information of cal made sum days bak> is it posible?

du i hav to use RMS or is der a beta option? wud stil apreciate any form of asisitance? thanks..amuche

obinnahenree
Offline
Joined: 2007-03-08

amuche, you could simply store each entry in two hash tables where key is an int (convert to string and use as key) you increment all the time. clear the textfields for another entry. at the end fetch you data from the hashtables bearing in mind the same key in both points to a price/qty pair.

say current count is 1, your hashtables would have [1,price] and the other [1, qty], then at the end of entry, process your data from the hashtables.

Hope it helps.

amuche
Offline
Joined: 2011-09-05

i kinda understood you..but wudnt mind a sample code to clarify ma assumptions??? can any1 plis help me? its urgent!!!

obinnahenree
Offline
Joined: 2007-03-08

try this, hope its what you want though:

import com.sun.lwuit.*;

import com.sun.lwuit.events.ActionEvent;

import com.sun.lwuit.layouts.BoxLayout;

import java.util.Enumeration;

import java.util.Hashtable;

import javax.microedition.midlet.MIDlet;

/**

* @author Obinna

*/

public class SampleMidlet extends MIDlet {

private Form mainForm;

private TextField txtPrice;

private TextField txtQuantity;

private Label lblResult;

private Hashtable tmpPriceStorage = new Hashtable();

private Hashtable tmpQtyStorage = new Hashtable();

private int count;

private Command cmdExit = new Command("Exit") {

public void actionPerformed(ActionEvent e) {

Display.getInstance().exitApplication();

}

};

public void startApp() {

Display.init(this);

Display.getInstance().callSerially(

new Runnable() {

public void run() {

showMainForm();

}

});

}

public void showMainForm() {

mainForm = new Form("Addition Stuff");

mainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

Label lblPrice = new Label("Price:");

mainForm.addComponent(lblPrice);

txtPrice = new TextField();

mainForm.addComponent(txtPrice);

txtQuantity = new TextField();

mainForm.addComponent(txtQuantity);

lblResult = new Label("Totals: ");

mainForm.addComponent(lblResult);

Command cmdNext = new Command("Next") {

public void actionPerformed(ActionEvent e) {

String priceStr = txtPrice.getText();

String qtyStr = txtQuantity.getText();

//I AM SKIPING VALIDATIONS.

//YOU BETTER DO VALIDATIONS HERE TO MAKE SURE VALID NUMBERS WHERE ENTERED

try {

Double.parseDouble(priceStr);

Double.parseDouble(qtyStr);

count = count + 1;

tmpPriceStorage.put(Integer.toString(count), priceStr);

tmpQtyStorage.put(Integer.toString(count), qtyStr);

} catch (Exception exc) {

Dialog.show("Message", "Inputs Must Be Numbers", "OK", null);

}

txtPrice.setText("");

txtQuantity.setText("");

lblResult.setText("");

}

};

Command cmdAdd = new Command("Show Totals") {

public void actionPerformed(ActionEvent e) {

double totalPrice = 0;

double totalQty = 0;

Enumeration keys = tmpPriceStorage.keys();

while (keys.hasMoreElements()) {

String k = keys.nextElement().toString();

double price = Double.parseDouble((String) tmpPriceStorage.get(k));

double qty = Double.parseDouble((String) tmpQtyStorage.get(k));

totalPrice = totalPrice + price;

totalQty = totalQty + qty;

}

//u now have the total price

//and total qty so u can do what you like

lblResult.setText(totalPrice + "," + totalQty);

//empty the hash tables and reset counter

count = 0;

tmpPriceStorage.clear();

tmpQtyStorage.clear();

txtPrice.setText("");

txtQuantity.setText("");

}

};

mainForm.addCommand(cmdNext);

mainForm.addCommand(cmdAdd);

mainForm.addCommand(cmdExit);

mainForm.show();

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

notifyDestroyed();

}

}

amuche
Offline
Joined: 2011-09-05

10xs obina...bt hw du i mek results of calculations or anyfin retain its information after the app. loads again? if results of calculations are viewed in a label

and i hav anoda buton that can alow user view last information of cal made sum days bak> is it posible?

du i hav to use RMS or is der a beta option? wud stil apreciate any form of asisitance? thanks..amuche

obinnahenree
Offline
Joined: 2007-03-08

in my opinion,

u could do this in two ways

1) using a RecordStore to save the information locally on the device

2) upload the result of every calculation to a database on a server, and retrieve later.

depending on the scope of your application, i would choose option 2, since a record store would be lost if the device gets missing or something. but if all the info is on a server, then if say device gets missing or crashes, the user just gets a new device, re-installs your app and has his info again.

You could combine both, For ex, in am application m writing currently, all data is stored online on a server and if for some reason the data cold not be uploaded (be it network error), the user has a n option to save the data locally in a recordstore and there is functionality to upload the data in the record store back to the server and deleted from the record store.

it all depends on the scope of what ur doind though.