Advertisement
LEANDRONIEVA

Clase cliente tp5

Oct 14th, 2023
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.Random;
  2. import javax.lang.model.element.Element;
  3.  
  4. public class Cliente extends Thread{
  5.  
  6.     private String nombre;
  7.     private static int indice = 0;
  8.     private Mostrador<Element> cola;
  9.     private Random random = new Random();
  10.     private int lim_superior_compra = 400;
  11.     private int lim_inferior_compra = 200;
  12.     private int lim_superior_llegada = 1500;
  13.     private int lim_inferior_llegada = 800;
  14.    
  15.     public Cliente(Mostrador<Element> mostrador) {
  16.         indice++;
  17.         this.nombre = "Cliente "+ indice;
  18.         this.cola = mostrador;
  19.     }
  20.    
  21.     public void run() {
  22.  
  23.         try {
  24.             Thread.sleep(random.nextInt(this.lim_inferior_llegada, this.lim_superior_llegada));
  25.         } catch (InterruptedException e) {
  26.             Thread.currentThread().interrupt();
  27.         }
  28.        
  29.         if(cola.noHayBizc()&&cola.noHayFact()) {
  30.             System.out.println("Mostrador vacío, cliente "+indice+" esperando");
  31.             try {
  32.                 wait();
  33.             } catch (InterruptedException e) {
  34.                 // TODO Auto-generated catch block
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.         int bizcocho = cola.comprarBizcocho();
  39.         int factura = cola.comprarFactura();
  40.  
  41.         System.out.println(this.nombre + " compró: bizcocho " + bizcocho + " y facura " + factura);
  42.         try {
  43.             Thread.sleep(random.nextInt(this.lim_inferior_compra, this.lim_superior_compra));
  44.         } catch (InterruptedException e) {
  45.             Thread.currentThread().interrupt();
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement