Skip to content

Commit

Permalink
Merge pull request #64 from ucsd-cse110-fa23/(Feature-4)-bug-a
Browse files Browse the repository at this point in the history
  • Loading branch information
Tantime authored Nov 21, 2023
2 parents be2705e + cee683a commit 9a38d32
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
11 changes: 0 additions & 11 deletions app/src/main/java/cse/gradle/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,14 @@ public void start(Stage primaryStage) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
ArrayList<Recipe> arrayList = new ArrayList<Recipe>();


try {
ArrayList<Recipe> rList = (ArrayList<Recipe>)objectMapper.readValue(response, new TypeReference<List<Recipe>>() {});
arrayList = new ArrayList<Recipe>(rList);
} catch (Exception e) {
System.out.println("Error reading JSON from server on startup");
}



// RecipeList recipeList = new RecipeList(arrayList);
//AppFramePopUp appFramePopUp = new AppFramePopUp(recipeList, recipe);



// DisplayRecipe.DisplayRecipe(appFramePopUp);

// Setting the Layout of the Window- Should contain a Header, Footer and the TaskList
//RecipeList root = recipeList;
AppScenes appScenes = new AppScenes(primaryStage, arrayList);
RecipeList recipeList = new RecipeList(appScenes, arrayList);

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/cse/gradle/AppScenes.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ void setListeners(){
//this.appScenes.displayScene(this.nextScene);
//this.appScenes.getRecipeListRoot().addButton(new RecipeGenerator().generateNewRecipe());
Recipe newRecipe = new RecipeGenerator().generateNewRecipe();
this.appScenes.getRecipeListRoot().addButton(newRecipe);
this.appScenes.getRecipeListRoot().getRecipes().add(newRecipe);
this.appScenes.getRecipeListRoot().addButton(0, newRecipe);
this.appScenes.getRecipeListRoot().getRecipes().add(0, newRecipe);
//this.appScenes.getRecipeListRoot().addButton(new Recipe("a","b","c","d"));
this.appScenes.displayScene(this.cancelScene);
});
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/cse/gradle/RecipeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public RecipeList(List<Recipe> list) {
recipes = list;
}

// adds button to the end of button array
public void addButton(Recipe r){
Button b = new Button(r.getName());
System.out.println("new recipe name: " + r.getName());
Expand All @@ -107,6 +108,20 @@ public void addButton(Recipe r){
vBox.getChildren().add(b);
}

// overloaded method for adding a button at a specific index
public void addButton(int index, Recipe r){
Button b = new Button(r.getName());
System.out.println("new recipe name: " + r.getName());
buttons.add(index, b);
b.setPrefSize(500, 20);
b.setStyle("-fx-background-color: #DAE5EA; -fx-border-width: 1; -fx-border-color: #737778;"); // sets style of button
b.setOnAction(e ->{
DisplayRecipe.DisplayRecipe(new AppFramePopUp(this, r));
});
//add button to vBox
vBox.getChildren().add(index, b);
}

public void removeButton(Recipe r){
this.refresh();
for(Button button: buttons){
Expand Down
24 changes: 3 additions & 21 deletions app/src/main/java/cse/gradle/Server/LocalDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,9 @@ public static Recipe removeNewLines(Recipe r) {
public static void saveRecipeToLocal(Recipe recipe) {
recipe = removeNewLines(recipe);

if ((new File("src/main/java/cse/gradle/Server/recipes.csv")).exists()) {
try {
FileWriter writer = new FileWriter("src/main/java/cse/gradle/Server/recipes.csv", true);
writer.write(recipe.getIngredients() + ";" + recipe.getInstructions() + ";" + recipe.getCategory() + ";"
+ recipe.getName() + ";" + recipe.getId() + "\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
FileWriter writer = new FileWriter("src/main/java/cse/gradle/Server/recipes.csv", true);
writer.write(
"Ingredients" + ";" + "Instructions" + ";" + "Category" + ";" + "Name" + ";" + "Id" + "\n");
writer.write(recipe.getIngredients() + ";" + recipe.getInstructions() + ";" + recipe.getCategory() + ";"
+ recipe.getName() + ";" + recipe.getId() + "\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ArrayList<Recipe> newRecipes = (ArrayList<Recipe>)LocalDatabase.readLocal();
newRecipes.add(0, recipe);
LocalDatabase.saveListToLocal(newRecipes);
}

public static void deleteRecipeFromLocal(Recipe recipeFromServer) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/test/java/cse/gradle/Feature8Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ void updateEditDeleteCSVTest() {
Model model = new Model();
String response = model.performRequest("POST", null, originalRecipe);
List<Recipe> readRecipes = LocalDatabase.readLocal();
assertEquals(Recipe.equals(originalRecipe, readRecipes.get(readRecipes.size() - 1)), true);
assertEquals(Recipe.equals(originalRecipe, readRecipes.get(0)), true);

// update recipe and put request it, then check the csv file to see if it was updated properly
originalRecipe.setIngredients("tomaotes");
response = model.performRequest("PUT", originalRecipe.getId().toString(), originalRecipe);
readRecipes = LocalDatabase.readLocal();
assertEquals(Recipe.equals(originalRecipe, readRecipes.get(readRecipes.size() - 1)), true);
assertEquals(Recipe.equals(originalRecipe, readRecipes.get(0)), true);

// delete recipe and delete request it, then check the csv file to see if it was deleted properly
response = model.performRequest("DELETE", originalRecipe.getId().toString(), null);
Expand All @@ -114,7 +114,7 @@ void getAllTest() throws JsonMappingException, JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
ArrayList<Recipe> rList2 = (ArrayList<Recipe>)objectMapper.readValue(response, new TypeReference<List<Recipe>>() {});
for (int i = 0; i < rList2.size(); i++) {
assertEquals(true, Recipe.equals(rList.get(i), rList2.get(i)));
assertEquals(true, Recipe.equals(rList.get(i), rList2.get(rList2.size()-1-i)));
model.performRequest("DELETE", rList.get(i).getId().toString(), null);
}
}
Expand Down

0 comments on commit 9a38d32

Please sign in to comment.