Skip to content

Instantly share code, notes, and snippets.

@manucorporat
Created November 28, 2017 17:33
Show Gist options
  • Save manucorporat/80211c603c634c815a80ced1479fc96c to your computer and use it in GitHub Desktop.
Save manucorporat/80211c603c634c815a80ced1479fc96c to your computer and use it in GitHub Desktop.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
public class ConversorMonedasJavaME extends MIDlet implements CommandListener {
private Display display;
private Form form;
private ChoiceGroup lista;
private TextField cantidad;
private Command salirCommand;
private Command convertirCommand;
private String name;
public ConversorMonedasJavaME() {
name = "Conversor de Moneda";
display = Display.getDisplay(this);
// un item para preguntar que tipo del grupo se escoger para el alert
lista = new ChoiceGroup("Elije la moneda de origen:", ChoiceGroup.EXCLUSIVE);
lista.append("$", null);
lista.append("CAD", null);
lista.append("CHF", null);
lista.append("Libra", null);
lista.append("PST", null);
lista.append("Yen", null);
lista.setSelectedIndex(0, true);
cantidad = new TextField("Introduce la cantidad", "", 100, TextField.DECIMAL);
form = new Form(name);
form.append(lista);
form.append(cantidad);
convertirCommand = new Command("Convertir", Command.OK, 0);
salirCommand = new Command("Salir", Command.EXIT, 3);
volverCommand = new Command("Volver", Command.BACK, 4);
form.addCommand(convertir);
form.addCommand(salir);
form.setCommandListener(this);
}
protected void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public double factorForIndex(int index) {
switch(index) {
case 0: return 1.18;
case 1: return 1.5;
case 2: return 1.16;
case 3: return 0.89;
case 4: return 166.386;
case 5: return 131.95;
}
return 0;
}
public String nameForIndex(int index) {
switch(index) {
case 0: return "USD";
case 1: return "CAD";
case 2: return "CHF";
case 3: return "GBP";
case 4: return "PTS";
case 5: return "JPY";
}
return "";
}
public void commandAction(Command c, Displayable d) {
// Miramos si salimos o mostramos la alerta
if (c == salirCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == volverCommand) {
display.setCurrent(form);
} else if (c == convertirCommand) {
int index = lista.getSelectedIndex();
double factor = factorForIndex(index);
String name = nameForIndex(index);
double original = Double.parseDouble(cantidad.getString());
double result = original * factor;
String message = String.format("$.2f %s = %.10f €", original, name, result);
Screen screen = new Screen();
screen.setTitle(name);
screen.append(message);
screen.addCommand(salir);
screen.addCommand(volverCommand);
display.setCurrent(screen);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment