//even as you gave him authority over all flesh, he will give eternal life to all whom you have given him. (John 17:2)
-------------------------------------------------------Solution.java------------------------------------------------------------------
package com.javarush.task.task17.task1715;
import java.util.ArrayList;
import java.util.List;
/*
ÐпÑека
*/
public class Solution {
public static DrugsController drugsController = new DrugsController();
public static boolean isStopped = false;
public static void main(String[] args) throws InterruptedException {
Thread apteka = new Thread(new Apteka());
Thread man = new Thread(new Person(), "ÐÑжÑина");
Thread woman = new Thread(new Person(), "ÐенÑина");
apteka.start();
man.start();
woman.start();
Thread.sleep(1000);
isStopped = true;
}
public static class Apteka implements Runnable {
@Override
public void run() {
while (!isStopped) {
drugsController.buy(getRandomDrug(), getRandomCount());
waitAMoment();
waitAMoment();
waitAMoment();
}
}
}
public static class Person implements Runnable {
@Override
public void run() {
while (!isStopped) {
drugsController.sell(getRandomDrug(), getRandomCount());
waitAMoment();
}
}
}
public static int getRandomCount() {
return (int) (Math.random() * 3) + 1;
}
public static Drug getRandomDrug() {
int index = (int) ((Math.random() * 1000) % (drugsController.allDrugs.size()));
List drugs = new ArrayList<>(drugsController.allDrugs.keySet());
return drugs.get(index);
}
private static void waitAMoment() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------Drug.java-------------------------------------------------------------------
package com.javarush.task.task17.task1715;
public class Drug {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------DrugsController.java-----------------------------------------------------------
package com.javarush.task.task17.task1715;
import java.util.HashMap;
import java.util.Map;
public class DrugsController {
public static Map allDrugs = new HashMap(); // <ÐекаÑÑÑво, ÐолиÑеÑÑво>
static {
Drug panadol = new Drug();
panadol.setName("Ðанадол");
allDrugs.put(panadol, 5);
Drug analgin = new Drug();
analgin.setName("ÐналÑгин");
allDrugs.put(analgin, 18);
Drug placebo = new Drug();
placebo.setName("ÐлаÑебо");
allDrugs.put(placebo, 1);
}
public synchronized void sell(Drug drug, int count) {
String name = Thread.currentThread().getName();
if (!allDrugs.containsKey(drug)) {
System.out.println("ÐÐµÑ Ð² налиÑии");
}
Integer currentCount = allDrugs.get(drug);
if (currentCount < count) {
System.out.println(String.format("%s Ñ
оÑÐµÑ %s %d ÑÑ. РналиÑии - %d", name, drug.getName(), count, currentCount));
} else {
allDrugs.put(drug, (currentCount - count));
System.out.println(String.format("%s кÑпил(а) %s %d ÑÑ. ÐÑÑалоÑÑ - %d", name, drug.getName(), count, (currentCount - count)));
}
}
public synchronized void buy(Drug drug, int count) {
System.out.println("ÐакÑпка " + drug.getName() + " " + count);
if (!allDrugs.containsKey(drug)) {
allDrugs.put(drug, 0);
}
Integer currentCount = allDrugs.get(drug);
allDrugs.put(drug, (currentCount + count));
}
}
--------------------------------------------------------------------------------------------------------------------------------------
/*
ÐпÑека
РеализÑй инÑеÑÑÐµÐ¹Ñ Runnable в клаÑÑаÑ
Apteka и Person.
ÐÑе ниÑи Ð´Ð¾Ð»Ð¶Ð½Ñ ÑабоÑаÑÑ Ð¿Ð¾ÐºÐ° не isStopped.
Ðогика Ð´Ð»Ñ Apteka: drugsController должен ÑделаÑÑ Ð·Ð°ÐºÑÐ¿ÐºÑ ÑлÑÑайного лекаÑÑÑва (getRandomDrug) в колиÑеÑÑве (getRandomCount) и подождаÑÑ 300 мÑ.
Ðогика Ð´Ð»Ñ Person: drugsController должен ÑделаÑÑ Ð¿ÑÐ¾Ð´Ð°Ð¶Ñ ÑлÑÑайного лекаÑÑÑва (getRandomDrug) в колиÑеÑÑве (getRandomCount) и подождаÑÑ 100 мÑ.
РаÑÑÑÐ°Ð²Ñ synchronized Ñам, где ÑÑо необÑ
одимо.
ТÑебованиÑ:
1.âÐлаÑÑ Solution должен ÑодеÑжаÑÑ public static поле drugsController Ñипа DrugsController.
2.âÐлаÑÑ Solution должен ÑодеÑжаÑÑ public static поле isStopped Ñипа boolean.
3.âÐлаÑÑ Solution должен ÑодеÑжаÑÑ private static void меÑод waitAMoment(), коÑоÑÑй должен ждаÑÑ 100 мÑ.
4.âÐлаÑÑ Apteka должен ÑеализовÑваÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ Runnable.
5.âÐиÑÑ Apteka должна ÑабоÑаÑÑ Ð¿Ð¾ÐºÐ° isStopped = false.
6.âÐиÑÑ Apteka должна иÑполÑзоваÑÑ drugsController Ð´Ð»Ñ Ð·Ð°ÐºÑпки ÑлÑÑайного лекаÑÑÑва (getRandomDrug) в колиÑеÑÑве (getRandomCount).
7.âÐиÑÑ Apteka должна ждаÑÑ 300Ð¼Ñ + "Ð¼ÐµÐ¶Ð´Ñ Ð·Ð°ÐºÑпками", иÑполÑзÑÑ Ð¼ÐµÑод waitAMoment().
8.âÐлаÑÑ Person должен ÑеализовÑваÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ Runnable.
9.âÐиÑÑ Person должна ÑабоÑаÑÑ Ð¿Ð¾ÐºÐ° isStopped = false.
10.âÐиÑÑ Person должна иÑполÑзоваÑÑ drugsController Ð´Ð»Ñ Ð¿Ñодажи ÑлÑÑайного лекаÑÑÑва (getRandomDrug) в колиÑеÑÑве (getRandomCount).
11.âÐиÑÑ Person должна ждаÑÑ 100Ð¼Ñ + "Ð¼ÐµÐ¶Ð´Ñ Ð·Ð°ÐºÑпками", иÑполÑзÑÑ Ð¼ÐµÑод waitAMoment().
12.âÐеÑÐ¾Ð´Ñ ÐºÐ»Ð°ÑÑа DrugsController Ð´Ð¾Ð»Ð¶Ð½Ñ Ð±ÑÑÑ synchronized.
-------------------------------------------------------Solution.java------------------------------------------------------------------
package com.javarush.task.task17.task1715;
import java.util.ArrayList;
import java.util.List;
/*
ÐпÑека
*/
public class Solution {
public static DrugsController drugsController = new DrugsController();
public static boolean isStopped = false;
public static void main(String[] args) throws InterruptedException {
Thread apteka = new Thread(new Apteka());
Thread man = new Thread(new Person(), "ÐÑжÑина");
Thread woman = new Thread(new Person(), "ÐенÑина");
apteka.start();
man.start();
woman.start();
Thread.sleep(1000);
isStopped = true;
}
public static class Apteka {
}
public static class Person {
}
public static int getRandomCount() {
return (int) (Math.random() * 3) + 1;
}
public static Drug getRandomDrug() {
int index = (int) ((Math.random() * 1000) % (drugsController.allDrugs.size()));
List drugs = new ArrayList<>(drugsController.allDrugs.keySet());
return drugs.get(index);
}
private static void waitAMoment() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------Drug.java-------------------------------------------------------------------
package com.javarush.task.task17.task1715;
public class Drug {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------DrugsController.java-----------------------------------------------------------
package com.javarush.task.task17.task1715;
import java.util.HashMap;
import java.util.Map;
public class DrugsController {
public static Map allDrugs = new HashMap(); // <ÐекаÑÑÑво, ÐолиÑеÑÑво>
static {
Drug panadol = new Drug();
panadol.setName("Ðанадол");
allDrugs.put(panadol, 5);
Drug analgin = new Drug();
analgin.setName("ÐналÑгин");
allDrugs.put(analgin, 18);
Drug placebo = new Drug();
placebo.setName("ÐлаÑебо");
allDrugs.put(placebo, 1);
}
public void sell(Drug drug, int count) {
String name = Thread.currentThread().getName();
if (!allDrugs.containsKey(drug)) {
System.out.println("ÐÐµÑ Ð² налиÑии");
}
Integer currentCount = allDrugs.get(drug);
if (currentCount < count) {
System.out.println(String.format("%s Ñ
оÑÐµÑ %s %d ÑÑ. РналиÑии - %d", name, drug.getName(), count, currentCount));
} else {
allDrugs.put(drug, (currentCount - count));
System.out.println(String.format("%s кÑпил(а) %s %d ÑÑ. ÐÑÑалоÑÑ - %d", name, drug.getName(), count, (currentCount - count)));
}
}
public void buy(Drug drug, int count) {
System.out.println("ÐакÑпка " + drug.getName() + " " + count);
if (!allDrugs.containsKey(drug)) {
allDrugs.put(drug, 0);
}
Integer currentCount = allDrugs.get(drug);
allDrugs.put(drug, (currentCount + count));
}
}
--------------------------------------------------------------------------------------------------------------------------------------
*/