Search code examples
javaswingparametersreturn

Java: How to properly return result using a method


I writing a program that will print "Based on the temperature it is likely" season A, B, C, or D by calling a method.

Code :

import java.util.Scanner;
import javax.swing.*;

public class Seasons {

  public static void main(String[] args) {
    int inputTemp;
    String response = JOptionPane.showInputDialog(null,
            "Enter the temperature");
    inputTemp = Integer.parseInt(response);
    String message = "Based on the temperature of " + inputTemp
            + " it is most likely " + determineSeason(inputTemp);
    JOptionPane.showMessageDialog(null, message);

  }

  public static String determineSeason(int inputTemp) {
    String season = null;
    if (inputTemp > 130 || inputTemp < -20) { 
        JOptionPane.showMessageDialog(null,"Invalid");
    }

    else if (inputTemp >= 90) {
        JOptionPane.showMessageDialog(null,"summer"); }

    else if (inputTemp >= 70 && inputTemp < 90) {
        JOptionPane.showMessageDialog(null,"spring"); }
    else { 
        JOptionPane.showMessageDialog(null,"winter"); }

    return season; 
  }
}

The program is returning with a JOptionPane the season from my method, then presents the JOptionPane from main method where I need it to say "Based on the temperature the season is Season A, B, C, or D.

Any advice on what I'm missing would be appreciated!


Solution

  • determineSeason should be a private method that doesn't deal with UI parts (doesn't interact with Swing components).

    private String determineSeason(int inputTemp) {
        if (inputTemp > 130 || inputTemp < -20) { 
            throw new IllegalArgumentException("Invalid");
        }
    
        if (inputTemp >= 90) {
            return "summer";
        } else if (inputTemp >= 70 && inputTemp < 90) {
            return "spring";
        } else { 
            return "winter";
        }
    }
    

    season can't be "Invalid", you aren't going to display "it is most likely Invalid", so it's better to throw an exception from the method, catch it on the caller side and show an error message window:

    class Seasons {
    
        public static void main(String[] args) {
            String response = JOptionPane.showInputDialog(null, "Enter the temperature");
            int inputTemp = Integer.parseInt(response);
            try {
                String message = String.format("Based on the temperature of %d, it is most likely %s", inputTemp, determineSeason(inputTemp));
                JOptionPane.showMessageDialog(null, message);
            } catch (IllegalTemperatureValueException | NumberFormatException e) {
                JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
    
        }
    
        private static String determineSeason(int inputTemp) throws IllegalTemperatureValueException {
            if (inputTemp > 130 || inputTemp < -20) {
                throw new IllegalTemperatureValueException("incorrect temperature value");
            }
    
            return inputTemp >= 90 ? "summer" : (inputTemp >= 70 ? "spring" : "winter");
        }
    }
    
    class IllegalTemperatureValueException extends Exception {
        public IllegalTemperatureValueException(String message) {
            super(message);
        }
    }