| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 
 | import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
 
public final class Convertisseur extends MIDlet implements CommandListener {
 
/** Nombre de chiffre possible ds le champ text **/
private static final int NUM_SIZE=20;
 
/** Boutton Exit **/
private final Command exitCmd = new Command("Exit", Command.EXIT, 2);
 
/** Boutton Calcul **/
private final Command calcCmd = new Command("Calc", Command.SCREEN,1);
 
private final ChoiceGroup euroFrcs = new ChoiceGroup ("", ChoiceGroup.POPUP, new String[] {" En Francs:", "En Euros:"}, null);
 
/** Champ éditable **/
private final TextField aConvertir = new TextField(null, "", NUM_SIZE, TextField.DECIMAL);
 
private final TextField Result = new TextField(null, "", NUM_SIZE, TextField.UNEDITABLE);
 
private final Alert alert = new Alert ("Error", "", null, AlertType.ERROR);
 
private final double euro = 6.559;
 
/** Indique si l'applacation est lancée **/
private boolean isInitialized = false;
 
protected void startApp(){
 
if(isInitialized){return;}
/* creation d'un objet formulaire sur lequel on peut placer des composants */
Form myForm = new Form("Convertisseur Frcs-Euro");
/* Ajout des composant au formulaie */
myForm.append("Somme à convertir :");
myForm.append(aConvertir);
myForm.append(euroFrcs);
myForm.append("Résultat = ");
myForm.append(Result);
myForm.addCommand(exitCmd);
myForm.addCommand(calcCmd);
myForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(myForm);
alert.addCommand(new Command("Back", Command.SCREEN, 1));
isInitialized =true;
}
 
protected void destroyApp(boolean unconditional) {}
 
protected void pauseApp() {}
 
public void commandAction(Command c, Displayable d){
/* lors du clic sur le bouton Exit */
if(c == exitCmd){
/* appel manuel à la fonction de fermeture */
destroyApp(false);
notifyDestroyed();
return;
}
 
 
/* Note: c'est à partir de la CLDC 1.1 que le type "float" est reconnu */
double res = 0.0;
 
try{
double n1 = getNumber(aConvertir, "First");
/** Euro->Francs ou Francs->Euro **/
switch (euroFrcs.getSelectedIndex())
{
case 0: res = n1 * euro; break;
case 1: res = n1/euro; break;
default:
}
}catch(NumberFormatException e){return;}
 
String res_str = Double.toString(res);
 
/* On adapte la taille de la fenêtre Result en fonction du résultat*/ if(res_str.length()> Result.getMaxSize()){
Result.setMaxSize(res_str.length());
}
 
/* Affichage du résultat */
Result.setString (res_str);
}
 
/** Methode permettant d'effectuer des tests sur le nombre à convertir **/
private double getNumber(TextField t, String type)
throws NumberFormatException {
 
String s = t.getString();
 
/* On test si le champ Somme à convertir est rempli */
if (s.length() == 0) {
alert.setString("No " + type + " Argument");
Display.getDisplay(this).setCurrent(alert);
throw new NumberFormatException();
}
 
double n;
try {
n = Double.parseDouble(s);
/* On test si le nombre est positif */
if(n<0)
{
Display.getDisplay(this).setCurrent(alert);
throw new NumberFormatException();
}
}catch (NumberFormatException e) {
alert.setString(type + " argument is out of range.");
Display.getDisplay(this).setCurrent(alert);
throw e;
}
return n;
} | 
Partager