Ajout d'un habit et d'un parfum fonctionnel

master
Louis PERRET 3 years ago
parent 8ade9ad842
commit 3e344a8f74

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefHeight="400.0" prefWidth="600.0">
<HBox>
<Label text="Nom : "/>
<TextField fx:id="nomHabit"/>
</HBox>
<HBox>
<Label text="Prix : "/>
<TextField fx:id="prixHabit"/>
</HBox>
<HBox>
<Button text="Ajouter parfum" onAction="#ajouterHabit"/>
<Button text="Annuler" onAction="#annuler"/>
</HBox>
</VBox>

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefHeight="400.0" prefWidth="600.0">
<HBox>
<Label text="Nom : "/>
<TextField fx:id="nomParfum"/>
</HBox>
<HBox>
<Label text="Prix : "/>
<TextField fx:id="prixParfum"/>
</HBox>
<VBox>
<HBox>
<Label text="Fragrance : "/>
<TextField fx:id="fragranceParfum"/>
<Button onAction="#ajouterFragrance" text="Ajouter fragrance"/>
</HBox>
<Label fx:id="fragrancesAjoutees"/>
</VBox>
<HBox>
<Button text="Ajouter parfum" onAction="#ajouterParfum"/>
<Button text="Annuler" onAction="#annuler"/>
</HBox>
</VBox>

@ -16,8 +16,8 @@
<VBox alignment="CENTER">
<ComboBox/>
<ListView fx:id="listeProduit"/>
<Button text="Ajouter habit"/>
<Button text="Ajouter parfum"/>
<Button text="Ajouter habit" onAction="#lancerFenetreAjoutHabit"/>
<Button text="Ajouter parfum" onAction="#lancerFenetreAjoutParfum"/>
<Button text="Supprimer l'objet sélectionné"/>
</VBox>
</left>

