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

Commit

Permalink
Presentable de laboratorio
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Jul 4, 2024
1 parent 216a402 commit 5ed25bb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.utp.clsEstructuraDatos.Estructuras.linked_list.LinkedList;

public abstract class AbstractLinkedQueue<T> implements IQueue<T> {
protected LinkedList<T> inner;
protected LinkedList<T> inner = new LinkedList<>();
protected int len = 0;

@Override
Expand All @@ -25,4 +25,15 @@ public void clear() {
this.len = 0;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < inner.len(); i++) {
sb.append(inner.get(i).get().toString());
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append("]");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public T remove() {
if (this.isEmpty()) {
return null;
}
if (inner.len() == 0) {
if (inner.len() == 1) {
len = 0;
}
return inner.remove_first().get();
Expand All @@ -39,4 +39,5 @@ public T peek() {
public LinkedList<T> extricate() {
return inner;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public int len() {

@Override
public void clear() {
len = 0;
inner.clear();
}

Expand All @@ -64,4 +65,16 @@ public LinkedList<T> to_inverted() {
}
return inner;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < len; i++) {
sb.append(inner.get(i).get().toString());
sb.append("| ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append("]");
return sb.toString();
}
}
89 changes: 71 additions & 18 deletions src/main/java/com/utp/clsEstructuraDatos/laboratorio_4/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.utp.clsEstructuraDatos.laboratorio_4;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
Expand All @@ -11,6 +12,7 @@
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import com.utp.clsEstructuraDatos.Estructuras.linked_list.LEColaCircular;
import com.utp.clsEstructuraDatos.Estructuras.linked_list.LEColaLineal;
Expand All @@ -20,15 +22,18 @@
import static com.utp.clsEstructuraDatos.laboratorio_4.App.AppEvent.*;

public class Main {

public static void main(String[] args) {
App app = new App();
app.run();
}
}

class App {
static final String LABORATORIO = "Laboratorio 4";
AppCollection<Integer> collection = null;
JFrame frame = new JFrame(LABORATORIO);
JLabel collection_type = new JLabel(App.UNDEFINED_COLLECTION);
JLabel message_label = new JLabel("");
JTextArea message_label = new JTextArea("");
JButton btn_push = new JButton("Push");
JButton btn_pop = new JButton("Pop");
JButton btn_peek = new JButton("Peek");
Expand All @@ -37,7 +42,7 @@ class App {
JButton btn_display = new JButton("Display");

public static final Color Red = Color.RED;
public static final Color Green = Color.GREEN;
public static final Color Green = new Color(1, 113, 72);
public static final Color Black = Color.BLACK;
public static final String UNDEFINED_COLLECTION = "Colección no definida";

Expand All @@ -51,11 +56,12 @@ class App {
btn_pop.setEnabled(false);
btn_peek.setEnabled(false);
btn_clear.setEnabled(false);
btn_display.addActionListener(e -> SendEvent(new Display("", Black)));
btn_display.addActionListener(e -> SendEvent(new Display()));
btn_pop.addActionListener(e -> SendEvent(new Pop()));
btn_peek.addActionListener(e -> SendEvent(new Peek()));
btn_clear.addActionListener(e -> SendEvent(new Clear()));
btn_create.addActionListener(e -> SendEvent(new Create()));
message_label.setEditable(false);
btn_push.addActionListener(e -> {
String input = JOptionPane.showInputDialog("Ingrese un número entero");
if (input == null) {
Expand Down Expand Up @@ -111,30 +117,35 @@ void SendEvent(AppEvent event) {
switch (event) {
case Push(int element) -> {
collection.insert(element);
SendEvent(new Display("Elemento insertado: " + element, Green));
SendEvent(new UpdateMsg("Elemento insertado: " + element, Green));
}
case Pop() -> {
if (collection.isEmpty()) {
SendEvent(new UpdateMsg("Collecion vacía", Red));
error_dialogue("No se puede quitar de una colección vacía.");
return;
}
Optional<Integer> popped = collection.pop();
String popped_string = popped.get().toString();
SendEvent(new Display("Elemento quitado: " + popped_string, Black));
SendEvent(new UpdateMsg("Elemento quitado: " + popped_string, Black));
}
case Peek() -> {
if (collection.isEmpty()) {
SendEvent(new Display("Collecion vacía", Red));
SendEvent(new UpdateMsg("Colección vacía", Red));
error_dialogue("No se puede ver el elemento de una colección vacía.");
return;
}
var elemento = collection.peek();
String elemento_string = elemento.get().toString();
SendEvent(new Display("Viendo Elemento: " + elemento_string, Black));
SendEvent(new UpdateMsg("Viendo Elemento: " + elemento_string, Black));
}
case Clear() -> {
if (collection.isEmpty()) {
SendEvent(new UpdateMsg("La colección ya está vacía", Black));
return;
}
collection.clear();
SendEvent(new Display("Colección limpiada", Black));
SendEvent(new UpdateMsg("Colección limpiada", Black));
}
case Create() -> {
new_collection();
Expand All @@ -144,23 +155,46 @@ case Create() -> {
} else {
String collection_type = collection.getClass().getSimpleName();
this.collection_type.setText("Tipo de collección: " + collection_type);
SendEvent(new Display(String.format("Colección `%s` creada", collection_type), Green));
btn_display.setEnabled(true);
btn_push.setEnabled(true);
btn_pop.setEnabled(true);
btn_peek.setEnabled(true);
btn_clear.setEnabled(true);
SendEvent(new UpdateMsg(String.format("Colección `%s` creada", collection_type), Green));
}
}
case ExtendFrom(AppCollection<Integer> source) -> {
collection.extend_from(source);
throw new UnsupportedOperationException("Not implemented");
}
case Display(String msg, Color clr) -> {
btn_display.setText(msg);
btn_display.setForeground(clr);
case Display() -> {
if (collection.isEmpty()) {
SendEvent(new UpdateMsg("Colección vacía", Red));
return;
}
StringBuilder sb = new StringBuilder();
sb.append("Elementos en la colección: ");
sb.append(collection.len());
sb.append("\n");
sb.append("Elementos: ");
sb.append(collection);
SendEvent(new UpdateMsg(sb.toString(), Black));
}
case UpdateMsg(String msg, Color clr) -> {
message_label.setText(msg);
message_label.setForeground(clr);
try_pack();
}
}
}

void run() {
frame.add(content_pane());
frame.pack();
frame.setLocationRelativeTo(null);
var prefered = frame.getPreferredSize();
prefered.width = 280;
frame.setSize(prefered);
frame.setVisible(true);
}

Expand Down Expand Up @@ -200,6 +234,7 @@ JPanel button_pane() {
panel.add(btn_clear, gbc);
gbc.gridy = 3;
gbc.gridx = 0;
gbc.gridwidth = 2;
panel.add(btn_display, gbc);

return panel;
Expand All @@ -210,14 +245,25 @@ void error_dialogue(String msg) {
JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE);
}

// Packs the frame to the preferred size, updating if undersized
public void try_pack() {
Dimension current_size = frame.getSize();
Dimension preferred_size = frame.getPreferredSize();
int new_width = Math.max(current_size.width, preferred_size.width);
int new_height = Math.max(current_size.height, preferred_size.height);
if (new_width != current_size.width || new_height != current_size.height)
frame.setSize(new_width, new_height);
}

public sealed static interface AppEvent {
// @formatter:off
public static record Push(int element) implements AppEvent {}
public static record Pop() implements AppEvent {}
public static record Peek() implements AppEvent {}
public static record Clear() implements AppEvent {}
public static record Create() implements AppEvent {}
public static record Display(String msg, Color color) implements AppEvent {}
public static record Display() implements AppEvent {}
public static record UpdateMsg(String msg, Color color) implements AppEvent {}
public static record ExtendFrom(AppCollection<Integer> source) implements AppEvent {}
// @formatter:on
}
Expand All @@ -234,12 +280,19 @@ sealed interface AppCollection<T> {
int len();
// @formatter:on

default AppCollection<T> extend_from(AppCollection<T> source) {
public default AppCollection<T> extend_from(AppCollection<T> source) {
switch (source) {
case Stack<T>(var stack) -> {
LinkedList<T> list = stack.to_inverted();
while (!list.isEmpty()) {
this.insert(list.remove_first().get());

if (this instanceof Stack<T>) {
LinkedList<T> source_list = stack.to_inverted();
while (!source_list.isEmpty()) {
this.insert(source_list.remove_first().get());
}
} else {
while (!stack.isEmpty()) {
this.insert(stack.pop());
}
}
}
case Queue<T>(LEColaLineal<T> queue) -> {
Expand Down

0 comments on commit 5ed25bb

Please sign in to comment.