Java Code Solutions for Bank Account Exercise
Question 1: Creating the `CompteEnBanque` Class
public class CompteEnBanque {
private int numeroCompte;
private String nom;
private String prenom;
private String ville;
private int leSolde;
public void initialisation(int numCompte, String leNom, String lePrenom, String laVille) {
this.numeroCompte = numCompte;
this.nom = leNom;
this.prenom = lePrenom;
this.ville = laVille;
this.leSolde = 0;
Question 2: Writing the Methods of the `CompteEnBanque` Class
public class CompteEnBanque {
// Class properties and constructor omitted for brevity
public void crediter(int laSomme) {
this.leSolde += laSomme;
}
public boolean debiter(int laSomme) {
if (laSomme <= this.leSolde) {
this.leSolde -= laSomme;
return true;
} else {
return false;
public int donneSolde() {
return this.leSolde;
public int donneNumCompte() {
return this.numeroCompte;
public void affiche() {
System.out.println("Numéro de Compte: " + this.numeroCompte);
System.out.println("Nom: " + this.nom);
System.out.println("Prénom: " + this.prenom);
System.out.println("Ville: " + this.ville);
System.out.println("Solde: " + this.leSolde + " EUR");
Question 3: Instantiating Objects and Setting Properties
public class Main {
public static void main(String[] args) {
CompteEnBanque ceb1 = new CompteEnBanque();
ceb1.initialisation(1, "Dupont", "Jean", "Paris");
CompteEnBanque ceb2 = new CompteEnBanque();
ceb2.initialisation(2, "Martin", "Alice", "Lyon");
ceb1.affiche();
ceb2.affiche();
Question 4: Displaying Client Information
public class Main {
public static void main(String[] args) {
// Initialization code omitted for brevity
ceb1.affiche();
ceb2.affiche();
Question 5: Crediting Accounts
public class Main {
public static void main(String[] args) {
// Initialization code omitted for brevity
ceb1.crediter(300);
ceb2.crediter(500);
ceb1.affiche();
ceb2.affiche();
Question 6: Displaying Balances
public class Main {
public static void main(String[] args) {
// Initialization and crediting code omitted for brevity
System.out.println("Solde de Ceb1: " + ceb1.donneSolde() + " EUR");
System.out.println("Solde de Ceb2: " + ceb2.donneSolde() + " EUR");
Question 7: Debiting Accounts
public class Main {
public static void main(String[] args) {
// Initialization and crediting code omitted for brevity
ceb2.debiter(400);
System.out.println("Solde de Ceb2 après débit: " + ceb2.donneSolde() + " EUR");
}
Question 8: Displaying Balances After Debit
public class Main {
public static void main(String[] args) {
// Initialization, crediting, and debiting code omitted for brevity
System.out.println("Solde de Ceb1: " + ceb1.donneSolde() + " EUR");
System.out.println("Solde de Ceb2: " + ceb2.donneSolde() + " EUR");
Question 9: Transferring Money Between Accounts
public class CompteEnBanque {
// Class properties, constructor, and methods omitted for brevity
public boolean transferer(CompteEnBanque compteDestinataire, int laSomme) {
if (this.debiter(laSomme)) {
compteDestinataire.crediter(laSomme);
return true;
} else {
return false;
}
public class Main {
public static void main(String[] args) {
// Initialization, crediting, and debiting code omitted for brevity
boolean transfert = ceb1.transferer(ceb2, 100);
System.out.println("Transfert de 100 EUR de Ceb1 vers Ceb2: " + (transfert ? "Réussi" : "Échoué"));
System.out.println("Solde de Ceb1 après transfert: " + ceb1.donneSolde() + " EUR");
System.out.println("Solde de Ceb2 après transfert: " + ceb2.donneSolde() + " EUR");
Question 10: Comparing Two Accounts
public class CompteEnBanque {
// Class properties, constructor, and methods omitted for brevity
public static int compare(CompteEnBanque compte1, CompteEnBanque compte2) {
if (compte1.donneSolde() > compte2.donneSolde()) {
return 1;
} else if (compte1.donneSolde() == compte2.donneSolde()) {
return 0;
} else {
return -1;
}
public class Main {
public static void main(String[] args) {
// Initialization, crediting, and debiting code omitted for brevity
int comparison = CompteEnBanque.compare(ceb1, ceb2);
if (comparison == 1) {
System.out.println("Ceb1 a plus d'argent que Ceb2.");
} else if (comparison == 0) {
System.out.println("Ceb1 et Ceb2 ont le même solde.");
} else {
System.out.println("Ceb1 a moins d'argent que Ceb2.");