import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import java.io.*; import java.util.*; public class proov3 extends MIDlet implements CommandListener{ Command c1=new Command("Välju", Command.EXIT, 1); Command c2=new Command("Skoorid", Command.SCREEN, 1); //Canvas louend=new Louend4(); Louend4 louend=new Louend4(); String hoidlanimi="hoidla1"; RecordStore rs; RecordEnumeration re; ValikuVorm valik=new ValikuVorm(); public proov3(){ louend.addCommand(c1); louend.addCommand(c2); louend.setCommandListener(this); Display.getDisplay(this).setCurrent(louend); 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(louend); } 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); } public void commandAction(Command c, Displayable d){ try{ if(c==c1){ notifyDestroyed(); } if(c==c2){ Display.getDisplay(proov3.this).setCurrent(valik); } }catch(Exception e){ teade(e.getMessage()); } } 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("Salvesta", null); append("Vaata", null); 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(proov3.this).setCurrent(lisamine); System.out.println(louend.punkte); } if(getSelectedIndex()==1){ Display.getDisplay(proov3.this).setCurrent(vaatamine); } } } //public void commandAction-->lopp } //ValikuVorm extends-->lopp class LisamisVorm extends Form implements CommandListener{ TextField tfnimi=new TextField("Eesnimi", "", 20, TextField.ANY); //StringItem punktid=new StringItem("Punkte", "0"); Command c3=new Command("Salvesta", Command.SCREEN, 1); Command c4=new Command("Edasi", Command.SCREEN, 1); Command yles=new Command("Üles", Command.EXIT, 1); Displayable avaja; LisamisVorm(Displayable avaja){ super("Lisamine"); this.avaja=avaja; append(tfnimi); //append(punktid); addCommand(c3); //addCommand(c4); addCommand(yles); setCommandListener(this); } public void commandAction(Command c, Displayable d){ if(c==yles){ Display.getDisplay(proov3.this).setCurrent(avaja); } if(c==c3){ lisaKirje(); Display.getDisplay(proov3.this).setCurrent(avaja); } } void lisaKirje(){ byte[] bm=leiaBaidiMassiiv(); try{ rs.addRecord(bm, 0, bm.length); tfnimi.setString(""); }catch(Exception e){ teade(e.getMessage()); } } byte[] leiaBaidiMassiiv(){ ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(bos); try{ dos.writeUTF(tfnimi.getString() +" "+ (String.valueOf(louend.punkte))+ " "+(String.valueOf(louend.aeg/1000))); }catch(Exception e){teade(e.getMessage());} return bos.toByteArray(); } } class VaatamisVorm extends Form implements CommandListener{ StringItem snimi=new StringItem("Nimi", ""); 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); addCommand(edasi); addCommand(tagasi); addCommand(yles); setCommandListener(this); } public void commandAction(Command c, Displayable d){ try{ if(c==yles){ Display.getDisplay(proov3.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()); }catch(IOException e){ teade(e.getMessage()); } } } //class VaatamisVorm-->lopp } //public class proov3-->lopp