Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto 4 Pablo Rey #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions reto4/Caja.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Caja {
private int clientesProcesados;
private int itemsRestantes;
private boolean libre;

public Caja() {
this.clientesProcesados = 0;
this.itemsRestantes = 0;
this.libre = true;
}

public boolean isLibre() {
return libre;
}

public void ocuparCaja(int items) {
this.clientesProcesados++;
this.itemsRestantes = items;
this.libre = false;
}

public void procesarItem() {
if (itemsRestantes > 0) {
itemsRestantes--;
if (itemsRestantes == 0) {
libre = true;
}
}
}

public int getClientesProcesados() {
return clientesProcesados;
}

public int getItemsRestantes() {
return itemsRestantes;
}
}

6 changes: 6 additions & 0 deletions reto4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Tienda tienda=new Tienda();
tienda.simularDia();
}
}
88 changes: 88 additions & 0 deletions reto4/Tienda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.util.Random;

public class Tienda {
private static final int MINUTOS_EN_UN_DIA = 12 * 60;
private static final int PROBABILIDAD_LLEGADA = 40;
private Caja[] cajas;
private int cola;
private int minutosSinCola;
private int totalItems;
private boolean activa;
private Random random;

public Tienda() {
this.cajas = new Caja[5];
for (int i = 0; i < cajas.length; i++) {
cajas[i] = new Caja();
}
this.cola = 0;
this.minutosSinCola = 0;
this.totalItems = 0;
this.activa = false;
this.random = new Random();
}

public void simularDia() {
for (int tiempo = 1; tiempo < MINUTOS_EN_UN_DIA; tiempo++) {
if (random.nextInt(100) < PROBABILIDAD_LLEGADA) { // 40% de probabilidad de que llegue un nuevo cliente
cola++; // cada nuevo cliente va a la cola
asignarClienteACaja();
}

procesarItemsEnCajas();

if (cola == 0) {
minutosSinCola++;
}

imprimirEstado(tiempo);
verificarActivacionCajaExtra();
}
imprimirResumen();
}

private void asignarClienteACaja() {
for (int i = 0; i < cajas.length; i++) {
if (cajas[i].isLibre() && (i < 4 || activa)) {
int items = random.nextInt(11) + 5;
cajas[i].ocuparCaja(items);
totalItems += items;
cola--;
break;
}
}
}

private void procesarItemsEnCajas() {
for (Caja caja : cajas) {
caja.procesarItem();
}
}

private void imprimirEstado(int tiempo) {
System.out.println("--------------------------------------------------------------");
System.out.print("MINUTO " + tiempo + " - En cola: " + cola);
for (int i = 0; i < cajas.length; i++) {
System.out.print(" | Caja" + (i + 1) + ":[" + cajas[i].getItemsRestantes() + "]");
}
System.out.println();
}

private void verificarActivacionCajaExtra() {
activa = cola >= 15;
}

private void imprimirResumen() {
for (int i = 0; i < cajas.length; i++) {
System.out.println("Clientes totales que pasan por la caja " + (i + 1) + " : " + cajas[i].getClientesProcesados());
}
int totalClientes = 0;
for (Caja caja : cajas) {
totalClientes += caja.getClientesProcesados();
}
System.out.println("Personas que han pasado por la tienda: " + totalClientes);
System.out.println("Hoy se han vendido " + totalItems + " productos");
System.out.println("La cola ha estado vacía durante " + minutosSinCola + " minutos");
System.out.println("Quedan " + cola + " clientes al finalizar el día en la cola.");
}
}