Skip to content

Commit

Permalink
Moved crappy check from InputValidator to individual menu options so …
Browse files Browse the repository at this point in the history
…I don't have to mess with the Validator every time I add a menu option
  • Loading branch information
nathanh89 committed Nov 18, 2012
1 parent bbc472a commit ca78d26
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 67 deletions.
2 changes: 1 addition & 1 deletion resources/cliOutput.properties
Expand Up @@ -41,4 +41,4 @@ newNameOption = Edit budget name
newStartOption = Edit budget start date
newEndOption = Edit budget end date
toMainOption = Return to Main Menu
dateNotSet = You haven't set that date yet! Better get on it.
dateNotSet = You haven't set this date yet! Better get on it.
Expand Up @@ -61,7 +61,7 @@ public static void main(String args[]){
String currentUser = System.getProperty("user.name");
defaultDirectory = "/home/" + currentUser + "/Documents/";
mainMenu = new MainMenu(currentBudget);
Printer.printPrompt("welcome");
Printer.print("welcome");
Listener.getInput();

while (stillUsingBudgetBoss){
Expand Down
Expand Up @@ -61,7 +61,7 @@ public void setEndDate(String newDate) {


public String toString(){
return ("\nBudget name: " + getName() + "\nBudget start: " + getStartDate() +
return ("\nBudget Name: " + getName() + "\nBudget Start: " + getStartDate() +
"\nBudget End: " + getEndDate() + "\n");
}

Expand Down
Expand Up @@ -14,7 +14,7 @@ public boolean inputNotAnInteger(String toCheck){
try{
Integer.parseInt(toCheck);
}catch(NumberFormatException e){
Printer.printPrompt("notEvenANumberGenius");
Printer.print("notEvenANumberGenius");
return true;
}
return false;
Expand All @@ -31,16 +31,16 @@ public boolean pathIsInvalid(String toCheck){
return false;
}
if(toCheck.contains(" ")){
Printer.printPrompt("whitespaceGenius");
Printer.print("whitespaceGenius");
System.out.println("The default is: " + BudgetBoss.getDefaultDirectory());
return true;
}
if(!toCheck.endsWith("/")){
Printer.printPrompt("endsInSlash");
Printer.print("endsInSlash");
System.out.println("The default is: " + BudgetBoss.getDefaultDirectory());
return true;
}else{
Printer.printPrompt("badPathInput");
Printer.print("badPathInput");
System.out.println("The default is: " + BudgetBoss.getDefaultDirectory());
return true;
}
Expand All @@ -52,12 +52,12 @@ public boolean inputNotABudget(String toCheck, int highestChoice){
if(inputNotAnInteger(toCheck))
return true;
if(Integer.valueOf(toCheck).equals(0)){
Printer.printPrompt("youreAZero");
Printer.print("youreAZero");
return true;
}
int userChoice = Integer.valueOf(toCheck);
if(userChoice > highestChoice){
Printer.printPrompt("thatsNotAChoice");
Printer.print("thatsNotAChoice");
return true;
}
else
Expand All @@ -70,16 +70,12 @@ public boolean menuChoiceIsInvalid(String toCheck, MasterMenu menu){
if(inputNotAnInteger(toCheck))
return true;
int userChoice = Integer.valueOf(toCheck);
if(BudgetBoss.getCurrentBudget().equals("No budget loaded") && ((userChoice < 5))){
Printer.printPrompt("noBudgetLoaded");
return true;
}
if(Integer.valueOf(toCheck).equals(0)){
Printer.printPrompt("youreAZero");
Printer.print("youreAZero");
return true;
}
if(userChoice > menu.getNumberOfOptions()){
Printer.printPrompt("thatsNotAChoice");
Printer.print("thatsNotAChoice");
return true;
}
else
Expand All @@ -90,58 +86,58 @@ public boolean dateIsInvalid(String toCheck){
if(itsTimeToExit(toCheck))
return false;
if(!toCheck.contains("/")){
Printer.printPrompt("wrongDateFormat");
Printer.print("wrongDateFormat");
return true;
}
if(toCheck.contains(" ")){
Printer.printPrompt("wrongDateFormat");
Printer.print("wrongDateFormat");
return true;
}
String delimiter = "/";
String[] date = toCheck.split(delimiter);
if(!(date.length == 3)){
Printer.printPrompt("wrongDateFormat");
Printer.print("wrongDateFormat");
return true;
}
for(int i = 0; i < 3; i++){
try{
Integer.parseInt(date[i]);
}catch(NumberFormatException e){
Printer.printPrompt("lessLettersPorFavor");
Printer.print("lessLettersPorFavor");
return true;
}
}
int month = Integer.valueOf(date[0].toString());
int day = Integer.valueOf(date[1].toString());
int year = Integer.valueOf(date[2].toString());
if((month < 1) || (month > 12)){
Printer.printPrompt("notAMonth");
Printer.print("notAMonth");
return true;
}
if((month == 4) || (month == 6) || (month == 9) || (month == 11)){
if((day < 1) || (day > 30)){
Printer.printPrompt("notADay");
Printer.print("notADay");
return true;
}
}else if(month == 2){
if((day == 29)){
Printer.printPrompt("dontBeASmartass");
Printer.print("dontBeASmartass");
return true;
}
if((day < 1) || (day > 28)){
Printer.printPrompt("notADay");
Printer.print("notADay");
return true;
}
}else{
if((day < 1) || (day > 31)){
Printer.printPrompt("notADay");
Printer.print("notADay");
return true;
}
}
DateTime timeNow = new DateTime();
int currentYear = timeNow.getYear();
if(year < currentYear){
Printer.printPrompt("backToTheFuture");
Printer.print("backToTheFuture");
return true;
}else
return false;
Expand Down
Expand Up @@ -13,7 +13,7 @@ public static String getPrintout(String toGet){
return cliOutput.getString(toGet);
}

public static void printPrompt(String toGet){
public static void print(String toGet){
System.out.println(cliOutput.getString(toGet));
}

Expand Down
Expand Up @@ -10,9 +10,9 @@ public class Opener {
InputValidator validator = new InputValidator();

public void loadFromDirectory(){
Printer.printPrompt("savedInDefault");
Printer.print("savedInDefault");
System.out.println(BudgetBoss.getDefaultDirectory());
Printer.printPrompt("whereSaved");
Printer.print("whereSaved");
String toCheck = Listener.getInput();
while(validator.pathIsInvalid(toCheck))
toCheck = Listener.getInput();
Expand All @@ -25,18 +25,18 @@ public void loadFromDirectory(){
}

private void searchDirectory(String directoryToSearch){
Printer.printPrompt("searchingDirectory");
Printer.print("searchingDirectory");
File [] foundBudgets = Finder.findBudgets(directoryToSearch);
if(foundBudgets.length > 0){
selectBudget(foundBudgets);
BudgetBoss.setDefaultDirectory(directoryToSearch);
}else
Printer.printPrompt("noBudgetFound");
Printer.print("noBudgetFound");
}

private void selectBudget(File[] foundBudgets){
Printer.printFoundBudgets(foundBudgets);
Printer.printPrompt("openBudget");
Printer.print("openBudget");
int index = getNumberToOpen(foundBudgets);
if(!(index == -5)){
System.out.println("Opening " + foundBudgets[index].getName() + "...\n");
Expand All @@ -63,25 +63,25 @@ private Budget loadBudget(int index, File[] foundBudgets){
try {
newBudget = new FileInputStream(foundBudgets[index].toString());
} catch (FileNotFoundException e) {
Printer.printPrompt("wTF");
Printer.print("wTF");
System.out.println("Error making the FileInputStream");
}
try {
toLoad = new ObjectInputStream(newBudget);
} catch (IOException e) {
Printer.printPrompt("wTF");
Printer.print("wTF");
System.out.println("Error making the ObjectInputStream");
}
try {
loadedBudget = toLoad.readObject();
} catch (ClassNotFoundException | IOException e) {
Printer.printPrompt("wTF");
Printer.print("wTF");
System.out.println("IO exception, maybe the file is bad?");
}
try {
toLoad.close();
} catch (IOException e) {
Printer.printPrompt("wTF");
Printer.print("wTF");
System.out.println("Couldn't close the Object/File input streams.");
}
BudgetBoss.endLoadSavedBudget();
Expand Down
Expand Up @@ -11,7 +11,7 @@ public class Salvation {
InputValidator validator = new InputValidator();

private String getSaveDirectory(){
Printer.printPrompt("getSaveDirectoryPath");
Printer.print("getSaveDirectoryPath");
System.out.println("The default is: " + BudgetBoss.getDefaultDirectory());
String toCheck = Listener.getInput();
while(validator.pathIsInvalid(toCheck))
Expand All @@ -33,7 +33,7 @@ public void writeBudgetToDisk(String fileName, Budget budget){
ObjectOutputStream saveOutput = new ObjectOutputStream(saveFile);
saveOutput.writeObject(budget);
saveOutput.close();
Printer.printPrompt("budgetSaved");
Printer.print("budgetSaved");
BudgetBoss.setDefaultDirectory(pathToSalvation);
}catch(Exception ex){
ex.printStackTrace();
Expand All @@ -48,7 +48,7 @@ public void writeBudgetToText(String fileName, Budget budget){
PrintStream budgetToText = new PrintStream(new FileOutputStream(pathToSalvation + fileName + ".txt"));
budgetToText.println(budget.toString());
budgetToText.close();
Printer.printPrompt("budgetSaved");
Printer.print("budgetSaved");
BudgetBoss.setDefaultDirectory(pathToSalvation);
} catch (FileNotFoundException e) {
System.out.println("Rethink this son");
Expand Down
Expand Up @@ -7,7 +7,7 @@
public class TheCreator {

public void createBudget(){
Printer.printPrompt("getBudgetName");
Printer.print("getBudgetName");
String desiredName = Listener.getInput();
if(!(desiredName.equals("exit"))){
System.out.println("Creating budget " + desiredName + "...\n");
Expand Down
Expand Up @@ -11,7 +11,6 @@
public class EditorMenu implements MasterMenu{

private Budget toEdit;
private int currentMenuChoice;
private boolean stillEditing = true;
private InputValidator validator = new InputValidator();

Expand Down Expand Up @@ -50,16 +49,16 @@ public int getNumberOfOptions() {

public void displayMenu(){
AnsiConsole.out.println(ansi().eraseScreen());
Printer.printPrompt("editorMenuHeader");
Printer.print("editorMenuHeader");
System.out.println("Working with budget: " + toEdit.getName() + "\n");
Printer.printMenuOptions(menuOptions);
InputValidator validator = new InputValidator();
String userInput = Listener.getInput();
while(validator.menuChoiceIsInvalid(userInput, this))
userInput = Listener.getInput();
if(!(userInput.equals("exit"))){
currentMenuChoice = Integer.valueOf(userInput);
chooseOption();
int optionChose = Integer.valueOf(userInput);
chooseOption(optionChose);
}else
stillEditing = false;
}
Expand All @@ -70,7 +69,7 @@ private void choseToSaveText(){
}

private void getNewName(){
Printer.printPrompt("getNewName");
Printer.print("getNewName");
String userInput = Listener.getInput();
if(!(userInput.equals("exit")))
toEdit.setName(userInput);
Expand All @@ -83,7 +82,7 @@ private void getNewName(){
}

private void getNewStartDate(){
Printer.printPrompt("getNewStartDate");
Printer.print("getNewStartDate");
String userInput = Listener.getInput();
while(validator.dateIsInvalid(userInput))
userInput = Listener.getInput();
Expand All @@ -92,16 +91,16 @@ private void getNewStartDate(){
}

private void getNewEndDate(){
Printer.printPrompt("getNewEndDate");
Printer.print("getNewEndDate");
String userInput = Listener.getInput();
while(validator.dateIsInvalid(userInput))
userInput = Listener.getInput();
if(!(userInput.equals("exit")))
toEdit.setEndDate(userInput);
}

public void chooseOption(){
int index = (currentMenuChoice -1);
public void chooseOption(int optionChose){
int index = (optionChose - 1);
menuOptions[index].optionMethod();
}
}

0 comments on commit ca78d26

Please sign in to comment.