Sunteți pe pagina 1din 15

package domain; import java.io.

Serializable; public class Masina implements Serializable { private String Marca; private String NrMatr; private boolean Inchiriata; public Masina(String Marca, String NrMatr, boolean Inchiriata) { this.Marca = Marca; this.NrMatr = NrMatr; this.Inchiriata = Inchiriata; } public String getMarca() { return Marca; } public String getNrMatr() { return NrMatr; } public boolean getInchiriata() { return Inchiriata; } public void SetMarca(String Marca) { this.Marca = Marca; } public void SetNrMatr(String NrMatr) { this.NrMatr = NrMatr; } public void SetInchiriere(boolean Inchiriata) { this.Inchiriata = Inchiriata; } public boolean equals(Masina m) { if( this.NrMatr.equals(m.NrMatr) ) // String-urile se compara cu: s1.equals(s2) if( this.Marca.equals(m.Marca) ) if( this.Inchiriata == m.Inchiriata ) // boolean (tipurile primitive) se compara cu: == return true; return false; } public String toString() { return Marca+" "+NrMatr+" "+Inchiriata+" "; } }

package repository; import import import import import import java.io.BufferedReader; java.io.BufferedWriter; java.io.FileReader; java.io.FileWriter; java.io.IOException; domain.Masina;

public class Repository

{ private FileReader fileReader; input private FileWriter fileWriter; output private BufferedReader bufferedReader; // this is the buffered reader private BufferedWriter bufferedWriter; // this is the buffered writer private String file; private int Capacitate = 100; private Masina[] masini; private int Dimensiune; //dimensiunea vectorului, adica cate pozitii din vector sunt ocupate public Repository() { this.Dimensiune = 0; masini = new Masina[Capacitate]; readFile(file); } public Repository(String file) { this.file = file; this.Dimensiune = 0; masini = new Masina[Capacitate]; readFile(file); } public int getDimensiune() { return this.Dimensiune; } public String getFile() { return this.file; } public boolean EImprumutata(Masina m) { return m.getInchiriata(); } public void clear() { Dimensiune = 0; } public String toString() { String s=""; for(int i=0; i<Dimensiune; i++) { s = s + masini[i].toString() + "\n"; } return s; } public Masina[] getMasini() { Masina[] masini = new Masina[Dimensiune]; for(int i=0; i<Dimensiune; i++) masini[i] = this.masini[i]; // this is the // this is the

return masini; } public void Dublare() { Capacitate = Capacitate*2; Masina[] nou = new Masina[Capacitate]; for(int i=0; i<Dimensiune; i++) { nou[i] = masini[i]; } masini = nou; } public void Adaugare(Masina m) throws RepositoryException { if(Dimensiune > Capacitate) // daca nu mai avem capacitate, o dublam Dublare(); for(int i=0; i<Dimensiune; i++) // cautam daca exista duplicat (sa aiba acelasi nr de inmatriculare) if(masini[i].getNrMatr().equals(m.getNrMatr())) throw new RepositoryException("Eroare: NumarMatricol " + m.getNrMatr() + " duplicat!"); masini[Dimensiune] = m; // adaugam masina Dimensiune = Dimensiune + 1; // crestem dimensiunea repositoriului writeFile(file); // actualizam fisierul } public void Modificare(String nr, Masina m) { for(int i = 0; i < Dimensiune; i++) if(masini[i].getNrMatr().equals(nr) ) masini[i] = m; writeFile(file); fisierul } public void Sterge(String NrMatr) { for(int i =0; i < Dimensiune; i++) if ( masini[i].getNrMatr().equals(NrMatr)) { for(int j = i ; j < Dimensiune-1 ; j++) elementul de la pozitia i masini[j] = masini [j+1]; aducem elementele cu o pozitie mai la stanga Dimensiune = Dimensiune - 1; actualizam dimensiunea } writeFile(file); si fisierul } public void writeFile(String filename)

// actualizam

// stergem // //

// actualizam

{ try { fileWriter = new FileWriter(filename); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(new Integer(Dimensiune).toString()+"\n"); for(int i=0; i < Dimensiune; i++) { bufferedWriter.write(masini[i].toString()+"\n"); } bufferedWriter.close(); fileWriter.close(); } catch (IOException e) { } public void readFile(String filename) { try { fileReader = new FileReader(filename); bufferedReader = new BufferedReader(fileReader); clear(); int dim = Integer.parseInt(bufferedReader.readLine()); Masina m; String line; String str[] = new String[3]; boolean Da; for(int i=0; i<dim; i++) { line = bufferedReader.readLine(); str = line.split(" "); if( str[2].equals("true") ) Da = true; else Da = false; m = new Masina(str[0], str[1], Da); try { Adaugare(m); } catch (RepositoryException e){} vreun duplicat il ignora } bufferedReader.close(); fileReader.close(); }//try catch (IOException e) { System. out.println(e); } } System.out.println(e); }

// daca a fost

} package repository; public class RepositoryException extends Exception { String error; public RepositoryException(String error) { this.error = error; } public String getError() { return error;} }

package controller; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import model.ModelException; import model.Model; import view.View; public class Controller { private Model model; private View view; public Controller(View view, Model model) { this.view = view; this.model = model; this.view.addAddListener(new addListener()); this.view.addDelListener(new delListener()); this.view.addModListener(new modListener()); } class modListener implements ActionListener { public void actionPerformed(ActionEvent e) { String s_vechi_nr; String s_nr; String s_marca; boolean b_inchiriat; try { s_vechi_nr = view.getNrMatVechi(); if ( s_vechi_nr.equals("") ) throw new Exception(); arunca exceptie } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare

// daca e vid

JOptionPane.showMessageDialog(view, "Dati vechiul numar de inmatriculare!", "Error", JOptionPane.ERROR_MESSAGE ); return; // sari peste restul validarilor } try { s_nr = view.getNrMat(); if ( s_nr.equals("") ) throw new Exception(); arunca exceptie } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Dati noul numar de inmatriculare!", "Error", JOptionPane.ERROR_MESSAGE ); return; // sari peste restul validarilor } try { s_marca = view.getMarca(); if ( s_marca.equals("") ) throw new Exception(); arunca exceptie } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Dati noua marca!", "Error", JOptionPane.ERROR_MESSAGE ); return; // sari peste restul validarilor } try { b_inchiriat = view.getInchiriata(); } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Setati Inchiriata!", "Error", JOptionPane.ERROR_MESSAGE ); return; // sari peste restul validarilor } // daca ajunge aici atunci toate sunt valide model.Modificare(s_vechi_nr, s_marca, s_nr, b_inchiriat); // modifica masina model.setValue(model.toVector()); // actualizez lista }

// daca e vid

// daca e vid

} class addListener implements ActionListener { public void actionPerformed(ActionEvent e) { String s_nr; String s_marca; boolean b_inchiriat; try { s_nr = view.getNrMat(); if ( s_nr.equals("") ) throw new Exception(); arunca exceptie } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Dati numar de inmatriculare!", "Error", JOptionPane.ERROR_MESSAGE ); return; sari peste restul validarilor } try { s_marca = view.getMarca(); if ( s_marca.equals("") ) throw new Exception(); arunca exceptie } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Dati marca!", "Error", JOptionPane.ERROR_MESSAGE ); return; sari peste restul validarilor } try { b_inchiriat = view.getInchiriata(); } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Setati Inchiriata!", "Error", JOptionPane.ERROR_MESSAGE ); return; // sari peste restul validarilor } // daca ajunge aici atunci toate sunt valide try {

// daca e vid

//

// daca e vid

//

model.AddMasina(s_marca, s_nr, b_inchiriat); // adauga masina } catch (ModelException e1) { JOptionPane.showMessageDialog(view, e1.getError(), "Error", JOptionPane.ERROR_MESSAGE ); } model.setValue(model.toVector()); // actualizez lista } } class delListener implements ActionListener { public void actionPerformed(ActionEvent e) { String s_nr; // numar de inmatriculare try { s_nr = view.getNrMat(); if ( s_nr.equals("") ) throw new Exception(); // daca e vid arunca exceptie model.DeleteMasina(s_nr); // altfel sterge masina model.setValue(model.toVector()); // actualizez lista } catch (Exception ex) { // daca a fost o exceptie afiseaza eroare JOptionPane.showMessageDialog(view, "Dati numar de inmatriculare!", "Error", JOptionPane.ERROR_MESSAGE ); } } } }

package model; import java.util.Observable; import java.util.Vector; import domain.Masina; import repository.Repository; import repository.RepositoryException; public class Model extends Observable { private Repository repo; private Vector list; public Model(Repository r) { this.repo = r; reset(); } public Masina[] getMasini() { return this.repo.getMasini(); } public Vector toVector() // returneaza un vector de elemente generice E

{ Vector masini = new Vector(); Masina[] toate = getMasini(); for(int i=0; i<toate.length; i++ ) masini.add(toate[i]); return masini; } public void DeleteMasina(String NrMatr) { repo.Sterge(NrMatr); } public void AddMasina (String marca,String NrMatr, boolean Inchiriata) throws ModelException { Masina m = new Masina (marca, NrMatr, Inchiriata); try { repo.Adaugare(m); } catch (RepositoryException e) { throw new ModelException(e.getError()); } } public void Modificare (String NrVechi, String MarcaNou, String NrNou, boolean InchiriataNou) { Masina m = new Masina (MarcaNou, NrNou, InchiriataNou); repo.Modificare(NrVechi, m); } public void Clear() { repo.clear(); } /** Reset to initial value. */ public void reset() { setValue(this.toVector()); } public void setValue(Vector vec) { list = vec; setChanged();//!!!!!!!!!!!!!!!!! notifyObservers(); } public Vector getValue() { return list; } } package model; public class ModelException extends Exception { String error; public ModelException(String error) { this.error = error; } public String getError() { return error;}

package view; import import import import import import import import import import import import import import import import import import import import import import import java.awt.Color; java.awt.Dimension; java.awt.GridLayout; java.awt.Window; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.event.WindowAdapter; java.awt.event.WindowEvent; java.util.Observable; java.util.Observer; java.util.Vector; javax.swing.DefaultListModel; javax.swing.JButton; javax.swing.JCheckBox; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JList; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JScrollPane; javax.swing.JTextField; javax.swing.ListSelectionModel; javax.swing.UIManager;

import model.ModelException; import model.Model; import controller.Controller; public class View extends JFrame implements Observer { private Model model; private JLabel label_NrInmat; private JLabel label_NrInmatVechi; private JLabel label_Marca; private JLabel label_Masini; private JTextField nrInmat_vechi; private JTextField nrInmat; private JTextField Marca; private JButton btn_add; private JButton btn_mod; private JButton btn_del; private JCheckBox checkBox_Inchiriata; private JList lista; private DefaultListModel listModel; public View(Model model) { this.model = model; model.addObserver(this);

try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName () ); (iconite, bare, culori) } catch (Exception e){ e.printStackTrace(); } this.setTitle("Masini Application"); this.setLayout( new GridLayout(1,2,10,10) ); spatiere_orizontala, spatiere_verticala this.addWindowListener(new WindowCloser()); this.setSize(this.getPreferredSize()); this.pack(); //frame.setVisible(true); initComponents(); componentele reset(); } public Dimension getPreferredSize() { return new Dimension(600, 300); } // pixeli orizontala, pixeli verticala public DefaultListModel getListModel() { return listModel; } // returnam modelul listei ca sa il putem accesa din afara clasei public String getNrMat() { return nrInmat.getText(); } // returnam continutul fieldului nrInmat public String getNrMatVechi() { return nrInmat_vechi.getText(); } returnam continutul fieldului nrInmat_vechi public String getMarca() { return Marca.getText(); } // returnam continutul fieldului Marca public boolean getInchiriata() inchiriata sau nu { boolean da = false; if (checkBox_Inchiriata.isSelected()) da = true; return da; cum e } // returnam daca e // // adauga // pune tema sistemului

// linii, coloane,

// nu e selectata // daca e selectata ii dam true // returnam

public void addAddListener(ActionListener add) { btn_add.addActionListener(add); } public void addModListener(ActionListener mod) { btn_mod.addActionListener(mod); }

public void addDelListener(ActionListener del) { btn_del.addActionListener(del); } private JPanel addCheckBox() { JPanel subPanel = new JPanel(); creez un nou panel checkBox_Inchiriata = new JCheckBox("Inchiriata"); // checkBox_Inchiriata.addActionListener(this); listener subPanel.add(checkBox_Inchiriata); adaug checkboxul la panel return subPanel; // returnez panelul } private JPanel addButonAdd() { JPanel subPanel = new JPanel(); btn_add = new JButton("Adauga"); subPanel.add(btn_add); return subPanel; } private JPanel addButonModifica() { JPanel subPanel = new JPanel(); btn_mod = new JButton("Modifica"); subPanel.add(btn_mod); return subPanel; } private JPanel addButonSterge() { JPanel subPanel=new JPanel(); btn_del = new JButton("Sterge"); subPanel.add(btn_del); return subPanel; } private JPanel addInmatVechi() { JPanel subPanel = new JPanel(); label_NrInmatVechi = new JLabel("Numar Inmat Pt Modif:"); subPanel.add(label_NrInmatVechi); nrInmat_vechi = new JTextField(10); label_NrInmatVechi.setLabelFor(nrInmat_vechi); subPanel.add(nrInmat_vechi); return subPanel; } private JPanel addInmat() { JPanel subPanel = new JPanel(); label_NrInmat = new JLabel("Numar Inmatriculare:");

// // creez checkbox // ii adaug un //

subPanel.add(label_NrInmat); nrInmat = new JTextField(10); label_NrInmat.setLabelFor(nrInmat); subPanel.add(nrInmat); return subPanel; } private JPanel addMarca(){ JPanel subPanel = new JPanel(); label_Marca = new JLabel("Marca:"); subPanel.add(label_Marca); Marca = new JTextField(10); label_Marca.setLabelFor(Marca); subPanel.add(Marca); return subPanel; } private JPanel addList() { JPanel subPanel = new JPanel(); label_Masini = new JLabel("Masini"); nou label listModel = new DefaultListModel(); creez un nou model pt lista (ca sa pot face actualizari) lista = new JList(listModel); valorile initiale

// creez un // // creez o lista cu

lista.setSelectionMode( ListSelectionModel. SINGLE_SELECTION); lista sa suporte selectie simpla (cea multipla e implicita) lista.setSelectedIndex(0); // selectez primul index

// fac

JScrollPane listScrollPane = new JScrollPane(lista); // pun lista intr-un scrollPane(in caz ca elementele nu incap, vor aparea bare de scroll) label_Masini.setLabelFor(listScrollPane); // setez labelul la lista subPanel.add(label_Masini); // adaug label la panel subPanel.add(listScrollPane); adaug scrollPane la panel return subPanel; // returnez panelul }

//

private void actualizareLista(DefaultListModel model, Vector v) // function to reload the list { int size = v.size(); model.clear(); // golesc modelul model.setSize(size); // ii pun noua dimensiune for(int i=0; i<size; i++) model.set(i, v.get(i)); }

// ii setez fiecare element

private void initComponents() { JPanel subPanel = new JPanel(); panel JPanel JPanel JPanel JPanel subPanel_stanga = new JPanel(); subPanel_dreapta = new JPanel(); subPanel_dreapta_sus = new JPanel(); subPanel_dreapta_jos = new JPanel();

// creeaza

subPanel_stanga.add( addList() ); lista actualizareLista(listModel, model.toVector()); subPanel_dreapta_sus.add( addInmatVechi() ); subPanel_dreapta_sus.add( addInmat() ); nou subPanel_dreapta_sus.add( addMarca() ); marca subPanel_dreapta_sus.add( addCheckBox() ); inchiriata subPanel_dreapta_jos.add( addButonAdd() ); adaugare subPanel_dreapta_jos.add( addButonModifica() ); modifica

// adauga // actualizez lista

// adauga camp nr vechi // adauga camp nr // adauga camp // adauga camp

// adauga buton // adauga buton

subPanel_dreapta_jos.add( addButonSterge() ); // adauga buton stergere subPanel_dreapta.setLayout( new GridLayout(2,1) ); coloana subPanel_dreapta.add(subPanel_dreapta_sus); partea de sus subPanel_dreapta.add(subPanel_dreapta_jos); partea de jos add(subPanel_stanga); add(subPanel_dreapta); } private void reset() { model.reset(); actualizareLista(listModel, model.getValue()); } public void update(Observable a, Object b) { actualizareLista(listModel, model.getValue()); } public class WindowCloser<E> extends WindowAdapter inchide aplicatia de la x { public void windowClosing(WindowEvent e) { // ca sa se poata // adauga stanga // adauga dreapta // adaug // adaug // 2 linii, 1

Window win = e.getWindow(); // recomandat win.setVisible(false); System.exit(0); // necesar } } } APPlication: import import import import repository.Repository; controller.Controller; view.View; model.Model;

public class App { public static void main( String [ ] args ) { Repository repo = new Repository("repository"); Model model = new Model(repo); View view = new View(model); Controller controller = new Controller(view, model); view.setVisible(true); } }