Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
RubricEnric Java - rubricaRelase3.java

rubricaRelase3.java

Caricato da: Mutelg
Scarica il programma completo

  1. /**
  2.  * Autore :Alenadri Enrico
  3.  * Classe : III G
  4.  * Data :  6/02/2009
  5.  * Nome File:  rubrica.java
  6.  */
  7.  
  8.  import javax.swing.*;
  9.  import java.awt.*;
  10.  import java.awt.event.*;
  11.  import java.io.*;
  12.  
  13.  
  14.  public class rubrica{
  15.  
  16.         public static void main(String[] args)
  17.      {
  18.          int xLoc=400,   //posizione x dei frame nello schermo
  19.              yLoc=400;   //posizione y dei frame nello schermo
  20.  
  21.  
  22.      framePri frame = new framePri(xLoc,yLoc);
  23.      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.      frame.show();
  25.      }
  26.  }
  27.  
  28.  
  29.  class panelInserimento extends JPanel implements ActionListener
  30.  {
  31.    private boolean exist=true;
  32.    private int x=0,
  33.                num=0,
  34.                        linee=0,
  35.                        lineeStm=0,
  36.                        i,
  37.                        cont,
  38.                        con;
  39.    private String nameRub,
  40.                   linea,
  41.                   stringa,
  42.                   risCerca="";
  43.    private TextField inNome,
  44.                      inCognome,
  45.                      inNumero,
  46.                      inIndirizzo,
  47.                      inNameRub,
  48.                      inCerca;
  49.  
  50.    private FileOutputStream fileS;
  51.    private FileReader fileL;
  52.    private File exFile;
  53.    private PrintStream scrittura;
  54.    private BufferedReader lettura;
  55.    private TextArea risultati;
  56.    private int[] cerca = new int[100];
  57.  
  58.    private JLabel testoIns,
  59.                   testoStp,
  60.                   nome,
  61.                   Congome,
  62.                   Numero,
  63.                   Indirizzo,
  64.                   index,
  65.                   spazio,
  66.                   stmNome,
  67.                   stmCognome,
  68.                   stmNumero,
  69.                   stmIndirizzo,
  70.                   infoCerca;
  71.    private JButton ok,
  72.                    reset,
  73.                    savExit,
  74.                    invio,
  75.                    invio_,
  76.                    indIns,
  77.                    indStp,
  78.                    indCer,
  79.                    avanti,
  80.                    back,
  81.                    indietro,
  82.                    exit,
  83.                    Cerca;
  84.    public panelInserimento()
  85.     {
  86.  
  87.      exit = new JButton("esci");
  88.      spazio= new JLabel("                              ");
  89.  
  90.      //Componenti Index
  91.          index = new JLabel("       Cosa vuoi fare?                                    ");
  92.          indIns = new JButton("Crea rubrica");
  93.          indStp = new JButton("Visualizza contatti");
  94.          indCer = new JButton("Cerca Contatti");
  95.  
  96.  
  97.  
  98.  
  99.      //Componenti inserimento nome Rubrica-inserimento scherda/stampa scheda
  100.          testoIns = new JLabel("Inserisci il nome con cui salvare la rubrica        ");
  101.          testoStp = new JLabel("Inserisci il nome della rubrica        ");
  102.          inNameRub = new TextField(10);
  103.          invio = new JButton("Invio");
  104.          invio_ = new JButton("Invio ");
  105.  
  106.          //Componenti Cerca
  107.          Cerca = new JButton("Cerca");
  108.          infoCerca = new JLabel("Puoi cercare per Nome-Cognome-Numero-indirizzo");
  109.          inCerca = new TextField(10);
  110.          risultati = new TextArea(10,35);
  111.          risultati.setEditable(false);
  112.          risultati.setCaretPosition(0);
  113.  
  114.  
  115.  
  116.          //Componenti inserimento scheda
  117.           nome = new JLabel("                Nome");
  118.           Congome = new JLabel("                Cognome");
  119.           Numero = new JLabel("                Numero Casa");
  120.           Indirizzo = new JLabel("                Indirizzo");
  121.           ok = new JButton("Add");
  122.           reset = new JButton("Reset");
  123.           savExit = new JButton("Save & Back");
  124.           inNome = new TextField(17);
  125.           inCognome = new TextField(17);
  126.           inNumero = new TextField(17);
  127.           inIndirizzo = new TextField(17);
  128.  
  129.      //Componenti stampa Scheda
  130.      stmNome = new JLabel("Nome:");
  131.      stmCognome = new JLabel("Cognome:");
  132.      stmNumero = new JLabel("Numero:");
  133.      stmIndirizzo = new JLabel("Indirizzo");
  134.      avanti = new JButton(">>");
  135.      back = new JButton("Indietro");
  136.      indietro = new JButton("<<");
  137.  
  138.  
  139.  
  140.      nameRub = inNameRub.getText();
  141.  
  142.       //Aggiungo componenti index
  143.       add(index);
  144.       add(indIns);
  145.       add(indStp);
  146.       add(indCer);
  147.       add(exit);
  148.  
  149.       indIns.addActionListener(this);
  150.       indStp.addActionListener(this);
  151.       indCer.addActionListener(this);
  152.       exit.addActionListener(this);
  153.  
  154.       //Aggiungo componenti Inserimento rubrica
  155.       add(testoStp);
  156.           add(testoIns);
  157.           add(inNameRub);
  158.           add(invio);
  159.           add(invio_);
  160.  
  161.           invio.addActionListener(this);
  162.           invio_.addActionListener(this);
  163.  
  164.  
  165.      //Aggiungo componenti Cerca
  166.       add(risultati);
  167.       add(infoCerca);
  168.       add(inCerca);
  169.       add(Cerca);
  170.  
  171.       Cerca.addActionListener(this);
  172.  
  173.     ///Aggiungo componenti Input contatti
  174.           add(nome);
  175.           add(inNome);
  176.           add(Congome);
  177.           add(inCognome);
  178.           add(Numero);
  179.           add(inNumero);
  180.           add(Indirizzo);
  181.           add(inIndirizzo);
  182.           add(ok);
  183.           add(reset);
  184.           add(savExit);
  185.  
  186.           ok.addActionListener(this);
  187.           reset.addActionListener(this);
  188.           savExit.addActionListener(this);
  189.  
  190.  
  191.       //Aggiungo componenti Visualizzazione contatti
  192.       add(stmNome);
  193.       add(stmCognome);
  194.       add(stmNumero);
  195.       add(stmIndirizzo);
  196.       add(indietro);
  197.       add(back);
  198.       add(avanti);
  199.       indietro.setEnabled(false);
  200.  
  201.       avanti.addActionListener(this);
  202.       back.addActionListener(this);
  203.       indietro.addActionListener(this);
  204.  
  205.  
  206.  
  207.       //Setto i componenti per visualizzare la scherata di index
  208.           pannelloInput(false);
  209.           pannelloOutput(false);
  210.           pannelloRubIns(false);
  211.           pannelloCerca(false);
  212.           pannelloRisultati(false);
  213.  
  214.  
  215.  
  216.          }
  217.     public void actionPerformed(ActionEvent evt)
  218.                      {
  219.                            String nome = inNome.getText();
  220.                            String cognome = inCognome.getText();
  221.                            String numero = inNumero.getText();
  222.                            String indirizzo = inIndirizzo.getText();
  223.                            String nameRub = inNameRub.getText();
  224.                boolean test;
  225.  
  226.                            String command = evt.getActionCommand();
  227.                            command = command.toLowerCase();
  228.                          if(command.equals("<<"))
  229.                                                     {
  230.                                                           if(num>0)
  231.                                                             {
  232.                                                                  num--;
  233.                                                                  avanti.setEnabled(true);
  234.                                                             }
  235.  
  236.                                                       if(num==0)
  237.                                                          indietro.setEnabled(false);
  238.  
  239.                                                            letturaFile(nameRub);
  240.                                                        stampa();
  241.                                 }
  242.                          if(command.equals(">>"))
  243.                             {
  244.                                   if((num+1)*4<lineeStm)
  245.                                     {
  246.                                          num++;
  247.                                          indietro.setEnabled(true);
  248.                                     }
  249.                               if((num+1)*4==lineeStm)
  250.                                  avanti.setEnabled(false);
  251.  
  252.                                          letturaFile(nameRub);
  253.                                  stampa();
  254.                                 }
  255.                          if(command.equals("indietro"))
  256.                             {
  257.                  num=0;
  258.                  risCerca="";
  259.                  testoStp.setText("Inserisci il nome della rubrica        ");
  260.                  testoIns.setText("Inserisci il nome con cui salvare la rubrica        ");
  261.                  avanti.setEnabled(true);
  262.                  indietro.setEnabled(false);
  263.                                  pannelloIndex(true);
  264.                                  pannelloInput(false);
  265.                      pannelloOutput(false);
  266.                                  pannelloRubIns(false);
  267.                                  pannelloRubStp(false);
  268.                                  pannelloCerca(false);
  269.                                  pannelloRisultati(false);
  270.  
  271.                                 }
  272.                  if(command.equals("add"))
  273.                     {
  274.                                 try{
  275.                                  scrittura.println(nome);
  276.                                  scrittura.println(cognome);
  277.                                  scrittura.println(numero);
  278.                                  scrittura.println(indirizzo);
  279.                   }catch(NullPointerException ex)
  280.                    {
  281.                         System.out.println("File insesistente...                        ");
  282.                      }
  283.                      reset();
  284.  
  285.                             }
  286.  
  287.                           if(command.equals("invio"))
  288.                                                     {
  289.                                                                 test=true;
  290.  
  291.                                               if(nameRub.equals(""))
  292.                                                    test=false;
  293.                                                      exFile = new File(nameRub);
  294.                                                      if( ((exFile).exists()) || (test==false))
  295.                                                        {
  296.                                                         inNameRub.setText("");
  297.                                                         if(test==false)
  298.                                                            testoIns.setText("Nome File non corretto.Riprova                        ");
  299.                                                         else
  300.  
  301.                                                            testoIns.setText("File già esistente. Riprova                         ");
  302.                                                                     }
  303.                                                                  else
  304.                                                                    {
  305.  
  306.                                                                    pannelloRubIns(false);
  307.                                                    pannelloInput(true);
  308.                                                     try{
  309.                                                                              fileS = new FileOutputStream(nameRub);
  310.                                                                              scrittura = new PrintStream(fileS);
  311.                                                                         }catch(FileNotFoundException ex)
  312.                                                                         {
  313.                                                                            System.out.println("Errore in lettura File...");
  314.                                                         }
  315.  
  316.                                                                    }
  317.  
  318.  
  319.                                 }
  320.  
  321.                                 if(command.equals("invio "))
  322.                                                             {
  323.                                                                         test=true;
  324.  
  325.                                                     if(nameRub.equals(""))
  326.                                                          test=false;
  327.                                                              exFile = new File(nameRub);
  328.                                                              if( ((exFile).exists()) && (test==true))
  329.                                                                {
  330.                                                                                 letturaFile(nameRub);
  331.                                                                                 stampa();
  332.                                                                             pannelloRubStp(false);
  333.                                                             pannelloOutput(true);
  334.  
  335.                                                                 }
  336.                                                                          else
  337.                                                                            {
  338.                                                                                 inNameRub.setText("");
  339.                                                                 if(test==false)
  340.                                                                    testoStp.setText("Nome File non corretto.Riprova!                       ");
  341.                                                                 else
  342.  
  343.                                                                    testoStp.setText("File non esistente. Riprova!                        ");
  344.                                                                             }
  345.  
  346.  
  347.                                 }
  348.  
  349.                       if(command.equals("reset"))
  350.                         {
  351.                                  reset();
  352.                                 }
  353.                      if(command.equals("save & back"))
  354.                         {
  355.                      pannelloInput(false);
  356.                      pannelloRubIns(false);
  357.                      pannelloRubStp(false);
  358.                      pannelloIndex(true);
  359.                      pannelloOutput(false);
  360.                             }
  361.                          if(command.equals("cerca"))
  362.                            {
  363.                                  test=true;
  364.                                  if(nameRub.equals(""))
  365.                    test=false;
  366.                          exFile = new File(nameRub);
  367.                  if( ((exFile).exists()) && (test==true))
  368.                                     {
  369.                                      stringa = inCerca.getText();
  370.                                          cont=0;
  371.                                          con=0;
  372.                                          linee=0;
  373.                                          cerca = new int[100];
  374.                                      cerca(stringa,nameRub);
  375.                                      i=0;
  376.                      if(cerca[0]!=0)
  377.                        {
  378.                                             do{
  379.                                                stampaRisultati(cerca[i]-1,nameRub);
  380.                                                i++;
  381.                             }while(cerca[i]!=0);
  382.                                          }else{
  383.                                            risCerca="Nessun contatto trovato!!";
  384.                                      }
  385.                          risultati.setText(risCerca);
  386.                          pannelloCerca(false);
  387.                          pannelloRisultati(true);
  388.                                  }else{
  389.                                          inNameRub.setText("");
  390.                                          if(test==false)
  391.                                             testoStp.setText("Nome File non corretto.Riprova!                       ");
  392.                                          else
  393.                                             testoStp.setText("File non esistente. Riprova!                        ");
  394.                              }
  395.  
  396.  
  397.                            }
  398.                      if(command.equals("esci"))
  399.                         {
  400.                      System.exit(0);
  401.                             }
  402.                           if(command.equals("crea rubrica"))
  403.                             {
  404.                      pannelloRubIns(true);
  405.                      pannelloIndex(false);
  406.                                 }
  407.                           if(command.equals("visualizza contatti"))
  408.                             {
  409.                 pannelloRubStp(true);
  410.                 pannelloIndex(false);
  411.                                 }
  412.               if(command.equals("cerca contatti"))
  413.                {
  414.                                  pannelloIndex(false);
  415.                                  pannelloCerca(true);
  416.                            }
  417.                          }
  418.     void letturaFile(String nomeFile)
  419.        {
  420.                   int cont=0;
  421.                   lineeStm=0;
  422.  
  423.       try{
  424.          fileL = new FileReader(nomeFile);
  425.          lettura = new BufferedReader(fileL);
  426.  
  427.          while( (linea = lettura.readLine()) != null )
  428.                 {
  429.                          lineeStm++;
  430.                 }
  431.  
  432.          fileL = new FileReader(nomeFile);
  433.          lettura = new BufferedReader(fileL);
  434.  
  435.             while( cont<num*4 )
  436.                   {
  437.                            cont++;
  438.                            linea = lettura.readLine();
  439.                 }
  440.  
  441.      }catch(FileNotFoundException ex)
  442.          {
  443.           System.out.println("Errore in lettura file, Nessun file trovato!");
  444.          }
  445.          catch(IOException ex)
  446.          {
  447.           System.out.println("Errore in lettura file..");
  448.          }
  449.  
  450.            }
  451.  
  452.      void cerca(String stringRicerca,String nomeFile)
  453.      {
  454.       i=0;
  455.           try{
  456.               fileL = new FileReader(nomeFile);
  457.                   lettura= new BufferedReader(fileL);
  458.                   while( (linea = lettura.readLine()) != null )
  459.                     {
  460.                      linee++;
  461.                     }
  462.                   fileL = new FileReader(nomeFile);
  463.                   lettura = new BufferedReader(fileL);
  464.                   num=1;
  465.                   do{
  466.                      while( cont<num*4 )
  467.                        {
  468.                         cont++;
  469.                             linea = lettura.readLine();
  470.                             if(linea != null)
  471.                               {
  472.                                if(linea.equals(stringRicerca))
  473.                                      {
  474.                                       cerca[i]=num;
  475.                                       i++;
  476.                                  }
  477.                                   }
  478.                    }
  479.                    num++;
  480.                  }while(cont<linee);
  481.          }catch(FileNotFoundException ex)
  482.      {
  483.       System.out.println("Errore in lettura file..");
  484.      }catch(IOException ex)
  485.      {
  486.       System.out.println("Errore in lettura file..");
  487.      }
  488.         }
  489.  
  490.  
  491.     void stampaRisultati(int nume,String nomeFile)
  492.     {
  493.         cont=0;
  494.         con=0;
  495.         linee=0;
  496.             try{
  497.                   fileL = new FileReader(nomeFile);
  498.                   lettura = new BufferedReader(fileL);
  499.  
  500.                    while( (linea = lettura.readLine()) != null )
  501.                          {
  502.                                   linee++;
  503.                           }
  504.                    fileL = new FileReader(nomeFile);
  505.                    lettura = new BufferedReader(fileL);
  506.                    while(cont<nume*4){
  507.                                    linea = lettura.readLine();
  508.                                    cont++;
  509.                           }
  510.  
  511.                                 linea = lettura.readLine();
  512.                                   risCerca+="Nome: "+linea;
  513.  
  514.                                 linea = lettura.readLine();
  515.                                   risCerca+="\nCognome: "+linea;
  516.  
  517.                                 linea = lettura.readLine();
  518.                                   risCerca+="\nNumero Casa: "+linea;
  519.  
  520.                                 linea = lettura.readLine();
  521.                                   risCerca+="\nIndirizzo: "+linea+"\n\n";
  522.  
  523.               }catch(FileNotFoundException ex)
  524.                  {
  525.                   System.out.println("Errore in lettura file..");
  526.                  }
  527.                  catch(IOException ex)
  528.                  {
  529.                   System.out.println("Errore in lettura file..");
  530.               }
  531.  
  532.         }
  533.  
  534.         void stampa()
  535.           {
  536.         try{
  537.           linea = lettura.readLine();
  538.         stmNome.setText("                        Nome :  "+linea+"                                            ");
  539.  
  540.                   linea = lettura.readLine();
  541.             stmCognome.setText("                        Cognome :  "+linea+"                                  ");
  542.  
  543.                   linea = lettura.readLine();
  544.         stmNumero.setText("                         Numero :  "+linea+"                                  ");
  545.  
  546.                   linea = lettura.readLine();
  547.         stmIndirizzo.setText("                        Indirizzo :  "+linea+"                                  ");
  548.     }catch(IOException ex)
  549.     {
  550.           System.out.println("Errore in stampa Dati File.....");
  551.         }catch(NullPointerException ex)
  552.         {
  553.       System.out.println("Nessun file ricevuto...");
  554.         }
  555.  
  556.           }
  557.  
  558.     void reset()
  559.       {
  560.                 inNome.setText("");
  561.                 inCognome.setText("");
  562.                 inNumero.setText("");
  563.                 inIndirizzo.setText("");
  564.           }
  565.  
  566.          void pannelloCerca(boolean att)
  567.            {
  568.                 testoStp.setVisible(att);
  569.                 inNameRub.setVisible(att);
  570.                 infoCerca.setVisible(att);
  571.                 inCerca.setVisible(att);
  572.                 Cerca.setVisible(att);
  573.                 back.setVisible(att);
  574.  
  575.            }
  576.     void pannelloRisultati(boolean att)
  577.       {
  578.            risultati.setVisible(att);
  579.            back.setVisible(att);
  580.           }
  581.          void pannelloInput(boolean att)
  582.           {
  583.           nome.setVisible(att);
  584.           inNome.setVisible(att);
  585.           Congome.setVisible(att);
  586.           inCognome.setVisible(att);
  587.           Numero.setVisible(att);
  588.           inNumero.setVisible(att);
  589.           Indirizzo.setVisible(att);
  590.           inIndirizzo.setVisible(att);
  591.  
  592.           ok.setVisible(att);
  593.           reset.setVisible(att);
  594.           savExit.setVisible(att);
  595.  
  596.           }
  597.          void pannelloOutput(boolean att)
  598.           {
  599.        stmNome.setVisible(att);
  600.            stmCognome.setVisible(att);
  601.        stmNumero.setVisible(att);
  602.            stmIndirizzo.setVisible(att);
  603.            avanti.setVisible(att);
  604.            back.setVisible(att);
  605.        indietro.setVisible(att);
  606.           }
  607.           void pannelloRubIns(boolean att)
  608.            {
  609.                 testoStp.setVisible(false);
  610.             testoIns.setVisible(att);
  611.             inNameRub.setVisible(att);
  612.             invio.setVisible(att);
  613.             invio_.setVisible(false);
  614.            }
  615.  
  616.           void pannelloRubStp(boolean att)
  617.            {
  618.                 testoIns.setVisible(false);
  619.             testoStp.setVisible(att);
  620.             inNameRub.setVisible(att);
  621.             invio.setVisible(false);
  622.             invio_.setVisible(att);
  623.             back.setVisible(att);
  624.            }
  625.           void pannelloIndex(boolean att)
  626.            {
  627.  
  628.          index.setVisible(att);
  629.          indIns.setVisible(att);
  630.          indStp.setVisible(att);
  631.          indCer.setVisible(att);
  632.          exit.setVisible(att);
  633.          spazio.setVisible(att);
  634.        }
  635.  
  636.  }
  637.  
  638.  
  639.  
  640.  
  641. class framePri extends JFrame
  642.  {
  643.     public framePri(int xLoc,int yLoc)
  644.     {
  645.  
  646.         setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  647.         setTitle("Rubrica 3.0  by Enrico");
  648.         setResizable(false);
  649.         setLocation(xLoc,yLoc);
  650.         panelInserimento panelIns = new panelInserimento();
  651.         Container contentPane = getContentPane();
  652.         contentPane.add(panelIns);
  653.     }
  654.      public static final int DEFAULT_WIDTH =300 ;
  655.      public static final int DEFAULT_HEIGHT =245 ;
  656. }