Junior — Middle+
Converting a string to an integer using polymorphic parsers
livecode
Task condition
Create a program that takes a string value and returns its integer representation if present. Possible input formats:
- a regular decimal representation, for example: "42";
- a JSON string containing a "value" field: "{"value":42}". If the string does not contain a number (or the format does not match the specified), the function should return None. Implement the solution in an object-oriented style using polymorphism: create two separate parser classes (one handling regular strings, the other handling JSON), which implement a common interface/abstract class.
Example usage:
parser = get_parser(input_str) # function selects the appropriate parser
result = parser.parse()
where parse returns the found integer or None.