Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Separating ui from logic allows you to increase scalability and readability of your code. In your case one class should provide methods to support interaction with user in order to provide necessary data and other that will handle convertion process. Both classes should not be aware of each other.</p> <p>For example ui can be implemented as follows:</p> <p><pre><code>public class ConverterUI {</p> <pre><code>public Double askForConversionRate() { System.out.println("Welcome to Currency converter!" + "\n"); System.out.println("Please select an option below: "); System.out.println("1 &gt;&gt; Pound to Euro " + "\n2 &gt;&gt; Euro to Pound"); Scanner s = new Scanner(System.in); String selection = s.next(); switch (selection) { case "1": System.out.println("Enter the conversion rate from Pound to Euro"); break; case "2": System.out.println("Enter the conversion rate from Euro to Pound"); break; } Scanner rate = new Scanner(System.in); return rate.nextDouble(); } public Double askForAmountToConvert() { System.out.println("Now enter the amount to be converted: "); Scanner input = new Scanner(System.in); return input.nextDouble(); } public void showResult(Double result) { System.out.println("Convertion result is: " + result); } </code></pre> <p>}</pre></code></p> <p>It has three simple methods that you can use to get data from user but there is no information about what to do with it. Converter class is responsible for this part:</p> <p><pre><code> public class Converter {</p> <pre><code>private Double conversionRate; private Double amount; public Double convert() { return conversionRate * amount; } public void setConversionRate(Double conversionRate) { this.conversionRate = conversionRate; } public void setAmount(Double amount) { this.amount = amount; } </code></pre> <p>} </pre></code></p> <p>It allows you to set convertion rate and amount values and make some calculations with convert() method. It doesn't need to know what is the source of data - you should support valid arguments and it will provide response.</p> <p>In this situation main class can look like this:</p> <p><pre> public class Main {</p> <pre><code>public static void main(String[] args) { ConverterUI ui = new ConverterUI(); Converter converter = new Converter(); Double convertionRate = ui.askForConversionRate(); converter.setConversionRate(convertionRate); Double amount = ui.askForAmountToConvert(); converter.setAmount(amount); Double result = converter.convert(); ui.showResult(result); } </code></pre> <p>} </pre></p> <p>We create two objects - ui and converter. Calling ui methods provides data which are set to converter, and result of convertion is passed back to ui to inform user.</p> <p>This approach allows you to add new UI (defining an interfaces would be a nice idea) and converter implementations without need of editing existing ones.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload