Permalink
Browse files

File operations are pretty much done, for now. Can open/load Budgets …

…from any directory where they've been saved, can save modified or new ones in any directory.
  • Loading branch information...
nathanh89 committed Nov 10, 2012
1 parent 172bc35 commit b3553324facaa38644280499bb3526bb1cc8f638
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,7 +2,7 @@
import com.VSSBudgetBoss.main.BudgetBoss;
-public class Prompts {
+public class ConsoleOutput {
public static void welcomeToBudgetBoss(){
System.out.println("Welcome to BudgetBoss!");
@@ -73,6 +73,22 @@ public static void badPathInput(String defaultDirectory){
public static void pathToSalvationCheck(String defaultDirectory){
System.out.println("Save in " + defaultDirectory + "? (y/n)");
}
+
+ public static void NotEvenANumberGenius() {
+ System.out.println("That's...not a number. Try again!");
+ }
+
+ public static void ThatsNotAChoice() {
+ System.out.println("That's not one you can choose from, yo. WTF? Try again!");
+ }
+
+ public static void wTF(){
+ System.out.println("Something has gone wrong. ABORT THE BABIES!");
+ }
+
+ public static void openingSelectedBudget(String name) {
+ System.out.println("Opening " + name);
+ }
}
@@ -31,7 +31,25 @@ else if(validateUserPath(toCheck)){
return toCheck;
}
else
- Prompts.badPathInput(defaultDirectory);
+ ConsoleOutput.badPathInput(defaultDirectory);
return "ERROR";
}
+
+ public boolean validateBudgetSelection(String toCheck, File[] foundBudgets){
+ try{
+ Integer.parseInt(toCheck);
+ }catch(NumberFormatException e){
+ ConsoleOutput.NotEvenANumberGenius();
+ return false;
+ }
+ int highestSelection = foundBudgets.length;
+ int userSelection = Integer.valueOf(toCheck);
+ if(userSelection > highestSelection){
+ ConsoleOutput.ThatsNotAChoice();
+ return false;
+ }
+ else
+ return true;
+ }
}
+
@@ -1,10 +1,9 @@
package com.VSSBudgetBoss.fileops;
-import java.io.File;
+import java.io.*;
-import com.VSSBudgetBoss.cli.InputListener;
-import com.VSSBudgetBoss.cli.InputValidator;
-import com.VSSBudgetBoss.cli.Prompts;
+import com.VSSBudgetBoss.budget.Budget;
+import com.VSSBudgetBoss.cli.*;
import com.VSSBudgetBoss.main.BudgetBoss;
public class Opener {
@@ -14,43 +13,84 @@
public void askToOpenBudget(String defaultDirectory){
InputValidator validator = new InputValidator();
InputListener listener = new InputListener();
- Prompts.existingBudgetPrompt();
+ ConsoleOutput.existingBudgetPrompt();
String validatedInput = validator.inputIsEitherYOrN(listener.listenForInput());
if(validatedInput.equals("y"))
getUserDirectoryPath(defaultDirectory);
else if(validatedInput.equals("n")){
- Prompts.dontSearchBudgets();
+ ConsoleOutput.dontSearchBudgets();
promptNeedsToClear = false;
}
else
- Prompts.invalidEntryPromptYOrN();
+ ConsoleOutput.invalidEntryPromptYOrN();
}
private void getUserDirectoryPath(String defaultDirectory){
String validatedPath = "ERROR";
BudgetFinder finder = new BudgetFinder();
- Prompts.getLoadDirectoryPath();
+ ConsoleOutput.getLoadDirectoryPath();
while(validatedPath.equals("ERROR")){
InputValidator validator = new InputValidator();
InputListener listener = new InputListener();
String userPath = listener.listenForInput();
validatedPath = validator.defaultDirectoryCheck(userPath, defaultDirectory);
}
- Prompts.searchingDirectory();
+ ConsoleOutput.searchingDirectory();
File[] foundBudgets = finder.findBudgets(validatedPath);
if(foundBudgets.length > 0){
- printBudgets(finder, foundBudgets);
+ finder.printFoundBudgets(foundBudgets);
+ ConsoleOutput.openBudgetPrompt();
+ int index = -1;
+ while(index < 0)
+ index = getBudgetNumberToOpen(foundBudgets);
+ ConsoleOutput.openingSelectedBudget(foundBudgets[index].getName());
+ BudgetBoss.setCurrentBudget(loadBudget(index, foundBudgets));
BudgetBoss.setDefaultDirectory(validatedPath);
TheCreator.notStillBudgetless();
promptNeedsToClear = false;
}
else
- Prompts.noBudgetFound();
+ ConsoleOutput.noBudgetFound();
}
- private void printBudgets(BudgetFinder finder, File[] foundBudgets){
- finder.printFoundBudgets(foundBudgets);
- Prompts.openBudgetPrompt();
+ private int getBudgetNumberToOpen(File[] foundBudgets){
+ InputListener listener = new InputListener();
+ InputValidator validator = new InputValidator();
+ String toCheck = listener.listenForInput();
+ if(validator.validateBudgetSelection(toCheck, foundBudgets))
+ return (Integer.valueOf(toCheck) - 1);
+ else return -1;
+ }
+
+ private Budget loadBudget(int index, File[] foundBudgets){
+ FileInputStream newBudget = null;
+ ObjectInputStream toLoad = null;
+ Object loadedBudget = null;
+ try {
+ newBudget = new FileInputStream(foundBudgets[index].toString());
+ } catch (FileNotFoundException e) {
+ ConsoleOutput.wTF();
+ System.out.println("Error making the FileInputStream");
+ }
+ try {
+ toLoad = new ObjectInputStream(newBudget);
+ } catch (IOException e) {
+ ConsoleOutput.wTF();
+ System.out.println("Error making the ObjectInputStream");
+ }
+ try {
+ loadedBudget = toLoad.readObject();
+ } catch (ClassNotFoundException | IOException e) {
+ ConsoleOutput.wTF();
+ System.out.println("IO exception, maybe the file is bad?");
+ }
+ try {
+ toLoad.close();
+ } catch (IOException e) {
+ ConsoleOutput.wTF();
+ System.out.println("Couldn't close the Object/File input streams.");
+ }
+ return (Budget) loadedBudget;
}
public boolean promptNeedsToClear(){
@@ -5,7 +5,7 @@
import com.VSSBudgetBoss.budget.Budget;
import com.VSSBudgetBoss.cli.InputListener;
import com.VSSBudgetBoss.cli.InputValidator;
-import com.VSSBudgetBoss.cli.Prompts;
+import com.VSSBudgetBoss.cli.ConsoleOutput;
public class Salvation {
@@ -27,12 +27,12 @@ private String askForSaveDirectory(String defaultDirectory){
public void askToSaveBudget(String fileName, Budget budget, String pathToSalvation){
InputValidator validator = new InputValidator();
- Prompts.pathToSalvationCheck(pathToSalvation);
+ ConsoleOutput.pathToSalvationCheck(pathToSalvation);
String validatedInput = validator.inputIsEitherYOrN(getInput());
if(validatedInput.equals("y"))
saveOnExit(fileName, budget, pathToSalvation);
else if(validatedInput.equals("n")){
- Prompts.getSaveDirectoryPath(pathToSalvation);
+ ConsoleOutput.getSaveDirectoryPath(pathToSalvation);
pathToSalvation = askForSaveDirectory(pathToSalvation);
saveOnExit(fileName, budget, pathToSalvation);
}
@@ -9,25 +9,25 @@
private static boolean budgetless = true;
public void bestMakeABudgetNow(){
- Prompts.createNewBudgetPrompt();
+ ConsoleOutput.createNewBudgetPrompt();
InputValidator validator = new InputValidator();
InputListener listener = new InputListener();
String validatedInput = validator.inputIsEitherYOrN(listener.listenForInput());
if(validatedInput.equals("y"))
getBudgetName();
else if(validatedInput.equals("n")){
- Prompts.fuckThisProgram();
+ ConsoleOutput.fuckThisProgram();
budgetless = false;
}
else
- Prompts.invalidEntryPromptYOrN();
+ ConsoleOutput.invalidEntryPromptYOrN();
}
private void getBudgetName(){
InputListener listener = new InputListener();
- Prompts.getBudgetNamePrompt();
+ ConsoleOutput.getBudgetNamePrompt();
String desiredName = listener.listenForInput();
- Prompts.creatingBudget(desiredName);
+ ConsoleOutput.creatingBudget(desiredName);
Budget newBudget = new Budget(desiredName);
BudgetBoss.setCurrentBudget(newBudget);
budgetless = false;
@@ -28,18 +28,20 @@ public static void main(String args[]){
String currentUser = System.getProperty("user.name");
defaultDirectory = "/home/" + currentUser + "/Documents/";
- Prompts.welcomeToBudgetBoss();
+ ConsoleOutput.welcomeToBudgetBoss();
while(opener.promptNeedsToClear())
opener.askToOpenBudget(defaultDirectory);
while(TheCreator.isSlackingOnFinances())
god.bestMakeABudgetNow();
+ //System.out.println("Confirm correct Budget loaded:" + currentBudget.getName());
+
try{
savior.askToSaveBudget(currentBudget.getName(), currentBudget, defaultDirectory);
}catch (NullPointerException e){
- Prompts.noBudgetToSave();
+ ConsoleOutput.noBudgetToSave();
}
}
}

0 comments on commit b355332

Please sign in to comment.