2

I have the to classes VacationCostCalculator and a main:

public class Program {
    
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Not enough input arguments to run this program");
        } else {
            String transportMethod = args[0];
            String distance = args[1];
            VacationCostCalculator calculator = new VacationCostCalculator(Double.parseDouble(distance));
            double result = calculator.costOfVacation(transportMethod);
            System.out.println(result);
        }
    }
}
package com.itminds.vacationcost;

    public class VacationCostCalculator {
    
    private double distanceToDestination;

    public VacationCostCalculator(double distance) {
        distanceToDestination = distance;
    }

    public double costOfVacation(String transportMethod) {
        switch (transportMethod) {
            case "Car":
                return distanceToDestination * 1;
            case "Plane":
                return distanceToDestination * 2;
            default:
                throw new UnsupportedOperationException();
        }
    }

    public double getDistanceToDestination() {
        return distanceToDestination;
    }

    public void setDistanceToDestination(double distanceToDestination) {
        this.distanceToDestination = distanceToDestination;
    }
}

My task is to use the right design patterns to finish this application, but I have never learned what the values in the parameter in main is for. I cant find a decent doc that exlplains it well. What do i have to do to make objects of the arguments bigger??? I dont even know what to ask for properly here.. How to run a sample test on this?? is it only through CMD terminal using Javac commands or can i test this by creating other classes that runs this????? i am currently using intellij

Rosso
  • 93
  • 2
  • 9
  • In main, you will receive a String[] which is an array of Strings. For example, il will be `foo1, foo2`. Here you expect to receive 2 parameters : at least : args.length < 2``. You are parsing these strings to get the good type : `Double.parseDouble(distance)` – Kirjava Nov 02 '20 at 10:29
  • yes i understand that, but how to test this?? How to run a sample test on this?? is it only through CMD terminal using Javac commands or can i test this by creating other classes that runs this????? i am currently using intellij – Rosso Nov 02 '20 at 10:32
  • To test this, either, you had the arguments into the IDE or you can test by typing them into the cmd space separated. ex : `myprogram.exe foo1 foo2` (I'm assuming it is a windows exe cause you spoke about cmd) – Kirjava Nov 02 '20 at 10:33
  • 1
    Edit your "Run configuration" that intellij created when you first ran your `main`. There you can add parameters. – f1sh Nov 02 '20 at 10:33

0 Answers0