1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| package partie2;
import java.awt.Color;
import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import partie_1.Employe;
import partie_1.ListeEmployes;
public class AppletEmploye extends JApplet {
// Déclaration des objets
JOptionPane boite_informations = new JOptionPane ();
JOptionPane coor = new JOptionPane ();
JLabel gestion_employe = new JLabel ("GESTION DES EMPLOYÉS");
// Initialisation de l'applet
public void init () {
this.setSize(500, 200);
this.setLayout(new FlowLayout ());
this.getContentPane().setBackground(Color.DARK_GRAY);
gestion_employe.setForeground(Color.blue);
this.getContentPane().add(gestion_employe);
// Création de la liste des employés
ListeEmployes mesEmployes = new ListeEmployes ();
// Quelques employés pour l'exemple
Employe employe1 = new Employe (01, "Dupont", "Gérard");
Employe employe2 = new Employe (02, "Dupond", "Patrick");
Employe employe3 = new Employe (03, "Durand", "Suzie");
mesEmployes.ajouterEmploye(employe1);
mesEmployes.ajouterEmploye(employe2);
mesEmployes.ajouterEmploye(employe3);
ImageIcon img = new ImageIcon("Images/TD2Applet/icone.png");
String choix = (String) boite_informations.showInputDialog(null, "1-Ajouter\n2-Supprimer\n3-Afficher\n4-Ajouter à l'aide des paramètres\n (ex: 4 num nom prenom) où num est un chiffre\n5-Ajouter à l'aide d'un fichier\n6-Sauvegarder un employe (sérialisation)", "input", JOptionPane.QUESTION_MESSAGE, img, null, null);
String numero = "", nom = "", prenom = "";
if (choix.indexOf("1") != -1) {
numero = (String) boite_informations.showInputDialog(null, "Numero ?", "Ajouter un employé", JOptionPane.QUESTION_MESSAGE, img, null, null);
nom = (String) boite_informations.showInputDialog(null, "Nom ?", "Ajouter un employé", JOptionPane.QUESTION_MESSAGE, img, null, null);
prenom = (String) boite_informations.showInputDialog(null, "prénom ?", "Ajouter un employé", JOptionPane.QUESTION_MESSAGE, img, null, null);
Employe ajout_employe = new Employe (Integer.parseInt(numero), nom, prenom);
mesEmployes.ajouterEmploye(ajout_employe);
}
else if (choix.indexOf("2") != -1) {
numero = (String) boite_informations.showInputDialog(null, "Numero ?", "Supprimer un employé", JOptionPane.QUESTION_MESSAGE, img, null, null);
mesEmployes.supprimerEmploye(Integer.parseInt(numero));
}
else if (choix.indexOf("3") != -1) {
numero = (String) boite_informations.showInputDialog(null, "Numero ?", "Afficher un employé", JOptionPane.QUESTION_MESSAGE, img, null, null);
boite_informations.showMessageDialog(null, mesEmployes.trouverEmploye(Integer.parseInt(numero)), "Afficher un employé", JOptionPane.INFORMATION_MESSAGE, img);
}
else if (choix.indexOf("4") != -1) {
String [] s = choix.split(" ");
Employe ajout_employe_param = new Employe (Integer.parseInt(s[1]), s[2], s[3]);
mesEmployes.ajouterEmploye(ajout_employe_param);
}
else if (choix.indexOf("5") != -1) {
JFileChooser choix_fichier = new JFileChooser ();
int retour = choix_fichier.showOpenDialog(null);
if (retour == JFileChooser.APPROVE_OPTION) {
//lecture du fichier texte
try{
InputStream ips = new FileInputStream(choix_fichier.getSelectedFile().getAbsolutePath());
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine()) != null){
String [] s = ligne.split(";");
Employe ajout_employe_fichier = new Employe (Integer.parseInt(s[0]), s[1], s[2]);
mesEmployes.ajouterEmploye(ajout_employe_fichier);
}
br.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
else if (choix.indexOf("6") != -1) {
numero = (String) boite_informations.showInputDialog(null, "Numero ?", "Sauvegarder un employé", JOptionPane.QUESTION_MESSAGE, img, null, null);
Employe employe_a_sauvegarder = mesEmployes.trouveEmploye(Integer.parseInt(numero));
try {
ObjectOutputStream serialise = new ObjectOutputStream(new FileOutputStream("fichier.dat"));
serialise.writeObject(employe_a_sauvegarder);
serialise.flush();
serialise.close();
boite_informations.showMessageDialog(null, "Attention vous venez de serialiser un employé.\nLe fichier est stocké ici :\n/chemin/de/votre/workspace/bin/", "Serialisation", JOptionPane.WARNING_MESSAGE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(mesEmployes.toString());
}
} |
Partager