import javax.microedition.midlet.*; import javax.microedition.rms.*; import javax.microedition.lcdui.*; import java.io.*; import java.util.*; public class Salvestus3 extends MIDlet{ String hoidlanimi="hoidla2"; RecordStore rs; RecordEnumeration re; ValikuVorm valik=new ValikuVorm(); public Salvestus3(){ try{ rs=RecordStore.openRecordStore(hoidlanimi, true); re=rs.enumerateRecords(null, null, true); }catch(Exception e){teade(e.getMessage());} } protected void startApp() throws MIDletStateChangeException{ Display.getDisplay(this).setCurrent(valik); } protected void pauseApp(){} protected void destroyApp(boolean kohustus) throws MIDletStateChangeException{ try{ rs.closeRecordStore(); } catch (Exception e){teade(e.getMessage());} } void teade(String teade){ Alert a=new Alert("Teade", teade, null, AlertType.CONFIRMATION); a.setTimeout(5); Display.getDisplay(this).setCurrent(a); } class ValikuVorm extends List implements CommandListener{ LisamisVorm lisamine=new LisamisVorm(this); VaatamisVorm vaatamine=new VaatamisVorm(this); Command ava=new Command("Ava", Command.SCREEN, 1); Command valju=new Command("Välju", Command.EXIT, 1); ValikuVorm(){ super("Tegevuse valik", List.IMPLICIT); append("Lisa", null); Image vaatamispilt=null; try{ vaatamispilt=Image.createImage("/silmad.png"); } catch(IOException e){ teade("Probleem pildi laadimisel"); } append("Vaata", vaatamispilt); setSelectCommand(ava); addCommand(valju); addCommand(ava); setCommandListener(this); } public void commandAction(Command c, Displayable d){ if(c==valju){ notifyDestroyed(); } if(c==ava){ if(getSelectedIndex()==0){ Display.getDisplay(Salvestus3.this).setCurrent(lisamine); } if(getSelectedIndex()==1){ Display.getDisplay(Salvestus3.this).setCurrent(vaatamine); } } } } class LisamisVorm extends Form implements CommandListener{ TextField tfnimi=new TextField("Eesnimi", "", 20, TextField.ANY); DateField dfsynniaeg=new DateField("Sünnipäev", DateField.DATE); Command lisa=new Command("Lisa", Command.SCREEN, 1); Command yles=new Command("Üles", Command.EXIT, 1); Displayable avaja; LisamisVorm(Displayable avaja){ super("Lisamine"); this.avaja=avaja; append(tfnimi); append(dfsynniaeg); addCommand(lisa); addCommand(yles); setCommandListener(this); } public void commandAction(Command c, Displayable d){ if(c==yles){ Display.getDisplay(Salvestus3.this).setCurrent(avaja); } if(c==lisa){ lisaKirje(); } } void lisaKirje(){ byte[] bm=leiaBaidiMassiiv(); try{ rs.addRecord(bm, 0, bm.length); dfsynniaeg.setDate(null); tfnimi.setString(""); }catch(Exception e){ teade(e.getMessage()); } } byte[] leiaBaidiMassiiv(){ ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(bos); try{ dos.writeUTF(tfnimi.getString()); dos.writeLong(dfsynniaeg.getDate().getTime()); }catch(Exception e){teade(e.getMessage());} return bos.toByteArray(); } } class VaatamisVorm extends Form implements CommandListener{ StringItem snimi=new StringItem("Nimi", ""); DateField dfsynniaeg=new DateField("Sünnipäev", DateField.DATE); Command edasi=new Command("Edasi", Command.SCREEN, 1); Command tagasi=new Command("Tagasi", Command.SCREEN, 1); Command yles=new Command("Üles", Command.BACK, 1); Displayable avaja; VaatamisVorm(Displayable avaja){ super("Vaatamine"); this.avaja=avaja; append(snimi); append(dfsynniaeg); addCommand(edasi); addCommand(tagasi); addCommand(yles); setCommandListener(this); } public void commandAction(Command c, Displayable d){ try{ if(c==yles){ Display.getDisplay(Salvestus3.this).setCurrent(avaja); } if(c==edasi){ if(re.hasNextElement()){ loeBaidiMassiivist(re.nextRecord()); } } if(c==tagasi){ if(re.hasPreviousElement()){ loeBaidiMassiivist(re.previousRecord()); } } }catch(Exception e){ teade(e.getMessage()); } } void loeBaidiMassiivist(byte[] b){ try{ DataInputStream dis=new DataInputStream(new ByteArrayInputStream(b)); snimi.setText(dis.readUTF()); dfsynniaeg.setDate(new Date(dis.readLong())); }catch(IOException e){ teade(e.getMessage()); } } } }