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

Commit

Permalink
Define Table Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Jun 17, 2024
1 parent b4f5ca7 commit f719bc8
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.utp.clsHerramientas.pry6_trabajo_final;

public record Cliente(String codigo, String nombre, String apellido, String direccion, String telefono_1,
String telefono_2) {

public static final Integer generales_clientes_len = 20;

public static Cliente[] cargar_clientes() {
Cliente[] clientes = new Cliente[generales_clientes_len];
String[] datosGral = new String[generales_clientes_len];
Datos.generalesCliente(datosGral);
for (int i = 0; i < generales_clientes_len; i++) {
String[] datos = datosGral[i].split(" +");
clientes[i] = new Cliente(datos[0], datos[1], datos[2], datos[3], datos[4], datos[5]);
}
return clientes;
}

public static void main(String[] args) {
Cliente[] clientes = cargar_clientes();
for (int i = 0; i < clientes.length; i++) {
System.out.println(i + "-)" + clientes[i]);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.utp.clsHerramientas.pry6_trabajo_final;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public record Factura(String codigo, int numero_factura, LocalDate fecha, BigDecimal monto)
implements Comparable<Factura> {

public static final Integer FACTURAS_CLIENTE_LEN = 60;
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy");
@Override
public int compareTo(Factura o) {
return codigo.compareTo(o.codigo);
}

public Factura(String codigo, int numero_factura, String fecha, String monto) {
this(codigo, numero_factura, LocalDate.parse(fecha, FORMATTER),
new BigDecimal(monto, new MathContext(24, RoundingMode.HALF_EVEN)));
}

public String fecha_as_string() {
return fecha.format(FORMATTER);
}

public String monto_as_string() {
var format = NumberFormat.getInstance();
format.setMinimumFractionDigits(2);
return format.format(monto);
}

public static void main(String[] args) {
Factura[] facturas = cargar_facturas();
for (int i = 0; i < facturas.length; i++) {
System.out.println(i + "-)" + facturas[i]);
}
System.out.println(facturas[58].monto_as_string());
}

public static Factura[] cargar_facturas() {
Factura[] facturas = new Factura[FACTURAS_CLIENTE_LEN];
String[] datosFac = new String[FACTURAS_CLIENTE_LEN];
Datos.facturasCliente(datosFac);
for (int i = 0; i < FACTURAS_CLIENTE_LEN; i++) {
String[] datos = datosFac[i].split(" +");
facturas[i] = new Factura(datos[0], Integer.parseInt(datos[1]), datos[2], datos[3]);
}
Arrays.sort(facturas);
return facturas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ public class Main {
public static void main(String[] args) {
}
}

/**
* Bibliografías :
* 1. https://www.rapidtables.com/web/color/RGB_Color.html
* 2. https://stackoverflow.com/questions/20165564/calculating-days-between-two-dates-with-java
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.utp.clsHerramientas.pry6_trabajo_final;

import java.util.ArrayList;

public final class Querier {
public static Factura[] selectFacturas(Cliente cliente, Factura[] facturas) {
ArrayList<Factura> facturasCliente = new ArrayList<Factura>();
for (Factura fact : facturas) {
if (fact.codigo().equals(cliente.codigo()))
facturasCliente.add(fact);
}
return facturasCliente.toArray(new Factura[0]);
}
}
174 changes: 174 additions & 0 deletions src/main/java/com/utp/clsHerramientas/pry6_trabajo_final/ui/Table.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.utp.clsHerramientas.pry6_trabajo_final.ui;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

import com.utp.clsHerramientas.pry6_trabajo_final.Cliente;
import com.utp.clsHerramientas.pry6_trabajo_final.Factura;

public final class Table {
static final Integer MAX_HEIGHT = 300;
static final Color DarkCyan = UI.DarkCyan;
static final Color DarkGreen = UI.DarkGreen;
final Factura[] facturas;
final Cliente cliente;

final JLabel[] encabezados = { new JLabel("FECHA dd/MM/yyyy"), new JLabel("NUM. FACTURA"), new JLabel("MONTO"),
new JLabel("MES CORRIENTE"),
new JLabel("30 DÍAS"), new JLabel("60 DÍAS"), new JLabel("90 DÍAS"), new JLabel("+120 DÍAS") };
final JPanel[] columnas = { new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(),
new JPanel(), new JPanel() };
final BigDecimal[] montos = { new BigDecimal("0.00"), new BigDecimal("0.00"), new BigDecimal("0.00"),
new BigDecimal("0.00"), new BigDecimal("0.00"), new BigDecimal("0.00") };
final static Integer[] rangos = { 0, 30, 60, 90, 120 };

Table(Factura[] facturas, Cliente cliente) {
// this.facturas = Querier.selectFacturas(cliente, facturas);
this.facturas = facturas;
this.cliente = cliente;
}

public JScrollPane as_panel() {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;

// Insertar los encabezados de la tabla
for (int col = 0; col < encabezados.length; col++) {
columnas[col].setLayout(new GridBagLayout());
var prefered_size = encabezados[col].getPreferredSize();
encabezados[col].setPreferredSize(new Dimension(prefered_size.width + 34, prefered_size.height + 20));
columnas[col].add(encabezados[col], c);
// columnas[col].add(, c);
encabezados[col].setHorizontalAlignment(JLabel.CENTER);
encabezados[col].setVerticalAlignment(JLabel.CENTER);
encabezados[col].setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));
encabezados[col].setForeground(DarkCyan);
encabezados[col].setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, UI.Violet));
}

c.insets = new Insets(2, 2, 2, 2);
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 1;
for (Factura factura : this.facturas) {

// Insertar datos a la fila
JLabel[] datos = { new JLabel(factura.fecha_as_string()),
new JLabel(String.valueOf(factura.numero_factura())),
new JLabel(factura.monto_as_string()) };
c.anchor = GridBagConstraints.CENTER;
for (int col = 0; col < datos.length; col++) {
columnas[col].setAlignmentX(1.0f);
datos[col].setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
datos[col].setForeground(UI.Black);
if (col >= 1) {
c.anchor = GridBagConstraints.EAST;
}
columnas[col].add(datos[col], c);
}

// Insertar los montos de la tabla
c.anchor = GridBagConstraints.EAST;
boolean populated = false;
long dias = -ChronoUnit.DAYS.between(LocalDate.now(), factura.fecha());
c.weightx = 0;
for (int col = encabezados.length - 1; col >= 3; col--) {
JLabel label = new JLabel();
label.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
if (!populated && dias > rangos[col - 3]) {
label.setForeground(DarkCyan);
label.setText(factura.monto_as_string());
montos[col - 2] = montos[col - 2].add(factura.monto());
populated = true;
} else {
label.setText("0.00");
label.setForeground(UI.Gray);
}
columnas[col].add(label, c);
}
montos[0] = montos[0].add(factura.monto());
c.gridy++;
}

JLabel[] summary_labels = { new JLabel("Trab. Final"), new JLabel("Total"),
new JLabel(montos[0].toString()),
new JLabel(montos[1].toString()),
new JLabel(montos[2].toString()),
new JLabel(montos[3].toString()),
new JLabel(montos[4].toString()),
new JLabel(montos[5].toString()) };
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(4, 0, 4, 0);
for (JLabel label : summary_labels) {
label.setHorizontalAlignment(JLabel.RIGHT);
if (label.getText().equals("0.00")) {
label.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
label.setForeground(UI.Gray);
} else {
label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));
label.setForeground(DarkGreen);
}
label.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, UI.Violet));
}
summary_labels[0].setForeground(UI.DarkCyan);
summary_labels[0].setHorizontalAlignment(JLabel.LEFT);
summary_labels[1].setForeground(UI.DarkCyan);
summary_labels[1].setHorizontalAlignment(JLabel.LEFT);

for (int col = 0; col < summary_labels.length; col++) {
columnas[col].add(summary_labels[col], c);
}

JPanel table_content = new JPanel(new GridBagLayout());
c.gridy = 0;
c.gridx = 0;
c.insets = new Insets(0, 0, 0, 0);

// Acoplar cada columna a la tabla
for (JPanel columna : columnas) {
table_content.add(columna, c);
var border = BorderFactory.createMatteBorder(2, 0, 2, 1, UI.Violet);
columna.setBorder(border);
c.gridx++;
}
columnas[0].setBorder(BorderFactory.createMatteBorder(2, 2, 2, 1, UI.Violet));
columnas[columnas.length - 1].setBorder(BorderFactory.createMatteBorder(2, 0, 2, 2, UI.Violet));
JScrollPane scrollPane = new JScrollPane(table_content);

var prefered = scrollPane.getPreferredSize();
int prefered_height = Math.min(MAX_HEIGHT, prefered.height);
int minimum_width = (MAX_HEIGHT < prefered.height) ? prefered.width + scrollPane.getVerticalScrollBar().getWidth() : prefered.width;
scrollPane.setPreferredSize(new Dimension(minimum_width, prefered_height));
return scrollPane;
}

public static void main(String[] args) {
JFrame frame = new JFrame("Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 600);
Factura[] facturas = Factura.cargar_facturas();
Cliente[] cliente = Cliente.cargar_clientes();
// Table table = new Table(Querier.selectFacturas(cliente[15], facturas), cliente[15]);
Table table = new Table(facturas, cliente[15]);
frame.add(table.as_panel());
frame.pack();
frame.setVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.utp.clsHerramientas.pry6_trabajo_final.ui;

import java.awt.Color;

public final class UI {
public static final Color Black = Color.BLACK;
public static final Color DarkCyan = new Color(0, 102, 204);
public static final Color DarkGreen = new Color(0, 153, 76);
public static final Color Gray = new Color(192, 192, 192);
public static final Color Violet = new Color(153, 0, 153);
}

0 comments on commit f719bc8

Please sign in to comment.