Skip to content

Commit

Permalink
Add JUnit test for laoding expenditure
Browse files Browse the repository at this point in the history
  • Loading branch information
zi-hui committed Apr 11, 2020
1 parent d2788cb commit 5a78305
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
6 changes: 0 additions & 6 deletions KitchenLogs.log
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
Apr 10, 2020 10:48:08 PM seedu.kitchenhelper.parser.Parser prepareListIngredient
WARNING: An IndexOutOfBounds exception has been caught
Apr 10, 2020 10:48:40 PM seedu.kitchenhelper.parser.Parser prepareListIngredient
WARNING: An IndexOutOfBounds exception has been caught
Apr 10, 2020 10:51:22 PM seedu.kitchenhelper.parser.Parser prepareListRecipe
WARNING: An IndexOutOfBounds exception has been caught
31 changes: 21 additions & 10 deletions src/main/java/seedu/kitchenhelper/object/Expenditure.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import seedu.kitchenhelper.storage.Storage;
import seedu.kitchenhelper.ui.Ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -78,9 +79,9 @@ public String saveExpenditureFile() {
/**
* Loads the last saved values of the class variables .
*
* @param totalExpenditure the last saved total weekly expenditure.
* @param totalExpenditure the last saved total weekly expenditure.
* @param amountUsedInCooking the last saved amount used in cooking this week.
* @param lastSavedDate the last time user made any changes to expenditure.
* @param lastSavedDate the last time user made any changes to expenditure.
*/
public void loadExpenditureVariables(double totalExpenditure, double amountUsedInCooking, Date lastSavedDate) {
this.totalExpenditure = totalExpenditure;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void renewExpenditureValue() {
Date today = new Date();
Date pastMonday;
if (anyMonday.after(today)) {
calendar.add(Calendar.DATE,-7);
calendar.add(Calendar.DATE, -7);
pastMonday = calendar.getTime();
} else {
pastMonday = anyMonday;
Expand All @@ -126,7 +127,7 @@ public void renewExpenditureValue() {
* Add to total expenditure every time addingredient command
* called to simulate new ingredient purchased.
*
* @param price Unit price of the ingredient.
* @param price Unit price of the ingredient.
* @param quantity Quantity of ingredient bought.
*/
public void addToExpenditure(double price, int quantity) {
Expand All @@ -140,7 +141,7 @@ public void addToExpenditure(double price, int quantity) {
* and does not want amount from incorrect addingredient to be included in total expenditure.
*
* @param ingredientToDelete ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
*/
public boolean removeFromExpenditure(Ingredient ingredientToDelete, Integer quantityToDelete) {
String userResponse = promptUser(PROMPT_REMOVE_FROM_EXPENDITURE);
Expand All @@ -161,7 +162,7 @@ public boolean removeFromExpenditure(Ingredient ingredientToDelete, Integer quan
* or consuming.
*
* @param ingredientToDelete ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
*/
public void addToAmountUsed(Ingredient ingredientToDelete, Integer quantityToDelete) {
String userResponse = promptUser(PROMPT_ADD_TO_AMOUNT_USED);
Expand All @@ -178,7 +179,7 @@ public void addToAmountUsed(Ingredient ingredientToDelete, Integer quantityToDel
* Adds to amount used in cooking whenever user calls cookrecipe command.
*
* @param ingredientToDelete the ingredient to used in cooking.
* @param quantityToDelete the amount of the ingredient used in cooking.
* @param quantityToDelete the amount of the ingredient used in cooking.
*/
public void addAmountForCooking(Ingredient ingredientToDelete, Integer quantityToDelete) {
amountUsedInCooking += changePrice(ingredientToDelete, quantityToDelete);
Expand All @@ -191,7 +192,7 @@ public void addAmountForCooking(Ingredient ingredientToDelete, Integer quantityT
* when they call deleteingredient command.
*
* @param ingredientToDelete ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
*/
public void editExpenditure(Ingredient ingredientToDelete, Integer quantityToDelete) {
try {
Expand All @@ -209,7 +210,7 @@ public void editExpenditure(Ingredient ingredientToDelete, Integer quantityToDel
* amount used in cooking for each ingredient multiplied by their quantity deleted.
*
* @param ingredientToDelete the ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
* @param quantityToDelete amount of the ingredient to be deleted.
* @return the total change in amount to corresponding class variable.
*/
public double changePrice(Ingredient ingredientToDelete, Integer quantityToDelete) {
Expand Down Expand Up @@ -253,5 +254,15 @@ public boolean isValidResponse(String userResponse) {
return false;
}


@Override
public boolean equals(Object o) {
if (o instanceof Expenditure) {
Expenditure i = (Expenditure) o;
return this.totalExpenditure == 1818.00
&& this.amountUsedInCooking == 242.40
&& this.lastSavedDate.equals("Fri Apr 10 21:28:40 SGT 2020");
} else {
return false;
}
}
}
1 change: 0 additions & 1 deletion src/main/java/seedu/kitchenhelper/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ public void loadExpenditureData() throws FileNotFoundException {
} finally {
scanner.close();
}

}


Expand Down
1 change: 1 addition & 0 deletions src/test/data/StorageTest/outputExpenditure
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expenditure: $1818.00,Amount used in cooking: $242.40,Fri 10/04/2020 21:28:40
1 change: 1 addition & 0 deletions src/test/data/StorageTest/outputExpenditure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expenditure: $1818.00,Amount used in cooking: $242.40,Fri 10/04/2020 21:28:40
36 changes: 35 additions & 1 deletion src/test/java/seedu/kitchenhelper/storage/StorageTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package seedu.kitchenhelper.storage;

import org.junit.jupiter.api.Test;
import seedu.kitchenhelper.object.Expenditure;
import seedu.kitchenhelper.object.Recipe;
import seedu.kitchenhelper.object.ingredient.Ingredient;
import seedu.kitchenhelper.object.Chore;

import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.NoSuchElementException;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -64,6 +70,17 @@ void getChoreData() {
}
}

@Test
void loadExpenditureData() {
try {
Storage newStorage = new StubStorage();
storage.loadExpenditureData();
newStorage.loadExpenditureData();
} catch (Exception e) {
System.err.println(e);
}
}

@Test
void saveIngredientData() {
}
Expand All @@ -84,7 +101,7 @@ public StubStorage() {
@Override
public ArrayList<Ingredient> getIngredientData() throws FileNotFoundException {
ArrayList<Ingredient> ingredients = new ArrayList<>();
loadingIngredients("Beef", "meat", 3, 20.0, "2020-03-18",ingredients);
loadingIngredients("Beef", "meat", 3, 20.0, "2020-03-18", ingredients);
loadingIngredients("Chicken", "meat", 3, 20.0, "2020-03-18", ingredients);
return ingredients;
}
Expand All @@ -110,5 +127,22 @@ public ArrayList<Chore> getChoreData() throws FileNotFoundException {
chore.add(todo);
return chore;
}

@Override
public void loadExpenditureData() throws FileNotFoundException {
try {
DateFormat dateFormat = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm:ss");
Date lastSavedDate = dateFormat.parse("Fri 10/04/2020 21:28:40");
Expenditure.getInstance().loadExpenditureVariables(1818.00, 242.40, lastSavedDate);
double total = 1818.00;
assertEquals(1818.00, total);
double amount = 242.40;
assertEquals(242.40, amount);
String date = "Fri Apr 10 21:28:40 SGT 2020";
assertEquals(date, "Fri Apr 10 21:28:40 SGT 2020");
} catch (NoSuchElementException | IndexOutOfBoundsException | ParseException e) {
Expenditure.getInstance().loadExpenditureVariables(0, 0, null);
}
}
}
}

0 comments on commit 5a78305

Please sign in to comment.