Bonsoir, je suis encore débutant en Java (j'essaye de prendre de l'avance comme j'aurai des cours dans quelques semaines) et j'essaye de coder un petit programme de "gestion" d'une pharmacie (sans système de base de donnée, c'est majoritairement pour tester ce que j'ai appris jusqu'ici).
Bref dans une boucle while (ligne 127), je vérifie que le nombre entré par l'utilisateur (qui est l'indice de son achat), est bien dans l'intervalle possible de mon Arraylist, pour ensuite ajouter l'objet Medicament dans l'Arraylist représentant les achats du client. Une fois fait je demande si le client à acheter d'autres produits avant de relancer la boucle si le premier char est 'o' et de partir de celle-ci si le premier char est 'n'.
Seulement quand je réponds par 'o', la boucle ne se recommence pas correctement, elle demande continuellement si le client désire un autre produit au lieu de demander lequel il veut acheter. J'ai cherché pendant un petit temps déjà sans vrais résultats, je viens donc demander votre aide ici :)
Voici mon code :La classe client :Code:
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; //TODO add custom exception(med only letters). public class Main { public static void main(String[] args) throws InterruptedException { //introduction System.out.println("Bienvenue"); System.out.println("Préparation"); Thread.sleep(1000); System.out.println("."); Thread.sleep(1000); System.out.println("."); Thread.sleep(1000); System.out.println("."); Thread.sleep(1000); System.out.println("Ready"); Thread.sleep(1000); System.out.println("==============="); System.out.println("Vous allez maintenant devoir les données relatives à votre stock."); Thread.sleep(1000); //Input MED ArrayList<Medicament> medList = new ArrayList(); Scanner scan = new Scanner(System.in); boolean medRestart = true; int iMed = 1; int iMedList = 0; while(medRestart == true){ System.out.println("Médicament numéro " + iMed+"..."); System.out.println("Nom du médicament?"); boolean bNomMed = true; String nomMed = ""; while(bNomMed == true){ try{ nomMed =scan.nextLine(); bNomMed = false; } catch(InputMismatchException e){ System.out.println("Nom invalide."); scan.next(); } } System.out.println("Prix du médicament?"); int prixMed = -1; boolean bPrixMed = false; while(bPrixMed == false){ try{ prixMed = scan.nextInt(); bPrixMed = true; } catch(InputMismatchException e) { System.out.println("Le prix doit être un nombre entier."); scan.next(); }} medList.add(new Medicament(nomMed, prixMed)); System.out.println("Autre medicament? (O/N)"); scan.nextLine(); char medRestartScan = scan.nextLine().toLowerCase().charAt(0); while(medRestartScan != 'o' && medRestartScan != 'n'){ System.out.println("Veuillez répondre par 'O' ou 'N'."); medRestartScan = scan.nextLine().toLowerCase().charAt(0); } if(medRestartScan == 'n'){ medRestart = false; } iMed++; iMedList++; } //input CLIENTS INTRO //TODO try catch ArrayList<Client> cList = new ArrayList(); System.out.println("Préparation"); Thread.sleep(1000); System.out.println("."); Thread.sleep(1000); System.out.println("."); Thread.sleep(1000); System.out.println("."); System.out.println("Prêt à recevoir les clients."); System.out.println("===================="); Thread.sleep(1000); Client c = new Client(); boolean cRestart = true; //CLIENT INPUT DONNEES while(cRestart ==true){ int ic = 1; int icList = 0; System.out.println("Client numéro "+ic+"..."); System.out.println("Nom client?"); String nomC = scan.nextLine(); System.out.println("Prénom client?"); String prénomC = scan.nextLine(); System.out.println("Ville du client?"); String villeC = scan.nextLine(); System.out.println("Age client?"); boolean bMismatchAgeC = true; int ageC = -1; while(bMismatchAgeC == true){ try{ ageC = scan.nextInt(); bMismatchAgeC = false; } catch(InputMismatchException e){ System.out.println("L'âge doit être un nombre entier."); scan.nextLine(); }} cList.add(new Client(nomC, prénomC, villeC, ageC)); //CLIENT ACHATS System.out.println("Preparation de la liste pour impression..."); Thread.sleep(1000); System.out.println("."); Thread.sleep(1000); System.out.println("."); System.out.println("============="); Thread.sleep(1000); int iSizeCorrected = (medList.size())-1; for(int i = 0; i <= iSizeCorrected;i++){ System.out.println(i+" : "+ medList.get(i).getNom()); } boolean medBoughtStatus = true; int medBought = -1; while(medBoughtStatus == true){ while(intervallContains(0, (medList.size()-1), medBought) == false){ System.out.println("Med. acheté? (répondre par l'indice de la liste ci dessus)."); medBought = scan.nextInt(); } cList.get(icList).cMedList.add(medList.get(medBought)); System.out.println("Autre med. acheté? (O/N)"); scan.nextLine(); String medBoughtStatusBis = scan.nextLine(); char medBoughtStatusBisChar = medBoughtStatusBis.toLowerCase().charAt(0); while (medBoughtStatusBisChar != 'o' && medBoughtStatusBisChar != 'n') { System.out.println("Veuillez répondre par 'O' ou 'N'."); medBoughtStatusBis = scan.nextLine(); } if(medBoughtStatusBisChar == 'n'){ medBoughtStatus = false; } else if(medBoughtStatusBisChar == 'o'){ medBoughtStatus = true; } } //NEXT CLIENT (?) ic++; icList++; System.out.println("Autre client? (O/N)"); String charC = scan.nextLine().toLowerCase(); boolean bCharC = true; while(bCharC == true){ if(charC.charAt(0) != 'n' && charC.charAt(0) != 'o'){ System.out.println("Veuillez répondre par 'O' ou 'N'."); charC = scan.nextLine().toLowerCase(); } else { bCharC = false; } } if(charC.charAt(0)=='n') cRestart = false; } } public static boolean intervallContains(int low, int high, int n) { return n >= low && n <= high; } }
Et la classe Medicament (car oui c'est une pharmacie dans mon exemple) :Code:
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 import java.util.ArrayList; public class Client { private String Nom, prenom, ville; private int age; ArrayList<Medicament> cMedList = new ArrayList(); public Client (){ this.Nom = "?"; this.prenom = "?"; this.ville = "?"; this.age = -1; } public Client(String nom, String prenom, String ville, int age) { super(); Nom = nom; this.prenom = prenom; this.ville = ville; this.age = age; } public String getNom() { return Nom; } public void setNom(String nom) { Nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public String getVille() { return ville; } public void setVille(String ville) { this.ville = ville; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //arraylist gestion public void cAddMed (Medicament med){ cMedList.add(med); } }
Ps : ce n'est pas encore terminé c'est donc normal si je n'ai encore mis aucune action à la fin de celui-ci :)Code:
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 public class Medicament { private String nom; private int prix; public Medicament(){ nom = "?"; prix = -1; } public Medicament(String nom, int prix) { super(); this.nom = nom; this.prix = prix; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public int getPrix() { return prix; } public void setPrix(int prix) { this.prix = prix; } }
Merci d'avance !