Advertisement
Nalkit

PW_Zad3

Apr 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. package prod_cons;
  2.  
  3. public class PC
  4. {
  5.     public static void main(String[] args)
  6.                         throws InterruptedException
  7.     {
  8.         final Func func = new Func();
  9.  
  10.         //Producent
  11.         Thread t1 = new Thread(new Runnable()
  12.         {
  13.             @Override
  14.             public void run()
  15.             {
  16.                 try
  17.                 {
  18.                     func.produce();
  19.                 }
  20.                 catch(InterruptedException e)
  21.                 {
  22.                     e.printStackTrace();
  23.                 }
  24.             }
  25.         });
  26.  
  27.         //Konsumer
  28.         Thread t2 = new Thread(new Runnable()
  29.         {
  30.             @Override
  31.             public void run()
  32.             {
  33.                 try
  34.                 {
  35.                     func.consume();
  36.                 }
  37.                 catch(InterruptedException e)
  38.                 {
  39.                     e.printStackTrace();
  40.                 }
  41.             }
  42.         });
  43.  
  44.         t1.start();
  45.         t2.start();
  46.        
  47.     }
  48.     public static class Func
  49.     {
  50.         int capacity = 1;
  51.         int value = 0;
  52.         int PosA = 0, PosB = 0, PosC = 0, PosD = 0;
  53.         char[] Passw = {'X','X','X','X'};
  54.         boolean EndFlag = false;
  55.         public void produce() throws InterruptedException
  56.         {
  57.            
  58.             while (true)
  59.             {
  60.                 synchronized (this)
  61.                 {
  62.                    
  63.                     if(value != capacity)
  64.                     {
  65.                         if(EndFlag) return;
  66.                         CreatePass();
  67.                         System.out.println("Password produced: ");
  68.                         System.out.println(Passw);
  69.                         value++;
  70.                         notifyAll();
  71.                     }
  72.                     if(value == capacity)
  73.                     {
  74.                         notifyAll();
  75.                         wait();
  76.                     }
  77.                     Thread.sleep(500);
  78.                 }
  79.             }
  80.         }
  81.  
  82.        
  83.         public void consume() throws InterruptedException
  84.         {
  85.             char[] RandPass = {'A', 'A', 'A', 'C'};
  86.             while (true)
  87.             {
  88.                 synchronized (this)
  89.                 {
  90.                     while (value==0)  
  91.                     {
  92.                         notifyAll();
  93.                         wait();
  94.                     }
  95.                     value--;
  96.                     if(Passw[0] == RandPass[0] && Passw[1] == RandPass[1] && Passw[2] == RandPass[2] &&  Passw[3] == RandPass[3])
  97.                     {
  98.                         System.out.println("Passwords Match!");
  99.                         EndFlag = true;
  100.                         notifyAll();
  101.                         return;
  102.                     }
  103.                     else
  104.                     {
  105.                         System.out.println("No match!");
  106.                     }
  107.                     Thread.sleep(200);
  108.                 }
  109.             }
  110.         }
  111.              
  112.        
  113.         void CreatePass()
  114.         {
  115.             char charA = 0, charB=0,charC=0,charD=0;
  116.             switch(PosA)
  117.             {
  118.             case(0):
  119.                 charA = 'A';
  120.                 break;
  121.             case(1):
  122.                 charA = 'B';
  123.                 break;
  124.             case(2):
  125.                 charA = 'C';
  126.                 break;
  127.             case(3):
  128.                 charA = 'D';
  129.                 break;
  130.             default:
  131.                 System.out.println("WTF-A");
  132.             }
  133.            
  134.            
  135.             switch(PosB)
  136.             {
  137.             case(0):
  138.                 charB = 'A';
  139.                 break;
  140.             case(1):
  141.                 charB = 'B';
  142.                 break;
  143.             case(2):
  144.                 charB = 'C';
  145.                 break;
  146.             case(3):
  147.                 charB = 'D';
  148.                 break;
  149.             default:
  150.                 System.out.println("WTF-B");
  151.             }
  152.            
  153.            
  154.             switch(PosC)
  155.             {
  156.             case(0):
  157.                 charC = 'A';
  158.                 break;
  159.             case(1):
  160.                 charC = 'B';
  161.                 break;
  162.             case(2):
  163.                 charC = 'C';
  164.                 break;
  165.             case(3):
  166.                 charC = 'D';
  167.                 break;
  168.             default:
  169.                 System.out.println("WTF-C");
  170.             }
  171.            
  172.            
  173.             switch(PosD)
  174.             {
  175.             case(0):
  176.                 charD = 'A';
  177.                 break;
  178.             case(1):
  179.                 charD = 'B';
  180.                 break;
  181.             case(2):
  182.                 charD = 'C';
  183.                 break;
  184.             case(3):
  185.                 charD = 'D';
  186.                 break;
  187.             default:
  188.                 System.out.println("WTF-D");
  189.             }
  190.             Passw[0] = charA;
  191.             Passw[1] = charB;
  192.             Passw[2] = charC;
  193.             Passw[3] = charD;
  194.            
  195.             if(PosD == 3)
  196.             {
  197.                 PosD = 0;
  198.                 if(PosC == 3)
  199.                 {
  200.                     PosC = 0;
  201.                     if(PosB == 3)
  202.                     {
  203.                         PosB =0;
  204.                         if(PosA == 3)
  205.                         {
  206.                             System.out.println("All passwords exhausted");
  207.                         }
  208.                         else
  209.                         {
  210.                             PosA++;
  211.                         }
  212.                     }
  213.                     else
  214.                     {
  215.                         PosB++;
  216.                     }
  217.                 }
  218.                 else
  219.                 {
  220.                     PosC++;
  221.                 }
  222.             }
  223.             else {PosD++;}
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement