Sei sulla pagina 1di 4

Realice un programa en Eclipse utilizando lenguaje Java para lo siguiente: Realice un formulario para la compra de mens.

Existen 3 mens, cada uno con diferentes valores; men 1 tiene un valor de $1.000, men 2 tiene un valor de $2.000 y men 2 tiene un valor de $3.000. Asigne estos valores a variables. El cliente puede cancelar con tarjeta o en efectivo, si cancela en efectivo tiene un descuento de un 10%. El funcionamiento es el siguiente; se debe seleccionar uno o varios mens, en el JTextArea aparecer el subtotal mientras se van seleccionando los mens. Si cancela en efectivo deber digitar en el JTextField "Paga Con" con cunto dinero cancelar. Luego click en el JCheckBox "Efectivo" y deber aparecer el valor final a cancelar y de cunto fue el vuelto. Si cancela con tarjeta no posee descuento. Los controles a utilizar son 2 JCheckBox, y de izquierda a derecha 1 JList, 1 JTextArea, 3 JLabel con sus respectivos JTextField Trabaje con los ndices cada vez que corresponda. Para el mtodo ValueChanged deber utilizar lo siguiente en la primera lnea del cdigo:

import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener;

public class P1 extends JFrame implements ActionListener, ListSelectionListener{ private JCheckBox chEfectivo; private JCheckBox chTarjeta; private JList lista; private JLabel et1; private JLabel et2;

private JLabel et3; private JTextField text; private JTextField text2; private JTextField text3; private JTextArea area; private String var=""; private int total = 0, descuento=0, pagaCon=0, vuelto=0; private int pmenu1=1000; private int pmenu2=2000; private int pmenu3=3000; public P1(){ setSize(800, 400); initComponents(); } private void initComponents() { Container c = getContentPane(); getContentPane().setLayout(null); chEfectivo= new JCheckBox("EFECTIVO"); getContentPane().add(chEfectivo); chEfectivo.setBounds(30, 30, 100, 20); chTarjeta= new JCheckBox("TARJETA"); getContentPane().add(chTarjeta); chTarjeta.setBounds(150, 30, 100, 20); String[] datos = {"menu 1", "menu 2", "menu 3"}; lista = new JList(datos); c.add(lista); lista.setBounds(30, 100, 100, 60); area = new JTextArea(); getContentPane().add(area); area.setBounds(150, 100, 200, 60); et1= new JLabel("$"); getContentPane().add(et1); et1.setBounds(380, 100, 20, 20); text = new JTextField(); getContentPane().add(text); text.setBounds(400, 100, 80, 20); et2=new JLabel("Paga con $"); getContentPane().add(et2); et2.setBounds(500, 100, 100, 20);

text2 = new JTextField(); getContentPane().add(text2); text2.setBounds(580, 100, 100, 20); et3=new JLabel("Vuelto $"); getContentPane().add(et3); et3.setBounds(500, 200, 100, 20); text3 = new JTextField(); getContentPane().add(text3); text3.setBounds(580, 200, 100, 20); chEfectivo.addActionListener(this); chTarjeta.addActionListener(this); lista.addListSelectionListener(this); } public static void main(String[] args) { new P1().setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==chTarjeta){ text.setText(Integer.toString(total)); } else if(e.getSource()==chEfectivo){ descuento=total*10/100; total=total-descuento; pagaCon=Integer.parseInt(text2.getText()); vuelto = pagaCon - total; text.setText(Integer.toString(total)); text3.setText(Integer.toString(vuelto));

} }

@Override public void valueChanged(ListSelectionEvent e) { if(e.getValueIsAdjusting())

if(e.getSource()==lista){ if(lista.getSelectedIndex()==0) { total = total + pmenu1; area.setText(Integer.toString(total)); } else if(lista.getSelectedIndex()==1){ total = total + pmenu2; area.setText(Integer.toString(total));} else if(lista.getSelectedIndex()==2){ total = total + pmenu3; area.setText(Integer.toString(total));} } } }

Potrebbero piacerti anche