Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Laboratorio 3 - Colas simples y circulares
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Jun 16, 2024
1 parent e86b9ef commit d852694
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.utp.clsEstructuraDatos.Estructuras.colas;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
Expand Down Expand Up @@ -73,6 +74,11 @@ public void reset() {
this.longitud = 0;
}

/** Cantidad de elementos accesibles en la cola */
public int len() {
return this.longitud;
}

/**
* Devuelve una representación opinionada de la cola en forma de cadena
*/
Expand Down Expand Up @@ -134,8 +140,12 @@ public int capacity() {

public JPanel as_panel() {
JPanel panel = new JPanel(new GridBagLayout());
var c = new GridBagConstraints(0, 0, 1, 5, 0, 0, GridBagConstraints.CENTER, 0, new Insets(5, 5, 5, 5), 0,
0);
var c = new GridBagConstraints();
c.insets = new Insets(2, 5, 5, 5);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;

panel.add(front_surplus, c);
c.gridy = 1;
panel.add(frente_item, c);
Expand Down Expand Up @@ -166,66 +176,90 @@ public JPanel as_panel() {
*/
public void aligned() {
int frente = this.frente();
int cola = this.cola();
int capacidad = this.capacity();
int longitud = this.longitud();

if (frente > 0)
this.front_surplus.setText("[" + (frente - 1) + "]");
else
if (frente > 0) {
this.front_surplus.setText("[+" + (frente) + "...]");
this.front_surplus.setForeground(Color.BLACK);
this.front_surplus.setToolTipText("Espacio libre antes del frente");
} else {
this.front_surplus.setText("");
this.front_surplus.setToolTipText(null);
}
update_content();

// ******************************************

this.frente_item.setText("[" + this.cola.inner[frente] + "]");
// ******************************************

if (longitud > 0)
this.middle.setText("[..." + longitud + "...]");
else
this.middle.setText("[0]");

// ******************************************
this.cola_item.setText("[" + this.cola.inner[cola] + "]");

// ******************************************

if (cola < capacidad - 1)
this.cola_surplus.setText("[+" + (capacidad - (cola + 1)) + "...]");
else
this.cola_surplus.setText("");
int capacidad = this.capacity();
if (this.longitud() < capacidad) {
this.cola_surplus.setText("[...+" + (capacidad - this.cola()) + "]");
this.cola_surplus.setForeground(Color.BLACK);
this.cola_surplus.setToolTipText("Espacio libre para inserción");
} else {
this.cola_surplus.setText("[Full]");
this.cola_surplus.setForeground(Color.RED);
this.cola_surplus.setToolTipText(null);
}
}

/**
* llamado cuando frente (donde se retira de la fila) es mayor que el indice de
* la cola
*/
public void wrapped() {

int frente = this.frente();
int cola = this.cola();
int capacidad = this.capacity();
int longitud = this.longitud();
this.front_surplus.setText("Frente idx: (" + frente + ")");
this.front_surplus.setToolTipText("Indice de frente");
update_content();

if (this.longitud() < this.capacity()) {
this.cola_surplus.setText("[...+" + (frente - this.cola()) + "]");
this.cola_surplus.setForeground(Color.BLACK);
this.cola_surplus.setToolTipText("Espacio libre para inserción");
} else {
this.cola_surplus.setText("[Full]");
this.cola_surplus.setForeground(Color.RED);
this.cola_surplus.setToolTipText(null);
}
}

this.front_surplus.setText("(" + frente + ")");
void update_content() {

// ******************************************

this.frente_item.setText("[" + this.cola.inner[frente] + "]");

if (longitud > 0) {
this.frente_item.setText("Frente: [" + this.cola.inner[frente] + "]");
this.frente_item.setForeground(Color.BLACK);
this.frente_item.setToolTipText("Elemento en la posición de frente");
} else {
this.frente_item.setText("Frente: [_EMPTY_]");
this.frente_item.setForeground(Color.RED);
this.frente_item.setToolTipText("La cola está vacía");
}
// ******************************************

if (longitud > 0)
this.middle.setText("[..." + longitud + "...]");
else
this.middle.setText("[0]");
if (longitud > 0) {
this.middle.setText("Longitud: [..." + longitud + "...]");
this.middle.setForeground(Color.BLACK);
this.middle.setToolTipText("Cantidad de elementos en la cola");
} else {
this.middle.setText("Longitud: [0]");
this.middle.setForeground(Color.RED);
this.middle.setToolTipText("No hay elementos en la cola");
}

// ******************************************
this.cola_item.setText("[" + this.cola.inner[cola] + "]");
int cola_index_item = (this.cola() - 1) % capacidad;
if (cola_index_item < 0) {
cola_index_item += capacidad;
}
if (longitud > 0) {
this.cola_item.setText("Cola: [" + this.cola.inner[cola_index_item] + "]");
this.cola_item.setForeground(Color.BLACK);
this.cola_item.setToolTipText("Elemento en la posición de cola");
} else {
this.cola_item.setText("Cola: [_EMPTY_]");
this.cola_item.setForeground(Color.RED);
this.cola_item.setToolTipText("La cola está vacía");
}

// ******************************************

this.cola_surplus.setText("[" + (capacidad - (cola + 1)) + "]");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Option<T> quitar() {
return new Option.None<>();

T elemento = this.inner[frente];
this.inner[frente] = null;
frente = (frente + 1) % capacidad;
if (frente == cola)
this.reset();
Expand Down
90 changes: 60 additions & 30 deletions src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javax.management.RuntimeErrorException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

Expand Down Expand Up @@ -31,7 +30,7 @@ class ColasApp {
final JButton btn_quitar = new JButton("Quitar");
final JButton btn_limpiar = new JButton("Limpiar");
final JButton btn_mostrar = new JButton("Mostrar");
final JPanel panel_cola = new JPanel();
final JPanel display_area = new JPanel();
final JFrame frame = new JFrame(LABORATORIO);
AbstractCola<Integer>.PanelDrawer drawer;
AbstractCola<Integer> cola;
Expand All @@ -45,26 +44,26 @@ public void run_app() {
frame.add(content());
frame.setVisible(true);
frame.pack();
var pref_size = frame.getPreferredSize();
int pref_width = Math.max(pref_size.width, 275);
frame.setSize(pref_width, pref_size.height);
this.send_command(null);
}

ColasApp() {
this.btn_crear_cola.addActionListener(e -> {
// TODO: Descomentar

// String input = JOptionPane.showInputDialog(frame, "Ingrese la capacidad de la
// cola", "Crear Cola",
// JOptionPane.QUESTION_MESSAGE);
// try {
// Integer.parseInt(input);
// } catch (NumberFormatException e1) {
// JOptionPane.showMessageDialog(frame, "Debe ingresar un número entero",
// "Error",
// JOptionPane.ERROR_MESSAGE);
// return;
// }
// int capacidad_cola = Integer.parseInt(input);
var capacidad_cola = 10;

String input = JOptionPane.showInputDialog(frame, "Ingrese la capacidad de la cola", "Crear Cola",
JOptionPane.QUESTION_MESSAGE);
try {
Integer.parseInt(input);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(frame, "Debe ingresar un número entero",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
int capacidad_cola = Integer.parseInt(input);
this.send_command(new CrearCola(capacidad_cola));
});
this.btn_insertar.addActionListener(e -> {
Expand Down Expand Up @@ -102,7 +101,7 @@ JPanel content() {
content.add(buttons_panel(), c);

c.gridy = 1;
content.add(panel_cola, c);
content.add(display_area, c);
return content;
}

Expand Down Expand Up @@ -149,7 +148,6 @@ public int prompt_colas_type() {
LABORATORIO + ": Tipo de Cola",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
new String[] { "Cola Circular", "Cola Simple" }, "Cola Circular");
System.out.println("Seleccion del tipo: " + num);
return num;
}

Expand All @@ -167,25 +165,34 @@ case Pack() -> {
this.try_pack();
}
case Redraw() -> {

if (this.cola == null)
return;
else if (this.drawer.cola() < this.drawer.frente() || (this.drawer.longitud() == this.drawer.capacity()
&& this.drawer.cola() <= this.drawer.frente()))
this.drawer.wrapped();
else
this.drawer.aligned();
this.try_pack();
}
case CrearCola(int capacidad_cola) -> {
// TODO: Descomentar
// int cola = prompt_colas_type();
// if (cola == COLA_CIRCULAR) {
// this.cola = new ColaCircular<>(capacidad_cola);
// } else {
// this.cola = new ColaSimple<>(capacidad_cola);
// }
this.cola = new ColaCircular<>(10);
int cola = prompt_colas_type();
if (this.cola != null) {
this.cola.limpiar();
this.drawer.aligned();
}
if (cola == COLA_CIRCULAR) {
this.cola = new ColaCircular<>(capacidad_cola);
} else {
this.cola = new ColaSimple<>(capacidad_cola);
}
String new_text = String.format("[%s (c:%d)]Recrear Cola", this.cola.getClass().getSimpleName(),
this.cola.capacity());
this.btn_crear_cola.setText(new_text);
switch_buttons(true);
this.drawer = this.cola.as_drawer();
this.panel_cola.removeAll();
this.panel_cola.add(drawer.as_panel());
this.display_area.removeAll();
this.display_area.add(drawer.as_panel());
drawer.aligned();
this.send_command(new Redraw());
this.try_pack();
}
Expand Down Expand Up @@ -213,11 +220,34 @@ case Mostrar() -> {
if (this.cola == null) {
this.btn_crear_cola.setText("Crear Cola");
switch_buttons(false);
} else { // Only operations that can be done on a non-null queue
if (this.cola.len() <= 0) {
this.btn_quitar.setEnabled(false);
this.btn_quitar.setToolTipText("No hay elementos para quitar");
this.btn_limpiar.setEnabled(false);
this.btn_limpiar.setToolTipText("No hay elementos para limpiar");
} else {
this.btn_quitar.setEnabled(true);
this.btn_quitar.setToolTipText(null);
this.btn_limpiar.setEnabled(true);
this.btn_limpiar.setToolTipText(null);
}

if (this.cola.len() == this.cola.capacity()) {
this.btn_insertar.setEnabled(false);
this.btn_insertar.setToolTipText("La cola está llena");
} else {
this.btn_insertar.setEnabled(true);
this.btn_insertar.setToolTipText(null);
}
}
}

public static sealed interface CMD {
// @formatter:off
/**
* Agranda la ventana al tamaño preferido, actualizando si está subdimensionada.
*/
public static record Pack() implements CMD {}
public static record CrearCola(int capacidad_cola) implements CMD {}
public static record Insertar<T>(T elemento) implements CMD {}
Expand Down

0 comments on commit d852694

Please sign in to comment.