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

Commit

Permalink
Stage changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Jul 2, 2024
1 parent 1fc3ddc commit e1a2440
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

public class LEColaLineal<T> extends AbstractLinkedQueue<T> {

/**
* Append at the end of the queue
*/
@Override
public void insert(T elemento) {
len += 1;
inner.insert_last(elemento);
}

/**
* Remove the latest inserted element from the queue
*/
@Override
public T remove() {
if (this.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public boolean isFull() {
return false;
}

public LinkedList<T> extricate() {
public LinkedList<T> to_inverted() {
var inner = new LinkedList<T>();
for (int i = len - 1; i >= 0; i--) {
inner.insert_first(this.inner.get(i).get());
}
return inner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int size() {
return len;
}

boolean isEmpty() {
public boolean isEmpty() {
return (len == 0);
}

Expand Down
212 changes: 198 additions & 14 deletions src/main/java/com/utp/clsEstructuraDatos/laboratorio_4/Main.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,204 @@
package com.utp.clsEstructuraDatos.laboratorio_4;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Optional;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.utp.clsEstructuraDatos.Estructuras.linked_list.LEColaCircular;
import com.utp.clsEstructuraDatos.Estructuras.linked_list.LEColaLineal;
import com.utp.clsEstructuraDatos.Estructuras.linked_list.LEPila;
import com.utp.clsEstructuraDatos.Estructuras.linked_list.LinkedList;

import static com.utp.clsEstructuraDatos.laboratorio_4.App.AppEvent.*;

public class Main {

}

class App {
static final String LABORATORIO = "Laboratorio 4";
Optional<AppCollection<Integer>> collection;
AppCollection<Integer> collection = null;
JFrame frame = new JFrame(LABORATORIO);
JLabel collection_type = new JLabel(App.UNDEFINED_COLLECTION);
JLabel message_label = new JLabel("");
JButton btn_push = new JButton("Push");
JButton btn_pop = new JButton("Pop");
JButton btn_peek = new JButton("Peek");
JButton btn_clear = new JButton("Clear");
JButton btn_create = new JButton("Create");
JButton btn_display = new JButton("Display");

public static final String UNDEFINED_COLLECTION = "Colección no definida";

App() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
collection_type.setForeground(java.awt.Color.RED);
collection_type.setHorizontalAlignment(JLabel.CENTER);
btn_display.setEnabled(false);
btn_push.setEnabled(false);
btn_pop.setEnabled(false);
btn_peek.setEnabled(false);
btn_clear.setEnabled(false);
btn_display.addActionListener(e -> SendEvent(new Display("")));
}

void new_collection() {
String[] options = { "Pila", "Cola Lineal", "Cola Circular" };
int choice = JOptionPane.showOptionDialog(null, "Seleccione el tipo de colección", LABORATORIO,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
AppCollection<Integer> new_collection = null;
switch (choice) {
case 0 -> {
new_collection = new AppCollection.Stack<>(new LEPila<>());
}
case 1 -> {
new_collection = new AppCollection.Queue<>(new LEColaLineal<>());
}
case 2 -> {
new_collection = new AppCollection.CircularQueue<>(new LEColaCircular<>());
}
}
if (collection == null) {
collection = new_collection;
return;
}
String prompt = "Quieres mantener los datos antiguos?";

int keep_data = JOptionPane.showConfirmDialog(null, prompt, LABORATORIO, JOptionPane.YES_NO_OPTION);
if (keep_data == JOptionPane.YES_OPTION) {
boolean new_coll_is_same_type_as_self = collection.getClass().equals(new_collection.getClass());
// skip extending if the new collection is the same type as the old one
if (new_coll_is_same_type_as_self) {
return;
}
new_collection.extend_from(this.collection);
this.collection = new_collection;
} else {
// No good reason to explicitly clear the old collection.
this.collection = new_collection;
}
}

void SendEvent(AppEvent event) {
switch (event) {
case Push(int element) -> {
collection.insert(element);
SendEvent(new Display("Elemento insertado: " + element));
}
case Pop() -> {
if (collection.isEmpty()) {
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));
}
case Peek() -> {
if (collection.isEmpty()) {
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));
}
case Clear() -> {
collection.clear();
SendEvent(new Display("Colección limpiada"));
}
case Create() -> {
new_collection();
if (collection == null) {
collection_type.setText(UNDEFINED_COLLECTION);
return;
} 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)));
}
}
case ExtendFrom(AppCollection<Integer> source) -> {
collection.extend_from(source);
throw new UnsupportedOperationException("Not implemented");
}
case Display(String msg) -> {
btn_display.setText(msg);
}
}
}

void run() {
frame.add(button_pane());
frame.pack();
frame.setVisible(true);
}

JPanel content_pane() {
JPanel content_pane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(4, 1, 4, 1);
gbc.gridx = 0;
gbc.gridy = 0;
content_pane.add(collection_type, gbc);
gbc.gridy = 1;
content_pane.add(button_pane(), gbc);
gbc.gridy = 2;
content_pane.add(message_label, gbc);
return content_pane;
}

void new_collection() {}
JPanel button_pane() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 1, 2, 1);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(btn_create, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
panel.add(btn_push, gbc);
gbc.gridx = 1;
panel.add(btn_pop, gbc);
gbc.gridy = 2;
gbc.gridx = 0;
panel.add(btn_peek, gbc);
gbc.gridx = 1;
panel.add(btn_clear, gbc);
gbc.gridy = 3;
gbc.gridx = 0;
panel.add(btn_display, gbc);

return panel;

}

void error_dialogue(String msg) {
JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE);
}

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) implements AppEvent {}
public static record ExtendFrom(AppCollection<Integer> source) implements AppEvent {}
// @formatter:on
}
}

sealed interface AppCollection<T> {
Expand All @@ -28,27 +210,29 @@ sealed interface AppCollection<T> {
boolean isFull();
void clear();
int len();
default AppCollection<T> extend_from(AppCollection<T> source){
// @formatter:on

default AppCollection<T> extend_from(AppCollection<T> source) {
switch (source) {
case Stack<T>(var stack) -> {
var element = stack.extricate
this.insert()
LinkedList<T> list = stack.to_inverted();
while (!list.isEmpty()) {
this.insert(list.remove_first().get());
}
}
case Queue<T>(var queue) -> {
for (int i = 0; i < list.len(); i++) {
queue.insert(list.get(i).get());
case Queue<T>(LEColaLineal<T> queue) -> {
while (!queue.isEmpty()) {
this.insert(queue.remove());
}
return new Queue<>(queue);
}
case CircularQueue<T>(var circular_queue) -> {
for (int i = 0; i < list.len(); i++) {
circular_queue.insert(list.get(i).get());
case CircularQueue<T>(LEColaCircular<T> circular_queue) -> {
while (!circular_queue.isEmpty()) {
this.insert(circular_queue.remove());
}
return new CircularQueue<>(circular_queue);
}
}
return this;
}
// @formatter:on

public record Stack<T>(LEPila<T> pila) implements AppCollection<T> {

Expand Down

0 comments on commit e1a2440

Please sign in to comment.