Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • team13/tdat1006
  • eivindrt/tdat1006
  • alexholt/idatt1002
  • elinang/idatt1002
  • surya/idatt1002
  • kjellit/idata-1002-demo-project
  • eskillr/idatt1002
  • G1-06/idatt-1002-2022-1-06
  • emillaa/idatt1002
  • andreksv/idatt-1002-2023-9
  • melane/idatt-1002-2023-5
  • ramtinfs/idatt1002
  • surya/idatx1005
  • itatt1005_2024_group15/purchase-planner
  • systemutvikling-gruppe-19/idatt-1005-2024-group-19
15 results
Show changes
Showing
with 1674 additions and 117 deletions
package no.ntnu.idatt1002.demo.data.Economics;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.time.LocalDate;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingSelectedBudget;
/**
* FileHandling is a class for writing and reading
* ItemRegister-objects to and from a file.
*
* @author andreas
*/
public class FileHandling{
private static final String filePath = "src/main/resources/budgets/Economics/";
private static final String path = "src/main/resources/";
private static final String fileType = ".register";
private static final String date = "date=";
private static final String description = "description=";
private static final String amount = "amount=";
private static final String isRecurring = "isRecurring=";
private static final String category = "category=";
/**
* Method for writing (adding) an ItemRegister to a file.
*
* @param itemRegister the ItemRegister you want to write to a file.
* @param fileTitle the name of the file you want to check
* @throws IOException if an input or output exception occurred.
*/
public static <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException {
//try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + fileTitle + fileType))) {
if (itemRegister.isEmpty()){
bw.write("");
} else{
bw.write(itemRegister.toString());
}
} catch (IOException ex) {
throw new IOException("Error writing story to file: " + ex.getMessage());
}
}
/**
* Method for checking if a .register file is empty.
*
* @param fileTitle the name of the file you want to check
* @return true or false depending on if file is empty.
* @throws IOException if an input or output exception occurred.
*/
public static boolean isEmpty(String fileTitle) throws IOException {
/*try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
if (br.readLine() == null) {
return true;
} else {
return false;
}
}*/
try (BufferedReader br = new BufferedReader(new FileReader(path + fileTitle + fileType))) {
return br.readLine() == null;
}
}
/**
* Method for reading (getting) an IncomeRegister from a file.
*
* @param fileTitle the name of the file you want to read from.
* @return the IncomeRegister from the file.
* @throws IOException if an input or output exception occurred.
*/
public static IncomeRegister readIncomeRegisterFromFile(String fileTitle) throws IOException {
IncomeRegister incomeRegister = new IncomeRegister();
LocalDate date = null;
String description = "";
double amount = 0;
boolean reoccuring = false;
IncomeCategory incomeCategory = null;
try (BufferedReader br = new BufferedReader(new FileReader(path + fileTitle + fileType))) {
//try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
String line;
String nextLine = br.readLine();
while ((line = nextLine) != null) {
nextLine = br.readLine();
if(line.startsWith(FileHandling.date)) {
date = LocalDate.parse(line.replace(FileHandling.date, ""));
} else if (line.startsWith(FileHandling.description)) {
description = line.replace(FileHandling.description,"");
} else if (line.startsWith(FileHandling.amount)) {
amount = Double.parseDouble(line.replace(FileHandling.amount,""));
} else if (line.startsWith(FileHandling.isRecurring)) {
reoccuring = line.replace(FileHandling.isRecurring,"").equals("Recurring");
} else if (line.startsWith(FileHandling.category)) {
line = line.replace(FileHandling.category,"");
incomeCategory = switch (line) {
case "GIFT" -> IncomeCategory.GIFT;
case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN;
default -> IncomeCategory.SALARY;
};
} if(line.isEmpty() || (nextLine == null)) {
if(description.equals("")){
incomeRegister.addItem(new Income(amount, reoccuring, incomeCategory, date));
} else{
incomeRegister.addItem(new Income(description, amount, reoccuring, incomeCategory, date));
}
description = "";
}
}
}
return incomeRegister;
}
/**
* Method for reading (getting) an ExpenseRegister from a file.
*
* @param fileTitle the name of the file you want to read from.
* @return the ExpenseRegister from the file.
* @throws IOException if an input or output exception occurred
*/
public static ExpenseRegister readExpenseRegisterFromFile(String fileTitle) throws IOException {
ExpenseRegister expenseRegister = new ExpenseRegister();
LocalDate date = null;
String description = "";
double amount = 0;
boolean reoccuring = false;
ExpenseCategory expenseCategory = null;
try (BufferedReader br = new BufferedReader(new FileReader(path + fileTitle + fileType))) {
//try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
String line;
String nextLine = br.readLine();
while ((line = nextLine) != null) {
nextLine = br.readLine();
if (line.startsWith(FileHandling.date)) {
date = LocalDate.parse(line.replace(FileHandling.date, ""));
} else if (line.startsWith(FileHandling.description)) {
description = line.replace(FileHandling.description, "");
} else if (line.startsWith(FileHandling.amount)) {
amount = Double.parseDouble(line.replace(FileHandling.amount, ""));
} else if (line.startsWith(FileHandling.isRecurring)) {
reoccuring = line.replace(FileHandling.isRecurring, "").equals("Recurring");
} else if (line.startsWith(FileHandling.category)) {
line = line.replace(FileHandling.category, "");
expenseCategory = switch (line) {
case "FOOD" -> ExpenseCategory.FOOD;
case "CLOTHES" -> ExpenseCategory.CLOTHES;
case "BOOKS" -> ExpenseCategory.BOOKS;
default -> ExpenseCategory.OTHER;
};
}
if (line.isEmpty() || (nextLine == null)) {
if (description.equals("")) {
expenseRegister.addItem(new Expense(amount, reoccuring, expenseCategory, date));
} else {
expenseRegister.addItem(new Expense(description, amount, reoccuring, expenseCategory, date));
}
description = "";
}
}
}
return expenseRegister;
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import java.time.LocalDate;
import java.util.Objects;
/**
* The Income class inherits from the Item class. The Item class additionally has a private field for an
* enum value of IncomeCategory.
*
* @author Hanne-Sofie
*/
public class Income extends Item{
private ObjectProperty<IncomeCategory> category;
/**
* This constructor uses the super constructor to set the fields for 'amount' and 'recurring' and then sets the
* category. A IllegalArgumentException from this constructor if the category is left blank. The description
* field is left to the default blank string.
*
* @param amount The amount of the current income object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the Income belongs to, provided as a value of IncomeCategory.
* @param date The date of the Income at format "dd.mm.yy".
*/
public Income(double amount, boolean recurring, IncomeCategory category, LocalDate date) {
super(amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = new SimpleObjectProperty<>(category);
}
/**
* This constructor uses the super constructor to set the fields for 'amount', 'description' and 'recurring'
* and then sets the category. A IllegalArgumentException from this constructor if the category is left blank.
* @param description A description of the income as a String.
* @param amount The amount of the current income object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the income belongs to, provided as a value of IncomeCategory.
* @param date The date of the Income at format "dd.mm.yy".
*/
public Income(String description, double amount, boolean recurring, IncomeCategory category, LocalDate date) {
super(description, amount, recurring, date);
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category = new SimpleObjectProperty<>(category);
}
public ObjectProperty<IncomeCategory> incomeCategoryObjectProperty() {
return this.category;
}
/**
* The method returns the category to which the Income belongs as a value of the IncomeCategory enum.
* @return The category to which the Income belongs to as a value of the IncomeCategory enum.
*/
public IncomeCategory getCategory() {
return category.get();
}
/**
* The method sets the category of an item to a value of the Category enum class.
* @param category The category to which the Income belongs to as a value of the IncomeCategory enum.
*/
public void setCategory(IncomeCategory category) {
if(category == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category.set(category);
}
/**
* Returns a String that represents the Income.
* Also includes the IncomeCategory
* @return The Income as a String
*/
@Override
public String toString() {
return super.toString()+"category="+category.get()+"\n\n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Income income)) return false;
if (!super.equals(o)) return false;
return Objects.equals(category.get(), income.category.get());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), category);
}
}
package no.ntnu.idatt1002.demo.data.Economics;
public enum IncomeCategory {
SALARY("salary"),
STUDENT_LOAN("student loan"),
GIFT("gift");
public final String label;
IncomeCategory(String label) {
this.label = label;
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;
/**
* IncomeRegister is a class for
* storing Income. Subclass of
* ItemRegister.
*
* @author andreas
*/
public class IncomeRegister extends ItemRegister<Income>{
/**
* Class constructor that creates an empty List for storing Income.
*/
public IncomeRegister(){
super();
}
/**
* Class constructor that takes in a List of Income as argument.
*
* @param income the List of Income you want to register.
*/
public IncomeRegister(List<Income> income){
super(income);
}
/**
* Method for getting every Income
* in a given IncomeCategory.
*
* @param category the IncomeCategory you want to get every Income of.
* @return a new IncomeRegister of every Income with category.
*/
public IncomeRegister getIncomeByCategory(IncomeCategory category){
return new IncomeRegister(items.stream()
.filter(income -> income.getCategory().compareTo(category) == 0)
.toList());
}
/**
* Get the Income that are registered with a date corresponding to the given month and year.
* @param yearMonth month and year for which to withdraw T's=(Income or Expenses).
* @return list of Income for a certain month and year.
*/
public IncomeRegister getIncomeByMonth(YearMonth yearMonth) {
return new IncomeRegister(items.stream()
.filter(income -> YearMonth.from(income.getDate()).equals(yearMonth))
.collect(Collectors.toList()));
}
/**
* Get the Income that are recurring.
*
* @return a new IncomeRegister of recurring Income.
*/
public IncomeRegister getRecurringIncome(){
return new IncomeRegister(items.stream()
.filter(Item::isRecurring).toList());
}
}
package no.ntnu.idatt1002.demo.data.Economics;
import java.time.LocalDate;
import java.util.Objects;
import javafx.beans.property.*;
/**
* The Item class represents a good or service purchased in real life. The item belongs to a category and
* has an amount, description and a date. The description may be left blank, but the Item must belong to a category and must
* have a price.
* @author HanneSofie
*
*/
public abstract class Item {
private StringProperty description = new SimpleStringProperty("");
private final DoubleProperty amount;
private BooleanProperty recurring;
private ObjectProperty<LocalDate> date; // Format example: 09.08.23
/**
* The constructor of a new Item object takes in an amount as a double. If the amount is left blank, an
* IllegalArgumentException is thrown.
* @param amount price of an Item as a float.
*/
public Item (double amount, boolean recurring, LocalDate date){
if(amount <= 1.0f || date.toString().isBlank()) {
throw new IllegalArgumentException("Both amount and date must be provided.");
} else {
this.amount = new SimpleDoubleProperty(amount);
this.recurring = new SimpleBooleanProperty(recurring);
this.date = new SimpleObjectProperty<>(date);
}
}
/**
* The constructor instantiates a new Item object with a category, a description and a price as arguments. It
* overloads the first constructor to set the category and price and then sets the description of the item.
* If either 0category or price is left blank, an IllegalArgumentException is thrown.
* @param description A description of the item as a String.
* @param amount The price of the item as a float.
*/
public Item (String description, double amount, boolean recurring, LocalDate date){
this(amount, recurring, date);
this.description = new SimpleStringProperty(description);
}
public StringProperty descriptionProperty() {
return this.description;
}
/**
* The method returns the description of the given Item object as a String.
* @return The description of the Item as a String.
*/
public String getDescription() {
return description.get();
}
/**
* The method sets the description of the Item object equal to the passed String. The String may be empty.
* @param description The new description of the Item object as a String.
*/
public void setDescription(String description) {
this.description.set(description);
}
public DoubleProperty amountProperty() {
return this.amount;
}
/**
* The method returns the price of the given Item object as a float.
* @return The amount of the Item as a float.
*/
public double getAmount() {
return amount.get();
}
/**
* The method changes the price of the given Item to the passed price as a float.
* @param amount The new price of the Item as a float.
*/
public void setAmount(double amount) {
if(amount <= 1.0f ) {
throw new IllegalArgumentException("A positive amount must be provided");
}
this.amount.set(amount);
}
/**
* The method returns the BooleanProperty of recurring.
* @return BooleanProperty whose value is either true or false.
*/
public BooleanProperty recurringProperty() {
return this.recurring;
}
/**
* The method returns true if the transaction is recurring, false otherwise.
* @return True if the transaction is a recurring one, false otherwise.
*/
public boolean isRecurring() {
return recurring.get();
}
/**
* The method changes the boolean value of the 'recurring' field of the Item object.
* @param recurring A boolean value being true if the Item is recurring and false otherwise.
*/
public void setRecurring(boolean recurring) {
this.recurring.set(recurring);
}
/**
* The method returns the ObjectProperty of date.
* @return
*/
public ObjectProperty<LocalDate> dateProperty() {
return this.date;
}
/**
* The method returns the date of the item object (income/expense).
* @return The date of the transaction.
*/
public LocalDate getDate() {
return this.date.get();
}
/**
* Sets the date field of the Item object to a new LocalDate provided as an argument.
*
* @param newDate The new date with which to record the Item.
*/
public void setDate(LocalDate newDate) {
this.date.set(newDate);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Item item)) return false;
return Double.compare(item.amount.get(), amount.get()) == 0 && item.recurring.get() == recurring.get() && Objects.equals(description.get(), item.description.get()) && Objects.equals(date.get(), item.date.get());
}
@Override
public int hashCode() {
return Objects.hash(description, amount, recurring, date);
}
/**
* Returns a String that represents the Item
* @return The Item as a String
*/
@Override
public String toString() {
String isRecurring = "";
if(recurring.get()){
isRecurring = "Recurring";
}
else{
isRecurring = "Not recurring";
}
if(!description.get().isBlank()){
return "date=" + date.get() +"\ndescription=" + description.get()+"\namount="+amount.get()+"\nisRecurring="+isRecurring+"\n";
}
else{
return "date="+date.get() +"\namount="+amount.get()+"\nisRecurring="+isRecurring+"\n";
}
}
/* @Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Item otherItem = (Item) obj;
return true;
*//*return Objects.equals(true);*//*
}*/
}
package no.ntnu.idatt1002.demo.data;
import java.util.Date;
import java.util.List;
/**
* This is just a simple Java-bean
* @author nilstes
*/
public class MyEntity {
private String id;
private String name;
public MyEntity() {
}
public MyEntity(String id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package no.ntnu.idatt1002.demo.data.recipes;
import java.util.ArrayList;
import java.util.List;
/**
* The class holds a collection of FoodItem constants in an ArrayList that represents the groceries that the
* user has available in real life. The class provides methods to add and remove food types from the collection
* as well as checking if a given food type is available, i.e. is part of the collection.
*
* @author hannesofie
*/
public class IngredientsAtHand {
private final ArrayList<FoodItem> ingredientsAtHand = new ArrayList<>();
/**
* The method returns the collection of ingredients at hand as an arraylist of FoodItem enum constants.
* @return The collection of food types at hand to the user.
*/
public List<FoodItem> getIngredientsAtHand() {
return ingredientsAtHand;
}
/**
* The method takes in an ingredient object and adds it to the collection of ingredients at hand.
* @param ingredient The food type to add to the collection of ingredients at hand.
*/
public void addIngredient(FoodItem ingredient) {
if(!this.atHand(ingredient) && ingredient != null) {
this.ingredientsAtHand.add(ingredient);
}
}
/**
* The method takes in a constant of the FoodType enum class and checks if it is present in the collection.
* If it is present, true is returned, otherwise false.
* @param foodItem A constant value of the FoodItem enum class to check for in the collection
* of ingredients at hand.
* @return True if the food type is at hand, otherwise false.
*/
public boolean atHand(FoodItem foodItem) {
return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem));
}
/**
* The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of
* ingredients at hand if it exists and returns true. If no ingredient of the given type was found in the
* collection, false is returned.
* @param ingredientType What type of food to remove from the list of ingredients at hand.
* @return True if the ingredient was found among the ingredients at hand and removed, false otherwise.
*/
public boolean removeIngredient(FoodItem ingredientType) {
return ingredientsAtHand.remove(ingredientType);
}
}
This diff is collapsed.
This diff is collapsed.