Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 7.85 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.text.DecimalFormat;
  3. import java.util.concurrent.Semaphore;
  4.  
  5. import os.simulation.SimulationContainer;
  6. import os.simulation.SimulationContainerLayout;
  7. import os.simulation.SimulationThread;
  8. import os.simulation.gui.NoAnimationPanel;
  9. import os.simulation.gui.SimulationFrame;
  10. import os.simulation.gui.SimulationPanel;
  11. import os.simulation.gui.swing.SwingSimulationPanel;
  12.  
  13. public class DecaITrambolina {
  14.    
  15.     public Trambolina trambolina = new Trambolina (MAX_IZDRZLJIVOST_KG, MAX_IZDRZLJIVOST_BR);
  16.     private class Trambolina {
  17.         private Semaphore[] mutexi;
  18.         private int[] brojaci;
  19.         private Semaphore slobodno;
  20.         private Semaphore tezina;
  21.         private Semaphore osobe;
  22.        
  23.         public Trambolina (int maxTezina, int maxOsoba) {
  24.             mutexi = new Semaphore[] {
  25.                 new Semaphore(1, false),
  26.                 new Semaphore(1, true)
  27.             };
  28.             brojaci = new int[] {
  29.                 0, 0
  30.             };
  31.            
  32.             slobodno = new Semaphore(1);
  33.             tezina = new Semaphore(maxTezina);
  34.             osobe = new Semaphore(maxOsoba);
  35.         }
  36.         public void popniSe(Dete d) {
  37.             int pol = d.pol == Pol.MUSKI ? 1 : 0;
  38.             mutexi[pol].acquireUninterruptibly();
  39.            
  40.             brojaci[pol]++;
  41.             if (brojaci[pol] == 1)
  42.                 slobodno.acquireUninterruptibly();
  43.            
  44.             mutexi[pol].release();
  45.            
  46.             tezina.acquireUninterruptibly(d.tezina);
  47.             osobe.acquireUninterruptibly(1);
  48.         }
  49.        
  50.         public void sidji(Dete d) {
  51.             int pol = d.pol == Pol.MUSKI ? 1 : 0;
  52.             mutexi[pol].acquireUninterruptibly();
  53.             brojaci[pol]--;
  54.            
  55.             if (brojaci[pol] == 0)
  56.                 slobodno.release();
  57.            
  58.             mutexi[pol].release();
  59.             tezina.release(d.tezina);
  60.             osobe.release(1);
  61.         }
  62.     }
  63.     private class Dete extends UtilThread {
  64.        
  65.         private final Pol pol = getPol();
  66.         private final int tezina = getTezina();
  67.        
  68.         @Override
  69.         protected void step() {
  70.             odmara();
  71.             trambolina.popniSe(this);
  72.             try {
  73.                 skace();
  74.             } finally {
  75.                 trambolina.sidji(this);
  76.             }
  77.             // TODO Sinhronizacija
  78.         }
  79.     }
  80.    
  81.     private enum Pol {
  82.         MUSKI, ZENSKI;
  83.     }
  84.    
  85.     // -------------------- //
  86.     // Parametri simulacije //
  87.     // -------------------- //
  88.    
  89.     public static final int    BROJ_DECE           = 25;
  90.     public static final double PROCENAT_DECAKA     = 0.5;
  91.     public static final double TEZINA_DETETA_MIN   = 25;
  92.     public static final double TEZINA_DETETA_MAX   = 80;
  93.     public static final int    DUZINA_SKAKANJA     = 3000;
  94.     public static final int    DUZINA_ODMARANJA    = 5000;
  95.     public static final int    MAX_IZDRZLJIVOST_KG = 300;
  96.     public static final int    MAX_IZDRZLJIVOST_BR = 5;
  97.     public static final double VEROVATNOCA         = 0.1;
  98.    
  99.     public static final Color  BOJA_DECAKA      = new Color(0xCC, 0xE6, 0xFF);
  100.     public static final Color  BOJA_DEVOJCICA   = new Color(0xFF, 0xCC, 0xE6);
  101.     public static final Color  BOJA_VAN         = null;
  102.     public static final Color  BOJA_UNUTRA_OK   = new Color(0xDD, 0xFF, 0xDD);
  103.     public static final Color  BOJA_UNUTRA      = new Color(0xFF, 0xDD, 0xDD);
  104.    
  105.     public static final String TEXT_VAN         = "\u041A\u043B\u0443\u043F\u0435";
  106.     public static final String TEXT_UNUTRA      = "\u0422\u0440\u0430\u043C\u0431\u043E\u043B\u0438\u043D\u0430";
  107.     public static final String TEXT_SKACE       = "\u0421\u043A\u0430\u0447\u0435";
  108.     public static final String TEXT_PAO         = "\u041F\u0430\u043E";
  109.     public static final String TEXT_PALA        = "\u041F\u0430\u043B\u0430";
  110.     public static final String TEXT_NE_SKACE    = "\u041D\u0435 \u0441\u043A\u0430\u0447\u0435";
  111.     public static final String TEXT_ODMARA      = "\u041E\u0434\u043C\u0430\u0440\u0430";
  112.     public static final String TEXT_NE_ODMARA   = "\u0427\u0435\u043A\u0430";
  113.    
  114.     // ------------------- //
  115.     //    Sistemski deo    //
  116.     // ------------------- //
  117.     // Ne dirati kod ispod //
  118.     // ------------------- //
  119.    
  120.     private static int countT;
  121.     private static int countF;
  122.     private static double countU;
  123.     private static int countN;
  124.     private class UtilThread extends SimulationThread {
  125.        
  126.         private final Pol pol;
  127.         private final int tezina;
  128.         private String text;
  129.        
  130.         protected UtilThread() {
  131.             setShowTime(true);
  132.             pol = Math.random() < PROCENAT_DECAKA ? Pol.MUSKI : Pol.ZENSKI;
  133.             tezina = (int) (TEZINA_DETETA_MIN + Math.random() * (TEZINA_DETETA_MAX - TEZINA_DETETA_MIN));
  134.             if (Pol.MUSKI == pol) {
  135.                 setName("\u041A\u043B\u0438\u043D\u0430\u0446 " + ++countT);
  136.                 setColor(BOJA_DECAKA);
  137.                 text = TEXT_PAO;
  138.             } else {
  139.                 setName("\u041A\u043B\u0438\u043D\u043A\u0430 " + ++countF);
  140.                 setColor(BOJA_DEVOJCICA);
  141.                 text = TEXT_PALA;
  142.             }
  143.         }
  144.        
  145.         public final Pol getPol() {
  146.             return pol;
  147.         }
  148.        
  149.         public final int getTezina() {
  150.             return tezina;
  151.         }
  152.        
  153.         protected void skace() {
  154.             update(tezina);
  155.             setContainer(unutra);
  156.             setText(TEXT_SKACE);
  157.             work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  158.             while (Math.random() < VEROVATNOCA) {
  159.                 setText(text);
  160.                 work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  161.                 setText(TEXT_SKACE);
  162.                 work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  163.             }
  164.             if (isInterrupted()) {
  165.                 setText(text);
  166.                 workUninterruptibly(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  167.             }
  168.             setText(TEXT_NE_SKACE);
  169.             update(-tezina);
  170.         }
  171.        
  172.         protected void odmara() {
  173.             setContainer(van);
  174.             setText(convert(tezina));
  175.             work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
  176.             setText(convert(tezina));
  177.         }
  178.        
  179.         private void update(double t) {
  180.             countU = countU + t;
  181.             countN = countN + (int) Math.signum(t);
  182.             if (countU > MAX_IZDRZLJIVOST_KG || countN > MAX_IZDRZLJIVOST_BR) {
  183.                 unutra.setColor(BOJA_UNUTRA);
  184.             } else {
  185.                 unutra.setColor(BOJA_UNUTRA_OK);
  186.             }
  187.         }
  188.     }
  189.    
  190.     // Glavni program
  191.     public static void main(String[] a) {
  192.         new DecaITrambolina();
  193.     }
  194.    
  195.     private static final DecimalFormat format = new DecimalFormat("#,##0.00 kg");
  196.     private static String convert(double value) {
  197.         return format.format(value);
  198.     }
  199.    
  200.     // Stanja
  201.     private final SimulationContainer van = new SimulationContainer(TEXT_VAN, BOJA_VAN, SimulationContainerLayout.BOX);
  202.     private final SimulationContainer unutra = new SimulationContainer(TEXT_UNUTRA, BOJA_UNUTRA_OK, SimulationContainerLayout.BOX) {public String getText() {return convert(countU) + " / " + countN;}};
  203.     private final SimulationContainer sve = new SimulationContainer(SimulationContainerLayout.COLUMN, van, unutra);
  204.    
  205.     public DecaITrambolina() {
  206.        
  207.         // Create frame
  208.         SimulationPanel panel = new SwingSimulationPanel(sve);
  209.         SimulationFrame frame = SimulationFrame.create("Trambolina", panel, new NoAnimationPanel());
  210.         frame.display();
  211.        
  212.         // Create threads
  213.         for (int i = 1; i <= BROJ_DECE; i++) {
  214.             new Dete().start();
  215.         }
  216.        
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement