Professional Documents
Culture Documents
Introducción
El programa hace uso de una interfaz clara y amigable elaborada la presentación para un
applet. El programa fue creado en el Entorno de desarrollo integrado (IDE) Netbeans el cual
hace que la elaboración de aplicaciones sea mucho más fácil y permite que el programador no
gaste tiempo en el desarrollo físico de las interfaces o en la parte gráfica del programa y se
pueda concentrar en la programación pura y en la resolución del problema principal para el que
se ha elaborado el programa.
2
Descripción
En la imagen que se muestra a continuación se ilustra la forma que tiene la interfaz gráfica del programa
en la cual podemos observar los campos en los que se debe introducir el valor y el precio del producto.
Así mismo se puede observar que la interface del programa se encuentran 3 botones los cuales se indica
a continuación cual es la función:
Calcular:
La function de este botón es la de poder realizar los cálculos a la hora de que hayan sido ingresados los
datos solicitados en el programa.
3
Si al momento de presionar este botón no se han ingresado datos en los campos precio y pago el
programa mostrara un mensaje de error para que ingresen los datos.
Pantalla de muestra que ejemplifica como se muestran los errores por dejar los campos vacios.
Borrar - Nuevo:
El texto incial de este boton es borrar. Este botón servirá para poder limpiar la pantalla de cualquier
tipo de información que hay sido ingresada hasta el momento.
Al momento de que se ejecute el calculo de los datos el nombre del botón cambia ha “Nuevo” lo cual
indica que al precionarlo se iniciara un nuevo calculo.
Salir:
Este botón servirá para terminar la ejecución del programa y poder salir de la ejecución del mismo.
4
Despues de rellenar los campos con la informacion solicitada el programa mostrara en pantalla las
sugerencias de billetes y de monedas para poder entregar el cambio al comprador del producto.
Si el pago realizado es inferior al precio del producto, se mostrara un mensaje indicando cuanto le hace
falta a la persona para poder cancelar totalmente el producto.
5
import javax.swing.JOptionPane; }
//Inicializo cantidad de monedas
/** for(int i=0;i<5;i++)
* {
* @author Junior monedas[1][i]=0;
*/ }
public class Cambio extends javax.swing.JFrame {
}
//Metodo para redondear la cantidades double con 2 decimales
public static double redondear(double num)
{ //Declaracion de variables
return Math.round(num *Math.pow(10,2))/Math.pow(10,2); double pago,precio,vuelto,cambio = redondear(0);
} String moneda = " Quetzales";
String centavos = " Centavos";
//Metodo para limpiar los objetos // Declaración del array de Billetes e inicialización del mismo.
private void limpieza() int billetes[][] = new int[2][7];
{ int monedas[][] = new int[2][5];
Precio.setText("");
Pago.setText("");
Resultado.setText("0.00" + " "); /** Creates new form Cambio */
Precio.requestFocus(); public Cambio() {
initComponents();
Sugerencia1.setText("");
Sugerencia2.setText(""); billetes[0][0] = 200;
Sugerencia3.setText(""); billetes[0][1] = 100;
Sugerencia4.setText(""); billetes[0][2] = 50;
Sugerencia5.setText(""); billetes[0][3] = 20;
Sugerencia6.setText(""); billetes[0][4] = 10;
Sugerencia7.setText(""); billetes[0][5] = 5;
Sugerenciam1.setText(""); billetes[0][6] = 1;
Sugerenciam2.setText("");
Sugerenciam3.setText(""); monedas[0][0] = 50;
Sugerenciam4.setText(""); monedas[0][1] = 25;
Sugerenciam5.setText(""); monedas[0][2] = 10;
monedas[0][3] = 5;
//Inicializo cantidad de billetes monedas[0][4] = 1;
for(int i=0;i<7;i++)
{ //Inicializo cantidad de billetes
billetes[1][i]=0; for(int i=0;i<7;i++)
{ jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
billetes[1][i]=0; jLabel2.setText("Pago");
}
//Inicializo cantidad de monedas Pago.setNextFocusableComponent(jButton1);
for(int i=0;i<5;i++)
{ jButton1.setText("Calcular");
monedas[1][i]=0; jButton1.setNextFocusableComponent(jButton3);
} jButton1.addActionListener(new
Resultado.setText("0.00" + " "); java.awt.event.ActionListener() {
} public void actionPerformed(java.awt.event.ActionEvent
evt) {
/** This method is called from within the constructor to jButton1ActionPerformed(evt);
* initialize the form. }
* WARNING: Do NOT modify this code. The content of this });
method is
* always regenerated by the Form Editor. Resultado.setBackground(new java.awt.Color(0, 0, 0));
*/ Resultado.setEditable(false);
@SuppressWarnings("unchecked") Resultado.setFont(new java.awt.Font("Impact", 1, 24));
// <editor-fold defaultstate="collapsed" desc="Generated Resultado.setForeground(new java.awt.Color(0, 204, 0));
Code">
private void initComponents() { Resultado.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
Resultado.setBorder(null);
Precio = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel(); jButton2.setText("Salir");
jLabel2 = new javax.swing.JLabel(); jButton2.setNextFocusableComponent(Precio);
Pago = new javax.swing.JTextField(); jButton2.addActionListener(new
jButton1 = new javax.swing.JButton(); java.awt.event.ActionListener() {
Resultado = new javax.swing.JTextField(); public void actionPerformed(java.awt.event.ActionEvent
jButton2 = new javax.swing.JButton(); evt) {
jLabel3 = new javax.swing.JLabel(); jButton2ActionPerformed(evt);
jLabel4 = new javax.swing.JLabel(); }
jButton3 = new javax.swing.JButton(); });
jPanel1 = new javax.swing.JPanel();
Sugerencia1 = new javax.swing.JLabel(); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
Sugerencia2 = new javax.swing.JLabel(); jLabel3.setText("Sugerencia de cambio:");
Sugerencia3 = new javax.swing.JLabel();
Sugerencia4 = new javax.swing.JLabel(); jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
Sugerencia5 = new javax.swing.JLabel(); jLabel4.setText("Cambio:");
Sugerencia6 = new javax.swing.JLabel();
Sugerencia7 = new javax.swing.JLabel(); jButton3.setText("Borrar");
jPanel2 = new javax.swing.JPanel(); jButton3.setNextFocusableComponent(jButton2);
Sugerenciam1 = new javax.swing.JLabel(); jButton3.addActionListener(new
Sugerenciam2 = new javax.swing.JLabel(); java.awt.event.ActionListener() {
Sugerenciam3 = new javax.swing.JLabel(); public void actionPerformed(java.awt.event.ActionEvent
Sugerenciam4 = new javax.swing.JLabel(); evt) {
Sugerenciam5 = new javax.swing.JLabel(); jButton3ActionPerformed(evt);
}
});
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupL .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.
ayout.Alignment.LEADING) RELATED)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(0, 0, 0) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
lignment.LEADING, false)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupL .addComponent(Pago)
ayout.Alignment.LEADING) .addComponent(Precio,
.addComponent(Sugerenciam5) javax.swing.GroupLayout.PREFERRED_SIZE, 97,
.addComponent(Sugerenciam3) javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(Sugerenciam4) .addComponent(jLabel4,
.addComponent(Sugerenciam2) javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Sugerenciam1)) .addComponent(Resultado,
.addGap(0, 0, 0))) javax.swing.GroupLayout.Alignment.LEADING,
); javax.swing.GroupLayout.DEFAULT_SIZE, 239,
jPanel2Layout.setVerticalGroup( Short.MAX_VALUE))
.addGap(59, 59, 59)
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Align
ment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
.addGap(0, 100, Short.MAX_VALUE) lignment.LEADING, false)
.addComponent(jButton3,
javax.swing.GroupLayout.DEFAULT_SIZE,
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupL javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
ayout.Alignment.LEADING) .addComponent(jButton2,
.addGroup(jPanel2Layout.createSequentialGroup() javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
.addGap(0, 0, 0) .addComponent(jButton1,
.addComponent(Sugerenciam1) javax.swing.GroupLayout.PREFERRED_SIZE, 82,
.addComponent(Sugerenciam2) javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup() .addComponent(jButton3)
.addComponent(jPanel1, .addGap(7, 7, 7)
javax.swing.GroupLayout.PREFERRED_SIZE, .addComponent(jButton2)))
javax.swing.GroupLayout.DEFAULT_SIZE, .addGap(11, 11, 11)
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement. lignment.LEADING)
RELATED) .addComponent(jPanel1,
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jPanel2,
.addContainerGap(13, Short.MAX_VALUE)) javax.swing.GroupLayout.PREFERRED_SIZE,
); javax.swing.GroupLayout.DEFAULT_SIZE,
layout.setVerticalGroup( javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(14, Short.MAX_VALUE))
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L );
EADING)
.addGroup(layout.createSequentialGroup() java.awt.Dimension screenSize =
.addContainerGap() java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-448)/2, (screenSize.height-
354)/2, 448, 354);
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A }// </editor-fold>
lignment.LEADING)
.addGroup(layout.createSequentialGroup() private void PrecioActionPerformed(java.awt.event.ActionEvent
.addGap(3, 3, 3) evt) {
// TODO add your handling code here:
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A }
lignment.BASELINE)
.addComponent(jLabel1) private void
.addComponent(Precio, jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) String precioS = Precio.getText();
try {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement. precio = redondear(Double.parseDouble(precioS));
RELATED)
} catch (NumberFormatException excep)
{
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
limpieza();
lignment.LEADING)
JOptionPane.showMessageDialog(null,"Ingrese Valores
.addComponent(Pago,
Numéricos ", "Error",JOptionPane.ERROR_MESSAGE);
javax.swing.GroupLayout.PREFERRED_SIZE,
}
javax.swing.GroupLayout.DEFAULT_SIZE,
String pagoS = Pago.getText();
javax.swing.GroupLayout.PREFERRED_SIZE)
try {
.addComponent(jLabel2))
pago = redondear(Double.parseDouble(pagoS));
.addGap(24, 24, 24)
.addComponent(jLabel4)
} catch (NumberFormatException excep)
{
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement. limpieza();
RELATED) JOptionPane.showMessageDialog(null,"Ingrese Valores
.addComponent(Resultado, Numéricos", "Error",JOptionPane.ERROR_MESSAGE);
javax.swing.GroupLayout.PREFERRED_SIZE, 55, }
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18) vuelto = pago - precio;
.addComponent(jLabel3, cambio = redondear(vuelto);
javax.swing.GroupLayout.PREFERRED_SIZE, 14, if (precio > pago)
javax.swing.GroupLayout.PREFERRED_SIZE)) {
.addGroup(layout.createSequentialGroup() JOptionPane.showMessageDialog(null,"Le hace
.addComponent(jButton1, falta"+vuelto+" para poder cancelar el producto",
javax.swing.GroupLayout.PREFERRED_SIZE, 49, "Error",JOptionPane.ERROR_MESSAGE);
javax.swing.GroupLayout.PREFERRED_SIZE) }
String resultado = Double.toString(redondear(vuelto));
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement. Resultado.setText(resultado + " ");
RELATED)
//Calculos para extraer billetes y monedas en variables Sugerencia4.setText(Integer.toString((int)billetes[1][3]) +
separadas " Billete(s) de " + billetes[0][3] + moneda);
int vuelto_billetes = (int) vuelto; a = a + 10;
double monedastemp =redondear(vuelto - vuelto_billetes) * }
100; if (billetes[1][4] > 0)
int vuelto_monedas = (int) monedastemp; {
Sugerencia5.setBounds(27,a,100,100);
while( vuelto_billetes > 0) Sugerencia5.setText(Integer.toString((int)billetes[1][4]) +
{ " Billete(s) de " + billetes[0][4] + moneda);
int j = 0; a = a + 10;
while (j < 7) }
{ if (billetes[1][5] > 0)
if (vuelto_billetes >= billetes[0][j] ) {
{ Sugerencia6.setBounds(27,a,100,100);
vuelto_billetes = vuelto_billetes - billetes[0][j]; Sugerencia6.setText(Integer.toString((int)billetes[1][5]) +
billetes[1][j] = billetes[1][j] + 1; " Billete(s) de " + billetes[0][5] + moneda);
} a = a + 10;
if (billetes[0][j] > vuelto_billetes) }
{ if (billetes[1][6] > 0)
j++; {
} Sugerencia7.setBounds(27,a,100,100);
} Sugerencia7.setText(Integer.toString((int)billetes[1][6]) +
} " Billete(s) de " + billetes[0][6] + moneda);
while (vuelto_monedas > 0) a = a + 10;
{ }
int a = 0;
while(a < 5) int h = 160;
{ if (monedas[1][0] > 0)
if (vuelto_monedas >= monedas[0][a]) {
{ Sugerenciam1.setBounds(27,h,100,100);
vuelto_monedas = vuelto_monedas - monedas[0][a]; Sugerenciam1.setText(Integer.toString((int)monedas[1]
monedas[1][a] = monedas[1][a] + 1; [0]) + " Moneda(s) de " + monedas[0][0] + centavos);
} h = h + 10;
if (monedas[0][a] > vuelto_monedas) }
{ if (monedas[1][1] > 0)
a++; {
} Sugerenciam2.setBounds(27,h,100,100);
} Sugerenciam2.setText(Integer.toString((int)monedas[1]
} [1]) + " Moneda(s) de " + monedas[0][1] + centavos);
h = h + 10;
//Muestro las sugerencias del cambio en pantalla }
int a = 160; if (monedas[1][2] > 0)
if (billetes[1][0] > 0) {
{ Sugerenciam3.setBounds(27,h,100,100);
Sugerencia1.setBounds(27,a,100,100); Sugerenciam3.setText(Integer.toString((int)monedas[1]
Sugerencia1.setText(Integer.toString((int)billetes[1][0]) + [2]) + " Moneda(s) de " + monedas[0][2] + centavos);
" Billete(s) de " + billetes[0][0] + moneda); h = h + 10;
a = a + 10; }
} if (monedas[1][3] > 0)
if (billetes[1][1] > 0) {
{ Sugerenciam4.setBounds(27,h,100,100);
Sugerencia2.setBounds(27,a,100,100); Sugerenciam4.setText(Integer.toString((int)monedas[1]
Sugerencia2.setText(Integer.toString((int)billetes[1][1]) + [3]) + " Moneda(s) de " + monedas[0][3] + centavos);
" Billete(s) de " + billetes[0][1] + moneda); h = h + 10;
a = a + 10; }
} if (monedas[1][4] > 0)
if (billetes[1][2] > 0) {
{ Sugerenciam5.setBounds(27,h,100,100);
Sugerencia3.setBounds(27,a,100,100); Sugerenciam5.setText(Integer.toString((int)monedas[1]
Sugerencia3.setText(Integer.toString((int)billetes[1][2]) + [4]) + " Moneda(s) de " + monedas[0][4] + centavos);
" Billete(s) de " + billetes[0][2] + moneda); h = h + 10;
a = a + 10; }
}
if (billetes[1][3] > 0) Precio.disable();
{ Pago.disable();
Sugerencia4.setBounds(27,a,100,100); jButton3.setText("Nuevo");
}
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//Salir de la aplicación
System.exit(0);
}
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
limpieza();
Precio.enable();
Pago.enable();
jButton3.setText("Borrar");
Resultado.setText("0.00" + " ");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Cambio().setVisible(true);
}
});
}