Vous êtes sur la page 1sur 4

1 class GestionPharmacie {

2 protected static Scanner scanner = new Scanner(System.in);


3
4 /**
5 * Le programme principal crée un objet Pharmacie
6 * sur lequel les opérations suivantes peuvent être effectuées:
7 * - achat (d'un médicament par un client),
8 * - approvisionnement (ajout d'un médicament au stock)
9 * - affichage (des données relatives à la pharmacie
10 */
11 public static void main(String args[]) {
12 Client[] clients = new Client[2];
13 Medicament[] medicaments = new Medicament[2];
14
15 clients[0] = new Client("Malfichu", 0.0);
16 clients[1] = new Client("Palichon", 0.0);
17
18 medicaments[0] = new Medicament("Aspiron", 20.40, 5);
19 medicaments[1] = new Medicament("Rhinoplexil", 19.15, 5);
20
21 Pharmacie1 p = new Pharmacie1(clients, medicaments);
22
23 int choix;
24 do {
25 choix = menu();
26
27 switch (choix) {
28 case 1:
29 p.achat();
30 break;
31 case 2:
32 p.approvisionnement();
33 break;
34 case 3:
35 p.affichage();
36 break;
37 case 4:
38 quitter();
39 break;
40 }
41 } while (choix < 4);
42 }
43 // Les méthodes utilitaires
44
45 static int menu() {
46 int choix = 0;
47 System.out.println();
48 System.out.println();
49 System.out.println("1 : Achat de médicament");
50 System.out.println("2 : Approvisionnement en médicaments");
51 System.out.println("3 : Etats des stocks et des crédits");
52 System.out.println("4 : Quitter");
53 while ((choix != 1) && (choix != 2) && (choix != 3) && (choix != 4)) {
54 choix = scanner.nextInt();
55 }
56 return choix;
57 }
58
59 static void quitter() {
60 System.out.println("Programme terminé!");
61 }
62 }
63
64
65 // Les classes d'objets
66
67 class Pharmacie1 {
68 // une pharmacie c'est une liste de client et
69 // une liste de médicaments
70 private Client[] clients;;
71 private Medicament[] medicaments;
72
73 public Pharmacie1(Client[] cs, Medicament[] ms) {
74 clients = cs;
75 medicaments = ms;
76 }
77
78 public Client[] getClients() {
79 return this.clients;
80 }
81
82 public Medicament[] getMedicaments() {
83 return this.medicaments;
84 }
85
86 /**
87 * Permet à un client d'acheter un médicament.
88 * si le client est connu de la pharmacie, son crédit
89 * va diminuer en fonction de son paiement.
90 * Le stock associé au médicament va diminuer
91 */
92 public void achat() {
93 Client client = this.lireClient();
94 Medicament medicament = this.lireMedicament();
95
96 double paiement = lirePaiement();
97 int quantite = lireQuantite();
98
99 if (quantite <= medicament.getStock()) {
100 medicament.diminuerStock(quantite);
101 client.augmenterCredit((medicament.getPrix() * quantite) - paiement);
102 } else {
103 System.out.println("Achat Impossible. Quantité insuffisante");
104 }
105 }
106
107 /**
108 * Permet à la pharmacie de s'approvisionner en un
109 * médicament connu.
110 */
111 public void approvisionnement() {
112 Medicament medicament = this.lireMedicament();
113 System.out.println("Donner la Quantité : ");
114 int quantite = GestionPharmacie.scanner.nextInt();
115 medicament.augmenterStock(quantite);
116 }
117
118 /**
119 * Affichage des stocks et des crédits de la pharmacie
120 */
121 public void affichage() {
122
123 System.out.println("Affichage des stocks");
124 for (int i = 0; i < medicaments.length; i++) {
125 System.out.println("Stock du médicament " + medicaments[i].getNom()
126 + " :" + medicaments[i].getStock());
127 }
128
129 System.out.println("Affichage des crédits");
130 for (int i = 0; i < clients.length; i++) {
131 System.out.println("Crédit du client " + clients[i].getNom()
132 + " :" + clients[i].getCredit());
133 }
134 }
135
136 /**
137 * Retourne l'objet Client associé à un nom (String)
138 * Le nom est lu et redemandé tant qu'il ne correspond
139 * pas au nom d'un client connu de la pharmacie
140 */
141 public Client lireClient() {
142 String nom;
143 boolean trouve = false;
144 Client c = null;
145 System.out.println("Nom du client?:");
146
147 do {
148 nom = GestionPharmacie.scanner.nextLine();
149 for (int i = 0; (!trouve && i < clients.length); i++) {
150 if ((clients[i].getNom()).equals(nom)) {
151 trouve = true;
152 c = clients[i];
153 }
154 }
155 if (!trouve) {
156 System.out.println("Client inconnu. Veuilliez recommencer");
157 }
158 } while (!trouve);
159
160 return c;
161 }
162
163 /**
164 * Retourne l'objet Medicament associé à un nom (String)
165 * Le nom est lu et redemandé tant qu'il ne correspond
166 * pas au nom d'un médicament connu de la pharmacie
167 */
168 public Medicament lireMedicament() {
169 String nom;
170 boolean trouve = false;
171 Medicament m = null;
172 System.out.println("Nom du medicament?:");
173
174 do {
175 nom = GestionPharmacie.scanner.nextLine();
176 for (int i = 0; (!trouve && i < medicaments.length); i++) {
177 if ((medicaments[i].getNom()).equals(nom)) {
178 trouve = true;
179 m = medicaments[i];
180 }
181 }
182 if (!trouve) {
183 System.out.println("Médicament inconnu. Veuilliez recommencer");
184 }
185 } while (!trouve);
186
187 return m;
188 }
189
190
191 static double lirePaiement() {
192 System.out.println("quel est le montant du paiement?");
193 return GestionPharmacie.scanner.nextDouble();
194 }
195
196 static int lireQuantite() {
197 System.out.println("quelle est la quantité achetée?");
198 return GestionPharmacie.scanner.nextInt();
199 }
200
201 }
202
203
204 class Client {
205 // Un client est caractérisé par son nom et un crédit.
206 private String nom = "";
207 private double credit = 0.0;
208
209 public Client(String nom, double credit) {
210 this.nom = nom;
211 this.credit = credit;
212 }
213
214 public void augmenterCredit(double montant) {
215 credit = credit + montant;
216 }
217
218 public void diminuerCredit(double montant) {
219 credit = credit - montant;
220 }
221
222 public double getCredit() {
223 return credit;
224 }
225
226 public String getNom() {
227 return nom;
228 }
229 }
230
231
232 class Medicament {
233 // un médicament a un nom, un prix et une quantité en stock
234 private String nom;
235 private double prix;
236 private int stock;
237
238 public Medicament(String nom, double prix, int stock) {
239 this.nom = nom;
240 this.prix = prix;
241 this.stock = stock;
242 }
243
244 public void augmenterStock(int quantite) {
245 stock = stock + quantite;
246 }
247
248 public void diminuerStock(int quantite) {
249 stock = stock - quantite;
250 }
251
252 public int getStock() {
253 return stock;
254 }
255
256 public double getPrix() {
257 return prix;
258 }
259
260 public String getNom() {
261 return nom;
262 }
263 }

Vous aimerez peut-être aussi