Last active
September 11, 2020 10:34
-
-
Save AHaliq/fdcc323090fd32eaecff6c1f867bcbaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE read Responses.java first itll make more sense | |
public class CreateInputCommand implements ResponseFunc { | |
private String front = ""; | |
private String back = ""; | |
// handle types?? | |
@Override | |
public boolean funcCall(String i, State s) { | |
if (s.state == StateEnum.CREATE_STATE_FRONT) { | |
front = i; | |
s.state = StateEnum.CREATE_STATE_BACK; | |
} else if (s.state == StateEnum.CREATE_STATE_BACK) { | |
back = i; | |
s.getDeck(s.createdDeckName).addCard(new FrontBackCard(front, back)); | |
// TODO generalize for other types | |
s.state = StateEnum.CREATE_STATE_FRONT; | |
}else { | |
return false; | |
// entered at wrong state | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Responses { | |
LOGGER(".*", (i,s) -> { | |
s.log(i); | |
return false; | |
}), | |
... | |
CREATE("^create deck/ .+", (i,s) -> { | |
if (s.state != StateEnum.DEFAULT) return false; | |
// dont intercept input if not default state | |
// note all commands should have something like this | |
// even import export | |
String deckName = i.split("deck/")[1].trim(); | |
if(s.hasDeck(deckName)) { | |
// REPORT DECK EXISTS | |
} else { | |
s.state = StateEnum.CREATE_STATE_FRONT; | |
s.createdDeckName = deckName; | |
s.addDeck(new Deck(deckName)); | |
// PRINT INSTRUCTIONS TO USER HOW TO CREATE DECK | |
} | |
}), | |
CREATE_INPUT(".*", new CreateInputCommand()), | |
CREATE_ONE_SHOT("front/ .+ back .+", (i,s) -> { | |
if (s.state != StateEnum.CREATE_STATE_FRONT && | |
s.state != StateEnum.CREATE_STATE_BACK) return false; | |
String[] inpsplit = i.split("front/")[1].split("back/"); | |
String front = inpsplit[0].trim(); | |
String back = inpsplit[1].trim(); | |
//s.getDeck(s.createdDeckName).addCard(new FrontBackzcard(front, back)); | |
s.state = StateEnum.CREATE_STATE_FRONT; | |
Responses.CREATE_INPUT.call(front, s); | |
Responses.CREATE_INPUT.call(back,s); | |
return true; | |
CREATE_QUIT("quit", (i,s) -> { | |
if (s.state != StateEnum.CREATE_STATE_FRONT && | |
s.state != StateEnum.CREATE_STATE_BACK) return false; | |
s.state = StateEnum.DEFAULT; | |
// MESSAGE USER END CREATION MODE | |
return true; | |
}), | |
... | |
UNKNOWN_INPUT(".*", (i,s) -> { | |
System.out.println("I don't understand :-("); | |
return true; | |
}); | |
} | |
/** NOTES: | |
* 0. i did not write proper regex this is all just pseudocode. | |
* 1. The first enum can be Logger with regex that match all, logs command, | |
* then return false so input can be matched by the corresponding enum. | |
* 2. Create, uses the ResponseFunc interface instance | |
* 3. Make sure last has a regex with match all, for when all other | |
* commands fail to capture is; unknown command. | |
* 4. If you want to specify more details you can always add more s.log() calls | |
* in the lambdas. | |
* 5. You can even consider adding dateTime stamp to log, and save log to file via | |
* Storage manager, and this could be a feature under your name. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment