Solución: Semana 4: Evidencia: Aplicación de métodos - Parte II

Solución: Elabore una aplicación llamada PruebaLaptop2 que utilice una clase laptop, ésta deberá contar con los siguientes descriptores:

  1. Manejar como variables de instancia: marca (string), Modelo (string), Tamaño de memoria (integer) y precio (double).
  2. Manejar como variable estática: cuenta.
  3. Tener un constructor que cree un objeto laptop sin parámetros.
  4. Poseer un constructor que cree un objeto laptop con los cuatro parámetros.
  5. Sobrecargar el constructor con cuatro parámetros para que utilice el precio (integer).
  6. Tener los métodos de acceso correspondientes.
  7. Tener los métodos modificadores correspondientes.
  8. Sobrecargar los métodos modificadores de memoria y de precio para que acepten double e int correspondientemente.
  9. Tener un método estático que regresa el valor de la variable, cuenta (integer).
  10. Sobrecargar el método estático para que regrese a un valor double.

La variable cuenta debe ser definida como estática, junto con las demás variables de instancia y con un valor de cero:

private static int cuenta = 0;

Cada constructor deberá incrementar esta variable en uno.

También debe construir una aplicación como las que se definen en el material de formación de esta actividad de aprendizaje, creando al menos tres objetos de la clase laptop que muestren el uso de todos los métodos de la clase, empleando para ello los diferentes objetos creados.

Cada vez que se modifique algún valor de algún objeto, es importante que se desplieguen todos los objetos utilizados. Utilice también el método estático.

Según el ejercicio las condiciones de la cuenta se especifican en la clase laptop.java, así:

/**
 *
 * @author gfrodriguez
 */
public class laptop {

  private String marca, modelo;
  private int tamanoMemoria;
  private double precio;
  private static int cuenta = 0;

  public laptop() {
    marca = "";
    modelo = "";
    tamanoMemoria = 0;
    precio = 0;
    cuenta++;
  }

  public laptop(String marcaPC, String modeloPC, int memoria, double precioDouble) {
    marca = marcaPC;
    modelo = modeloPC;
    tamanoMemoria = memoria;
    precio = precioDouble;
    cuenta = cuenta + 1;
  }

  public laptop(String marcaPC, String modeloPC, int memoria, int precioInteger) {
    marca = marcaPC;
    modelo = modeloPC;
    tamanoMemoria = memoria;
    precio = precioInteger;
    cuenta = cuenta + 1;
  }

  public void setMarca(String marcaPC) {
    marca = marcaPC;
  }

  public void setModelo(String modeloPC) {
    modelo = modeloPC;
  }

  public void setTamanoMemoria(int memoria) {
    tamanoMemoria = memoria;
  }

  public void setTamanoMemoria(double memoria) {
    tamanoMemoria = (int) memoria;
  }

  public void setPrecio(double precioDouble) {
    precio = precioDouble;
  }

  public void setPrecio(int precioInteger) {
    precio = precioInteger;
  }

  public int getCuenta() {
    return cuenta;
  }

  public double getCuenta(double x) {
    return (double) cuenta;
  }

  public String getMarca() {
    return marca;
  }
  public String getModelo() {
    return modelo;
  }

  public int getTamanoMemoria() {
    return tamanoMemoria;
  }

  public int getPrecio() {
    return (int) precio;
  }

  public double getPrecio(double x) {
    return (double) precio;
  }
}

Se crea el archivo de clase con el nombre PruebaLaptop2.java para la interfaz y gestionar las ordenes.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author gfrodriguez
 */
public class PruebaLaptop2 implements ActionListener {

    private laptop pc1, pc2, pc3;

    private JPanel panel;
    private JScrollPane jScrollPane1;
    private JLabel labelContador;
    private JLabel labelDatos;
    private JLabel labelMarca;
    private JLabel labelModelo;
    private JLabel labelPC1;
    private JLabel labelPC2;
    private JLabel labelPC3;
    private JLabel labelPrecio;
    private JLabel labelTamanoMemoria;
    private JTextArea textAreaDatos;
    private JTextField textFieldContador;
    private JTextField textFieldMarcaPC1;
    private JTextField textFieldMarcaPC2;
    private JTextField textFieldMarcaPC3;
    private JTextField textFieldModeloPC1;
    private JTextField textFieldModeloPC2;
    private JTextField textFieldModeloPC3;
    private JTextField textFieldPrecioPC1;
    private JTextField textFieldPrecioPC2;
    private JTextField textFieldPrecioPC3;
    private JTextField textFieldTamanoMemoriaPC1;
    private JTextField textFieldTamanoMemoriaPC2;
    private JTextField textFieldTamanoMemoriaPC3;

