Cardinality in java

1

Good evening, my question is about in this case, how would you implement this, "The employee can have 1 to 3 services", how would that represent in code? This is what I have:

package Hojatrabajo;

public class Empleado {

    //ATRIBUTOS
    private String nombre;
    private String cedula;
    private String cargo; 
    private int anoIngreso;

    //RELACIONES
    private Servicio servicio1;
    private Servicio servicio2;
    private Servicio servicio3;

    //METODOS
    public Empleado(String nombre, String cedula, String cargo){
        this.nombre = nombre;
        this.cedula = cedula;
    }   

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getCedula() {
        return cedula;
    }

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }

    public String getCargo() {
        return cargo;
    }

    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    public int getAnoIngreso() {
        return anoIngreso;
    }

    public void setAnoIngreso(int anoIngreso) {
        this.anoIngreso = anoIngreso;
    }

    public boolean agregarServicio(String){

    }

    public boolean eliminarServicio(String){

    }

    public boolean calcularCostoPromedioServicios(){
        boolean promedio = (servicio1.calcularIngresosMasImpuestos() + servicio2.calcularIngresosMasImpuestos() + servicio3.calcularIngresosMasImpuestos())/3;
    }
}
    
asked by David 28.09.2017 в 03:20
source

1 answer

0

The rule says:

  

The employee can have from 1 to 3 services

This means:

  • The attribute must support multiple services (independent of the size that is the limitation). This means that you must have a structure as an arrangement, list or other.
  • The attribute must support at least 1 service . This means that at least 1 service must be requested at the time of construction of the class.

Knowing these two conditions from the rule, we can elaborate the design of our class (I ignore the other fields to simplify the answer):

public class Empleado {
    private List<String> listaServicios;

    public Empleado(String servicio) {
        //esta lista no permite agregar más elementos ni remover los elementos actuales
        listaServicios = Arrays.asList(servicio);
    }

    public Empleado(String servicio1, String servicio2) {
        listaServicios = Arrays.asList(servicio1, servicio2);
    }

    public Empleado(String servicio1, String servicio2, String servicio3) {
        listaServicios = Arrays.asList(servicio1, servicio2, servicio3);
    }

    public String getServicio(int indice) {
        if (indice <= 0 || indice >= listaServicios.size()) {
            return null;
        }
        return listaServicios.get(indice);
    }
}

If you need to design the list in a mutable way, that is, that allows you to support more data in the future, you should only wrap the result of Arrays.asList within a ArrayList . Example:

public Empleado(String servicio) {
    //esta lista sí permite agregar más elementos ni remover los elementos actuales
    listaServicios = new ArrayLists<>(Arrays.asList(servicio));
}

If you can not use a collection, then you just have to use a design declaring the required variables:

public class Empleado {

    private String servicio1;
    private String servicio2;
    private String servicio3;

    public Empleado(String servicio) {
        this(servicio, "", "");
    }

    public Empleado(String servicio1, String servicio2) {
        this(servicio1, servicio2, "");
    }

    public Empleado(String servicio1, String servicio2, String servicio3) {
        this.servicio1 = servicio1;
        this.servicio2 = servicio2;
        this.servicio3 = servicio3;
    }

    //getters y setters...
}
    
answered by 28.09.2017 в 04:59