Java Lesson 19: New argument class

From Erlands Wiki

Jump to: navigation, search

The main class still looks like this:

public class MyClass {
    public static void main(String[] args) {
        ArgumentHelper helper = new ArgumentHelper(args);
        Argument argument = helper.getNextArgument();
        while(argument!=null) {
            printStuff(argument);
            argument = helper.getNextArgument();
        }
    }
 
    public static void printStuff(Argument data) {
        System.out.println("You specified: "+data.getValue());
    }
 
}

The Argument class is the same as before:

public abstract class Argument {
    public abstract String getValue();
}

The IntArgument class is the same as before:

public class IntArgument extends Argument {
    private int intArgument;
    public IntArgument(int intArgument) {
        this.intArgument = intArgument;
    }
    public int getInt() {
        return intArgument;
    }
    public String getValue() {
        return String.valueOf(intArgument);
    }
}

The StringArgument class is the same as before:

public class StringArgument extends Argument {
    private String strArgument;
    public void setString(String arg) {
        this.strArgument = arg;
    }
    public String getString() {
        return strArgument;
    }
    public String getValue() {
        return String.valueOf(strArgument);
    }
}

The helper class has been changed like this:

public class ArgumentHelper {
    private String[] args;
    int currentArgument = 0;
    public ArgumentHelper(String[] args) {
        this.args = args;
    }
    public Argument getNextArgument() {
        if(args.length>currentArgument) {
            ColorArgument colorArgument = getColorArgument(args[currentArgument]);
            if(colorArgument!=null){
                currentArgument++;
                return colorArgument;
            }else{
 
                int argument = 0;
                try {
                    argument = Integer.valueOf(args[currentArgument]);
                    // Move to next argument
                    currentArgument++;
                    // If we end up here it has successfully converted the parameter to an integer
                    return new IntArgument(argument);
 
                } catch (NumberFormatException e) {
                    // If we end up here the parameter could not be converted to an integer and
                    StringArgument strArgument = new StringArgument();
                    strArgument.setString(args[currentArgument]);
                    // Move to next argument
                    currentArgument++;
                    return strArgument;
                }
            }
        }else {
            return null;
        }
    }
}

The program shall be possible to execute with:

java MyClass 5 blue Hello

And then give an output like:

You specified: 5
You specified: Blue color
You specified: Hello

The color parameter “blue” can have the following values, and should output the corresponding string as shown below:

  • blue = Blue color
  • red = Red color
  • green = Green color

There is also a Color class with a number of constants available which you can choose to use you if you want to:

public class Color {
    public static final int RED = 1;
    public static final String RED_STRING = "Red";
 
    public static final int GREEN = 2;
    public static final String GREEN_STRING = "Green";
 
    public static final int BLUE = 3;
    public static final String BLUE_STRING = "Blue";
}

Your work is to add the getColorArgument method to the ArgumentHelper class and also to create the completely new ColorArgument class.

Personal tools