@ -1,5 +1,7 @@
package Modele.metier;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -8,6 +10,10 @@ public class Boutique {
private List<Produit> produits;
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public static final String PROP_AJOUT = "AjoutProduit";
public Boutique(){
produits = new ArrayList<>();
}
@ -16,12 +22,26 @@ public class Boutique {
this.produits = produits;
}
public void addListener(PropertyChangeListener listener){
support.addPropertyChangeListener(listener);
}
public void ajouterProduit(Produit produit){
produits.add(produit);
}
public void ajouterHabit(String nom, int prix){
produits.add(new Habit(nom, prix));
Produit produit = new Habit(nom, prix);
produits.add(produit);
int index = produits.size() - 1;
support.fireIndexedPropertyChange(PROP_AJOUT, index, null, produit);
}
public void ajouterParfum(String nom, int prix){
produits.add(new Parfum(nom, prix));
public void ajouterParfum(String nom, int prix, List<String> fragrances){
Produit produit = new Parfum(nom, prix, fragrances);
produits.add(produit);
int index = produits.size() - 1;
support.fireIndexedPropertyChange(PROP_AJOUT, index, null, produit);
}
public List<Produit> getProduits() {

@ -13,6 +13,11 @@ public class Parfum extends Produit{
fragrances = new ArrayList<>();
}
public Parfum(String nom, int prix, List<String> fragrances) {
this(nom, prix);
this.fragrances = fragrances;
}
public void ajouterFragrance(String fragrance){
fragrances.add(fragrance);
}

@ -0,0 +1,51 @@
package view;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.util.ArrayList;
import java.util.List;
public class AjoutHabit {
@FXML
private TextField nomHabit;
@FXML
private TextField prixHabit;
private boolean isCanceled = false;
@FXML
private void ajouterHabit() {
fermerFenetre();
}
@FXML
private void annuler() {
isCanceled = true;
fermerFenetre();
}
private void fermerFenetre(){
nomHabit.getScene().getWindow().hide();
}
public String getNomHabit(){
return nomHabit.getText();
}
public int getPrixHabit(){
return Integer.parseInt(prixHabit.getText());
}
public boolean isCanceled() {
return isCanceled;
}
}

@ -0,0 +1,76 @@
package view;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import view_modele.ProduitVM;
import java.util.ArrayList;
import java.util.List;
public class AjoutParfum {
@FXML
private Label fragrancesAjoutees;
@FXML
private TextField nomParfum;
@FXML
private TextField prixParfum;
@FXML
private TextField fragranceParfum;
private final List<String> fragrances = new ArrayList<>();
private boolean isCanceled = false;
@FXML
private void ajouterParfum() {
fermerFenetre();
}
@FXML
private void annuler() {
isCanceled = true;
fermerFenetre();
}
private void fermerFenetre(){
fragranceParfum.getScene().getWindow().hide();
}
@FXML
private void ajouterFragrance() {
fragrances.add(fragranceParfum.getText());
fragrancesAjoutees.textProperty().bind(Bindings.concat("Fragrances ajoutées : ", fragrancesToString()));
fragranceParfum.setText("");
; }
private String fragrancesToString(){
StringBuilder str = new StringBuilder();
for(String s : fragrances){
str.append(String.format("%s,",s));
}
return str.toString();
}
public String getNomParfum(){
return nomParfum.getText();
}
public int getPrixParfum(){
return Integer.parseInt(prixParfum.getText());
}
public List<String> getFragrancesParfum(){
return fragrances;
}
public boolean isCanceled() {
return isCanceled;
}
}

@ -1,13 +1,20 @@
package view;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import javafx.util.converter.IntegerStringConverter;
import view.cellules.CelluleProduit;
import view_modele.BoutiqueVM;
import view_modele.ProduitVM;
import java.io.IOException;
public class MainWindow {
@FXML
@ -30,10 +37,62 @@ public class MainWindow {
nomProduit.textProperty().unbindBidirectional(oldV.nomProperty());
prixProduit.textProperty().unbindBidirectional(oldV.nomProperty());
}
if(newV != null){
if(newV != null) {
nomProduit.textProperty().bindBidirectional(newV.nomProperty());
prixProduit.textProperty().bindBidirectional(newV.nomProperty());
}
});
}
@FXML
private void lancerFenetreAjoutParfum(){
Stage secondStage = new Stage();
secondStage.initOwner(listeProduit.getScene().getWindow());
secondStage.initModality(Modality.WINDOW_MODAL);
AjoutParfum controlleur = initAjouterParfum(secondStage);
if(!controlleur.isCanceled()){
boutiqueVM.ajouterParfum(controlleur.getNomParfum(), controlleur.getPrixParfum(), controlleur.getFragrancesParfum());
}
}
private AjoutParfum initAjouterParfum(Stage stageAjoutParfum) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Ajout_Parfum.fxml"));
AjoutParfum controlleur = new AjoutParfum();
loader.setController(controlleur);
try{
stageAjoutParfum.setScene(new Scene(loader.load()));
stageAjoutParfum.showAndWait();
} catch (IOException e) {
Alert alerte = new Alert(Alert.AlertType.ERROR, "Erreur lors du lancement de la fenêtre", ButtonType.OK);
alerte.show();
}
return controlleur;
}
@FXML
private void lancerFenetreAjoutHabit(){
Stage secondStage = new Stage();
secondStage.initOwner(listeProduit.getScene().getWindow());
secondStage.initModality(Modality.WINDOW_MODAL);
AjoutHabit controlleur = initAjouterHabit(secondStage);
if(!controlleur.isCanceled()){
boutiqueVM.ajouterHabit(controlleur.getNomHabit(), controlleur.getPrixHabit());
}
}
private AjoutHabit initAjouterHabit(Stage stageAjoutHabit) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Ajout_Habit.fxml"));
AjoutHabit controlleur = new AjoutHabit();
loader.setController(controlleur);
try{
stageAjoutHabit.setScene(new Scene(loader.load()));
stageAjoutHabit.showAndWait();
} catch (IOException e) {
Alert alerte = new Alert(Alert.AlertType.ERROR, "Erreur lors du lancement de la fenêtre", ButtonType.OK);
alerte.show();
}
return controlleur;
}
}

@ -14,9 +14,13 @@ import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.List;
public class BoutiqueVM {
public class BoutiqueVM implements PropertyChangeListener {
private Boutique modele;
@ -41,16 +45,41 @@ public class BoutiqueVM {
}
initialiserListe();
modele.addListener(this);
}
public void initialiserListe(){
for(Produit p : modele.getProduits()){
if(p instanceof Habit){
produitsObs.add(new HabitVM((Habit)p));
}
else if(p instanceof Parfum){
produitsObs.add(new ParfumVM((Parfum)p));
}
produitsObs.add(creerProduitVM(p));
}
}
private ProduitVM creerProduitVM(Produit p){
ProduitVM produit = null;
if(p instanceof Habit){
produit = new HabitVM((Habit)p);
}
else if(p instanceof Parfum){
produit = new ParfumVM((Parfum)p);
}
return produit;
}
public void ajouterParfum(String nom, int prix, List<String> fragrances){
modele.ajouterParfum(nom, prix, fragrances);
}
public void ajouterHabit(String nom, int prix){
modele.ajouterHabit(nom, prix);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
switch (evt.getPropertyName()){
case Boutique.PROP_AJOUT:
produitsObs.add(((IndexedPropertyChangeEvent)evt).getIndex(), creerProduitVM((Produit)evt.getNewValue()));
break;
}
}
}

Loading…
Cancel
Save