Advertisement
Guest User

Phonebook class

a guest
Jul 4th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. package cartedetelefon;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.ObjectInput;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutput;
  14. import java.io.ObjectOutputStream;
  15. import java.io.OutputStream;
  16. import java.io.Serializable;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.swing.table.AbstractTableModel;
  22.  
  23. public class CarteDeTelefon extends AbstractTableModel implements Serializable {
  24.  
  25.     private static List<Abonat> listaContacte = new ArrayList<Abonat>();
  26.     public static File f;
  27.     private static final long serialVersionUID = 1L;
  28.     private static final Logger fLogger = Logger.getLogger(CarteDeTelefon.class.getPackage().getName());
  29.  
  30.     /**
  31.      * @return the listaContacte
  32.      */
  33.     public static List<Abonat> getListaContacte() {
  34.         return listaContacte;
  35.     }
  36.  
  37.     /**
  38.      * @param aListaContacte the listaContacte to set
  39.      */
  40.     public static void setListaContacte(List<Abonat> aListaContacte) {
  41.         listaContacte = aListaContacte;
  42.     }
  43.  
  44.     private final String[] numeColoane = {
  45.         "Nume",
  46.         "Prenume",
  47.         "CNP",
  48.         "Numar telefon"
  49.     };
  50.  
  51.     // adauga contact in lista
  52.     public void adaugareContact(String nume, String prenume, String cnp, String tel) {
  53.  
  54.         try {
  55.             try {
  56.                 Long s = Long.valueOf(tel);
  57.                 getListaContacte().add(new Abonat(nume, prenume, cnp, new NrTel(s)));
  58.  
  59.                 fireTableDataChanged();
  60.             } catch (NumberFormatException numberFormatException) {
  61.             }
  62.         } catch (Exception e) {
  63.         }
  64.  
  65.     }
  66.  
  67.     public void modificareContact(Abonat a, int row) {
  68.         try {
  69.             if (listaContacte.size() >= 0) {
  70.                 listaContacte.set(row, a);
  71.             }
  72.             fireTableDataChanged();
  73.         } catch (Exception e) {
  74.         }
  75.     }
  76.  
  77.     public void stergeContact(int row) {
  78.  
  79.         try {
  80.             try {
  81.                 getListaContacte().remove(row);
  82.                 fireTableDataChanged();
  83.             } catch (Exception e) {
  84.             }
  85.         } catch (Exception e) {
  86.         }
  87.     }
  88.  
  89.     public List<Abonat> cautareContact(String str) {
  90.         List<Abonat> returnareContacte = new ArrayList<Abonat>();
  91.         getListaContacte().stream().filter((abonat) -> (abonat.getPrenume().contains(str) || abonat.getNume().contains(str))).forEach((abonat) -> {
  92.             returnareContacte.add(abonat);
  93.         });
  94.         return returnareContacte;
  95.     }
  96.  
  97.     @Override
  98.     public int getRowCount() {
  99.         if (getListaContacte().size() <= 0) {
  100.             return 0;
  101.         } else {
  102.             return getListaContacte().size();
  103.         }
  104.  
  105.     }
  106.  
  107.     @Override
  108.     public int getColumnCount() {
  109.         return numeColoane.length;
  110.     }
  111.  
  112.     @Override
  113.     public String getColumnName(int col) {
  114.         return numeColoane[col];
  115.     }
  116.  
  117.     @Override
  118.     public Object getValueAt(int row, int col) {
  119.         if (col == 0) {
  120.             return getListaContacte().get(row).getNume();
  121.         } else if (col == 1) {
  122.             return getListaContacte().get(row).getPrenume();
  123.         } else if (col == 2) {
  124.             return getListaContacte().get(row).getCNP();
  125.         } else if (col == 3) {
  126.             return getListaContacte().get(row).getNrTel();
  127.         }
  128.  
  129.         return "Eroare";
  130.     }
  131.  
  132.     @Override
  133.     public void setValueAt(Object aValue, int rowIndex, int colIndex) {
  134.  
  135.         Abonat abonat = getListaContacte().get(rowIndex);
  136.         switch (colIndex) {
  137.             case 2:
  138.                 abonat.setCNP((String) aValue);
  139.                 break;
  140.             case 3:
  141.                 abonat.setNrTel((NrTel) aValue);
  142.                 break;
  143.         }
  144.         fireTableRowsUpdated(rowIndex, rowIndex);
  145.  
  146.     }
  147.  
  148.     @Override
  149.     public boolean isCellEditable(int row, int colNum) {
  150.         switch (colNum) {
  151.             case 2:
  152.                 return false;
  153.             default:
  154.                 return true;
  155.         }
  156.     }
  157.  
  158.     public void salvareContacte() throws FileNotFoundException, IOException {
  159.  
  160.         try (
  161.                 OutputStream file = new FileOutputStream("contacts.txt");
  162.                 OutputStream buffer = new BufferedOutputStream(file);
  163.                 ObjectOutput output = new ObjectOutputStream(buffer);) {
  164.             output.writeObject(listaContacte);
  165.         } catch (IOException ex) {
  166.             fLogger.log(Level.SEVERE, "Fisierul nu a putut fi salvat.", ex);
  167.         }
  168.  
  169.     }
  170.  
  171.     public void incarcareContacte() throws IOException, ClassNotFoundException {
  172.  
  173.         try (
  174.                 InputStream file = new FileInputStream("contacts.txt");
  175.                 InputStream buffer = new BufferedInputStream(file);
  176.                 ObjectInput input = new ObjectInputStream(buffer);) {
  177.  
  178.             @SuppressWarnings("unchecked")
  179.             List<Abonat> recoveredContacts = (List<Abonat>) input.readObject();
  180.  
  181.             listaContacte = recoveredContacts;
  182.             fireTableDataChanged();
  183.  
  184.         } catch (ClassNotFoundException | IOException ex) {
  185.             fLogger.log(Level.SEVERE, "Fisierul nu este valid", ex);
  186.         }
  187.     }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement