Enum as data repository



  • Abstract class available Measurments♪ All types Measurments inherit this class - TemperatureRainfall etc. Each type has a value and a unit of measure. I've been told that for all types of unit of measure one needs to be created. enum♪ Please tell me how to look like this. enum and how to tie it to each class where the units of measurements (%, m/s, mm/kv, y/s/s, etc.)?



  • The storage of the units of measurements can be achieved:

    public enum Unit {
        TEMP("C"),  // Температура
        WIND("м/сек"),  // Скорость ветра
        PREC("мм");  // Осадки
    
    private String unit;
    
    Unit(String unit) {
        this.unit = unit;
    }
    
    public String value() {
        return unit;
    }
    

    }

    For wind direction:

    public enum WindDirection {
    NORTH("север"),
    EAST("восток"),
    ...;

    private String direction;
    
    Units(String direction) {
        this.direction = direction;
    }
    
    public String value() {
         return direction;
    }
    

    }

    Use:

    public class Wind extends Measurments {
    private static final String UNIT = Unit.WIND.value();

    private String value;
    private WindDirection direction;
    
    Wind(String value, WindDirection direction) {
        this.value = value;
        this.direction = direction;
    }
    
    public String toString() {
        return "Ветер: " + 
               "скорость " + this.value + " " + UNIT + ", " +
               "направление " + this.direction.value();
    }
    

    }

    Thus Unit will be a repository for all units of measurements.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2