Created
February 25, 2014 14:02
-
-
Save Vox1oot/9209331 to your computer and use it in GitHub Desktop.
package com.javarush.test.level17.lesson10.home03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson10.home03; | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson10.home03; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class DrugsController { | |
public static Map<Drug, Integer> allDrugs = new HashMap<Drug, Integer>(); // <Лекарство, Количество> | |
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 currentount = allDrugs.get(drug); | |
allDrugs.put(drug, (currentount + count)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson10.home03; | |
import java.util.ArrayList; | |
import java.util.List; | |
/* Аптека | |
Реализуй интерфейс Runnable в класах Apteka и Person. | |
Все нити должны работать пока не isStopped | |
Логика для Apteka: drugsController должен сделать закупку случайного лекарства (getRandomDrug) в количестве (getRandomCount) и подождать 300 мс | |
Логика для Person: drugsController должен сделать продажу случайного лекарства (getRandomDrug) в количестве (getRandomCount) и подождать 100 мс | |
Расставь synchronized там, где это необходимо | |
*/ | |
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){ | |
try{ | |
drugsController.buy(getRandomDrug(), getRandomCount()); | |
Thread.sleep(300); | |
}catch (InterruptedException e) | |
{ | |
} | |
} | |
} | |
} | |
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<Drug> drugs = new ArrayList<Drug>(drugsController.allDrugs.keySet()); | |
return drugs.get(index); | |
} | |
private static void waitAMoment() { | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment