import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
public class Sauber extends MIDlet implements CommandListener {
private Display display;
private Form form;
private ChoiceGroup opzioni;
private Command ok;
private Command okAlert;
private Command exit;
private DateField aDateField, bDateField;
public void commandAction(Command c, Displayable s){
if(c == ok) {
Data begin = new Data(aDateField.getDate());
Data end = new Data(bDateField.getDate());
if(opzioni.getSelectedIndex() == 0) {
long days = begin.getDays(end);
display.setCurrent(createResultMsg("Fra le due date " + (days == 1 ? "passa ":"passano ") + days + (days == 1 ? " giorno.":" giorni.")));
} else if(opzioni.getSelectedIndex() == 1) {
long mesi = begin.getMonth(end);
display.setCurrent(createResultMsg("Fra le due date passano: " + mesi/10 + "," + (mesi - (mesi/10)*10) + " mesi."));
}
} else if(c == exit) {
destroyApp(true);
}
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
private Alert createResultMsg
(String res
) {
Alert result = new Alert("Risultato", res, null, AlertType.INFO);
result.setTimeout(Alert.FOREVER);
return result;
}
private Alert createErrorMessage
(String msg
) {
return new Alert("Errore", msg, null, AlertType.ERROR);
}
public void pauseApp() {
}
private void init() {
display = Display.getDisplay(this);
form = new Form("Sauber - il contagiorni by Matteo");
aDateField = new DateField("Inserire data di partenza", DateField.DATE);
bDateField = new DateField("Inserire data di arrivo", DateField.DATE);
opzioni
= new ChoiceGroup
("Inserire formato uscita", ChoiceGroup.
EXCLUSIVE,
new String[] {"Giorni",
"Mesi"},
null);
ok = new Command("Calcola!", Command.OK, 1);
exit = new Command("Exit", Command.EXIT, 1);
form.setCommandListener(this);
okAlert = new Command("Ok", Command.OK, 1);
}
public void startApp() {
init();
form.append(aDateField);
form.append(bDateField);
form.append(opzioni);
form.addCommand(ok);
form.addCommand(exit);
display.setCurrent(form);
}
}