diff --git a/src/main/java/seedu/trippie/Storage.java b/src/main/java/seedu/trippie/Storage.java index 9a9c39d483..631379d239 100644 --- a/src/main/java/seedu/trippie/Storage.java +++ b/src/main/java/seedu/trippie/Storage.java @@ -1,4 +1,130 @@ package seedu.trippie; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; +import java.util.Scanner; + public class Storage { + + + public void setup(PlaceList placeList, ExpenseList expenseList) { + File file = new File("trippie.txt"); + Scanner readFile = Storage.startFile(file); + assert readFile != null; + Storage.loadList(readFile, placeList, expenseList); + } + + public static Scanner startFile(File file) { + try { + if (file.createNewFile()) { + System.out.println("I can't find a file in your directory :("); + System.out.println("I created a new Trippie.txt file for you!"); + } else if (!file.createNewFile()) { + System.out.println("I found a file in your directory!\nSetting up file over here..."); + } + return new Scanner(file); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + return null; + } + } + + public void saveList(PlaceList placeList, ExpenseList expenseList) throws IOException { + FileWriter fileWriter = new FileWriter("trippie.txt"); + fileWriter.write("This file shows your saved trip under Trippie!" + System.lineSeparator() + + System.lineSeparator()); + + List places = placeList.getPlaceList(); + List expenses = expenseList.getExpenseList(); + savePlaceList(fileWriter, places); + saveExpenseList(expenseList, fileWriter, expenses); + + + fileWriter.close(); + } + + private void saveExpenseList(ExpenseList expenseList, FileWriter fileWriter, List expenses) + throws IOException { + if (expenses.size() == 0) { + fileWriter.write("There is currently nothing in your Expense list."); + } else { + fileWriter.write("These are your expenses!" + System.lineSeparator()); + fileWriter.write("Day | Item | Cost" + System.lineSeparator()); + for (Expense expense : expenses) { + fileWriter.write(expense.getExpenseDayBought() + " | " + expense.getExpenseName() + + " | $" + expense.getExpenseCost() + System.lineSeparator()); + } + } + + Float pricing = expenseList.getBudgetValue(); + if (pricing != null) { + fileWriter.write(System.lineSeparator() + "Total budget: $" + String.format("%.2f", pricing) + + System.lineSeparator()); + } else { + fileWriter.write(System.lineSeparator() + "Total budget has not been set" + System.lineSeparator()); + } + + } + + private void savePlaceList(FileWriter fileWriter, List places) throws IOException { + + if (places.size() == 0) { + fileWriter.write("Please add your itinerary!"); + } else { + fileWriter.write("Here is your itinerary! Enjoy your trip :)" + System.lineSeparator()); + fileWriter.write("Day | Start Time | End Time | Place" + System.lineSeparator()); + int maxDay = places.get(places.size() - 1).getPlaceDay(); + for (int i = 1; i <= maxDay; i++) { + for (Place place : places) { + if (place.getPlaceDay() == i) { + fileWriter.write(place.getPlaceDay() + " | " + + String.format("%04d", place.getPlaceStartTime()) + " | " + + String.format("%04d", place.getPlaceEndTime()) + " | " + + place.getPlaceName() + System.lineSeparator()); + } + } + } + } + fileWriter.write(System.lineSeparator()); + + } + + public static void loadList(Scanner readFile, PlaceList placeList, ExpenseList expenseList) { + List places = placeList.getPlaceList(); + List expenses = expenseList.getExpenseList(); + while (readFile.hasNext()) { + String line = readFile.nextLine(); + if (line.contains("Day | Start Time | End Time | Place")) { + String input; + input = readFile.nextLine(); + do { + String[] placeParameters = input.split(" \\| "); + places.add(new Place(placeParameters[3], Integer.parseInt(placeParameters[0]), + Integer.parseInt(placeParameters[1]), Integer.parseInt(placeParameters[2]))); + input = readFile.nextLine(); + } while (!input.equals("")); + } else if (line.contains("Day | Item | Cost")) { + String input; + input = readFile.nextLine(); + do { + String[] expenseParameters = input.split(" \\| "); + expenses.add(new Expense(expenseParameters[1], expenseParameters[2].substring(1), + expenseParameters[0])); + input = readFile.nextLine(); + } while (!input.equals("")); + } else if (line.contains("Total budget: $")) { + expenseList.setBudgetValue(extractBudgetValue(line)); + } + + } + } + + private static Float extractBudgetValue(String userInput) throws NullPointerException, NumberFormatException { + String budgetValueString = userInput.replace("Total budget: $","").trim(); + return Float.parseFloat(budgetValueString); + } + } diff --git a/src/main/java/seedu/trippie/Trippie.java b/src/main/java/seedu/trippie/Trippie.java index 2afde205ae..84feeb06af 100644 --- a/src/main/java/seedu/trippie/Trippie.java +++ b/src/main/java/seedu/trippie/Trippie.java @@ -2,43 +2,50 @@ import seedu.trippie.command.Command; +import java.io.IOException; + public class Trippie { - //private final Storage storage; private final Ui ui; - private final ExpenseList expenseList; - private final PlaceList placeList; + private ExpenseList expenseList; + private PlaceList placeList; + private final Storage storage; - public Trippie(String filePath) { + public Trippie() { ui = new Ui(); - expenseList = new ExpenseList(); - placeList = new PlaceList(); - // to be implemented with storage - // try { - // expenseList = new ExpenseList(storage.load()); - // placeList = new PlaceList(storage.load()); - // } catch (NullPointerException e) { - // System.out.println("No file detected"); - // expenseList = new ExpenseList(); - // placeList = new PlaceList(); - // } + storage = new Storage(); + try { + expenseList = new ExpenseList(); + placeList = new PlaceList(); + } catch (Exception e) { + System.out.println("No file detected"); + expenseList = new ExpenseList(); + placeList = new PlaceList(); + } } public static void main(String[] args) { - new Trippie("data/trippie.txt").run(); + new Trippie().run(); } public void run() { - ui.greetUser(); - boolean isExit = false; - while (!isExit) { - String fullCommand = ui.readCommand(); - ui.printLine(); - Command c = Parser.parse(fullCommand); - if (c != null) { - c.execute(ui, placeList, expenseList); - isExit = c.isExit(); + try { + ui.greetUser(); + boolean isExit = false; + storage.setup(placeList, expenseList); + while (!isExit) { + String fullCommand = ui.readCommand(); + ui.printLine(); + Command c = Parser.parse(fullCommand); + if (c != null) { + c.execute(ui, placeList, expenseList); + isExit = c.isExit(); + } + storage.saveList(placeList, expenseList); + ui.printLine(); } - ui.printLine(); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); } } } diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 39aed6dc61..10c4e59d87 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -8,6 +8,8 @@ Welcome to Travel made easy _________________________________________________________________________ +I can't find a file in your directory :( +I created a new Trippie.txt file for you! >> _________________________________________________________________________ Got it. I've added this place: 1200 - 1400 nus and ntu @@ -143,9 +145,19 @@ __________|____________________________________________________________ | _________________________________________________________________________ >> _________________________________________________________________________ -Noted. I've removed this item from the expenditure list. -Day 1: ice cream - $8.21 -There are 0 items in the list. +_________________________________________________________________________ +Got it! I've added the following item: Day 2: coconut - $5.00 +There are 2 items in the list. +_________________________________________________________________________ +_________________________________________________________________________ +>> _________________________________________________________________________ +_________________________________________________________________________ +Got it! I've added the following item: Day 3: bag - $50.30 +There are 3 items in the list. +_________________________________________________________________________ +_________________________________________________________________________ +>> _________________________________________________________________________ +Successfully set your total budget to 300.0 _________________________________________________________________________ >> _________________________________________________________________________ Bye. Hope to see you again soon! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index fc90f757a2..48f93c68f2 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -11,5 +11,7 @@ list /p buy /i ice cream /c 8.21 /d 1 list /e help -delete /e 1 +buy /i coconut /c 5.00 /d 2 +buy /i bag /c 50.30 /d 3 +budget 300 exit \ No newline at end of file