0% found this document useful (0 votes)
13 views

Contentpane Textfield Cbbmarca Txts Btnprocesar Txtcantidad Cantidad D

This Java code defines a Principal class that extends JFrame and implements ActionListener. It contains code to: 1. Create a GUI with labels, text fields, buttons to process a sale transaction 2. Define methods to calculate sale amounts like purchase price, discount, payment due 3. Register sale details like brand, payment amount and accumulate statistics 4. Print out results of the sale to a text area on the GUI

Uploaded by

ruben hinostroza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Contentpane Textfield Cbbmarca Txts Btnprocesar Txtcantidad Cantidad D

This Java code defines a Principal class that extends JFrame and implements ActionListener. It contains code to: 1. Create a GUI with labels, text fields, buttons to process a sale transaction 2. Define methods to calculate sale amounts like purchase price, discount, payment due 3. Register sale details like brand, payment amount and accumulate statistics 4. Print out results of the sale to a text area on the GUI

Uploaded by

ruben hinostroza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.awt.

BorderLayout;

public class Principal extends JFrame implements ActionListener {

private static final String ImpPagar = null;


private static final String Obs = null;
private JPanel contentPane;
private JTextField textField;
private JComboBox cbbMarca;
private JTextArea txtS;
private JButton btnProcesar;
private JLabel txtCantidad;
private int cantidad;
private String D;

//declaracion de variables globales


int ctvH, ctvK, ctvS;
private String ipaS;
private String ipaK;
private double ipaH;
double

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Principal frame = new Principal();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Principal() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblMarca = new JLabel("marca");


lblMarca.setBounds(10, 11, 46, 14);
contentPane.add(lblMarca);

cbbMarca = new JComboBox();


cbbMarca.setBounds(59, 8, 75, 20);
contentPane.add(cbbMarca);

JLabel lblCantidad = new JLabel("cantidad");


lblCantidad.setBounds(10, 36, 46, 14);
contentPane.add(lblCantidad);

textField = new JTextField();


textField.setBounds(59, 36, 86, 20);
contentPane.add(textField);
textField.setColumns(10);

btnProcesar = new JButton("Procesar");


btnProcesar.addActionListener(this);
btnProcesar.setBounds(335, 7, 89, 23);
contentPane.add(btnProcesar);

JScrollPane scrollPane = new JScrollPane();


scrollPane.setBounds(10, 76, 414, 157);
contentPane.add(scrollPane);

txtS = new JTextArea();


scrollPane.setViewportView(txtS);
}
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == btnProcesar) {
do_btnProcesar_actionPerformed(arg0);
}
}
protected void do_btnProcesar_actionPerformed(ActionEvent arg0) {
//Declaracion de variables locales
int marca, cantidad, obsequio;
double precio, descuento, ImpCompra, ImpPagar;

marca = getMarca();
cantidad = getCantidad();
ImpCompra = calcularImpCompra(cantidad, marca);
descuento = calcularDescuento(ImpCompra,cantidad);
ImpPagar = calcularImpPagar(ImpCompra, descuento);
obsequio = calcularObsequio(marca, cantidad);
registrarVentas(marca, ImpPagar);

imprimirResultado(ImpCompra, descuento, ImpPagar, obsequio);


}
private void imprimirResultado(double impCompra, double descuento,
double impPagar2, int obsequio) {
// TODO Auto-generated method stub

int getMarca(){return cbbMarca.getSelectedIndex();}


int getCantidad(){ return Integer.parseInt(txtCantidad.getText()); }

double calcularImpCompra(int cantidad, int marca){


switch(marca){
case 0: return 150 * cantidad;
case 1: return 140 * cantidad;
default : return 120.50 * cantidad;
}
}
double calcularDescuento(double IC, int cant){
if(cant <=5) return 0.035 * IC;
else if(cant>=6 && cant<=10) return 0.05 * IC;
else return 0.12 * IC;

}
double calcularImpPagar(double IC, double D){
return IC-D;
}

int calcularObsequio(double impCompra,double descuento){


if(impCompra ==1){return (cantidad / 3) * 2;}
else return 0;
}
void ImprimirResultado(double IC, double IP, int obs){
txtS.setText("Venta actual: \n");
imprimir("Importe de compra : " + IC);
imprimir("Descuento: " + D);
imprimir("Importe a pagar: " + IP);
imprimir("Obsequio: " + Obs + " mouse");

imprimir("=== estadisticas de ventas === ");


imprimir("1.cantidad total de ventas por marca");
imprimir("kingston :" + ctvK);
imprimir("HP : " + ctvH);
imprimir("Sony :" + ctvS);

imprimir("2.importe pagado acumulado");


imprimir("kingston : " + ipaK);
imprimir("HP : " + ipaH);
imprimir("Sony : " + ipaS);
}
void imprimir(String m){txtS.append(m + "\n");}

void registrarVentas(int marca, double IP){

switch(marca){
case 0:
//Kingston
//1.resgistrar una venta para kingston
ctvK++;
//2.acumulare el importe a pagar de la venta actual
ipaK+=IP;
break;
case 1:
//HP
ctvH++;
ipaH+=IP;
break;
case 2:
//Sony
ctvS++;
ipaS+=IP;
break;

}
}

You might also like