Advertisement
Guest User

Untitled

a guest
May 26th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. package semana09;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.JLabel;
  9. import javax.swing.JButton;
  10.  
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13.  
  14. import javax.swing.JTextField;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextArea;
  17. import javax.swing.UIManager;
  18. import javax.swing.JComboBox;
  19. import javax.swing.DefaultComboBoxModel;
  20.  
  21. import java.awt.Font;
  22.  
  23. public class Heladeria extends JFrame implements ActionListener {
  24.  
  25. // Declaración de variables
  26. private static final long serialVersionUID = 9206324162700448001L;
  27. private JPanel contentPane;
  28. private JLabel lblHelado;
  29. private JLabel lblCantidad;
  30. private JComboBox<String> cboHelado;
  31. private JTextField txtCantidad;
  32. private JButton btnProcesar;
  33. private JButton btnBorrar;
  34. private JScrollPane scpScroll;
  35. private JTextArea txtS;
  36.  
  37. // Lanza la aplicación
  38. public static void main(String[] args) {
  39. try {
  40. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  41. }
  42. catch (Throwable e) {
  43. e.printStackTrace();
  44. }
  45. EventQueue.invokeLater(new Runnable() {
  46. public void run() {
  47. try {
  48. Heladeria frame = new Heladeria();
  49. frame.setVisible(true);
  50. }
  51. catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. });
  56. }
  57.  
  58. // Crea la GUI
  59. public Heladeria() {
  60. setTitle("Heladeria");
  61. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. setBounds(100, 100, 450, 300);
  63.  
  64. contentPane = new JPanel();
  65. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  66. setContentPane(contentPane);
  67. contentPane.setLayout(null);
  68.  
  69. lblHelado = new JLabel("Helado");
  70. lblHelado.setBounds(10, 11, 80, 14);
  71. contentPane.add(lblHelado);
  72.  
  73. lblCantidad = new JLabel("Cantidad");
  74. lblCantidad.setBounds(10, 36, 80, 14);
  75. contentPane.add(lblCantidad);
  76.  
  77. cboHelado = new JComboBox<String>();
  78. cboHelado.setModel(new DefaultComboBoxModel<String>(new String[] {"Sol", "Fresa", "Mar", "Rico"}));
  79. cboHelado.setBounds(100, 8, 100, 20);
  80. contentPane.add(cboHelado);
  81.  
  82. txtCantidad = new JTextField();
  83. txtCantidad.setBounds(100, 33, 100, 20);
  84. contentPane.add(txtCantidad);
  85. txtCantidad.setColumns(10);
  86.  
  87. btnProcesar = new JButton("Procesar");
  88. btnProcesar.addActionListener(this);
  89. btnProcesar.setBounds(335, 7, 89, 23);
  90. contentPane.add(btnProcesar);
  91.  
  92. btnBorrar = new JButton("Borrar");
  93. btnBorrar.addActionListener(this);
  94. btnBorrar.setBounds(335, 32, 89, 23);
  95. contentPane.add(btnBorrar);
  96.  
  97. scpScroll = new JScrollPane();
  98. scpScroll.setBounds(10, 61, 414, 190);
  99. contentPane.add(scpScroll);
  100.  
  101. txtS = new JTextArea();
  102. txtS.setFont(new Font("Monospaced", Font.PLAIN, 12));
  103. scpScroll.setViewportView(txtS);
  104. }
  105.  
  106. // Direcciona eventos de tipo ActionEvent
  107. public void actionPerformed(ActionEvent arg0) {
  108. if (arg0.getSource() == btnBorrar) {
  109. actionPerformedBtnBorrar(arg0);
  110. }
  111. if (arg0.getSource() == btnProcesar) {
  112. actionPerformedBtnProcesar(arg0);
  113. }
  114. }
  115.  
  116. // Procesa la pulsación del botón Borrar
  117. protected void actionPerformedBtnBorrar(ActionEvent arg0) {
  118. txtCantidad.setText("");
  119. txtS.setText("");
  120. cboHelado.setSelectedIndex(0);
  121. txtCantidad.requestFocus();
  122. }
  123.  
  124. // Declaracion de Variables
  125. int indiceHelado, cantidad, caramelos;
  126. double precio, importeCompra, descuento, aPagar;
  127.  
  128. // Procesa la pulsación del botón Procesar
  129. protected void actionPerformedBtnProcesar(ActionEvent arg0) {
  130. entradaDeDatos();
  131. procesoDeCalculo();
  132. salidaDeResultado();
  133. }
  134. void imprimir (String cad) {
  135. txtS.append(cad + "\n");
  136. }
  137. void salidaDeResultado () {
  138. imprimir ("BOLETA DE VENTA");
  139. imprimir ("");
  140. imprimir ("Precion unitario : S/. " + precio);
  141. imprimir ("Importe Compra : S/. " + importeCompra);
  142. imprimir ("Desceunto : S/. " + descuento);
  143. imprimir ("Importe a Pagar : S/. " + aPagar);
  144. imprimir ("Obsequio : " +caramelos+ "caramelos");
  145. }
  146.  
  147. void entradaDeDatos() {
  148. indiceHelado = cboHelado.getSelectedIndex();
  149. cantidad = Integer.parseInt(txtCantidad.getText());
  150. }
  151. void procesoDeCalculo() {
  152. switch (indiceHelado) {
  153. case 0 :
  154. precio = 2.5;
  155. break;
  156.  
  157. case 1 :
  158. precio = 1.3;
  159. break;
  160.  
  161. case 2 :
  162. precio = 2.0;
  163. break;
  164.  
  165. default:
  166. precio = 1.7;
  167. }
  168. importeCompra = precio*cantidad;
  169.  
  170. if (importeCompra < 100)
  171. descuento = 0.05 * importeCompra;
  172.  
  173. else
  174. if (importeCompra >= 100 && importeCompra < 200)
  175. descuento = 0.07 * importeCompra;
  176. else
  177. if (importeCompra >= 200 && importeCompra < 300)
  178. descuento = 0.09 * importeCompra;
  179.  
  180. aPagar = importeCompra - descuento;
  181. if (indiceHelado == 0)
  182. caramelos = 2*(cantidad / 12);
  183. else
  184. caramelos = 0;
  185. }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement