import java.awt.Frame; import java.awt.event.*; import java.awt.*; import java.io.*; import java.io.RandomAccessFile; import java.lang.StringBuffer; import java.util.Vector; public class SearchFrame{ static Frame f; static Panel p1; static Panel top; static Panel top1; static Panel top2; static Panel top3; static TextField surname; static Button search; static List found; public static void main(String argumendid[]){ final Frame f=new Frame("Õpilaste otsimine"); Panel p1=new Panel(new BorderLayout()); Panel top=new Panel(new BorderLayout()); Panel top1=new Panel(new FlowLayout()); top1.add(new Label("Perekonnanimi")); top.add("West", top1); Panel top2=new Panel(new FlowLayout()); surname=new TextField("", 15); top2.add(surname); top.add("Center", top2); Panel top3=new Panel(new FlowLayout()); Button search=new Button("Otsi"); top3.add(search); search.addActionListener(new Search()); top.add("East", top3); found=new List(3); found.addActionListener(new ReadRecord()); f.add("North", top); f.add("Center", found); f.setSize(289, 115); f.setVisible(true); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.setVisible(false); Search.andmed.clear(); } }); } } /* class ListClick implements ActionListener{ static int valitud; public void actionPerformed(ActionEvent e){ valitud = ((Integer)Search.andmed.elementAt(SearchFrame.found.getSelectedIndex())).intValue(); System.out.println("Listist valiti kirje nr. "+valitud); } } */ class Search implements ActionListener{ // static int foundnr=0; static String surname2=""; static Vector andmed=new Vector(); public void actionPerformed (ActionEvent evt){ int mitmeshyyukas=0; int mitmeskirje=0; RandomAccessFile result1; char x=' '; String name2=""; String grade2=""; String foundstudent=""; String surname=""; try{ surname=SearchFrame.surname.getText(); } catch(Exception ex){ex.printStackTrace(); } System.out.println("Otsiti õpilast: "+surname); try{ result1 = new RandomAccessFile("result.dat","r"); try{ while ( x!='$' ) { System.out.println("Alustame kirje lugemist"); StringBuffer surnamebuf=new StringBuffer(); x = (char)result1.readByte(); while ( x!='!' ) { surnamebuf.append(x); x = (char)result1.readByte(); } //end while ( x!='!' ) mitmeshyyukas = mitmeshyyukas + 1; surname2 = surnamebuf.toString(); System.out.println("Leiti õpilane: "+surname2); mitmeskirje=mitmeskirje+1; //Kui perekonnanimed võrduvad, loetakse sisse eesnimi ja klass if (surname.equalsIgnoreCase(surname2)){ StringBuffer eesbuf=new StringBuffer(); char c=(char)result1.readByte(); while(c!='!'){ eesbuf.append(c); c=(char)result1.readByte(); } name2 = eesbuf.toString(); System.out.println("Leitud õpilase eesmini: "+name2); StringBuffer klassbuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ klassbuf.append(c); c=(char)result1.readByte(); } grade2 = klassbuf.toString(); System.out.println("Leitud õpilase klass on: "+grade2); foundstudent=(name2+" "+surname2+", "+grade2); System.out.println("Leiti järgmine õpilane: "+foundstudent); // Integer abi=new Integer(mitmeskirje); //System.out.println("abi valmis"); andmed.addElement(new Integer(mitmeskirje)); System.out.println("Kirje nr. "+mitmeskirje+" lisati vektorisse"); SearchFrame.found.add(foundstudent); }//eesnime ja klassi lugemise lõpp while(x!='#'){ x=(char)result1.readByte(); } // SearchFrame.found.add(surname2.replace(((char)(byte)245), 'õ')); }//end (while x!='$') }//try catch (EOFException e) { System.out.println("Faili lõpp"); return; } //catch result1.close(); } //try catch (IOException e) { System.out.println("Can't open file result.dat!"); System.out.println(e.toString()); return; // end the program }//catch } } class ReadRecord implements ActionListener{ //Otsib failist ja kuvab programmi põhiaknasse valitud //õpilase andmed. Vajaliku kirje number on muutuja //valitud public void actionPerformed (ActionEvent evt){ int mitmeskirje=1; int valitud; RandomAccessFile result1; char c=' '; char x=' '; String surname2=""; String name2=""; String grade2=""; String birth2=""; String id2=""; String gender2=""; String street2=""; String house2=""; String apartment2=""; String city2=""; String municipality2=""; String village2=""; valitud = ((Integer)Search.andmed.elementAt(SearchFrame.found.getSelectedIndex())).intValue(); System.out.println("Listist valiti kirje nr. "+valitud); try{ result1 = new RandomAccessFile("result.dat","r"); try{ while (mitmeskirje < valitud){ x=(char)result1.readByte(); if (x=='#'){ mitmeskirje=mitmeskirje+1; } } if (mitmeskirje == valitud){ System.out.println("Alustame kirje lugemist"); System.out.println("Mitmeskirje: "+mitmeskirje); StringBuffer surnamebuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ surnamebuf.append(c); c=(char)result1.readByte(); } surname2 = surnamebuf.toString(); Opilased.surname.setText(surname2); System.out.println("Leitud õpilase perekonnanimi: "+surname2); StringBuffer namebuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ namebuf.append(c); c=(char)result1.readByte(); } name2 = namebuf.toString(); Opilased.name.setText(name2); System.out.println("Leitud õpilase eesnimi: "+name2); StringBuffer gradebuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ gradebuf.append(c); c=(char)result1.readByte(); } grade2 = gradebuf.toString(); Opilased.grade.setText(grade2); System.out.println("Leitud õpilase klass: "+grade2); StringBuffer birthbuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ birthbuf.append(c); c=(char)result1.readByte(); } birth2 = birthbuf.toString(); Opilased.birth.setText(birth2); System.out.println("Leitud õpilase sünniaeg: "+birth2); StringBuffer idbuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ idbuf.append(c); c=(char)result1.readByte(); } id2 = idbuf.toString(); Opilased.id.setText(id2); System.out.println("Leitud õpilase isikukood: "+id2); StringBuffer genderbuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ genderbuf.append(c); c=(char)result1.readByte(); } gender2 = genderbuf.toString(); Opilased.gender.setText(gender2); System.out.println("Leitud õpilase sugu: "+gender2); StringBuffer streetbuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ streetbuf.append(c); c=(char)result1.readByte(); } street2 = streetbuf.toString(); Opilased.street.setText(street2); System.out.println("Leitud õpilase tänav: "+street2); StringBuffer housebuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ housebuf.append(c); c=(char)result1.readByte(); } house2 = housebuf.toString(); Opilased.house.setText(house2); System.out.println("Leitud õpilase maja nr: "+house2); StringBuffer apartmentbuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ apartmentbuf.append(c); c=(char)result1.readByte(); } apartment2 = apartmentbuf.toString(); Opilased.apartment.setText(apartment2); System.out.println("Leitud õpilase korteri nr: "+apartment2); StringBuffer citybuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ citybuf.append(c); c=(char)result1.readByte(); } city2 = citybuf.toString(); Opilased.city.setText(city2); System.out.println("Leitud õpilase linn: "+city2); StringBuffer municipalitybuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ municipalitybuf.append(c); c=(char)result1.readByte(); } municipality2 = municipalitybuf.toString(); Opilased.municipality.setText(municipality2); System.out.println("Leitud õpilase linnaosa/vald: "+municipality2); StringBuffer villagebuf=new StringBuffer(); c=(char)result1.readByte(); while(c!='!'){ villagebuf.append(c); c=(char)result1.readByte(); } village2 = villagebuf.toString(); Opilased.village.setText(village2); System.out.println("Leitud õpilase küla: "+village2); }//kirje lugemise lõpp }//try catch (EOFException e) { System.out.println("Faili lõpp"); return; } //catch result1.close(); } //try catch (IOException e) { System.out.println("Can't open file result.dat!"); System.out.println(e.toString()); return; // end the program }//catch } }