    private Button buttonPC1;
    private Button buttonPC2;
    private Button buttonPC3;

    private JFrame frame = new JFrame();

    public PruebaLaptop2() {

        panel = new JPanel();

        labelMarca = new javax.swing.JLabel();
        textFieldMarcaPC1 = new javax.swing.JTextField();
        labelModelo = new javax.swing.JLabel();
        textFieldModeloPC1 = new javax.swing.JTextField();
        labelTamanoMemoria = new javax.swing.JLabel();
        textFieldTamanoMemoriaPC1 = new javax.swing.JTextField();
        labelPrecio = new javax.swing.JLabel();
        textFieldPrecioPC1 = new javax.swing.JTextField();
        labelPC1 = new javax.swing.JLabel();
        textFieldMarcaPC2 = new javax.swing.JTextField();
        textFieldModeloPC2 = new javax.swing.JTextField();
        textFieldTamanoMemoriaPC2 = new javax.swing.JTextField();
        textFieldPrecioPC2 = new javax.swing.JTextField();
        labelPC2 = new javax.swing.JLabel();
        textFieldMarcaPC3 = new javax.swing.JTextField();
        textFieldModeloPC3 = new javax.swing.JTextField();
        textFieldTamanoMemoriaPC3 = new javax.swing.JTextField();
        textFieldPrecioPC3 = new javax.swing.JTextField();
        labelPC3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaDatos = new javax.swing.JTextArea();
        labelDatos = new javax.swing.JLabel();
        labelContador = new javax.swing.JLabel();
        textFieldContador = new javax.swing.JTextField();
        buttonPC1 = new Button("Guardar PC1");
        buttonPC2 = new Button("Guardar PC2");
        buttonPC3 = new Button("Guardar PC3");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(panel);
        panel.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 100, Short.MAX_VALUE)
        );

        labelMarca.setText("Marca");

        labelModelo.setText("Modelo");

        labelTamanoMemoria.setText("Tamaño memoria");

        labelPrecio.setText("Precio");

        labelPC1.setText("PC 1");

        labelPC2.setText("PC 2");

        labelPC3.setText("PC 3");

        textAreaDatos.setColumns(20);
        textAreaDatos.setRows(5);
        textAreaDatos.setLineWrap(true);
        jScrollPane1.setViewportView(textAreaDatos);

        labelDatos.setText("Datos");

        labelContador.setText("Contador");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane());
        frame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(labelTamanoMemoria, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(labelPrecio))
                                        .addComponent(labelModelo)
                                        .addComponent(labelMarca)
                                        .addComponent(labelDatos)
                                        .addComponent(labelContador))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jScrollPane1)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                .addComponent(textFieldTamanoMemoriaPC1, javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(textFieldModeloPC1, javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(textFieldMarcaPC1, javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                                        .addGap(31, 31, 31)
                                                                        .addComponent(labelPC1))
                                                                .addComponent(buttonPC1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(textFieldPrecioPC1, javax.swing.GroupLayout.Alignment.LEADING))
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                                        .addGap(18, 18, 18)
                                                                        .addComponent(labelPC2)
                                                                        .addGap(117, 117, 117))
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addGap(50, 50, 50)
                                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addComponent(textFieldPrecioPC2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(textFieldTamanoMemoriaPC2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(buttonPC2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                                .addComponent(textFieldMarcaPC2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(textFieldModeloPC2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                        .addGap(50, 50, 50)))
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(textFieldPrecioPC3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(textFieldTamanoMemoriaPC3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(textFieldModeloPC3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(textFieldMarcaPC3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(labelPC3)
                                                                        .addGap(46, 46, 46))
                                                                .addComponent(buttonPC3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addComponent(textFieldContador, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(labelPC2)
                                                                .addComponent(labelPC3)
                                                                .addComponent(labelPC1))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(textFieldMarcaPC2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(textFieldModeloPC2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(textFieldTamanoMemoriaPC2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(textFieldPrecioPC2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(labelMarca)
                                                                .addComponent(textFieldMarcaPC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(labelModelo)
                                                                .addComponent(textFieldModeloPC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(labelTamanoMemoria)
                                                                .addComponent(textFieldTamanoMemoriaPC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(labelPrecio)
                                                                .addComponent(textFieldPrecioPC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(20, 20, 20)
                                                .addComponent(textFieldMarcaPC3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textFieldModeloPC3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textFieldTamanoMemoriaPC3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textFieldPrecioPC3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(61, 61, 61)
                                                .addComponent(labelDatos, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                                                .addGap(61, 61, 61))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(buttonPC1, javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(buttonPC3, javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(buttonPC2))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(textFieldContador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(labelContador))
                                .addGap(16, 16, 16))
        );

        frame.setMinimumSize(new Dimension(300, 300));
        frame.setSize(600, 360);
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonPC1.addActionListener(this);
        buttonPC2.addActionListener(this);
        buttonPC3.addActionListener(this);
    }

    public static void main(String args[]) {
        new PruebaLaptop2();
    }

    public void actionPerformed(ActionEvent e) {
        int contador = 0;
        String datos = textAreaDatos.getText();
        if (e.getSource() == buttonPC1 && !textFieldMarcaPC1.getText().equals("") && !textFieldModeloPC1.getText().equals("") && !textFieldTamanoMemoriaPC1.getText().equals("") && !textFieldPrecioPC1.getText().equals("")) {
            laptop pc1 = new laptop();

            pc1.setMarca(textFieldMarcaPC1.getText());
            pc1.setModelo(textFieldModeloPC1.getText());
            pc1.setTamanoMemoria(Integer.parseInt(textFieldTamanoMemoriaPC1.getText()));
            pc1.setPrecio(Double.parseDouble(textFieldPrecioPC1.getText()));
            datos = datos + pc1.getMarca() + " " + pc1.getModelo() + " " + pc1.getTamanoMemoria() + " " + pc1.getPrecio(Double.parseDouble(textFieldPrecioPC1.getText())) + '\n';
            contador = pc1.getCuenta() + contador;
        }
        if (e.getSource() == buttonPC2 && !textFieldMarcaPC2.getText().equals("") && !textFieldModeloPC2.getText().equals("") && !textFieldTamanoMemoriaPC2.getText().equals("") && !textFieldPrecioPC2.getText().equals("")) {
            laptop pc2 = new laptop();

            pc2.setMarca(textFieldMarcaPC2.getText());
            pc2.setModelo(textFieldModeloPC2.getText());
            pc2.setTamanoMemoria(Double.parseDouble(textFieldTamanoMemoriaPC2.getText()));
            pc2.setPrecio(Integer.parseInt(textFieldPrecioPC2.getText()));
            datos = datos + pc2.getMarca() + " " + pc2.getModelo() + " " + pc2.getTamanoMemoria() + " " + pc2.getPrecio() + '\n';
            contador = pc2.getCuenta() + contador;
        }
        if (e.getSource() == buttonPC3 && !textFieldMarcaPC3.getText().equals("") && !textFieldModeloPC3.getText().equals("") && !textFieldTamanoMemoriaPC3.getText().equals("") && !textFieldPrecioPC3.getText().equals("")) {
            laptop pc3 = new laptop();
            pc3.setMarca(textFieldMarcaPC3.getText());
            pc3.setModelo(textFieldModeloPC3.getText());
            pc3.setTamanoMemoria(Double.parseDouble(textFieldTamanoMemoriaPC3.getText()));
            pc3.setPrecio(Double.parseDouble(textFieldPrecioPC3.getText()));
            datos = datos + pc3.getMarca() + " " + pc3.getModelo() + " " + pc3.getTamanoMemoria() + " " + pc3.getPrecio(Double.parseDouble(textFieldPrecioPC3.getText())) + '\n';
            contador = pc3.getCuenta() + contador;
        }
        textAreaDatos.setText(datos);
        textFieldContador.setText("" + contador + "");
    }
}

0 comments :

Publicar un comentario