jueves, 23 de febrero de 2017

EJERCICIO 1
package Semana4examen;

import Semana4examen.ArregloSueldos;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 * @author luz
 */
public  class Form_Sueldo extends JFrame implements ActionListener {

    JLabel lbl_taman;
    JLabel lbl_frase;
    JTextField txt_tamanio;
    JTextField txt_resul;
    JButton btn_agregar;
    JButton btn_mostrar;
    JButton btn_mosmayores;
    JButton btn_resultado;
    JButton btn_salir;
    JScrollPane contenedor;
    JTextArea jta_arregloSu;
    String rutaArchivo = "h:/Sueldo.txt";
    ArregloSueldos miarreglo = new ArregloSueldos(100);

    public Form_Sueldo() {
        interfaz();
    }

    public void interfaz() {
        lbl_taman = new JLabel("Ingrese tamaño ");
        lbl_taman.setBounds(20, 20, 100, 20);
        add(lbl_taman);

        txt_tamanio = new JTextField();
        txt_tamanio.setBounds(120, 20, 50, 20);
        add(txt_tamanio);

        btn_agregar = new JButton("AGREGAR");
        btn_agregar.setBounds(250, 70, 100, 20);
        btn_agregar.addActionListener(this);
        add(btn_agregar);

        btn_mostrar = new JButton("MOSTRAR");
        btn_mostrar.setBounds(250, 100, 100, 20);
        btn_mostrar.addActionListener(this);
        add(btn_mostrar);

        btn_mosmayores = new JButton("MAYORES");
        btn_mosmayores.setBounds(250, 130, 100, 20);
        btn_mosmayores.addActionListener(this);
        add(btn_mosmayores);

        lbl_frase = new JLabel(" ARREGLO DEL SUELDO ");
        lbl_frase.setBounds(60, 50, 140, 20);
        add(lbl_frase);

        jta_arregloSu = new JTextArea();
        contenedor = new JScrollPane(jta_arregloSu);
        contenedor.setBounds(40, 70, 200, 220);
        add(contenedor);

        txt_resul = new JTextField();
        txt_resul.setBounds(290, 195, 130, 20);
        add(txt_resul);

        btn_resultado = new JButton("PROMEDIO");
        btn_resultado.setBounds(250, 160, 100, 20);
        btn_resultado.addActionListener(this);
        add(btn_resultado);

        btn_salir = new JButton("SALIR");
        btn_salir.setBounds(290, 240, 100, 20);
        btn_salir.addActionListener(this);
        add(btn_salir);

    }

    public void mostrar() {
        for (int i = 0; i < miarreglo.getIndice(); i++) {
            System.out.println(" sueldo [" + (i + 1) + "] : " + miarreglo.getSueldos()[i]);
            jta_arregloSu.append(" sueldo [" + (i + 1) + "] : " + miarreglo.getSueldos()[i]);
            jta_arregloSu.append("\n");
        }
    }

    public void mayores() {
        int cont = 0;
        System.out.println("\n *** Sueldos mayores a 2000 ***");

        for (int i = 0; i < miarreglo.getIndice(); i++) {
            if (miarreglo.getSueldos()[i] > 2000) {
                System.out.println(" mayores [" + ( i + 1) + " ] : "+ miarreglo.getSueldos()[i] );
                jta_arregloSu.append("mayores [" + ( i + 1 )+ "] : "+ miarreglo.getSueldos()[i] );
                jta_arregloSu.append("\n");
                cont++;
            }
        }
        
        jta_arregloSu.append(" Mayores  " + cont);
        jta_arregloSu.append("\n");

    }

    public void promedio() {
        double promedio = 0;
        double suma = 0;
        int cont = 0;
        for (int i = 0; i < miarreglo.getIndice(); i++) {
            if (miarreglo.getSueldos()[i] > 2000) {
                suma = suma + miarreglo.getSueldos()[i];
                cont++;
            }
        }
        promedio = suma / cont;
        txt_resul.setText(String.valueOf(promedio));

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn_agregar) {

            System.out.println(Integer.parseInt(txt_tamanio.getText().trim()));
            miarreglo.Llenar_Sueldo(Integer.parseInt(txt_tamanio.getText().trim()));

        }

        if (e.getSource() == btn_mostrar) {

            this.mostrar();
            try {
                EscribirArchivo();
                System.out.println("\n");
            } catch (Exception ee) {
                System.out.println(""+ee.getMessage());
            }
        }

        if (e.getSource() == btn_mosmayores) {
            this.mayores();
            try {
                EscribirArchivo();
                System.out.println("\n");
            } catch (Exception ee) {
                System.out.println(""+ee.getMessage());
            }
        }

        if (e.getSource() == btn_resultado) {
            this.promedio();
            try {
                EscribirArchivo();
            } catch (Exception ee) {
                System.out.println(""+ee.getMessage());
            }
        }

        if (e.getSource() == btn_salir) {
            System.exit(0);
//            dispose();
        }
    }

    public static void main(String[] args) {
        Form_Sueldo Ir = new Form_Sueldo();
    
        Ir.setLayout(null);
        Ir.setSize(450, 400);
        Ir.setVisible(true);
    }

    public FileWriter AbrirArchivo() throws IOException {
        FileWriter temporalArchivo = null;
        try {
            temporalArchivo = new FileWriter(rutaArchivo);
        } catch (Exception e) {
            System.out.println("" + e.getMessage());
        }
        return temporalArchivo;
    }


package Semana4examen;

import java.util.Scanner;
import sun.security.util.Length;

/**
 *
 * @author luz
 */
public class ArregloSueldos {

    private int max;
    private int indice;
    private double sueldos[];

    public ArregloSueldos(int tam) {
      
        this.indice = 0;
        this.max=tam;
        this.sueldos= new double[this.max];
    }
  

   
    public int getIndice() {
        return indice;
    }

    public void setIndice(int indice) {
        this.indice = indice;
    }

    public double[] getSueldos() {
        return sueldos;
    }

    public void setSueldos(double[] sueldos) {
        this.sueldos = sueldos;
    }

    public void Llenar_Sueldo(int tamanio) {
        for (int i = 0; i < tamanio; i++) {
            this.sueldos[indice++]=(double) (500+(Math.random()*3000));
        }
    }
    
    public void Mostrar_Sueldo(){
        System.out.println("*** SUELDOS ***");
        for (int i = 0; i < getIndice() ; i++) {
            System.out.println("["+sueldos[i]+"]");
        }
    }
    
    public double Mayores_Sueldos(){
        int cont=0;
        System.out.println("\n *** Sueldos mayores a 2000 ***");
        for (int i = 0; i < getIndice(); i++) {
            
            if (sueldos[i] > 2000) {
                System.out.println("["+sueldos[i]+"]");
                cont++;
            }
        }
        return cont;
    }
    
    public void Promedio_Sueldos(){
        double suma=0;
        double promedio;
        for (int i = 0; i  = "+ promedio);
       
    }
    
    
}

No hay comentarios.:

Publicar un comentario