0% found this document useful (0 votes)
4 views17 pages

Codigo Tutoría 5

The document outlines a programming project for a stationery store application developed in Java, focusing on object-oriented programming principles. It includes the implementation of a graphical user interface (GUI) for product selection, quantity input, and total calculation, utilizing various Java Swing components. The application maintains a list of products with their prices and calculates subtotals, discounts, and net amounts based on user input.

Uploaded by

mateo 12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Codigo Tutoría 5

The document outlines a programming project for a stationery store application developed in Java, focusing on object-oriented programming principles. It includes the implementation of a graphical user interface (GUI) for product selection, quantity input, and total calculation, utilizing various Java Swing components. The application maintains a list of products with their prices and calculates subtotals, discounts, and net amounts based on user input.

Uploaded by

mateo 12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Trabajo de programación

Formulario Papelería la

Cipas 5

Esteban Velez
Emanuel Pérez
Miguel Vila
Mateo Palomino

Tutor

Alexander

Universidad Del Tolima


Programación orientada a objetos

ING en sistemas grupo 2 semestre 2


/*Que

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template

*/

package com.mycompany.papeleria;

import javax.swing.DefaultListModel;

import javax.swing.DefaultListModel;

import java.util.Map;

import java.util.LinkedHashMap;

import javax.swing.JOptionPane;

/**

* @author DALIA

*/

public class papeleriaa extends javax.swing.JFrame {

// Modelos para las listas (JList)

DefaultListModel<String> numeroModel = new DefaultListModel<>();

DefaultListModel<String> descripcionModel = new DefaultListModel<>();

DefaultListModel<String> precioModel = new DefaultListModel<>();

DefaultListModel<String> cantidadModel = new DefaultListModel<>();

DefaultListModel<String> subtotalModel = new DefaultListModel<>();

// Mapa con los productos y sus precios

Map<String, Double> productos = new LinkedHashMap<>();


// Contador para numerar productos

int contador = 1;

// Acumulador del total

double total = 0.0;

public papeleriaa() {

initComponents();

// Llenar el ComboBox con productos

cboProducto.addItem("Cuaderno 100 hojas cuadriculado");

cboProducto.addItem("Cuaderno 100 hojas líneas");

cboProducto.addItem("Cuaderno 50 hojas cuadriculado");

cboProducto.addItem("Cuaderno 50 hojas líneas");

cboProducto.addItem("Cuaderno 5 materias cuadriculado");

cboProducto.addItem("Cuaderno 5 materias líneas");

// Llenar el mapa con productos y precios

productos.put("Cuaderno 100 hojas cuadriculado", 7000.0);

productos.put("Cuaderno 100 hojas líneas", 5000.0);

productos.put("Cuaderno 50 hojas cuadriculado", 3500.0);

productos.put("Cuaderno 50 hojas líneas", 2500.0);

productos.put("Cuaderno 5 materias cuadriculado", 10000.0);

productos.put("Cuaderno 5 materias líneas", 8000.0);

// Enlazar los modelos con los JList

lstNumero.setModel(numeroModel);
lstDescripcion.setModel(descripcionModel);

lstPrecio.setModel(precioModel);

lstCantidad.setModel(cantidadModel);

lstSubtotal.setModel(subtotalModel);

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

cboProducto = new javax.swing.JComboBox<>();

txtCantidad = new javax.swing.JTextField();

btnComprar = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

lstNumero = new javax.swing.JList<>();

jScrollPane2 = new javax.swing.JScrollPane();

lstDescripcion = new javax.swing.JList<>();

jScrollPane3 = new javax.swing.JScrollPane();

lstPrecio = new javax.swing.JList<>();

jScrollPane4 = new javax.swing.JScrollPane();

lstCantidad = new javax.swing.JList<>();

jScrollPane5 = new javax.swing.JScrollPane();


lstSubtotal = new javax.swing.JList<>();

btnLimpiar = new javax.swing.JButton();

btnSalir = new javax.swing.JButton();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

lblSubtotal = new javax.swing.JLabel();

lblDescuento = new javax.swing.JLabel();

lblNeto = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setName("papeleria"); // NOI18N

jLabel1.setText("VENTA DE PRODUCTOS");

cboProducto.setBorder(javax.swing.BorderFactory.createTitledBorder("Descripcion del producto"));

cboProducto.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

cboProductoActionPerformed(evt);

});

txtCantidad.setBorder(javax.swing.BorderFactory.createTitledBorder("Cantidad"));

txtCantidad.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtCantidadActionPerformed(evt);

});
btnComprar.setText("Comprar");

btnComprar.addChangeListener(new javax.swing.event.ChangeListener() {

public void stateChanged(javax.swing.event.ChangeEvent evt) {

btnComprarStateChanged(evt);

});

btnComprar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnComprarActionPerformed(evt);

});

lstNumero.setBorder(javax.swing.BorderFactory.createTitledBorder("No"));

jScrollPane1.setViewportView(lstNumero);

lstDescripcion.setBorder(javax.swing.BorderFactory.createTitledBorder("Descripcion"));

jScrollPane2.setViewportView(lstDescripcion);

lstPrecio.setBorder(javax.swing.BorderFactory.createTitledBorder("Precio"));

jScrollPane3.setViewportView(lstPrecio);

lstCantidad.setBorder(javax.swing.BorderFactory.createTitledBorder("Cantidad"));

jScrollPane4.setViewportView(lstCantidad);

lstSubtotal.setBorder(javax.swing.BorderFactory.createTitledBorder("Subtotal"));

jScrollPane5.setViewportView(lstSubtotal);

btnLimpiar.setText("Limpiar");

btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

btnLimpiarActionPerformed(evt);

});

btnSalir.setText("Salir");

btnSalir.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnSalirActionPerformed(evt);

});

jLabel2.setText("Subtotal");

jLabel3.setText("Descuento");

jLabel4.setText("Neto a pagar");

lblSubtotal.setBorder(new
javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

lblDescuento.setBorder(new
javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

lblNeto.setBorder(new
javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(16, 16, 16)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 62,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
Short.MAX_VALUE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 66,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 85,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 71,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(10, 10, 10))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()

.addComponent(btnSalir)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel4))

.addGroup(layout.createSequentialGroup()

.addComponent(btnLimpiar)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel2))

.addGroup(layout.createSequentialGroup()

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel3)))))

.addGap(30, 30, 30)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(lblSubtotal, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(lblDescuento, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(lblNeto, javax.swing.GroupLayout.DEFAULT_SIZE, 115,


Short.MAX_VALUE))

.addGap(33, 33, 33))))

.addGroup(layout.createSequentialGroup()

.addGap(41, 41, 41)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(btnComprar, javax.swing.GroupLayout.PREFERRED_SIZE, 226,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createSequentialGroup()

.addComponent(cboProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 156,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(31, 31, 31)

.addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 85,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(315, Short.MAX_VALUE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 223,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(129, 129, 129))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel1)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(cboProducto, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(btnComprar)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(49, 49, 49)

.addComponent(btnLimpiar)

.addGap(18, 18, 18)

.addComponent(btnSalir))

.addGroup(layout.createSequentialGroup()

.addGap(20, 20, 20)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(lblSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 23,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel2))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(lblDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 24,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(lblNeto, javax.swing.GroupLayout.PREFERRED_SIZE, 26,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel4))))

.addGap(45, 45, 45))

);

pack();

}// </editor-fold>
private void txtCantidadActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void btnComprarStateChanged(javax.swing.event.ChangeEvent evt) {

// TODO add your handling code here:

private void cboProductoActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void btnComprarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

// Obtener producto seleccionado

String producto = cboProducto.getSelectedItem().toString();

// Obtener cantidad ingresada

String cantidadTexto = txtCantidad.getText().trim();

if (cantidadTexto.isEmpty() || !cantidadTexto.matches("\\d+")) {

JOptionPane.showMessageDialog(this, "Ingresa una cantidad válida.");

return;

int cantidad = Integer.parseInt(cantidadTexto);

// Obtener precio del producto

double precio = productos.get(producto);


// Calcular subtotal

double subtotal = cantidad * precio;

// Agregar datos a los modelos

numeroModel.addElement(String.valueOf(contador));

descripcionModel.addElement(producto);

precioModel.addElement(String.format("%.0f", precio));

cantidadModel.addElement(String.valueOf(cantidad));

subtotalModel.addElement(String.format("%.0f", subtotal));

// Actualizar total

total += subtotal;

lblSubtotal.setText(String.format("Subtotal: $ %.0f", total));

// Aumentar contador

contador++;

// Limpiar campo cantidad

txtCantidad.setText("");

// Calcular subtotal acumulado

double total = 0.0;

for (int i = 0; i < subtotalModel.size(); i++) {

total += Double.parseDouble(subtotalModel.get(i));

// Calcular descuento y neto a pagar

double descuento = 0.0;


if (total >= 100000) {

descuento = total * 0.10;

double netoPagar = total - descuento;

// Mostrar en etiquetas

lblSubtotal.setText("Subtotal: $ " + String.format("%.0f", total));

lblDescuento.setText("$ " + String.format("%.0f", descuento));

lblNeto.setText("$ " + String.format("%.0f", netoPagar));

private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

// Limpiar modelos

numeroModel.clear();

descripcionModel.clear();

precioModel.clear();

cantidadModel.clear();

subtotalModel.clear();

// Reiniciar contador y total

contador = 1;

total = 0.0;

// Limpiar subtotal visual

lblSubtotal.setText("Subtotal: $ 0");

// Limpiar campo de cantidad


txtCantidad.setText("");

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int confirmacion = JOptionPane.showConfirmDialog(this, "¿Deseas salir?", "Confirmar salida",


JOptionPane.YES_NO_OPTION);

if (confirmacion == JOptionPane.YES_OPTION) {

System.exit(0);

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(papeleriaa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(papeleriaa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(papeleriaa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(papeleriaa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new papeleriaa().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton btnComprar;

private javax.swing.JButton btnLimpiar;

private javax.swing.JButton btnSalir;


private javax.swing.JComboBox<String> cboProducto;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JScrollPane jScrollPane2;

private javax.swing.JScrollPane jScrollPane3;

private javax.swing.JScrollPane jScrollPane4;

private javax.swing.JScrollPane jScrollPane5;

private javax.swing.JLabel lblDescuento;

private javax.swing.JLabel lblNeto;

private javax.swing.JLabel lblSubtotal;

private javax.swing.JList<String> lstCantidad;

private javax.swing.JList<String> lstDescripcion;

private javax.swing.JList<String> lstNumero;

private javax.swing.JList<String> lstPrecio;

private javax.swing.JList<String> lstSubtotal;

private javax.swing.JTextField txtCantidad;

// End of variables declaration

You might also like