Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package semana09;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JTextField;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.UIManager;
- import javax.swing.JComboBox;
- import javax.swing.DefaultComboBoxModel;
- import java.awt.Font;
- public class Heladeria extends JFrame implements ActionListener {
- // Declaración de variables
- private static final long serialVersionUID = 9206324162700448001L;
- private JPanel contentPane;
- private JLabel lblHelado;
- private JLabel lblCantidad;
- private JComboBox<String> cboHelado;
- private JTextField txtCantidad;
- private JButton btnProcesar;
- private JButton btnBorrar;
- private JScrollPane scpScroll;
- private JTextArea txtS;
- // Lanza la aplicación
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch (Throwable e) {
- e.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Heladeria frame = new Heladeria();
- frame.setVisible(true);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- // Crea la GUI
- public Heladeria() {
- setTitle("Heladeria");
- 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);
- lblHelado = new JLabel("Helado");
- lblHelado.setBounds(10, 11, 80, 14);
- contentPane.add(lblHelado);
- lblCantidad = new JLabel("Cantidad");
- lblCantidad.setBounds(10, 36, 80, 14);
- contentPane.add(lblCantidad);
- cboHelado = new JComboBox<String>();
- cboHelado.setModel(new DefaultComboBoxModel<String>(new String[] {"Sol", "Fresa", "Mar", "Rico"}));
- cboHelado.setBounds(100, 8, 100, 20);
- contentPane.add(cboHelado);
- txtCantidad = new JTextField();
- txtCantidad.setBounds(100, 33, 100, 20);
- contentPane.add(txtCantidad);
- txtCantidad.setColumns(10);
- btnProcesar = new JButton("Procesar");
- btnProcesar.addActionListener(this);
- btnProcesar.setBounds(335, 7, 89, 23);
- contentPane.add(btnProcesar);
- btnBorrar = new JButton("Borrar");
- btnBorrar.addActionListener(this);
- btnBorrar.setBounds(335, 32, 89, 23);
- contentPane.add(btnBorrar);
- scpScroll = new JScrollPane();
- scpScroll.setBounds(10, 61, 414, 190);
- contentPane.add(scpScroll);
- txtS = new JTextArea();
- txtS.setFont(new Font("Monospaced", Font.PLAIN, 12));
- scpScroll.setViewportView(txtS);
- }
- // Direcciona eventos de tipo ActionEvent
- public void actionPerformed(ActionEvent arg0) {
- if (arg0.getSource() == btnBorrar) {
- actionPerformedBtnBorrar(arg0);
- }
- if (arg0.getSource() == btnProcesar) {
- actionPerformedBtnProcesar(arg0);
- }
- }
- // Procesa la pulsación del botón Borrar
- protected void actionPerformedBtnBorrar(ActionEvent arg0) {
- txtCantidad.setText("");
- txtS.setText("");
- cboHelado.setSelectedIndex(0);
- txtCantidad.requestFocus();
- }
- // Declaracion de Variables
- int indiceHelado, cantidad, caramelos;
- double precio, importeCompra, descuento, aPagar;
- // Procesa la pulsación del botón Procesar
- protected void actionPerformedBtnProcesar(ActionEvent arg0) {
- entradaDeDatos();
- procesoDeCalculo();
- salidaDeResultado();
- }
- void imprimir (String cad) {
- txtS.append(cad + "\n");
- }
- void salidaDeResultado () {
- imprimir ("BOLETA DE VENTA");
- imprimir ("");
- imprimir ("Precion unitario : S/. " + precio);
- imprimir ("Importe Compra : S/. " + importeCompra);
- imprimir ("Desceunto : S/. " + descuento);
- imprimir ("Importe a Pagar : S/. " + aPagar);
- imprimir ("Obsequio : " +caramelos+ "caramelos");
- }
- void entradaDeDatos() {
- indiceHelado = cboHelado.getSelectedIndex();
- cantidad = Integer.parseInt(txtCantidad.getText());
- }
- void procesoDeCalculo() {
- switch (indiceHelado) {
- case 0 :
- precio = 2.5;
- break;
- case 1 :
- precio = 1.3;
- break;
- case 2 :
- precio = 2.0;
- break;
- default:
- precio = 1.7;
- }
- importeCompra = precio*cantidad;
- if (importeCompra < 100)
- descuento = 0.05 * importeCompra;
- else
- if (importeCompra >= 100 && importeCompra < 200)
- descuento = 0.07 * importeCompra;
- else
- if (importeCompra >= 200 && importeCompra < 300)
- descuento = 0.09 * importeCompra;
- aPagar = importeCompra - descuento;
- if (indiceHelado == 0)
- caramelos = 2*(cantidad / 12);
- else
- caramelos = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement