Salut, je fais un projet de compte bancaire et je rencontre un problème...
J'aimerais que le résultat de mon programme donne quelque chose comme ça :
Mes classes sont les suivantes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 Client: Germaine Compte 1235 Transactions: No. Type Description montant 10002 DEP 750.0 10003 DEP 550.0 10004 TRA Transfert au compte 1234 1000.0 10005 RET -1000.0 SOLDE: 5300.0 Client: Alfred Compte 1234 Transactions: No. Type Description montant 10000 RET -500.0 10001 DEP 1000.0 10006 TRA Transfert du compte 1235 1000.0 10007 DEP 1000.0 SOLDE: 9500.0
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package ca.qc.cegepoutaouais.java146; import java.util.ArrayList; import ca.qc.cegepoutaouais.java146.TypeTransac.Transaction; /** * Un objet <code>Compte</code> represente un compte bancaire et permet des transactions. * Le possesseur du compte est un objet <code>Client</code> * @see Client * @author François Audet * @param <Transaction> * */ public class Compte { private Client client; private double solde; private int noCompte; private static int nextID = 1234; private ArrayList<Transaction>transacList; /** * * @param <transaction> * @param c <code>Client</code> qui possède le compte. * @param s Solde initial au compte * */ public Compte(Client c, double s) { client = c; solde = s; noCompte = nextID++; // No de compte transacList = new ArrayList<Transaction>(); } public Compte(Client c) { // constructeur à un seul argument this(c, 0); } public Compte(Client c, int i) { } public double getSolde() { return solde; } public Client getClient() { return client.clone(); } public int getNoCompte(){ return noCompte; } public void depot(double m){ solde += m; } /** * Retire le montant spécifié comme argument, si le solde le permet, * sinon met le compte à zero. * @param m le montant à retirer * @return le montant effectivement retiré du compte */ public double retrait(double m) { double retraitEffectif; if (m > solde) { retraitEffectif = solde; solde = 0; } else { retraitEffectif = m; solde -= m; } return retraitEffectif; } public void transfert(Compte autreCompte, double montant) { double retraitEffectif = retrait(montant); // si possible, retire montant de ce compte autreCompte.depot(retraitEffectif); // dépose le montant réel retiré dans l'autre compte } public void ajouteTransaction(Transaction t) { transacList.add(t); } @Override public String toString() { return getClass().getName() + "[client: " + client.toString() + "solde:" + solde + "]"; } @Override public boolean equals(Object autreObj) { if (this == autreObj) return true; if (autreObj == null) return false; if (getClass() != autreObj.getClass()) return false; Compte autreCompte = (Compte) autreObj; return client.equals(autreCompte.client) && solde == autreCompte.solde; } @Override public int hashCode() { return client.hashCode() + new Double(solde).hashCode(); } public String afficheEtatCompte() { for (Transaction t : transacList) { t.affiche(); System.out.println("SOLDE: " + solde); } return afficheEtatCompte(); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package ca.qc.cegepoutaouais.java146; /** * Un <code>Client</code> est un objet qui représente * le possesseur d'un objet <code>Compte</code>. * @see Compte * @author François Audet */ public class Client { private String nom; private int age; private static int nbClients = 0; /** Constructeur de la classe <code>Client</code> * * @param n le nom du client * @param a l'âge du client */ public Client(String n, int a) { nom = n; age = a; nbClients++; // compte le nombre de clients créés } public Client(String n) { // constructeur à un seul argument this(n, 18); } // constructeur utilisé uniquement par la méthode clone() // afin de ne pas incrémenter inutilement le compteur nbClients private Client(String n, int a, boolean byPass){ nom = n; age = a; } /** * * @return le nom du client */ public String getNom() { return nom; } /** * * @return l'âge du client */ public int getAge() { return age; } public static int getNbClients(){ return nbClients; } /** * @return une copie du client */ @Override public Client clone() { return new Client(this.nom, this.age, true); } @Override public String toString() { return getClass().getName() + "[nom: " + nom + " Âge: " + age + "]"; } @Override public boolean equals(Object autreObj) { if (this == autreObj) return true; if (autreObj == null) return false; if (getClass() != autreObj.getClass()) return false; Client autreClient = (Client) autreObj; return nom.equals(autreClient.nom) && age == autreClient.age; } @Override public int hashCode() { return 7 * nom.hashCode() + new Integer(age).hashCode() * 11; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package ca.qc.cegepoutaouais.java146; /** * Un objet <code>Transaction</code> représente les transactions d'un compte bancaire. * Celui qui fait des transactions est l'objet <code>Compte</code> * @see Compte * @author Yann Danis * */ enum TypeTransac {DEP, RET, TRA, CHE; public static class Transaction { // Déclaration de variables ... TypeTransac transacList; String description; private static int noTransaction = 10000; private double montant; // constructeurs public Transaction(TypeTransac t, String info, double m) { transacList = t; description = info; montant = m; noTransaction++; // Numéro transaction } public Transaction(TypeTransac t, double m) { // Constructeur à deux arguments } public String affiche() { System.out.println("Transaction:\n" + "No.\t" + "Type\t" + "Description\t" + "montant\n"); return description; } @Override public String toString() { return getClass().getName() + "No." + noTransaction + "Type" + transacList + "montant" + montant + "]"; } @Override public boolean equals(Object autreObj) { if (this == autreObj) return true; if (autreObj == null) return false; if (getClass() != autreObj.getClass()) return false; Transaction autreCompte = (Transaction) autreObj; return description.equals(autreCompte.description) && noTransaction == autreCompte.noTransaction; } public int hashcode() { return noTransaction; } } }Mon problème est le suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package ca.qc.cegepoutaouais.java146; public class CompteTest { public static void main(String[] args) { // Test la classe Compte // on se crée deux clients Client alfred = new Client ("Alfred", 65); Client germaine = new Client ("Germaine", 58); // On crée un compte pour Alfred avec 8000 $ Compte compteAlfred = new Compte(alfred, 8000); // On crée un compte pour Germaine avec 5000 $ Compte compteGermaine = new Compte(germaine, 5000); // opérations bancaires compteAlfred.retrait(500); compteAlfred.depot(1000); compteGermaine.depot(750); compteGermaine.depot(550); compteGermaine.transfert(compteAlfred, 1000); // Affiche les états de comptes compteGermaine.afficheEtatCompte(); compteAlfred.afficheEtatCompte(); } }
Merci de votre temps et j'apprécie toute aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 Exception in thread "main" java.lang.NullPointerException at ca.qc.cegepoutaouais.java146.Compte.afficheEtatCompte(Compte.java:101) at ca.qc.cegepoutaouais.java146.CompteTest.main(CompteTest.java:29)
P.S. S.V.P. soyez cordial je ne suis qu'un simple débutant en Java je me documente mais se n'est pas assez merci de votre compréhension...
Partager