import java.io.*; import java.awt.Frame; import java.awt.event.*; import java.awt.*; import java.util.*; import java.io.RandomAccessFile; import java.net.*; import javax.swing.*; import javax.swing.event.*; public class Opilased{ static Frame f; static Panel p2; static Panel p1; static Button search; static Button certificate; static Button add; static Button change; static Button delete; static Button clear; static Button help; static TextField name; static TextField surname; static TextField grade; static TextField birth; static TextField id; static TextField gender; static TextField street; static TextField house; static TextField apartment; static TextField city; static TextField municipality; static TextField village; static TextArea status; static int numberCt; public static void main(String argumendid[]){ kujundus(); f.addWindowListener(new CloseWindow()); } public static void kujundus(){ f=new Frame("Kooli õpilaste andmebaas"); //Paneelis p1 paiknevad akna paremas servas olevad nupud: //search - õpilase otsimine //certificate - tõendi väljatrükk //add - õpilase lisamine //change - õpilase kirje muutmine //delete - õpilase kustutamine //clear - väljade puhastamine //help - avab brauseris abiinfofaili (HTML) p1=new Panel(new GridLayout(7, 1)); search=new Button("Otsimine"); search.addActionListener(new act()); certificate=new Button("Tõend"); certificate.addActionListener(new act()); add=new Button("Lisamine"); add.addActionListener(new act()); add.addActionListener(new Clear()); change=new Button("Muutmine"); change.addActionListener(new act()); add.addActionListener(new Clear()); delete=new Button("Kustutamine"); delete.addActionListener(new act()); add.addActionListener(new Clear()); clear=new Button("Puhasta väljad"); clear.addActionListener(new Clear()); help=new Button("Abiinfo"); help.addActionListener(new Help()); p1.add(search); p1.add(certificate); p1.add(add); p1.add(change); p1.add(delete); p1.add(clear); p1.add(help); //Kirjeldatakse ära tekstiväljad Panel p2=new Panel(new GridLayout(12, 2)); name=new TextField("", 15); surname=new TextField("", 15); grade=new TextField("", 4); birth=new TextField("", 10); id=new TextField("", 11); gender=new TextField("", 1); street=new TextField("", 15); house=new TextField("", 3); apartment=new TextField("", 3); city=new TextField("", 15); municipality=new TextField("", 15); village=new TextField("", 15); //Lisatakse tekstiväljad ja sildid p2.add(new Label(" Eesnimi")); Panel p2a=new Panel(new FlowLayout()); p2a.add(name); p2.add(p2a); p2.add(new Label(" Perekonnanimi")); Panel p2b=new Panel(new FlowLayout()); p2b.add(surname); p2.add(p2b); p2.add(new Label(" Klass")); Panel p2c1=new Panel(new BorderLayout()); Panel p2c=new Panel(new FlowLayout()); p2c.add(grade); p2c1.add("West", p2c); p2.add(p2c1); grade.addTextListener(new LengthControl()); p2.add(new Label(" Sünniaeg")); Panel p2d1=new Panel(new BorderLayout()); Panel p2d=new Panel(new FlowLayout()); p2d.add(birth); p2d1.add("West", p2d); p2.add(p2d1); birth.addTextListener(new LengthControl()); p2.add(new Label(" Isikukood")); Panel p2e1=new Panel(new BorderLayout()); Panel p2e=new Panel(new FlowLayout()); p2e.add(id); p2e1.add("West", p2e); p2.add(p2e1); id.addTextListener(new LengthControl()); p2.add(new Label(" Sugu")); Panel p2f1=new Panel(new BorderLayout()); Panel p2f=new Panel(new FlowLayout()); p2f.add(gender); p2f1.add("West", p2f); p2.add(p2f1); gender.addTextListener(new LengthControl()); p2.add(new Label(" Tänav")); Panel p2g=new Panel(new FlowLayout()); p2g.add(street); p2.add(p2g); p2.add(new Label(" Maja nr.")); Panel p2h1=new Panel(new BorderLayout()); Panel p2h=new Panel(new FlowLayout()); p2h.add(house); p2h1.add("West", p2h); p2.add(p2h1); house.addTextListener(new LengthControl()); p2.add(new Label(" Korteri nr.")); Panel p2i1=new Panel(new BorderLayout()); Panel p2i=new Panel(new FlowLayout()); p2i.add(apartment); p2i1.add("West", p2i); p2.add(p2i1); apartment.addTextListener(new LengthControl()); p2.add(new Label(" Linn/maakond")); Panel p2j=new Panel(new FlowLayout()); p2j.add(city); p2.add(p2j); p2.add(new Label(" Linnaosa/vald")); Panel p2k=new Panel(new FlowLayout()); p2k.add(municipality); p2.add(p2k); p2.add(new Label(" Küla/asula")); Panel p2l=new Panel(new FlowLayout()); p2l.add(village); p2.add(p2l); // Luuaks olekukast status=new TextArea("", 3, 40, TextArea.SCROLLBARS_VERTICAL_ONLY); //Paneeli p3 pannakse paneelid p1 (nupud) ja p2 (sildid //ning väljad). Paneel lisatakse freimi ja luuakse aken. Panel p3=new Panel(new BorderLayout()); p3.add("East", p1); p3.add("West", p2); p3.add("South", status); f.add(p3); f.setSize(384, 421); f.setVisible(true); } //end void kujundus } //end class opilased //Puhastab kõik tekstiväljad ekraanil class Clear implements ActionListener{ public void actionPerformed (ActionEvent evt){ Opilased.name.setText(""); Opilased.surname.setText(""); Opilased.grade.setText(""); Opilased.birth.setText(""); Opilased.id.setText(""); Opilased.gender.setText(""); Opilased.street.setText(""); Opilased.house.setText(""); Opilased.apartment.setText(""); Opilased.city.setText(""); Opilased.municipality.setText(""); Opilased.village.setText(""); //Opilased.status.setText(""); } } // Avab brauseris abifaili HTML kujul class Help implements ActionListener{ public void actionPerformed (ActionEvent evt){ try{ URL u=new File("help.htm").toURL(); JFrame f=new JFrame("Abiinfo"); final JEditorPane jp=new JEditorPane(u); jp.setEditable(false); f.getContentPane().add(jp); // Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); // f.setSize(d.width, d.height); f.setSize(600, 400); f.setVisible(true); jp.addHyperlinkListener( new HyperlinkListener(){ public void hyperlinkUpdate(HyperlinkEvent e){ if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){ try{ jp.setPage(e.getURL()); }catch(IOException ex){ Opilased.status.setText(ex.toString()); } } } } ); }catch(IOException ex){ Opilased.status.setText(ex.toString()); } } } class LengthControl implements TextListener{ public void textValueChanged(TextEvent e){ if(Opilased.grade.getText().length()>4){ Opilased.grade.setText( Opilased.grade.getText().substring(0, 4) ); } if(Opilased.birth.getText().length()>10){ Opilased.birth.setText( Opilased.birth.getText().substring(0, 10) ); } if(Opilased.id.getText().length()>11){ Opilased.id.setText( Opilased.id.getText().substring(0, 11) ); } if(Opilased.gender.getText().length()>1){ Opilased.gender.setText( Opilased.gender.getText().substring(0, 1) ); } if(Opilased.gender.getText().equals("m")){ Opilased.gender.setText( Opilased.gender.getText().toUpperCase() ); } if(Opilased.gender.getText().equals("n")){ Opilased.gender.setText( Opilased.gender.getText().toUpperCase() ); } if(Opilased.house.getText().length()>3){ Opilased.house.setText( Opilased.house.getText().substring(0, 3) ); } if(Opilased.apartment.getText().length()>3){ Opilased.apartment.setText( Opilased.apartment.getText().substring(0, 3) ); } } } class CloseWindow extends WindowAdapter{ public void windowClosing(WindowEvent e){ Opilased.status.setText("End of program"); System.exit(0); } } class act implements ActionListener{ public void actionPerformed (ActionEvent evt){ opilane op1; String s= evt.getActionCommand(); op1=new opilane(); op1.surname=Opilased.surname.getText(); op1.name=Opilased.name.getText(); op1.grade=Opilased.grade.getText(); op1.birth=Opilased.birth.getText(); op1.id=Opilased.id.getText(); op1.gender=Opilased.gender.getText(); op1.street=Opilased.street.getText(); op1.house=Opilased.house.getText(); op1.apartment=Opilased.apartment.getText(); op1.city=Opilased.city.getText(); op1.municipality=Opilased.municipality.getText(); op1.village=Opilased.village.getText(); if(s.equals("Otsimine")){ SearchFrame.main(new String[0]); } else if(s.equals("Lisamine")){ // System.out.println(Opilased.status); Opilased.status.setText("Toimub õpilase lisamine! Palun oota..."); // System.out.println("Status: "+Opilased.status.getText()); Save.SaveOp(op1); } else if(s.equals("Kustutamine")){ Opilased.status.setText("Toimub õpilase kustutamine! Palun oota..."); opDelete.DeleteOP(op1); } else if(s.equals("Muutmine")){ Opilased.status.setText("Toimub õpilase muutmine! Palun oota..."); opDelete.DeleteOP(op1); Save.SaveOp(op1); } else if(s.equals("Tõend")){ Opilased.status.setText("Toimub andmete printimine! Palun oota..."); ToendPrint tp = new ToendPrint(); tp.departmentText = "KAASIKU MAAKONNA HARIDUSOSAKOND"; tp.schoolHeaderText = "KALMKÜLA KOLMAS KESKKOOL"; tp.nameText = Opilased.name.getText(); tp.surnameText = Opilased.surname.getText(); tp.schoolText = "Kalmküla 3. Keskkoolis"; tp.gradeText = Opilased.grade.getText() + "."; tp.birthDate = Opilased.birth.getText(); tp.addressText = "Vana-Siili 2, Kalmküla, 10224 Tel. +372 8244 423"; tp.init(); tp.doPrint(); } }//void } //actionlistener class Save{ public static void SaveOp(opilane op){ long cnt; RandomAccessFile result; TextReader data; char s; boolean b; // System.out.println ("Toimub õpilase lisamine! Palun oota..."); b = onOlemas(op); if (b==false){ try { result = new RandomAccessFile("result.dat","rw"); } catch (IOException e) { Opilased.status.setText("Can't open file result.dat!"); Opilased.status.setText(e.toString()); return; // end the program } cnt = 0; try{ for(cnt=0;true;cnt++){ result.seek(cnt); b=result.readBoolean(); }//for }//try catch (EOFException e) { System.out.println("EOF"); }//catch catch (IOException e) { Opilased.status.setText("Can't open file result.dat!"); Opilased.status.setText(e.toString()); return; // end the program } try { result.writeBytes(op.surname+'!'+op.name+'!'+op.grade+'!'+op.birth+'!'+ op.id+'!'+op.gender+'!'+op.street+'!'+op.house+'!'+op.apartment+'!'+ op.city+'!'+op.municipality+'!'+op.village+'!'+'#'); result.close(); Opilased.status.setText("Õpilane on sisestatud!"); } catch (IOException e) { Opilased.status.setText("Can't open file result.dat!"); Opilased.status.setText(e.toString()); return; // end the program } } else Opilased.status.setText("On Juba olemas!"); } public static boolean onOlemas(opilane op){ int numberCt; String s = op.id; TextReader data; // character input stream for reading data boolean olemas; try { // create the input stream data = new TextReader(new FileReader("result.dat")); olemas = setSeek(data,s); }//try catch (FileNotFoundException e) { Opilased.status.setText("Can't find file result.dat!"); return true; // end the program by returning from main() }//catch data.close(); return olemas;//ssss }//boolean public static String Find(TextReader data){ int tabCnt =0; TextReader data1; // character input stream for reading data char a=' '; char b=' '; String id=""; data1 = data; try { while (a!='#') { // read to end-of-file a = data1.getChar(); if (a=='!'){ tabCnt++; if (tabCnt==4){ while (b!='!'){ b = data1.getChar(); if (b!='!')id=id+b; }//while return id; }//if }//if }//while }//try catch (TextReader.Error e) { return id; } catch (IndexOutOfBoundsException e) { Opilased.status.setText("Too many numbers in data file."); Opilased.status.setText("Processing has been aborted."); } return id; } public static boolean setSeek(TextReader data,String s){ int tabCnt=0; TextReader data1; // character input stream for reading data char a=' '; char b=' '; String id=""; Opilased.numberCt = 1; TextReader data2 = data; String s1=s; String seek1; seek1=Find(data2); boolean s2= false; if (seek1.equalsIgnoreCase(s)) { s2=true; return s2; }//if else { try { while ( (!data2.eof() ) | (s2==true) ) { // read to end-of-file a = data2.getChar(); if (a=='#'){ seek1=Find(data2); Opilased.numberCt++; if (seek1.equalsIgnoreCase(s)){ s2=true; return s2; }//if }//if }//while } //try catch (TextReader.Error e) { Opilased.status.setText("sisend error: " + e.getMessage()); } catch (IndexOutOfBoundsException e) { Opilased.status.setText("Too many numbers in data file."); Opilased.status.setText("Processing has been aborted."); } }//else return s2;//qqqq } } //save class opDelete{ public static void DeleteOP(opilane op){ int mitmes=1; char x=' '; RandomAccessFile result1; RandomAccessFile result2; boolean d=false; boolean trell=false; d = OlemasDelete(op); if (d==false){ Opilased.status.setText("Antud õpilast ei eksisteeri!"); } //if else { try { result1 = new RandomAccessFile("result.dat","r"); result2 = new RandomAccessFile("temp.dat","rw"); try { while ( x!='$' ) { x = (char)result1.readByte(); trell = false; if (x=='#'){ mitmes++; trell = true; } //if if( (mitmes!=Opilased.numberCt) | ( (mitmes!=Opilased.numberCt) & (trell==true) ) ){ result2.writeByte((byte)x); }//if }//while }//try catch (EOFException e) { } //catch result1.close(); result2.close(); File oldfile=new File("result.dat"); oldfile.delete(); File tempfile=new File("temp.dat"); tempfile.renameTo(oldfile); Opilased.status.setText("Õpilane on kustutatud!"); } //try catch (IOException e) { Opilased.status.setText("Can't open file result.dat!"); Opilased.status.setText(e.toString()); return; // end the program }//catch } //else }//void public static boolean OlemasDelete(opilane op){ int numberCt; String s = op.id; TextReader data; // character input stream for reading data boolean olemas; try { // create the input stream data = new TextReader(new FileReader("result.dat")); olemas =Save.setSeek(data,s); }//try catch (FileNotFoundException e) { Opilased.status.setText("Can't find file result.dat!"); return true; // end the program by returning from main() }//catch data.close(); return olemas; }//boolean }//class