Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cartedetelefon;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInput;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutput;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.table.AbstractTableModel;
- public class CarteDeTelefon extends AbstractTableModel implements Serializable {
- private static List<Abonat> listaContacte = new ArrayList<Abonat>();
- public static File f;
- private static final long serialVersionUID = 1L;
- private static final Logger fLogger = Logger.getLogger(CarteDeTelefon.class.getPackage().getName());
- /**
- * @return the listaContacte
- */
- public static List<Abonat> getListaContacte() {
- return listaContacte;
- }
- /**
- * @param aListaContacte the listaContacte to set
- */
- public static void setListaContacte(List<Abonat> aListaContacte) {
- listaContacte = aListaContacte;
- }
- private final String[] numeColoane = {
- "Nume",
- "Prenume",
- "CNP",
- "Numar telefon"
- };
- // adauga contact in lista
- public void adaugareContact(String nume, String prenume, String cnp, String tel) {
- try {
- try {
- Long s = Long.valueOf(tel);
- getListaContacte().add(new Abonat(nume, prenume, cnp, new NrTel(s)));
- fireTableDataChanged();
- } catch (NumberFormatException numberFormatException) {
- }
- } catch (Exception e) {
- }
- }
- public void modificareContact(Abonat a, int row) {
- try {
- if (listaContacte.size() >= 0) {
- listaContacte.set(row, a);
- }
- fireTableDataChanged();
- } catch (Exception e) {
- }
- }
- public void stergeContact(int row) {
- try {
- try {
- getListaContacte().remove(row);
- fireTableDataChanged();
- } catch (Exception e) {
- }
- } catch (Exception e) {
- }
- }
- public List<Abonat> cautareContact(String str) {
- List<Abonat> returnareContacte = new ArrayList<Abonat>();
- getListaContacte().stream().filter((abonat) -> (abonat.getPrenume().contains(str) || abonat.getNume().contains(str))).forEach((abonat) -> {
- returnareContacte.add(abonat);
- });
- return returnareContacte;
- }
- @Override
- public int getRowCount() {
- if (getListaContacte().size() <= 0) {
- return 0;
- } else {
- return getListaContacte().size();
- }
- }
- @Override
- public int getColumnCount() {
- return numeColoane.length;
- }
- @Override
- public String getColumnName(int col) {
- return numeColoane[col];
- }
- @Override
- public Object getValueAt(int row, int col) {
- if (col == 0) {
- return getListaContacte().get(row).getNume();
- } else if (col == 1) {
- return getListaContacte().get(row).getPrenume();
- } else if (col == 2) {
- return getListaContacte().get(row).getCNP();
- } else if (col == 3) {
- return getListaContacte().get(row).getNrTel();
- }
- return "Eroare";
- }
- @Override
- public void setValueAt(Object aValue, int rowIndex, int colIndex) {
- Abonat abonat = getListaContacte().get(rowIndex);
- switch (colIndex) {
- case 2:
- abonat.setCNP((String) aValue);
- break;
- case 3:
- abonat.setNrTel((NrTel) aValue);
- break;
- }
- fireTableRowsUpdated(rowIndex, rowIndex);
- }
- @Override
- public boolean isCellEditable(int row, int colNum) {
- switch (colNum) {
- case 2:
- return false;
- default:
- return true;
- }
- }
- public void salvareContacte() throws FileNotFoundException, IOException {
- try (
- OutputStream file = new FileOutputStream("contacts.txt");
- OutputStream buffer = new BufferedOutputStream(file);
- ObjectOutput output = new ObjectOutputStream(buffer);) {
- output.writeObject(listaContacte);
- } catch (IOException ex) {
- fLogger.log(Level.SEVERE, "Fisierul nu a putut fi salvat.", ex);
- }
- }
- public void incarcareContacte() throws IOException, ClassNotFoundException {
- try (
- InputStream file = new FileInputStream("contacts.txt");
- InputStream buffer = new BufferedInputStream(file);
- ObjectInput input = new ObjectInputStream(buffer);) {
- @SuppressWarnings("unchecked")
- List<Abonat> recoveredContacts = (List<Abonat>) input.readObject();
- listaContacte = recoveredContacts;
- fireTableDataChanged();
- } catch (ClassNotFoundException | IOException ex) {
- fLogger.log(Level.SEVERE, "Fisierul nu este valid", ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement