online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
# # Welcome to GDB Online. # GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, # C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. # Code, Compile, Run and Debug online from anywhere in world. # # print("Hello World")
01 Cuaderno 4 5 02 Libro 20 5 03 Lapicero 3 5 04 Regla 1 5
01 Cuaderno 4.0 0 02 Libro 20.0 0 03 Lapicero 3.0 4 04 Regla 1.0 5
/* * 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 frames; import javax.swing.JTextField; import javax.swing.JTable; import java.util.ArrayList; import negocio.*; import dataBase.*; import java.awt.event.KeyEvent; import java.util.HashSet; import java.util.Set; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import vista.SwingValidator; /** * * @author carlos */ public class ProductoVentaApp extends javax.swing.JFrame { private final String FIELDSEP = "\t"; ProductoDAO productDAO = DAOFactory.getProductoDAO(); ArrayList<Producto> productos; // contendrá todos los productos de String cNames[] = {"Código", "Descripción", "Precio", "Stock", "Cantidad", "Total"}; String data[][] = {}; DefaultTableModel md = new DefaultTableModel(data, cNames); Producto p = null; ProductoTextFile pW; int inicio=1, nCompra=0, nFactura=1; double pago = 0; public ProductoVentaApp() { initComponents(); pW = new ProductoTextFile(); productos = pW.getProductos(); this.fillSeleccionar(productos); this.codigo.setText(""); this.descripcion.setText(""); this.precio.setText(""); this.stock.setText(""); cantidad.setEditable(false); this.tItems.setModel(md); // obtiene productos, llama a fillSeleccionar(...) y asigna modelo a tItems } private void fillSeleccionar(ArrayList<Producto> al) { for(Producto p : productos){ this.seleccionar.addItem(p.getCodigo() + "-"+ p.getDescripcion()); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); seleccionar = new javax.swing.JComboBox<>(); cantidad = new javax.swing.JTextField(); codigo = new javax.swing.JTextField(); descripcion = new javax.swing.JTextField(); precio = new javax.swing.JTextField(); stock = new javax.swing.JTextField(); comprar = new javax.swing.JButton(); pagar = new javax.swing.JButton(); total = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); tItems = new javax.swing.JTable(); factura = new javax.swing.JButton(); salir = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Venta de Produtos"); jLabel1.setText("Seleccione un producto: "); seleccionar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { seleccionarActionPerformed(evt); } }); cantidad.setEditable(false); cantidad.setHorizontalAlignment(javax.swing.JTextField.RIGHT); cantidad.setBorder(javax.swing.BorderFactory.createTitledBorder("Cantidad")); cantidad.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { cantidadFocusGained(evt); } }); cantidad.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cantidadActionPerformed(evt); } }); cantidad.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { cantidadKeyPressed(evt); } public void keyTyped(java.awt.event.KeyEvent evt) { cantidadKeyTyped(evt); } }); codigo.setEditable(false); codigo.setToolTipText(""); codigo.setBorder(javax.swing.BorderFactory.createTitledBorder("Código")); codigo.setFocusable(false); descripcion.setEditable(false); descripcion.setBorder(javax.swing.BorderFactory.createTitledBorder("Descripción")); descripcion.setFocusable(false); precio.setEditable(false); precio.setHorizontalAlignment(javax.swing.JTextField.RIGHT); precio.setBorder(javax.swing.BorderFactory.createTitledBorder("Precio")); precio.setFocusable(false); stock.setEditable(false); stock.setHorizontalAlignment(javax.swing.JTextField.RIGHT); stock.setBorder(javax.swing.BorderFactory.createTitledBorder("Stock")); stock.setFocusable(false); comprar.setText("Comprar"); comprar.setEnabled(false); comprar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { comprarActionPerformed(evt); } }); pagar.setText("Pagar"); pagar.setEnabled(false); pagar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pagarActionPerformed(evt); } }); total.setEditable(false); total.setHorizontalAlignment(javax.swing.JTextField.RIGHT); total.setBorder(javax.swing.BorderFactory.createTitledBorder("Total")); total.setFocusable(false); total.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { totalFocusGained(evt); } }); tItems.setBorder(javax.swing.BorderFactory.createTitledBorder("")); tItems.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null, null, null}, {null, null, null, null, null, null}, {null, null, null, null, null, null}, {null, null, null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6" } )); jScrollPane1.setViewportView(tItems); factura.setText("Factura nueva"); factura.setEnabled(false); factura.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { facturaActionPerformed(evt); } }); salir.setText("Salir"); salir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { salirActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(25, 25, 25) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 670, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(seleccionar, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(comprar)) .addGroup(layout.createSequentialGroup() .addComponent(codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(precio, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(stock, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pagar, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(factura) .addGap(65, 65, 65) .addComponent(salir, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap(44, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(21, 21, 21) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(seleccionar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(comprar)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(precio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(25, 25, 25) .addComponent(pagar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(factura) .addComponent(salir)) .addContainerGap(20, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void comprarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comprarActionPerformed if(nCompra == 0){ String datos[] = {"Factura" + this.nFactura}; md.addRow(datos); } String datos[] = {this.codigo.getText(),this.descripcion.getText(), this.precio.getText(), this.stock.getText(),this.cantidad.getText(),this.total.getText()}; md.addRow(datos); for(Producto p : productos){ if(p.getCodigo().equals(this.codigo.getText())){ p.setStock(p.getStock()-Integer.parseInt(this.cantidad.getText())); } } pago+= Double.parseDouble(this.total.getText()); this.codigo.setText(""); this.descripcion.setText(""); this.precio.setText(""); this.stock.setText(""); this.cantidad.setText(""); this.total.setText(""); this.pagar.setEnabled(true); nCompra++; this.comprar.setEnabled(false); // Pasa los datos de la compra a tItems, actualiza stock del producto // va calculando pago, limpia los datos. }//GEN-LAST:event_comprarActionPerformed private void seleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seleccionarActionPerformed String sel = (String)seleccionar.getSelectedItem(); sel = sel.substring(0,2); for(Producto p : productos){ if(p.getCodigo().equals(sel)){ this.codigo.setText(p.getCodigo()); this.descripcion.setText(p.getDescripcion()); this.precio.setText(p.getStringPrecio()); this.stock.setText(p.getStringStock()); this.p = p; cantidad.setEditable(true); cantidad.requestFocus(); } } }//GEN-LAST:event_seleccionarActionPerformed private void cantidadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadKeyTyped //hecho en el evento KeyPressed }//GEN-LAST:event_cantidadKeyTyped private void totalFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_totalFocusGained int cant = Integer.parseInt(this.cantidad.getText()); if(cant <= p.getStock()){ this.total.setText((p.getPrecio() * cant) + ""); this.comprar.setEnabled(true); }else{ this.cantidad.requestFocus(); } }//GEN-LAST:event_totalFocusGained private void cantidadFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cantidadFocusGained total.setFocusable(true); }//GEN-LAST:event_cantidadFocusGained private void pagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pagarActionPerformed String datos[] = {"","","","","","Total : " + pago}; md.addRow(datos); String datos1[] = {"","","","","","Ahorro 8% : " + pago *0.08}; md.addRow(datos1); String datos2[] = {"","","","","","Total descontado : " + pago*(1-0.08)}; md.addRow(datos2); String datos3[] = {"","","","","","IVA 18% : " + pago*(1-0.08) * 0.18}; md.addRow(datos3); String datos4[] = {"","","","","","Total factura : " + (pago*(1-0.08) + (pago*(1-0.08) * 0.18))}; md.addRow(datos4); this.factura.setEnabled(true); this.pagar.setEnabled(false); // cierra la factura en tItems }//GEN-LAST:event_pagarActionPerformed private void facturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_facturaActionPerformed md.setRowCount(0); nCompra = 0; nFactura++; this.factura.setEnabled(false); // limpia tItems: md.setRowCount(0) }//GEN-LAST:event_facturaActionPerformed private void salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirActionPerformed pW.saveProductos(); System.exit(0); // salva datos en productos2.txt }//GEN-LAST:event_salirActionPerformed private void cantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidadActionPerformed // TODO add your handling code here: }//GEN-LAST:event_cantidadActionPerformed private void cantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadKeyPressed if(evt.getKeyCode() == 10){ this.total.requestFocus(true); } }//GEN-LAST:event_cantidadKeyPressed /** * @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(ProductoVentaApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ProductoVentaApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ProductoVentaApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ProductoVentaApp.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 ProductoVentaApp().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField cantidad; private javax.swing.JTextField codigo; private javax.swing.JButton comprar; private javax.swing.JTextField descripcion; private javax.swing.JButton factura; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton pagar; private javax.swing.JTextField precio; private javax.swing.JButton salir; private javax.swing.JComboBox<String> seleccionar; private javax.swing.JTextField stock; private javax.swing.JTable tItems; private javax.swing.JTextField total; // End of variables declaration//GEN-END:variables }
package negocio; import java.text.NumberFormat; public class Producto{ private String codigo; private String descripcion; private double precio; private int stock; public Producto(){ codigo = ""; descripcion = ""; precio = 0.0; stock = 0; } public Producto(String codigo, String descripcion, double precio, int stock){ this.codigo = codigo; this.descripcion = descripcion; this.precio = precio; this.stock = stock; } public void setCodigo(String codigo){ this.codigo = codigo; } public String getCodigo(){ return codigo; } public void setDescripcion(String descripcion){ this.descripcion = descripcion; } public String getDescripcion(){ return descripcion; } public void setPrecio(double precio){ this.precio = precio; } public double getPrecio(){ return precio; } public String getStringPrecio(){ return Double.toString(precio); } public void setStock(int stock){ this.stock = stock; } public int getStock(){ return stock; } public String getStringStock(){ return Integer.toString(stock); } }
package dataBase; import negocio.*; public class DAOFactory { public static ProductoDAO getProductoDAO(){ // método ProductoDAO pDAO = new ProductoTextFile(); return pDAO; } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package negocio; public interface ProductoConstantes { int CODIGOSIZE = 4; int DESCRIPCIONSIZE = 40; }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package negocio; public interface ProductoDAO extends ProductoReader, ProductoWriter, ProductoConstantes{ }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package negocio; import java.util.ArrayList; public interface ProductoReader { Producto getProducto(String codigo); ArrayList<Producto> getProductos(); }
package dataBase; import java.util.*; import java.io.*; import java.nio.file.*; import negocio.*; public final class ProductoTextFile implements ProductoDAO{ private ArrayList<Producto> productos = null; private Path productosPath = null; private File productosFile = null; private final String FIELDSEP = "\t"; public ProductoTextFile() { productosPath = Paths.get("productos1.txt"); productosFile = productosPath.toFile(); productos = this.getProductos(); } @Override public ArrayList<Producto> getProductos(){ if (productos != null) return productos; productos = new ArrayList<>() ; if (Files.exists(productosPath)) { // previene FileNotFoundException try (BufferedReader in = new BufferedReader( new FileReader(productosFile))) { // leer todos los productos almacenados en el archivo al array list String line = in.readLine(); while(line != null){ String[] columns = line.split("\t"); String codigo = columns[0]; String descripcion = columns[1]; String precio = columns[2]; String stock = columns[3]; Producto p = new Producto(codigo, descripcion, Double.parseDouble(precio), Integer.parseInt(stock)); productos.add(p); line = in.readLine(); } } catch(IOException e){ System.out.println(e) ; return null; } } return productos; } @Override public Producto getProducto(String codigo) { for (Producto p : productos){ if (p.getCodigo().equals(codigo)){ return p; } } return null; } public boolean saveProductos(){ productosPath = Paths.get("productos2.txt"); productosFile = productosPath.toFile(); try (PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter(productosFile)))){ for (Producto p : productos){ // escribe products del array list al archivo out.print(p.getCodigo() + "\t"); out.print(p.getDescripcion() + "\t"); out.print(p.getPrecio() + "\t"); out.println(p.getStock()); } }catch(IOException e){ System.out.println(e); return false; } return true; } @Override public boolean addProducto(Producto p){ productos.add(p); return this.saveProductos(); } @Override public boolean deleteProducto(Producto p) { for(Producto q: productos) System.out.println(q.getCodigo() + " " + q.getDescripcion() + " " + q.getPrecio()); productos.remove(p); for(Producto q: productos) System.out.println(q.getCodigo() + " " + q.getDescripcion() + " " + q.getPrecio()); return this.saveProductos(); } @Override public boolean updateProducto(Producto newProducto){ // Toma el product viejo y lo remueve Producto oldProducto = this.getProducto(newProducto.getCodigo()); int i = productos.indexOf(oldProducto); productos.remove(i); productos.add(i, newProducto); // añade product actualizado return this.saveProductos(); } }
package negocio; public interface ProductoWriter { boolean addProducto(Producto p); boolean updateProducto(Producto p); boolean deleteProducto(Producto p); boolean saveProductos(); }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue