Fluent interface. Is anybody can comment whether it is right or not?


  • 0
    V

    I was given recently task to right code utilizing fluent interface.
    Implement a utility class/prototype with a Fluent Interface, which can perform conversions
    between various units of temperature measurement.
    0_1511364455876_ff39284e-3061-4532-9c47-aa35124d34f4-image.png Rules

    ● All conversions should be rounded to 2 decimal places.
    ● Invalid conversions, such as converting a length to a weight, must throw an exception
    ● Only the following conversions need to be supported:

    From / To Conversion
    Celsius to Fahrenheit  
    Kelvin to Celsius
    Fahrenheit to Kelvin

    Example Usage

    // Java
    double fahrenResult = new Conversion ()
    .convert( 100 , “C”)
    .to(“F”)
    .execute();

    This is my code:

    public class Conversion {
    	
      private double temp;
      private String from;
      private String to;
      private double converted;
     
      private final static String fromCelsius = "Only conversion from Celsius to Farenheit allowed";
      private final static String fromKelvin = "Only conversion from Kelvin to Celsius allowed";
      private final static String fromFarenheit = "Only conversion from Farenheit to Kelvin allowed";
    		
      private Conversion() {};
      
      public Conversion to(String to) {
    	this.to = to;
    	return this;
      }
      public Conversion convert(double temp, String from) {
    	this.temp = temp;
    	this.from = from;
    	return this;
      }
      public double execute() throws RuntimeException {
     	   
        	switch (this.to) {
        	  case "F": 
        		System.out.println("Conversipn from C to F");
        		if (this.from.equals("C") )
        		  this.converted = 32 + (this.temp * 9 / 5);
        		else {
        		  throw  new RuntimeException(fromCelsius);
        	    }
        		break;
        	  case "C":
        		System.out.println("Conversion from K to C");
        		if (this.from.equals("K"))
        		  this.converted = this.temp - 273.15;
        		else {
        		  throw  new RuntimeException(fromKelvin);
        		}
            break;
        	  case "K":
        		System.out.println("Conversion from F to K");
        		if (this.from.equals("F"))
        		  this.converted = (5.0/9.0 * (this.temp - 32) + 273.15);
        		else {
        		  throw  new RuntimeException(fromFarenheit);
        		}
     	   	break;
        	  }
        	  return this.converted = Math.round(converted*100.0)/100.0;
        	  
       }
    	
       public static void main (String[] args) {
        	 try {
        	   
          	/* happy scenarios */
           // conversion from Celsius to Farenheit		 
        		 double converted = new Conversion().convert(76.23, "C").to("F").execute();
        		 System.out.println("Converted " + converted);
        	  //conversion from Kelvin to Celsius	
        		 converted = new Conversion().convert(103.25, "K").to("C").execute();
        		 System.out.println("Converted " + converted);
         //conversion from Farenhaeit to Kelvin
        		 converted = new Conversion().convert(67, "F").to("K").execute();
        		 System.out.println("Converted " + converted);
        //This conversion from Celsius to Kelvin is ot allowed and throws RunTimeException
        		 converted = new Conversion().convert(76.23, "C").to("K").execute();
        	   
        		 System.out.println("Converted " + converted);
        } catch (RuntimeException e){
        	   System.out.println( e.getMessage());
        }I    	
      }
    }	
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.