RefactoreState pattern in Control (using Enums for the various Menus, Forms) So. NO USE of the Builders
Question:
RefactoreState pattern in Control (using Enums for the various Menus, Forms) So. NO USE of the Builders at all.
What I tried to do was take the code from toUIMenu() out of the menu builder and put it in control, but the problem is that Main and UI are separate packages so you can't create pairs or UIMenu from Control in Main package.
What I am reying to do for the enum, is :I try to take all the anonymous methods that create the MenuActions with their respective run methods, and I put those into an enum class. Instead of adding the anonymous methods, I would call the enum, like for default I called enum_name.DEFAULT which has the same code that was in the m.add method in addStart for default, is now in the enum for DEFAULT.
class Control { private static final int EXITED = 0; private static final int EXIT = 1; private static final int START = 2; private static final int NUMSTATES = 10; private UIMenu[] _menus; private int _state;
private UIForm _getVideoForm; private UIFormTest _numberTest; private UIFormTest _stringTest; private Inventory _inventory; private UI _ui; Control(Inventory inventory, UI ui) { _inventory = inventory; _ui = ui;
_menus = new UIMenu[NUMSTATES]; _state = START; addSTART(START); addEXIT(EXIT); UIFormTest yearTest = new UIFormTest() { public boolean run(String input) { try { int i = Integer.parseInt(input); return i > 1800 && i < 5000; } catch (NumberFormatException e) { return false; } } }; _numberTest = new UIFormTest() { public boolean run(String input) { try { Integer.parseInt(input); return true; } catch (NumberFormatException e) { return false; } } }; _stringTest = new UIFormTest() { public boolean run(String input) { return ! "".equals(input.trim()); } };
UIFormBuilder f = new UIFormBuilder(); f.add("Title", _stringTest); f.add("Year", yearTest); f.add("Director", _stringTest); _getVideoForm = f.toUIForm("Enter Video"); } void run() { try { while (_state != EXITED) { _ui.processMenu(_menus[_state]); } } catch (UIError e) { _ui.displayError("UI closed"); } } private void addSTART(int stateNum) { UIMenuBuilder m = new UIMenuBuilder(); m.add("Default", new UIMenuAction() { public void run() { _ui.displayError("doh!"); } }); m.add("Add/Remove copies of a video", new UIMenuAction() { public void run() { String[] result1 = _ui.processForm(_getVideoForm); Video v = Data.newVideo(result1[0], Integer.parseInt(result1[1]), result1[2]);
UIFormBuilder f = new UIFormBuilder(); f.add("Number of copies to add/remove", _numberTest); String[] result2 = _ui.processForm(f.toUIForm("")); Command c = Data.newAddCmd(_inventory, v, Integer.parseInt(result2[0])); if (! c.run()) { _ui.displayError("Command failed"); } } }); m.add("Check in a video", new UIMenuAction() { public void run() { // TODO String[] result1 = _ui.processForm(_getVideoForm); Video v = Data.newVideo(result1[0], Integer.parseInt(result1[1]),result1[2]); Command c = Data.newInCmd(_inventory, v); if (! c.run()) { _ui.displayError("Command failed"); } } });
public final class UIFormBuilder { private final List _menu = new ArrayList(); public void add(String prompt, UIFormTest test) { _menu.add(new Pair(prompt, test)); } public UIForm toUIForm(String heading) { if (null == heading) throw new IllegalArgumentException(); if (_menu.size() < 1) throw new IllegalStateException(); Pair[] array = new Pair[_menu.size()]; for (int i = 0; i < _menu.size(); i++) array[i] = _menu.get(i); return new UIForm(heading, array); } } m.add("Check out a video", new UIMenuAction() { public void run() { // TODO String[] result1 = _ui.processForm(_getVideoForm); Video v = Data.newVideo(result1[0], Integer.parseInt(result1[1]),result1[2]); Command c = Data.newOutCmd(_inventory, v); if (! c.run()) { _ui.displayError("Command failed"); } } }); m.add("Print the inventory", new UIMenuAction() { public void run() { _ui.displayMessage(_inventory.toString()); } }); m.add("Clear the inventory", new UIMenuAction() { public void run() { if (!Data.newClearCmd(_inventory).run()) { _ui.displayError("Command failed"); } } }); m.add("Undo", new UIMenuAction() { public void run() { if (!Data.newUndoCmd(_inventory).run()) { _ui.displayError("Command failed"); } } }); m.add("Redo", new UIMenuAction() { public void run() { if (!Data.newRedoCmd(_inventory).run()) { _ui.displayError("Command failed"); } } }); m.add("Print top ten all time rentals in order", new UIMenuAction() { public void run() { // TODO /*Iterator uses lambda expression * Iterator i = _inventory.iterator((r, rr) -> rr.numRentals() - r.numRentals()); int j = 0; StringBuilder s = new StringBuilder("The Top Ten Rentals: "); while (i.hasNext() && j < 10) { Record rrr = i.next(); s.append(j + 1).append(": ").append(rrr.toString()).append(" "); j++; } _ui.displayMessage(s.toString()); } });*/ if (_inventory.size() > 0) { Iterator it = _inventory.iterator(new java.util.Comparator() { public int compare(Record r1, Record r2) { return r2.numOut() - r1.numOut(); } }); StringBuilder b = new StringBuilder(); int counter = 1; b.append("top ten all time rentals in order: "); while (it.hasNext() && counter < 11) { Record r = it.next(); b.append(" " + r.video().title() + " [" + r.numOwned()+ "] "); counter++; } _ui.displayMessage(b.toString()); } else { _ui.displayError("Inventory is Empty!"); } } }); m.add("Exit", new UIMenuAction() { public void run() { _state = EXIT; } }); m.add("Initialize with bogus contents", new UIMenuAction() { public void run() { Data.newAddCmd(_inventory, Data.newVideo("a", 2000, "m"), 1).run(); Data.newAddCmd(_inventory, Data.newVideo("b", 2000, "m"), 2).run(); Data.newAddCmd(_inventory, Data.newVideo("c", 2000, "m"), 3).run(); Data.newAddCmd(_inventory, Data.newVideo("d", 2000, "m"), 4).run(); Data.newAddCmd(_inventory, Data.newVideo("e", 2000, "m"), 5).run(); Data.newAddCmd(_inventory, Data.newVideo("f", 2000, "m"), 6).run(); Data.newAddCmd(_inventory, Data.newVideo("g", 2000, "m"), 7).run(); Data.newAddCmd(_inventory, Data.newVideo("h", 2000, "m"), 8).run(); Data.newAddCmd(_inventory, Data.newVideo("i", 2000, "m"), 9).run(); Data.newAddCmd(_inventory, Data.newVideo("j", 2000, "m"), 10).run(); Data.newAddCmd(_inventory, Data.newVideo("k", 2000, "m"), 11).run(); Data.newAddCmd(_inventory, Data.newVideo("l", 2000, "m"), 12).run(); Data.newAddCmd(_inventory, Data.newVideo("m", 2000, "m"), 13).run(); Data.newAddCmd(_inventory, Data.newVideo("n", 2000, "m"), 14).run(); Data.newAddCmd(_inventory, Data.newVideo("o", 2000, "m"), 15).run(); Data.newAddCmd(_inventory, Data.newVideo("p", 2000, "m"), 16).run(); Data.newAddCmd(_inventory, Data.newVideo("q", 2000, "m"), 17).run(); Data.newAddCmd(_inventory, Data.newVideo("r", 2000, "m"), 18).run(); Data.newAddCmd(_inventory, Data.newVideo("s", 2000, "m"), 19).run(); Data.newAddCmd(_inventory, Data.newVideo("t", 2000, "m"), 20).run(); Data.newAddCmd(_inventory, Data.newVideo("u", 2000, "m"), 21).run(); Data.newAddCmd(_inventory, Data.newVideo("v", 2000, "m"), 22).run(); Data.newAddCmd(_inventory, Data.newVideo("w", 2000, "m"), 23).run(); Data.newAddCmd(_inventory, Data.newVideo("x", 2000, "m"), 24).run(); Data.newAddCmd(_inventory, Data.newVideo("y", 2000, "m"), 25).run(); Data.newAddCmd(_inventory, Data.newVideo("z", 2000, "m"), 26).run(); } }); _menus[stateNum] = m.toUIMenu("Bob's Video"); } private void addEXIT(int stateNum) { UIMenuBuilder m = new UIMenuBuilder(); m.add("Default", new UIMenuAction() { public void run() {} }); m.add("Yes", new UIMenuAction() { public void run() { _state = EXITED; } }); m.add("No", new UIMenuAction() { public void run() { _state = START; } }); _menus[stateNum] = m.toUIMenu("Are you sure you want to exit?"); } } public class UIMenuBuilder { private final List> _menu; public UIMenuBuilder() { _menu = new ArrayList>(); } public UIMenu toUIMenu(String heading) { if (null == heading) throw new IllegalArgumentException(); if (_menu.size() <= 1) throw new IllegalStateException(); Pair[] array = new Pair[_menu.size()]; for (int i = 0; i < _menu.size(); i++) { array[i] = _menu.get(i); } return new UIMenu(heading, array); } public void add(String prompt, UIMenuAction action) { if (null == action) throw new IllegalArgumentException(); _menu.add(new Pair(prompt, action)); } }
public final class UIForm extends UIMenuForm { private final Pair[] _form;
UIForm(String heading, Pair[] menu) { super(heading); _form = menu; } public int size() { return _form.length; } public String getHeading() { return super.getHeading(); } public String getPrompt(int i) { return _form[i].prompt; } public boolean checkInput(int i, String input) { if (null == _form[i]) return true; return _form[i].test.run(input); } }
public final class UIMenu extends UIMenuForm{ private final Pair[] _menu;
UIMenu(String heading, Pair[] menu) { super(heading); _menu = menu; } public int size() { return _menu.length; } public String getHeading() { return super.getHeading(); } public String getPrompt(int i) { return _menu[i].prompt; } public void runAction(int i) { _menu[i].test.run(); } }