Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hey, it compiles #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions app/src/main/java/de/buw/se4de/Ingredient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
package de.buw.se4de;

// The intent of this class is to allow us to dynamically adjust the amount of ingredients needed to create a recipe
// This way the user can simply enter how many portions they would like, and the program adjusts the amounts accordingly

// IMPORTANT: for conversion to work, ingredients in csv must be written in 'amount/unit/name;' format and
// edited ingredients must be written in 'amountunit name' format
public class Ingredient {
int amount;
float amount;
String unit;
String name;
}


// allows saving and parsing of ingredient names and amounts

public Ingredient(float amount, String unit, String name) {
this.amount = amount;
this.unit = unit;
this.name = name;
}
public String toString(){ // creates string for printing in jpanels
return amount + unit + " " + name;
}
public String toSaveString() { // String stores amount units and name in 'amount/unit/name;' format for csv saving ex.: '4/cups/flower'
return amount + "/" + unit + "/" + name + ";";
}

public void adjustAmount(float desiredServings, float recipeServings) { // we pass the amount the user desires, as well as the servings the original recipe can generate into the function
// just to be safe we ensure that the user wants a non-zero number of servings
if (desiredServings != 0.0f) {
float singleServing = amount / recipeServings;
amount = singleServing * desiredServings;
}
}
}
36 changes: 34 additions & 2 deletions app/src/main/java/de/buw/se4de/Rezept.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
package de.buw.se4de;

import java.util.ArrayList;

