codice:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.StringTokenizer;
public class GestioneVendite extends JFrame implements ActionListener{
// variabili booleane che indicano se i pulsanti sono abilitati o no
// mod=pulsante modifica;rim=pulsante rimuovi;
public boolean mod=true,rim=true,ins=true,esci=true;
public int g,ko;
public String[] t={"a","b","c"};
public JButton confermaModificaButton = new JButton("CONFERMA");
public JButton annullaModificaButton = new JButton("ANNULLA");
public JButton confermaInserisciButton = new JButton("INSERISCI");
public JButton annullaInserisciButton = new JButton("ANNULLA");
public ModificaCarrelloPanel pannelloMod;
public InserisciCarrelloPanel insPanel;
public Color tcolor=new Color(190,210,190);
public JButton inserisciButton= new JButton("INSERISCI ARTICOLO");
public JButton rimuoviButton= new JButton("RIMUOVI ARTICOLO");
public JButton modificaButton= new JButton("MODIFICA ARTICOLO");
public JButton confermaVenditaButton=new JButton("CONFERMA VENDITA");
public Label etichettaCarrello1=new Label(" ARTICOLI PRESENTI NEL CARRELLO");
public Label etichettaCarrello2=new Label(" CODICE TAGLIA QNT.");
public JButton esciButton= new JButton("Torna al menu principale");
public ListaCarrelloPanel listas;
JPanel su=new JPanel();
JPanel giu=new JPanel();
public GestioneVendite() {
super("G E S T I O N E V E N D I T E");
this.setSize(new Dimension(1024,768));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Carrello.svuota();
// setto i colori dei pulsanti
Container contMenu=this.getContentPane();
esciButton.setBackground(tcolor);
inserisciButton.setBackground(tcolor);
rimuoviButton.setBackground(tcolor);
modificaButton.setBackground(tcolor);
esciButton.setBackground(tcolor);
confermaVenditaButton.setBackground(tcolor);
// creo i pannelli
listas=new ListaCarrelloPanel();
JPanel p1=new MyPanel();
JPanel p2=new MyPanel();
JPanel p3=new MyPanel();
contMenu.add(p1,BorderLayout.WEST);
contMenu.add(p2,BorderLayout.CENTER);
contMenu.add(p3,BorderLayout.EAST);
p1.setLayout(new GridLayout(0,1));
p1.add(inserisciButton);
p1.add(rimuoviButton);
p1.add(modificaButton);
p1.add(confermaVenditaButton);
p2.setLayout(new GridLayout(2,1));
p2.add(su);
p2.add(giu);
su.setLayout(new GridLayout(3,1));
su.add(etichettaCarrello1);
//su.add(etichettaCarrello2);
su.add(listas,BorderLayout.WEST);
p3.add(esciButton);
this.setVisible(true); //riga 77: .<init>
//show();
confermaVenditaButton.addActionListener(this);
inserisciButton.addActionListener(this);
esciButton.addActionListener(this);
rimuoviButton.addActionListener(this);
modificaButton.addActionListener(this);
annullaModificaButton.addActionListener(this);
confermaModificaButton.addActionListener(this);
confermaInserisciButton.addActionListener(this);
annullaInserisciButton.addActionListener(this);
//gestione ingresso alla vendita
String idCliente=JOptionPane.showInputDialog(null,"INSERISCI UN CODICE CLIENTE VALIDO O DIGITA:\"nuovo\"","nuovo");
boolean validitaCliente =Cliente.ricerca(idCliente);
if (!validitaCliente){JOptionPane.showMessageDialog(null,"ATTENZIONE NON HAI INSERITO UN CODICE CLIENTE VALIDO PER CUI SARAI REINDIRIZZATO A GESTIONE CLIENTI","WARNING",JOptionPane.WARNING_MESSAGE);
this.setVisible(false);
//this.hide();
GestioneClienti gcl=new GestioneClienti();
}
}
public void actionPerformed(ActionEvent e){
// SCELTA INSERISCI
if (e.getSource()==inserisciButton && ins==true){
mod=rim=ins=esci=false;
System.out.println("inserisci articolo");
insPanel=new InserisciCarrelloPanel();
giu.add(insPanel);
insPanel.giu.add(confermaInserisciButton);
insPanel.giu.add(annullaInserisciButton);
this.setVisible(true);
//show();
}
// SCELTA CONFERMA INSERISCI
if(e.getSource()==confermaInserisciButton){
try{ int conta=0,quant;
quant=Integer.parseInt(insPanel.text_quantita.getText());
System.out.println("ho premuto conferma inserisci");
conta=Articolo.ricerca(insPanel.text_codice.getText(),insPanel.text_taglia.getText());
System.out.println("conta="+conta);
if(conta==0){
JOptionPane.showMessageDialog(null,"NON C'E DISPONIBILE NESSUN ARTICOLO CON QUEL CODICE","WARNING",JOptionPane.WARNING_MESSAGE);
System.out.println("articolo non disponibile");
}
else if(conta<quant){
JOptionPane.showMessageDialog(null,"ATTENZIONE HAI INSERITO UNA QUANTITA MAGGIORE DI QUELLA DISPONIBILE (inserito="+quant+")(disp="+conta+")","WARNING",JOptionPane.WARNING_MESSAGE);
System.out.println("articolo non disponibile");
}
else {Carrello car1=new Carrello(insPanel.text_codice.getText(),Integer.parseInt(insPanel.text_taglia.getText()),Integer.parseInt(insPanel.text_quantita.getText()));
car1.inserisci();
listas.refresh(listas.querynow);
insPanel.setVisible(false);
//insPanel.hide();
repaint();
mod=rim=ins=esci=true;
}}
catch(Exception er){
JOptionPane.showMessageDialog(null,"ATTENZIONE I DATI INSERITI NON SONO VALIDI","WARNING",JOptionPane.WARNING_MESSAGE);
}}
//SCELTA ANNULLA INSERISCI
if (e.getSource()==annullaInserisciButton){
insPanel.setVisible(false);;
//insPanel.hide();
repaint();
mod=rim=ins=esci=true;
}
// SCELTA ESCI
if (e.getSource()==esciButton && esci==true){
System.out.println("esci");
this.setVisible(false);
//this.hide();
new MenuPrincipale();
}
//SCELTA RIMUOVI
if (e.getSource()==rimuoviButton && rim==true && (listas.ritornaIndice()>=0)){
if(( JOptionPane.showConfirmDialog(null,"SEI SICURO??","QUESTION",JOptionPane.YES_NO_OPTION,2))==0)
{
g=listas.ritornaIndice();
Articolo.rimuovi(listas.getQuerys(),g);
listas.refresh(listas.querynow);
System.out.println("rimuovi g="+g+listas.querynow);
}
}
// SCELTA MODIFICA
if(e.getSource()==modificaButton && mod==true && (listas.ritornaIndice()>=0) ){
mod=rim=ins=esci=false;
listas.list.setEnabled(false);
g=listas.ritornaIndice();
String[] temp=listas.estraiDati(listas.getQuerys());
try{ StringTokenizer tsrt=new StringTokenizer(temp[g]);
for(int hi=0;hi<3;hi++){
t[hi]=tsrt.nextToken();}
pannelloMod=new ModificaCarrelloPanel(t);
giu.add(pannelloMod);
pannelloMod.giu.add(annullaModificaButton);
pannelloMod.giu.add(confermaModificaButton);
this.setVisible(true);
//show();
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,"Probema nell'inizializzazione\n dei campi del form MODIFICA","WARNING",JOptionPane.WARNING_MESSAGE);
mod=rim=ins=esci=true;}
}
//SCELTA ANNULLA MODIFICA
if (e.getSource()==annullaModificaButton){
giu.remove(pannelloMod);
System.out.println("annMod");
repaint();
this.setVisible(true);
//show();
mod=rim=ins=esci=true;
listas.list.setEnabled(true);
}
//SCELTA CONFERMA MODIFICA
if (e.getSource() == confermaModificaButton && listas.ritornaIndice()>=0) {
try {
String t2[]=new String[3];
for (int h=0;h<3;h++)t2[h]=pannelloMod.ritornaValori()[h];
int pezziPresenti=Articolo.ricerca(t2[0],""+t2[1]);
int quantity=Integer.parseInt(t2[2]);
if (quantity>pezziPresenti)JOptionPane.showMessageDialog(null,"ATTENZIONE HAI INSERITO QUANTITA="+quantity+" ,I PREZZI PRESENTI IN MAGAZZINO SONO:"+pezziPresenti ,"WARNING",JOptionPane.WARNING_MESSAGE);
else{System.out.println(g+"query pre conferma"+listas.querynow);
int ko=listas.ritornaIndice();
System.out.println("numeroko="+ko);
Carrello.rimuovi(listas.querynow,ko);
for(int k=0;k<3;k++)System.out.println(t2[k]);
Carrello.modifica(t2[0],Integer.parseInt(t2[1]),Integer.parseInt(t2[2]));
listas.refresh(listas.querynow);
listas.list.setEnabled(true);
giu.remove(pannelloMod);
repaint();
this.setVisible(true);
//show();
mod=rim=ins=esci=true;}}
catch(Exception exc){JOptionPane.showMessageDialog(null,"ATTENZIONE UNO O PIU DATI POTREBBERO ESSERE NON VALIDI","WARNING",JOptionPane.WARNING_MESSAGE);
}}
//SCELTA CONFERMA VENDITA
if (e.getSource() == confermaVenditaButton) {
System.out.println("indice"+listas.ritornaIndice());
if(Carrello.isVoid())JOptionPane.showMessageDialog(null,"ATTENZIONE NESSUN ARTICOLO IN CARRELLO","WARNING",JOptionPane.WARNING_MESSAGE);
else{
try {//niente
}
catch(Exception exc){JOptionPane.showMessageDialog(null,"ATTENZIONE ERRORE NELLA TRANSAZIONE","WARNING",JOptionPane.WARNING_MESSAGE);
}
}
}
}
}