Enum - como constantes e com construtores

Moeda.java

 

package market;

 

public enum Moeda {

 DOLAR(3.26), EURO(3.63), IENES(310.07);

 

 private double taxa;

 

 Moeda(double taxa) {

         this.taxa = taxa;

 }

 

 public double getTaxa() {

         return taxa;

 }

 

 public double converte(double valorReais) {

         return taxa * valorReais;

 }

}

 

 

Main.java

 

import static market.Moeda.*;

 

public class Principal {

 

 public static void main(String[] args) {

 

         double valorReais = 10.0;

 

         System.out.println(String.format("R$ %.2f em %s é %.2f", valorReais, DOLAR, DOLAR.converte(valorReais)));

         System.out.println(String.format("R$ %.2f em %s é %.2f", valorReais, EURO, EURO.converte(valorReais)));

         System.out.println(String.format("R$ %.2f em %s é %.2f", valorReais, IENES, IENES.converte(valorReais)));

 }

 

}

 

Saida

 

R$ 10,00 em DOLAR é 32,60

R$ 10,00 em EURO é 36,30

R$ 10,00 em IENES é 3100,70