condition is not satisfying..
package com.codegym.task.task17.task1715;
import java.util.ArrayList;
import java.util.List;
/*
Pharmacy
Requirements:
6. The Pharmacy thread must use drugController to purchase a random drug (getRandomDrug)
in a random amount (getRandomCount).
7. The Pharmacy thread should use the waitAMoment() method to wait 300 ms between purchases.
8. The Person class must implement the Runnable interface.
9. The Person thread should run as long as isStopped is false.
10. The Pharmacy thread must use drugController to sell a random drug (getRandomDrug) in
a random amount (getRandomCount).
11. The Person thread should use the waitAMoment() method to wait 100 ms between purchases.
12. The methods of the DrugController class must be synchronized.
*/
public class Solution {
public static DrugController drugController = new DrugController();
public static boolean isStopped = false;
public static void main(String[] args) throws InterruptedException {
Thread pharmacy = new Thread(new Pharmacy());
Thread man = new Thread(new Person(), "Man");
Thread woman = new Thread(new Person(), "Woman");
pharmacy.start();
man.start();
woman.start();
Thread.sleep(1000);
isStopped = true;
}
public static class Pharmacy implements Runnable{
public void run(){
while(!isStopped){
drugController.buy(getRandomDrug(),getRandomCount());
waitAMoment();
}
}
}
public static class Person implements Runnable{
public void run(){
while(!isStopped){
drugController.sell(getRandomDrug(),getRandomCount());
waitAMoment();
}
}
}
public static int getRandomCount() {
return (int) (Math.random() * 3) + 1;
}
public static Drug getRandomDrug() {
int index = (int) ((Math.random() * 1000) % (drugController.allDrugs.size()));
List<Drug> drugs = new ArrayList<>(drugController.allDrugs.keySet());
return drugs.get(index);
}
private static void waitAMoment() {
if(Thread.currentThread().getName().toString().equals("Thread-2")){
System.out.println("Current thread Pharmacy - "+Thread.currentThread().getName());
try {
Thread.sleep(300);
} catch (InterruptedException ignored) {
}
}else{
System.out.println("Current thread - "+Thread.currentThread().getName());
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
}
}
}