Created
April 9, 2020 13:42
-
-
Save GravenilvecTV/c53cc38544ba4eb43835c0ae996987bf to your computer and use it in GitHub Desktop.
Correction TP 21/30 - Java 4 - Banque (Part 2 JavaFX)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
public class CompteCourant { | |
// attributs | |
private String nomProprietaire; | |
private int montant; | |
private int decouvertAutorise; | |
// constructeur | |
public CompteCourant(String nomProprietaire, int montant){ | |
this.nomProprietaire = nomProprietaire; | |
this.montant = montant; | |
this.decouvertAutorise = 0; | |
} | |
// methodes | |
public void afficherSolde(){ | |
System.out.println("Solde : " + this.montant + "€"); | |
} | |
public void activerDecouvert(){ | |
this.decouvertAutorise = -150; | |
} | |
public void deposerArgent(int montant){ | |
this.montant += montant; | |
} | |
public int getSolde(){ | |
return this.montant; | |
} | |
public void retirerArgent(int montant){ | |
int montantApresCalcul = this.montant - montant; | |
if(montantApresCalcul >= this.decouvertAutorise){ | |
this.montant -= montant; | |
}else{ | |
System.out.println("Vous n'avez pas les fonds !"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.geometry.Pos; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
// création d'un objet compte courant | |
CompteCourant gravenMoney = new CompteCourant("Graven", 1200); | |
// personnaliser la fenetre | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setResizable(false); | |
// créer une boite | |
VBox box = new VBox(10); | |
box.setAlignment(Pos.CENTER); | |
// créer un composant texte -> Label | |
Label soldeTxt = new Label(); | |
soldeTxt.setFont(new Font(50)); | |
soldeTxt.setText(gravenMoney.getSolde() + "€"); | |
// créer un composant bouton -> Button | |
Button deposerArgentBtn = new Button(); | |
deposerArgentBtn.setText("Déposer 100€"); | |
deposerArgentBtn.setOnAction(e -> { | |
gravenMoney.deposerArgent(100); | |
soldeTxt.setText(gravenMoney.getSolde() + "€"); | |
}); | |
// créer un composant bouton -> Button | |
Button retirerArgentBtn = new Button(); | |
retirerArgentBtn.setText("Retirer 50€"); | |
retirerArgentBtn.setOnAction(e -> { | |
gravenMoney.retirerArgent(50); | |
soldeTxt.setText(gravenMoney.getSolde() + "€"); | |
}); | |
// créer un composant bouton -> Button | |
Button activerDecouvertBtn = new Button(); | |
activerDecouvertBtn.setText("Activer Decouvert"); | |
activerDecouvertBtn.setOnAction(e -> { | |
gravenMoney.activerDecouvert(); | |
}); | |
// ajoute le composant dans la boite | |
box.getChildren().addAll(soldeTxt, deposerArgentBtn, retirerArgentBtn, activerDecouvertBtn); | |
// créer la scene | |
primaryStage.setScene(new Scene(box, 300, 275)); | |
// afficher le resultat | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merci Graven