4
« en: 17 de Agosto 2021, 20:12 »
Gracias, verás, creé un objeto pero no me permite llamar
Código
Clase Padre Empleado
//clase Padre
public class Empleado {
protected String nombre;
protected String apellido;
protected int numeroSeguro;
//constructor
public Empleado(String nombre, String apellido, int numeroSeguro) {
this.nombre = nombre;
this.apellido = apellido;
this.numeroSeguro = numeroSeguro;
}
//metodos gett and setter
public Empleado(){
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public int getNumeroSeguro() {
return numeroSeguro;
}
public void setNumeroSeguro(int numeroSeguro) {
this.numeroSeguro = numeroSeguro;
}
@Override
public String toString(){
return this.nombre+"-"+this.apellido+"-"+this.numeroSeguro;
}
}
Clase hija 1 EmpleadoPorHora
//Clase hija
public class EmpleadoPorHora extends Empleado {
private int hora;
//Constructor
public EmpleadoPorHora(int hora, String nombre, String apellido, int numeroSeguro) {
super(nombre, apellido, numeroSeguro);
this.hora = hora;
}
public EmpleadoPorHora() {
}
//metodo
public double calcularSueldoSemanalPOrHora(){
double valorHora=3.50;
double sueldo;
sueldo= valorHora*hora;
return sueldo;
}
@Override
public String toString(){
return super.toString()+this.hora;
}
}
Clase hija 2 EmpleadoPorComision
//Clase Hija
public class EmpleadoPorComision extends Empleado {
private double venta;
//Constructor
public EmpleadoPorComision(double venta, String nombre, String apellido, int numeroSeguro) {
super(nombre, apellido, numeroSeguro);
this.venta = venta;
}
public EmpleadoPorComision(){
}
//metodo para calcular la comision
public double calcularComision(){
double comision;
double sueldoSemanal=170;
comision=sueldoSemanal+(0.20*venta);
return comision;
}
@Override
public String toString(){
return super.toString()+this.venta;
}
}
Clase Gui
//Gui
public class GUIEmpleado extends javax.swing.JFrame {
Empleado e1;
// Empleado e2 = new EmpleadoPorHora();
// Empleado e3 =new EmpleadoPorComision();
/**
* Creates new form GUIEmpleado
*/
public GUIEmpleado() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pnlDatos = new javax.swing.JPanel();
lblNombre = new javax.swing.JLabel();
lblApellido = new javax.swing.JLabel();
lblNumerodeSeguro = new javax.swing.JLabel();
txtNombre = new javax.swing.JTextField();
txtApellido = new javax.swing.JTextField();
txtNumerodeSeguro = new javax.swing.JTextField();
btnGuardar = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
txtVenta = new javax.swing.JTextField();
txtHoraTrabajadas = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
txaInformacion = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pnlDatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
lblNombre.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
lblNombre.setForeground(new java.awt.Color(255, 102, 255));
lblNombre.setText("Nombre");
lblApellido.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
lblApellido.setForeground(new java.awt.Color(255, 51, 255));
lblApellido.setText("Apellido");
lblNumerodeSeguro.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
lblNumerodeSeguro.setForeground(new java.awt.Color(255, 51, 204));
lblNumerodeSeguro.setText("Número de Seguro");
btnGuardar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
btnGuardar.setForeground(new java.awt.Color(153, 0, 153));
btnGuardar.setText("Guardar");
btnGuardar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGuardarActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 102, 204));
jLabel1.setText("Venta");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 0, 204));
jLabel2.setText("Horas Trabajadas");
javax.swing.GroupLayout pnlDatosLayout = new javax.swing.GroupLayout(pnlDatos);
pnlDatos.setLayout(pnlDatosLayout);
pnlDatosLayout.setHorizontalGroup(
pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlDatosLayout.createSequentialGroup()
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDatosLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(pnlDatosLayout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblNumerodeSeguro, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(lblNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
.addComponent(lblApellido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel2)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE)))
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtApellido, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
.addComponent(txtNombre, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtNumerodeSeguro, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtVenta, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtHoraTrabajadas, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(22, 22, 22))
);
pnlDatosLayout.setVerticalGroup(
pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlDatosLayout.createSequentialGroup()
.addContainerGap()
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblNombre)
.addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblApellido)
.addComponent(txtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblNumerodeSeguro)
.addComponent(txtNumerodeSeguro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtHoraTrabajadas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addComponent(btnGuardar)
.addContainerGap())
);
txaInformacion.setColumns(20);
txaInformacion.setRows(5);
jScrollPane1.setViewportView(txaInformacion);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnlDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(322, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(pnlDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int numSeguro=Integer.parseInt(txtNumerodeSeguro.getText());
e1=new Empleado(txtNombre.getText(), txtApellido.getText(),numSeguro);//En esta línea quiero llamar al metdo calcularsueldoSemanal
txaInformacion.setText(e1.toString());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUIEmpleado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUIEmpleado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUIEmpleado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUIEmpleado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUIEmpleado().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnGuardar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblApellido;
private javax.swing.JLabel lblNombre;
private javax.swing.JLabel lblNumerodeSeguro;
private javax.swing.JPanel pnlDatos;
private javax.swing.JTextArea txaInformacion;
private javax.swing.JTextField txtApellido;
private javax.swing.JTextField txtHoraTrabajadas;
private javax.swing.JTextField txtNombre;
private javax.swing.JTextField txtNumerodeSeguro;
private javax.swing.JTextField txtVenta;
// End of variables declaration
}