public class Rezept {
String name;
String[] zutaten;
//String[] zutaten;
ArrayList<Ingredient> ingredients;
String personen;
String[] kategorien;
String zeit;
String zubereitung;
String rating = "Unbewertet"; // allows recipes to be rated

public Rezept(String name, String[] zutaten, String personen, String[] kategorien, String zeit, String zubereitung) {
/*public Rezept(String name, String[] zutaten, String personen, String[] kategorien, String zeit, String zubereitung) {
this.name = name;
this.zutaten = zutaten;
this.personen = personen;
this.kategorien = kategorien;
this.zeit = zeit;
this.zubereitung = zubereitung;
this.rating = "Unbewertet";
}*/

public Rezept(String name, ArrayList<Ingredient> zutaten, String personen, String[] kategorien, String zeit, String zubereitung) {
this.name = name;
this.ingredients = zutaten;
this.personen = personen;
this.kategorien = kategorien;
this.zeit = zeit;
this.zubereitung = zubereitung;
this.rating = "Unbewertet";
}

/*
public Rezept(String name, Ingredient[] zutaten, String personen, String[] kategorien, String zeit, String zubereitung) {
this.name = name;
this.zutaten = zutaten;
this.personen = personen;
Expand All @@ -18,7 +42,15 @@ public Rezept(String name, String[] zutaten, String personen, String[] kategorie
this.zubereitung = zubereitung;
this.rating = "Unbewertet";
}
*/

public String toSaveString() {
String allIngredients = "";
for (Ingredient ingredient : ingredients) {
allIngredients.concat(ingredient.toSaveString());
}
return allIngredients;
}
// Wir brauchen die toString() Methode um die Namen im Listenmodus für den Export anzuzeigen
public String toString(){
return name;
Expand Down
42 changes: 37 additions & 5 deletions app/src/main/java/de/buw/se4de/Rezeptbuch.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.CSVPrinter;
import org.checkerframework.checker.units.qual.A;

public class Rezeptbuch {

Expand Down Expand Up @@ -55,6 +56,7 @@ JFrame init() {
// nimmt den pfad zu einer CSV Datei und gibt eine Liste mit Rezepten zurück
ArrayList<Rezept> load(String pfad) {
ArrayList<Rezept> temp = new ArrayList<Rezept>();
ArrayList<Ingredient> zutaten = new ArrayList<>();
try (Reader reader = Files.newBufferedReader(Paths.get(pfad), StandardCharsets.UTF_8);
@SuppressWarnings("deprecation")
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());) {
Expand All @@ -66,7 +68,15 @@ ArrayList<Rezept> load(String pfad) {
String zeit = csvRecord.get("Zeit");
String zubereitung = csvRecord.get("Zubereitung").replaceAll(";", "\n");

String[] zutaten = zutatenVoll.split(";");
String[] zutatenString = zutatenVoll.split(";");

// we go through the strings in the csv file and parse out the amount, units and names of all ingredients and add them to our list
for (String ingredientString : zutatenString) {
String[] ingredientData = ingredientString.split("\\/");
int value = Integer.parseInt(ingredientData[0]);
Ingredient tempIngredient = new Ingredient(value, ingredientData[1], ingredientData[2]);
zutaten.add(tempIngredient);
}
String[] kategorien = kategorienVoll.split(";");

Rezept rezept = new Rezept(name, zutaten, personen, kategorien, zeit, zubereitung);
Expand Down Expand Up @@ -210,7 +220,7 @@ public void windowClosing(WindowEvent e) {

//wenn Neu-Button gedrückt wird, erstellt man ein neues Rezept im Rezeptbuch
neuButton.addActionListener(e -> {
Rezept neu = new Rezept("neu", new String[]{"Zutaten"}, "0", new String[]{"Kategorien"}, "00:00:00", "Zubereitung");
Rezept neu = new Rezept("neu", new ArrayList<Ingredient>(), "0", new String[]{"Kategorien"}, "00:00:00", "Zubereitung");

ArrayList<Rezept> rezepte_temp = new ArrayList<Rezept>();
rezepte_temp.add(neu);
Expand Down Expand Up @@ -290,7 +300,7 @@ public void windowClosing(WindowEvent e) {
personenField.setText(rezept.personen);
zeitField.setText(rezept.zeit);
zubereitungArea.setText(rezept.zubereitung);
zutatenArea.setText(String.join("\n", rezept.zutaten));
zutatenArea.setText(String.join("\n", rezept.ingredients.toString()));
kategorienArea.setText(String.join(", ", rezept.kategorien));

rezeptPanel.add(nameLabel);
Expand Down Expand Up @@ -375,7 +385,29 @@ public void windowClosing(WindowEvent e) {

rezept.kategorien = kategorienArea.getText().split(", ");

rezept.zutaten = zutatenArea.getText().split("\n");
String[] temp = zutatenArea.getText().split("\n"); //

for (String ingredientString : temp) {
// gets length of integers at beginning of string, so that we can parse out amount + units
// example: 400g Mehl
int res = 0;
// this loop will check how many of the first n characters are part of an integer and return the number
// example: res = 400 at the end of the loop
for (int i=0; i < ingredientString.length(); i++) {
char c = ingredientString.charAt(i);
if (c < '0' || c > '9') continue;
res = res * 10 + (c - '0');
}
// we split the string at all empty spaces to separate the amount+unit from the name part
// example: substrings would be '400g' + 'Mehl'
String[] subIngredients = ingredientString.split(" ");
// We create a new ingredient to add to the recipe, with the following
rezept.ingredients.add(new Ingredient(res, // the number at the beginning of the first string, in our example '400'
ingredientString.substring(String.valueOf(res).length(),subIngredients[0].length()-1), // we add the substring of the amount+unit that denotes the unit.
// we do this by creating a substring of '400g' starting at the end of the length of the integer previously determined, up to the end of the actual string
// essentially everything past the last '0' in 400 and up to and including the 'g'. If someone were to write '400gramm' the entire word 'gramm' would be parsed
subIngredients[1])); // the second part of the split string, which would be 'Mehl'
}

save("./app/src/main/resources/rezeptebuch_LIVE.csv", rezepte);
});
Expand Down Expand Up @@ -491,7 +523,7 @@ ArrayList<String[]> getRecords(ArrayList<Rezept> rez) {
records.add(header);

for (Rezept rezept : rez) {
String[] record = {rezept.name, String.join(";", rezept.zutaten), rezept.personen, String.join(";", rezept.kategorien), rezept.zeit, rezept.zubereitung.replaceAll("\n", ";"), rezept.rating};
String[] record = {rezept.name, String.join(";", rezept.toSaveString()), rezept.personen, String.join(";", rezept.kategorien), rezept.zeit, rezept.zubereitung.replaceAll("\n", ";"), rezept.rating};
records.add(record);
}

Expand Down