Alejandro_S_Mercado

Untitled

Sep 24th, 2025
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. public class ConsumidorCliente extends Thread{
  2.     private ArrayList <Object> mostrador;
  3.     private int numCliente;
  4.     private Random random;
  5.  
  6.     public ConsumidorCliente(ArrayList <Object> mostrador,int numCliente) {
  7.         this.mostrador=mostrador;
  8.         this.numCliente=numCliente;
  9.         this.random=new Random();
  10.     }
  11.    
  12.    
  13.     public void run() {
  14.        
  15.             try {
  16.                 int timeConsumition=random.nextInt(400-200)+200;
  17.                 System.out.println("El cliente("+this.numCliente+") está comprando");
  18.                  boolean waitBizcocho = false;
  19.                  boolean waitFactura = false;
  20.                  Bizcocho b = null;
  21.                  Factura f = null;
  22.                  
  23.                 synchronized(mostrador) {
  24.                
  25.                     while(true) {
  26.                         if(mostrador.isEmpty()) {
  27.                             System.out.println("El mostrador está vacío");
  28.                             System.out.println("El cliente("+this.numCliente+") debe esperar");
  29.                             mostrador.wait();
  30.                         }
  31.                         else {
  32.                         for (Object e : mostrador) {
  33.                             if (e instanceof Bizcocho && b == null) {
  34.                                
  35.                                 b = (Bizcocho) e;
  36.                                 }
  37.                             if (e instanceof Factura && f == null) {
  38.                                 f = (Factura) e;
  39.                                 }
  40.                         }
  41.                        
  42.                        
  43.                          if (b != null && f != null) break;
  44.                        
  45.                         if (b == null && !waitBizcocho) {
  46.                             System.out.println("El cliente(" + numCliente + ") debe esperar por un bizcocho");
  47.                             waitBizcocho = true;
  48.                         }
  49.                        
  50.                        
  51.                         if (f == null && !waitFactura) {
  52.                             System.out.println("El cliente(" + numCliente + ") debe esperar por una factura");
  53.                             waitFactura = true;
  54.                         }
  55.                    
  56.                         mostrador.wait();
  57.                     }
  58.                 }
  59.                    
  60.                    
  61.                      Thread.sleep(timeConsumition);
  62.                      mostrador.remove(b);
  63.                      mostrador.remove(f);
  64.                    
  65.                      System.out.println("El cliente(" + numCliente + ") ha tomado el bizcocho(" + b.getNumBizcocho() + ", " + b.getTipoBizcocho() + ")");
  66.                      System.out.println("El cliente(" + numCliente + ") ha tomado la factura(" + f.getNumFactura() + ", " + f.getTipoFactura() + ")");
  67.                      System.out.println("El cliente(" + numCliente + ") se retiró del local.");
  68.                                
  69.                 }
  70.                            
  71.             }
  72.            
  73.            
  74.        
  75.             catch (InterruptedException e) {
  76.                 e.printStackTrace();
  77.             }          
  78.        
  79.        
  80.     }
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment