I am new to maps and scanners so bear with me. I am supposed to create a object that is able to read and understand commands. Any help would be appreciated this is what I have so far.
import sofia.micro.*; import student.IOHelper; import java.util.*; public class Minnow extends Actor { private Scanner input; private Map<String, Command> map; private Minnow raccoon; public Minnow) { this(IOHelper.createScannerForURL( "http://courses.cs.vt.edu/~cs1114/Fall2012/rocket-commands.txt")); map = new HashMap<String, Command>(); ForwardCommand bob = new ForwardCommand(raccoon); map.put("forward", bob); LeftCommand bill = new LeftCommand(raccoon); map.put("left", bill); RightCommand joe = new RightCommand(raccoon); map.put("right", joe); } public Minnow(Scanner scanner) { input = scanner; } public void act() { map.get(input.next()).execute(); } }
public class ForwardCommand implements Command { private Minnow minnow; public ForwardCommand(Minnow minnow) { minnow = new Minnow(); } public void execute() { minnow.move(1); } }
public class LeftCommand implements Command { private Minnow minnow; public LeftCommand(Minnow minnow) { minnow = new Minnow(); } public void execute() { minnow.turn(-90); }
public class RightCommand implements Command { private Minnow minnow; public RightCommand(Minnow minnow) { minnow = new Minnow(); } public void execute() { minnow.turn(90); }
public interface Command { void execute(); }