You are on page 1of 10

1

Introducción

El presente documento muestra la elaboración de un programa el cual fue elaborado en el


lenguaje de programación orientado a objetos (POO) JAVA, el cual se encarga de sugerir dado el
monto de un producto y el pago de la persona cual podría ser el cambio(vuelto) que se debería
de entregar al comprador.

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

El programa se encuentra elaborado en el lenguaje de programación orientado a objetos (POO) JAVA. El


propósito del programa es poder sugerir la forma de entregar el cambio (vuelto) a una persona,
partiendo de 2 entradas que se encuentran en la interfaz del programa las cuales solicitan al usuario el
valor del producto que se está comprando y la cantidad de dinero con la que se está pagando,
posteriormente el programa realiza los cálculos adecuados para poder sugerir la manera en la que se
debe entregar el vuelto.

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

Funcionamiento del programa:

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.

A continuacion se muestra la pantalla despues de haber sido rellenada correctamente y de haber el


programa realizado los calculus correspondientes:

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

A continuacion se muestra una captura con el mensaje:

Código del Proyecto.

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);
}
});

javax.swing.GroupLayout jPanel1Layout = new


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON javax.swing.GroupLayout(jPanel1);
_CLOSE); jPanel1.setLayout(jPanel1Layout);
setTitle("Calculo del cambio en una compra"); jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayo
Precio.setNextFocusableComponent(Pago); ut.Alignment.LEADING)
Precio.addActionListener(new java.awt.event.ActionListener() .addGap(0, 191, Short.MAX_VALUE)
{
public void actionPerformed(java.awt.event.ActionEvent .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupL
evt) { ayout.Alignment.LEADING)
PrecioActionPerformed(evt); .addGroup(jPanel1Layout.createSequentialGroup()
} .addGap(0, 0, 0)
});

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupL


jLabel1.setText("Precio"); ayout.Alignment.LEADING)
.addComponent(Sugerencia7)
.addComponent(Sugerencia1) .addComponent(Sugerenciam3)
.addComponent(Sugerencia2) .addComponent(Sugerenciam4)
.addComponent(Sugerencia3) .addComponent(Sugerenciam5)
.addComponent(Sugerencia4) .addGap(0, 0, 0)))
.addComponent(Sugerencia5) );
.addComponent(Sugerencia6))
.addGap(0, 0, 0))) javax.swing.GroupLayout layout = new
); javax.swing.GroupLayout(getContentPane());
jPanel1Layout.setVerticalGroup( getContentPane().setLayout(layout);
layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Align
ment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
.addGap(0, 100, Short.MAX_VALUE) EADING)
.addGroup(layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupL .addGap(25, 25, 25)
ayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
.addGap(0, 0, 0) lignment.LEADING)
.addComponent(Sugerencia1) .addComponent(jLabel3)
.addComponent(Sugerencia2) .addGroup(layout.createSequentialGroup()
.addComponent(Sugerencia3)
.addComponent(Sugerencia4) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
.addComponent(Sugerencia5) lignment.TRAILING, false)
.addComponent(Sugerencia6)
.addComponent(Sugerencia7)
.addGap(0, 0, 0))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
); layout.createSequentialGroup()

javax.swing.GroupLayout jPanel2Layout = new .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A


javax.swing.GroupLayout(jPanel2); lignment.LEADING)
jPanel2.setLayout(jPanel2Layout); .addComponent(jLabel1,
jPanel2Layout.setHorizontalGroup( javax.swing.GroupLayout.PREFERRED_SIZE, 78,
javax.swing.GroupLayout.PREFERRED_SIZE)
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Align .addComponent(jLabel2,
ment.LEADING) javax.swing.GroupLayout.PREFERRED_SIZE, 49,
.addGap(0, 197, Short.MAX_VALUE) javax.swing.GroupLayout.PREFERRED_SIZE))

.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);

}
});
}

// Variables declaration - do not modify


private javax.swing.JTextField Pago;
private javax.swing.JTextField Precio;
private javax.swing.JTextField Resultado;
private javax.swing.JLabel Sugerencia1;
private javax.swing.JLabel Sugerencia2;
private javax.swing.JLabel Sugerencia3;
private javax.swing.JLabel Sugerencia4;
private javax.swing.JLabel Sugerencia5;
private javax.swing.JLabel Sugerencia6;
private javax.swing.JLabel Sugerencia7;
private javax.swing.JLabel Sugerenciam1;
private javax.swing.JLabel Sugerenciam2;
private javax.swing.JLabel Sugerenciam3;
private javax.swing.JLabel Sugerenciam4;
private javax.swing.JLabel Sugerenciam5;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration

You might also like