Application de gestion de caisse en JAVA
Application de gestion de caisse en JAVA
Cet article propose en détaille une application de gestion de caisse en JAVA avec base de données.
Vous pourrez télécharger le fichier au format zip il contient le code sources complet (les classes, la base de donnée, etc.)
Extrait du code source :
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.util.prefs.Preferences;
public class Authentication extends JFrame {
private JPanel contentPane;
private JLabel authentication;
private JLabel idCaissier;
private JTextField loginField;
private JLabel passwordCaissier;
private JPasswordField passwordField;
private JCheckBox rememberme;
private JButton loginCaissier;
private Preferences prefs;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Authentication frame = new Authentication();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void close(){
this.dispose();
}
/**
* Create the frame.
*/
public Authentication() {
setTitle("Authentication");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 361, 261);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
authentication = new JLabel("Authentication");
authentication.setForeground(Color.BLUE);
authentication.setFont(new Font("Tahoma", Font.BOLD, 24));
authentication.setBounds(88, 11, 183, 23);
contentPane.add(authentication);
idCaissier = new JLabel("Login");
idCaissier.setFont(new Font("Tahoma", Font.BOLD, 14));
idCaissier.setBounds(27, 78, 52, 17);
contentPane.add(idCaissier);
passwordCaissier = new JLabel("Password");
passwordCaissier.setFont(new Font("Tahoma", Font.BOLD, 14));
passwordCaissier.setBounds(27, 118, 75, 14);
contentPane.add(passwordCaissier);
loginField = new JTextField();
loginField.setBounds(116, 75, 123, 20);
contentPane.add(loginField);
loginField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(116, 112, 123, 20);
contentPane.add(passwordField);
rememberme = new JCheckBox("Remember me");
rememberme.setBounds(116, 141, 123, 23);
contentPane.add(rememberme);
prefs=Preferences.userNodeForPackage(this.getClass());
final String PREF_NAME = "remember";
loginCaissier = new JButton("Login");
loginCaissier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ManipDB manip=new ManipDB();
Object source= evt.getSource();
String valuelogin = null;
int id=0;
if(source==loginCaissier){
valuelogin=loginField.getText();
id=Integer.parseInt(valuelogin);
char[] passChar=passwordField.getPassword();
String pass=new String(passChar);
if((!valuelogin.equals(""))&&(!pass.equals(""))){
if((manip.verifier(id,pass))==true){
close();
Coord cord=new Coord();
cord.affiche(id,pass);
cord.affiche();
cord.setVisible(true);
}
else
JOptionPane.showMessageDialog(null,"login or password inncorrect","Alert Message",JOptionPane.ERROR_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"login or password empty","Alert Message",JOptionPane.ERROR_MESSAGE);
if (true==rememberme.isSelected())
prefs.put(PREF_NAME, valuelogin);
else
prefs.remove(PREF_NAME);
}
}
});
loginCaissier.setFont(new Font("Tahoma", Font.BOLD, 14));
loginCaissier.setBounds(116, 171, 89, 23);
contentPane.add(loginCaissier);
}
}
…
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class ManipDB {
String query;
Statement stx;
ResultSet rset;
Connection conn;
public ManipDB(){
ConnDisc.connect();
conn=ConnDisc.getConnexion();
rset=null;
}
public ResultSet afficherCaissier(int id){
try{
stx=conn.createStatement();
query="SELECT idCaissier,nomCaissier,passwordCaissier FROM Caissier"+" WHERE idCaissier="+id+"";
rset=stx.executeQuery(query);
}
catch(SQLException ex){
ex.printStackTrace();
}
return rset;
}
public DefaultTableModel afficherStock(){
DefaultTableModel dt = new DefaultTableModel();
dt.addColumn("Stock Unitaire");
dt.addColumn("Stock Lot");
dt.addColumn("chiffre d'affaire Article");
dt.addColumn("chiffre d'affaire Lot");
dt.addColumn("chiffre d'affaire global");
try{
stx=conn.createStatement();
query="SELECT * FROM Stock";
rset=stx.executeQuery(query);
while(rset.next()){
Object []tableau={rset.getInt("stockU"),rset.getInt("stockLot"),rset.getDouble("chiffreArticle"),rset.getDouble("chiffreLot"),
rset.getDouble("chiffreAffaire")};
dt.addRow(tableau);
}
}
catch(SQLException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"ERROR","Message d’avertissement",JOptionPane.ERROR_MESSAGE);
}
return dt;
}
public void acheterArticle(int nb,String nom){
try{
double prixarticle=0.0;
double chiffreaffaire=0.0;
int stocku=0;
stx=conn.createStatement();
query="SELECT prixArticle FROM Article WHERE nomArticle="+nom+" ";
rset=stx.executeQuery(query);
while(rset.next()){
prixarticle=rset.getDouble("prixArticle");
}
query="SELECT stockU,chiffreAffaire FROM Stock";
rset=stx.executeQuery(query);
while(rset.next()){
chiffreaffaire=rset.getDouble("chiffreAffaire");
stocku=rset.getInt("stockU");
}
double prixArticle=prixarticle*nb;
chiffreaffaire+=prixArticle;
stocku-=nb;
if(stocku>0){
query="UPDATE Stock SET chiffreAffaire="+chiffreaffaire+",stockU="+stocku+",chiffreArticle="+prixArticle+"";
stx.executeUpdate(query);
}
else{
JOptionPane.showMessageDialog(null,"stock n'est plus disponible","Message d’avertissement",JOptionPane.ERROR_MESSAGE);
}
}
catch(SQLException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"ERROR","Message d’avertissement",JOptionPane.ERROR_MESSAGE);
}
}
public void acheterLot(int nb,String nom){
try{
double prixlot=0.0;
double chiffreaffaire=0.0;
int stocku=0;
int stocklot = 0;
stx=conn.createStatement();
query="SELECT prixLot FROM Lot"+" WHERE nomLot="+nom+"";
rset=stx.executeQuery(query);
while(rset.next()){
prixlot=rset.getDouble("prixLot");
}
query="SELECT stockU,stockLot,chiffreAffaire FROM Stock";
rset=stx.executeQuery(query);
while(rset.next()){
chiffreaffaire=rset.getDouble("chiffreAffaire");
stocku=rset.getInt("stockU");
stocklot=rset.getInt("stockLot");
}
double prixLot=prixlot*nb;
chiffreaffaire+=prixLot;
System.out.println(chiffreaffaire);
//supposons qu'on a un lot de 3 articles.
stocku-=(nb*3);
stocklot-=nb;
if(stocku>0)
{
query="UPDATE Stock SET chiffreAffaire="+chiffreaffaire+",stockU="+stocku+",stockLot="+stocklot+",chiffreLot="+prixLot+"";
stx.executeUpdate(query);
}
else{
JOptionPane.showMessageDialog(null,"stock n'est plus disponible","Message d’avertissement",JOptionPane.ERROR_MESSAGE);
}
}
catch(SQLException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"stock n'est plus disponible","Message d’avertissement",JOptionPane.ERROR_MESSAGE);
}
}
public DefaultTableModel afficherProduit(int code){
DefaultTableModel dt = new DefaultTableModel();
dt.addColumn("numero");
dt.addColumn("Reference");
dt.addColumn("Designation");
dt.addColumn("Nom");
dt.addColumn("Prix");
try{
stx=conn.createStatement();
query="SELECT * FROM Article WHERE codeArticle="+code+" UNION SELECT * FROM Lot WHERE codeLot="+code+"";;
rset=stx.executeQuery(query);
while(rset.next()){
Object []tableau={rset.getInt(1),rset.getString(2),rset.getString(3),rset.getString(4),rset.getDouble(5)};
dt.addRow(tableau);
}
}
catch(SQLException ex){
ex.printStackTrace();
}
return dt;
}
public boolean verifier(int login,String password){
boolean test=false;
int log =0;
String pass=null;
try{
stx=conn.createStatement();
query="SELECT idCaissier,passwordCaissier FROM caissier";
rset=stx.executeQuery(query);
while(rset.next()|| (test==true)){
log=rset.getInt("idCaissier");
pass=rset.getString("passwordCaissier");
if((log==login) && (pass.equals(password)))
test=true;
}
}
catch(SQLException ex){
ex.printStackTrace();
}
System.out.println(test);
System.out.println(log);
System.out.println(pass);
return test;
}
}