Skip to content

Commit

Permalink
Merge pull request #33 from weishuangtan/master
Browse files Browse the repository at this point in the history
Implement save into text file
  • Loading branch information
tengkianen authored Oct 13, 2020
2 parents 43aa0f1 + 0e0b637 commit f600dbc
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 30 deletions.
126 changes: 126 additions & 0 deletions src/main/java/seedu/trippie/Storage.java
Original file line number Diff line number Diff line change
@@ -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<Place> places = placeList.getPlaceList();
List<Expense> expenses = expenseList.getExpenseList();
savePlaceList(fileWriter, places);
saveExpenseList(expenseList, fileWriter, expenses);


fileWriter.close();
}

private void saveExpenseList(ExpenseList expenseList, FileWriter fileWriter, List<Expense> 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<Place> 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<Place> places = placeList.getPlaceList();
List<Expense> 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);
}

}
59 changes: 33 additions & 26 deletions src/main/java/seedu/trippie/Trippie.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
18 changes: 15 additions & 3 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down
4 changes: 3 additions & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f600dbc

Please sign in to comment.