Java Lesson 20: Separating color logic

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 looks the same as the ArgumentHelper class we started with in the previous lesson:

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 Color class has been changed so it looks like this:

public class Color {
    public static final Color RED = new Color(1,"Red");
    public static final Color GREEN = new Color(2,"Red");
    public static final Color BLUE = new Color(3,"Blue");
 
    private int colorId;
    private String colorString;
    private Color(int colorId, String colorString) {
        this.colorId = colorId;
        this.colorString = colorString;
    }
 
    /**
     * Get the Color object matching the specified text string.
     * null will be returned if the string could not be converted to
     * a Color object. 
     */
    public static Color getColor(String colorAsString) {
        if(colorAsString.equalsIgnoreCase(RED.colorString)){
            return RED;
        }else if(colorAsString.equalsIgnoreCase(GREEN.colorString)) {
            return GREEN;
        }else if(colorAsString.equalsIgnoreCase(BLUE.colorString)) {
            return BLUE;
        }
        return null;
    }
 
    public int getId() {
        return colorId;
    }
    public String getString() {
        return colorString;
    }
}

The ColorArgument class looks like this:

public class ColorArgument extends Argument {
    private Color color;
 
    public ColorArgument(Color color) {
    }
 
    public String getValue() {
    }
}

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

Your work is to add the getColorArgument method to the ArgumentHelper class and also to update the getValue method and constructor in the ColorArgument class so it works.

When you are finished we should be able to add support for “yellow” color only by making changes to the Color class, the rest of the classes should not have to be changed to add support for a new color.

Personal tools