If statements in methods or mainly?

Hi I am currently in chapter 5 in my book, and we have studied the instructions ifand .equals, as well as some other basic things. I noticed that in all our problems and in the examples in the book, we tend to give expressions ifinside these methods inside the class. Here is an exercise that I did, which is not complete but the picture is clear. I just ask the user to convert some units. However, when I was thinking about how to approach this problem, I did not want to use if statements in my methods, since I wanted the if statements to be related to user input, and as far as I know, we should always put it Scannerin main (well, this is what we have been doing all the time so far). And since I could not access these scanners from the main one, I used the operatorsif primarily.

Is this bad practice or is it generally better to do what is attached to the situation? I see how the main thing becomes ugly in large programs, if we put too many things there. I just coded one test case below, but you can see how ugly it would be if I made other combinations.

Here is my code

import java.util.Scanner;
class Conversion{
    public double mmToCm(double value){
        value = value / 10.0;
        return value;
    }
    public double cmToMm(double value){
        value = value * 10.0;
        return value;
    }
    public double cmToMeter(double value){
        value = value / 100.0;
        return value;
    }
    public double meterToCm(double value){
        value = value * 100.0;
        return value;
    }
    public double mmToMeter(double value){
        value = value / 1000.0;
        return value;
    }
    public double meterToMM(double value){
        value = value * (1000.0);
        return value;
    }   
}


public class test{
    public static void main(String [] args){
        Conversion convertThisVal = new Conversion();
        Scanner typeOne = new Scanner(System.in);
        Scanner typeTwo = new Scanner(System.in);
        Scanner valueOne = new Scanner(System.in);
        System.out.println("Convert From: ");
        String newTypeOne = typeOne.next();
        System.out.println("To: ");
        String newTypeTwo = typeTwo.next();
        System.out.println("Enter the values!");
        double newValueOne = valueOne.nextDouble();

        if (newTypeOne.equalsIgnoreCase("mm") && newTypeTwo.equalsIgnoreCase("cm"))
        {
            System.out.println("cm:" + convertThisVal.mmToCm(newValueOne));
        }
    }
}
+4
source share
2 answers

to create a good structured application that is created using separated components and separated I mean logically physically, you need to think of each component as an independent side, which means that the transformation logic must be separated from the input logic (by input, I mean getting input data)

, Scanner, , , -. , (IN) ( if, - ) ,

, , .

ex: Conversion

public double convert(String newTypeOne, String newTypeTwo, double newValueOne){
    if (newTypeOne.equalsIgnoreCase("mm") && newTypeTwo.equalsIgnoreCase("cm")){
        return mmToCm(newValueOne);
    }else if (...){
        return ...;
    }
    :
    : ... and so on
}

, , , , Scanner .

main :

public static void main(String [] args){
    Conversion convertThisVal = new Conversion();
    Scanner typeOne = new Scanner(System.in);
    Scanner typeTwo = new Scanner(System.in);
    Scanner valueOne = new Scanner(System.in);
    System.out.println("Convert From: ");
    String newTypeOne = typeOne.next();
    System.out.println("To: ");
    String newTypeTwo = typeTwo.next();
    System.out.println("Enter the values!");
    double newValueOne = valueOne.nextDouble();

    System.out.println("cm:" + convertThisVal.convert(newTypeOne,newTypeTwo, newValueOne ));
}

convert(), , .

, , , .

, , ( , - ..)

, .

+9

, if statement .

Converter,

interface Converter {
double convert(double value);
}

,

class MmToCmConverter implements Converter{
    double convert(double value){
    return value / 10.0;
    }
}

Map<String,Converter> lookup = new Hashap<>();
lookup.put("MM2CM",new MmToCmConverter());

, , :

String code = newTypeOne +"2"+newTypeTwo; //create code
Converter converter = lookup.get(code.toUpper());
double convertedValue = converter.convert(newValueOne);
+1

Source: https://habr.com/ru/post/1649300/


All Articles