0% found this document useful (0 votes)
88 views

Java Examan Final

This document defines classes for products, a store, product listeners, and GUI frames for displaying and managing products in a store. The main classes are: - Produs - Represents a product with a series and price - Magazin - Singleton class that manages the store's product list and notifies listeners of changes - ProdusListener - Interface for classes that listen for product list changes - MainFrame - GUI for displaying the product list and managing save/open operations - AddProductFrame - GUI popup for adding a new product to the store

Uploaded by

fain
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Java Examan Final

This document defines classes for products, a store, product listeners, and GUI frames for displaying and managing products in a store. The main classes are: - Produs - Represents a product with a series and price - Magazin - Singleton class that manages the store's product list and notifies listeners of changes - ProdusListener - Interface for classes that listen for product list changes - MainFrame - GUI for displaying the product list and managing save/open operations - AddProductFrame - GUI popup for adding a new product to the store

Uploaded by

fain
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

CLASA PRODUS

===================================================
public class Produs implements java.io.Serializable{
public String serie;
public double pret;
public Produs(){
}
public Produs(String serie, double pret){
this.serie=serie;
this.pret=pret;
}
public String toString(){
return serie+" "+pret;
}
}
===========================================================
CLASA MAGAZIN
============================================================
import java.util.*;
public class Magazin implements ProdusSubject, java.io.Serializable{
private static Magazin singleton;
private ArrayList<Produs> produse = new ArrayList<>();
private ArrayList<ProdusListener> produsListeners = new ArrayList<>();
private Magazin(){}
public static Magazin getInstance(){
if(singleton == null){
singleton = new Magazin();
}
return singleton;
}
public static void setInstance(Magazin m){
singleton.produse = m.produse;
singleton.notifyProdusListeners();
}
public void addProdus(Produs p){
produse.add(p);
notifyProdusListeners();
}
public void removeProdus(Produs p){
produse.remove(p);
notifyProdusListeners();
}
public ArrayList<Produs> getProduse(){
return produse;
}
public void addProdusListener(ProdusListener p){

produsListeners.add(p);
}
public void notifyProdusListeners(){
for(ProdusListener p: produsListeners){
p.listaProduseModificata();
}
}
}
==========================================================
CLASA PRODUS LISTENER
==========================================================
public interface ProdusListener extends java.io.Serializable{
public void listaProduseModificata();
}
==========================================================
CLASA PRODUS SUBJECT
==========================================================
public interface ProdusSubject{
public void addProdusListener(ProdusListener p);
public void notifyProdusListeners();
}

==========================================================
CLASA MAIN FRAME
==========================================================
import
import
import
import
import

javax.swing.*;
java.awt.*;
java.awt.event.*;
java.util.*;
java.io.*;

public class MainFrame extends JFrame{


private JMenuBar mb = new JMenuBar();
private JMenu m1 = new JMenu("Optiuni");
private JMenuItem mi1 = new JMenuItem("Adauga produs");
private JMenuItem mi2 = new JMenuItem("Save");
private JMenuItem mi3 = new JMenuItem("Open");
private JList<Produs> list = new JList<>();
private DefaultListModel<Produs> model = new DefaultListModel<>();
private JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel e1 = new JLabel("NR: 0");
public MainFrame(){
super("Produse");
setJMenuBar(mb);
mb.add(m1);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
mi1.addActionListener(
new ActionListener(){

public void actionPerformed(ActionEvent ev){


new AddProductFrame();
}
}
);
mi2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ev){
JFileChooser chooser = new JFileChooser();
int r = chooser.showSaveDialog(null);
if(r == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
ObjectOutputStream out = null;
try{

out = new ObjectOutputStream(


new FileOutputStream(file)
);

out.writeObject(Magazin.getInstance());
}catch(Exception e){
// tratarea exceptiei
}finally{
try{
if(out!=null) out.close();
}catch(Exception e){
// tratarea exceptiei
}
}

}
);
mi3.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ev){
JFileChooser chooser = new JFileChooser();
int r = chooser.showOpenDialog(null);
if(r == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
ObjectInputStream in = null;
try{
in = new ObjectInputStream(
new FileInputStream(file)
);
Magazin m = (Magazin) in.readObject();
Magazin.setInstance(m);
}catch(Exception e){
// tratarea exceptiei

}finally{
try{
if(in!=null) in.close();
}catch(Exception e){
// tratarea exceptiei
}
}
}

}
);
new Thread(){
public void run(){
while(true){
e1.setText("NR: "+Magazin.getInstance().getProduse().size());
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
}
}
}.start();
Magazin.getInstance().addProdusListener(
new ProdusListener(){
public void listaProduseModificata(){
model.clear();
ArrayList<Produs> produse =
Magazin.getInstance().getProduse();
for(Produs p: produse){
model.addElement(p);
}
}
}
);
list.addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent ev){
if(ev.getClickCount() == 2){
Produs p = list.getSelectedValue();
Magazin.getInstance().removeProdus(p);
}
}
}
);
add(list);
list.setModel(model);
add(p1, BorderLayout.SOUTH);
p1.add(e1);

setSize(500,500);
setLocationRelativeTo(null);
setVisible(true);

public static void main(String [] args){


new MainFrame();
}
}

==========================================================
CLASA ADD PRODUCT FRAME
==========================================================

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class AddProductFrame extends JFrame{
private JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel e1 = new JLabel("SERIE:");
private JLabel e2 = new JLabel("PRET:");
private JTextField t1 = new JTextField(20);
private JTextField t2 = new JTextField(20);
private JButton b1 = new JButton("Adauga produs");
public AddProductFrame(){
super("Adaugare produs");
setLayout(new GridLayout(3,1));
add(p1);add(p2);add(p3);

}
}

p1.add(e1);p1.add(t1);
p2.add(e2);p2.add(t2);
p3.add(b1);
b1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ev){
String serie = t1.getText();
double pret = Double.parseDouble(t2.getText());
Magazin.getInstance().addProdus(new Produs(serie,pret));
dispose();
}
}
);
pack();//RESTRANGE CONTROALELE PE FEREASTRA
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);

You might also like