Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import org.slf4j.*;
- import iznimke.*;
- public class Test {
- private static final Logger logger = LoggerFactory.getLogger(Test.class);
- public static final int CRVENO=0;
- public static final int ZUTO=1;
- public static final int ZELENO=2;
- public int stanje=ZELENO;
- private static void promijeniStanjeSemaforaUCrveno(Test stanjeSemafora) throws ZelenoUCrvenoException {
- if(stanjeSemafora.stanje == ZELENO){
- logger.debug("Čekam 3 sekunde!");
- waiting(3000, (int) stanjeSemafora.stanje);
- stanjeSemafora.stanje = ZUTO;
- logger.debug("Postavljeno stanje semafora u žuto!");
- logger.debug("Čekam 1 sekundu!");
- waiting(1000, stanjeSemafora.stanje-1);
- stanjeSemafora.stanje = CRVENO;
- logger.debug("Postavljeno stanje semafora u crveno");
- throw new ZelenoUCrvenoException("Zeleno u crveno exception!");
- }
- }
- private static void promijeniStanjeSemaforaUZeleno(Test stanjeSemafora) throws CrvenoUZelenoException {
- if (stanjeSemafora.stanje == CRVENO){
- logger.debug("Čekam 3 sekunde!");
- waiting(3000, (int) stanjeSemafora.stanje);
- stanjeSemafora.stanje = ZUTO;
- logger.debug("Postavljeno stanje semafora u zuto");
- logger.debug("Čekam 1 sekundu!");
- waiting(1000, (int) stanjeSemafora.stanje+1);
- stanjeSemafora.stanje = ZELENO;
- logger.debug("Postavljeno stanje semafora u zeleno");
- throw new CrvenoUZelenoException("Crveno u zeleno exception!");
- }
- }
- private static void waiting(final long p_amount, final int stanjeSemafora) {
- System.out.println(convertStanjeToString(stanjeSemafora));
- try {
- Thread.sleep(p_amount);
- } catch (InterruptedException e) {
- logger.debug("Prekid čekanja!", e);
- }
- }
- private static String convertStanjeToString(final int stanjeSemafora) {
- switch (stanjeSemafora){
- case CRVENO: return "Crveno";
- case ZUTO: return "Žuto";
- case ZELENO: return "Zeleno";
- }
- return "Ljubičasto";
- }
- public static void main(String[] args) {
- Test semafor = new Test();
- while(true){
- if(semafor.stanje == ZELENO){
- try {
- promijeniStanjeSemaforaUCrveno(semafor);
- } catch (ZelenoUCrvenoException e) {
- logger.debug(e.getMessage(), e);
- } finally {
- semafor.stanje = CRVENO;
- }
- }
- else if (semafor.stanje == CRVENO){
- try {
- promijeniStanjeSemaforaUZeleno(semafor);
- } catch (CrvenoUZelenoException e) {
- logger.debug(e.getMessage(), e);
- } finally {
- semafor.stanje = ZELENO;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement