I wrote several versions of this code that can produce the desired output and stopped at this one, though maybe it's not the smoothest one. Well, anyway, the validator doesn't believe I'm using the constants declared in SecondClass, while I think I am... What's wrong, I wonder?
package com.codegym.task.task24.task2405;
/*
Black box
*/
public class Solution implements Action {
public static int actionObjectCount;
private int param;
private Action solutionAction = new Action() {
@Override
public void someAction() {
SecondClass secondClass = new SecondClass();
if (param > 0) {
FirstClass firstClass = new FirstClass() {
@Override
public void someAction() {
super.someAction();
}
@Override
public Action getDependentAction() {
//SecondClass secondClass = new SecondClass();
secondClass.someAction();
System.out.println(SecondClass.SPECIFIC_ACTION_FOR_ANONYMOUS_SECOND_CLASS_PARAM.trim() + " " + param);
return null;
}
};
while (param > 0) {
System.out.println(param);
param--;
}
firstClass.someAction();
firstClass.getDependentAction();
} else {
while (param < 0) {
//SecondClass secondClass = new SecondClass();
secondClass.someAction();
System.out.println(SecondClass.SPECIFIC_ACTION_FOR_ANONYMOUS_SECOND_CLASS_PARAM.trim() + " " + param);
param++;
}
}
}
};
public Solution(int param) {
this.param = param;
}
@Override
public void someAction() {
solutionAction.someAction();
}
/**
* 5
* 4
* 3
* 2
* 1
* FirstClass class, someAction method
* SecondClass class, someAction method
* Specific action for anonymous SecondClass, param = 0
* The number of created Action objects is 2
* SecondClass class, someAction method
* Specific action for anonymous SecondClass, param = -1
* The number of created Action objects is 3
*/
public static void main(String[] args) {
Solution solution = new Solution(5);
solution.someAction();
System.out.println("The number of created Action objects is " + actionObjectCount);
solution = new Solution(-1);
solution.someAction();
System.out.println("The number of created Action objects is " + actionObjectCount);
}
}