Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package main;
  2.  
  3. import org.slf4j.*;
  4. import iznimke.*;
  5.  
  6. public class Test {
  7.  
  8.     private static final Logger logger = LoggerFactory.getLogger(Test.class);
  9.  
  10.     public static final int CRVENO=0;
  11.     public static final int ZUTO=1;
  12.     public static final int ZELENO=2;
  13.  
  14.     public int stanje=ZELENO;
  15.  
  16.     private static void promijeniStanjeSemaforaUCrveno(Test stanjeSemafora) throws ZelenoUCrvenoException {
  17.         if(stanjeSemafora.stanje == ZELENO){
  18.             logger.debug("Čekam 3 sekunde!");
  19.             waiting(3000, (int) stanjeSemafora.stanje);
  20.             stanjeSemafora.stanje = ZUTO;
  21.             logger.debug("Postavljeno stanje semafora u žuto!");
  22.             logger.debug("Čekam 1 sekundu!");
  23.             waiting(1000, stanjeSemafora.stanje-1);
  24.             stanjeSemafora.stanje = CRVENO;
  25.             logger.debug("Postavljeno stanje semafora u crveno");
  26.             throw new ZelenoUCrvenoException("Zeleno u crveno exception!");
  27.         }
  28.     }
  29.  
  30.     private static void promijeniStanjeSemaforaUZeleno(Test stanjeSemafora) throws CrvenoUZelenoException {
  31.         if (stanjeSemafora.stanje == CRVENO){
  32.             logger.debug("Čekam 3 sekunde!");
  33.             waiting(3000, (int) stanjeSemafora.stanje);
  34.             stanjeSemafora.stanje = ZUTO;
  35.             logger.debug("Postavljeno stanje semafora u zuto");
  36.             logger.debug("Čekam 1 sekundu!");
  37.             waiting(1000, (int) stanjeSemafora.stanje+1);
  38.             stanjeSemafora.stanje = ZELENO;
  39.             logger.debug("Postavljeno stanje semafora u zeleno");
  40.             throw new CrvenoUZelenoException("Crveno u zeleno exception!");
  41.         }
  42.     }
  43.  
  44.     private static void waiting(final long p_amount, final int stanjeSemafora) {
  45.         System.out.println(convertStanjeToString(stanjeSemafora));
  46.         try {
  47.             Thread.sleep(p_amount);
  48.         } catch (InterruptedException e) {
  49.             logger.debug("Prekid čekanja!", e);
  50.         }
  51.     }
  52.  
  53.     private static String convertStanjeToString(final int stanjeSemafora) {
  54.         switch (stanjeSemafora){
  55.             case CRVENO: return "Crveno";
  56.             case ZUTO: return "Žuto";
  57.             case ZELENO: return "Zeleno";
  58.         }
  59.         return "Ljubičasto";
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.  
  64.         Test semafor = new Test();
  65.         while(true){
  66.             if(semafor.stanje == ZELENO){
  67.                 try {
  68.                     promijeniStanjeSemaforaUCrveno(semafor);
  69.                 } catch (ZelenoUCrvenoException e) {
  70.                     logger.debug(e.getMessage(), e);
  71.                 } finally {
  72.                     semafor.stanje = CRVENO;
  73.                 }
  74.             }
  75.             else if (semafor.stanje == CRVENO){
  76.                 try {
  77.                     promijeniStanjeSemaforaUZeleno(semafor);
  78.                 } catch (CrvenoUZelenoException e) {
  79.                     logger.debug(e.getMessage(), e);
  80.                 } finally {
  81.                     semafor.stanje = ZELENO;
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement