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
Commits on Source (113)
Showing
with 1083 additions and 65 deletions
##############################
## Java
##############################
.mtj.tmp/
*.class
*.jar
*.war
*.ear
*.nar
##############################
## Maven
##############################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom.xml.bak
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
##############################
## IntelliJ
##############################
out/
.idea/*
*.iml
*.ipr
*.iws
##############################
## Database
##############################
*.db
*.DS_Store
......@@ -88,7 +88,7 @@ loadScripts(document, 'script');</script>
<h1 title="Class MyApp" class="title">Class MyApp</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">no.ntnu.idatt1002.demo.MyApp</div>
<div class="inheritance">no.ntnu.idatt1002.demo.view.MyApp</div>
</div>
<section class="class-description" id="class-description">
<hr>
......
......@@ -2,7 +2,7 @@
<html lang="en">
<head>
<!-- Generated by javadoc (19) on Thu Feb 02 10:19:17 CET 2023 -->
<title>Uses of Class no.ntnu.idatt1002.demo.MyApp (demo 1.0-SNAPSHOT API)</title>
<title>Uses of Class no.ntnu.idatt1002.demo.view.MyApp (demo 1.0-SNAPSHOT API)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="dc.created" content="2023-02-02">
......@@ -50,9 +50,9 @@ loadScripts(document, 'script');</script>
<div class="flex-content">
<main role="main">
<div class="header">
<h1 title="Uses of Class no.ntnu.idatt1002.demo.MyApp" class="title">Uses of Class<br>no.ntnu.idatt1002.demo.MyApp</h1>
<h1 title="Uses of Class no.ntnu.idatt1002.demo.view.MyApp" class="title">Uses of Class<br>no.ntnu.idatt1002.demo.view.MyApp</h1>
</div>
No usage of no.ntnu.idatt1002.demo.MyApp</main>
No usage of no.ntnu.idatt1002.demo.view.MyApp</main>
<footer role="contentinfo">
<hr>
<p class="legal-copy"><small>Copyright &#169; 2023. All rights reserved.</small></p>
......
......@@ -14,8 +14,8 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.8.1</junit.version>
<javafx.version>17.0.1</javafx.version>
<junit.version>5.9.2</junit.version>
<javafx.version>19.0.2.1</javafx.version>
</properties>
<repositories>
......@@ -152,7 +152,7 @@
<archive>
<manifest>
<mainClass>
no.ntnu.idatt1002.demo.MyApp
no.ntnu.idatt1002.demo.view.MyApp
</mainClass>
</manifest>
</archive>
......@@ -172,7 +172,7 @@
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>no.ntnu.idatt1002.demo/no.ntnu.idatt1002.demo.MyApp</mainClass>
<mainClass>no.ntnu.idatt1002.demo.view.MyApp</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
......
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA" serialisationVersion="2">
<checkstyleVersion>10.6.0</checkstyleVersion>
<scanScope>JavaOnly</scanScope>
<option name="thirdPartyClasspath" />
<option name="activeLocationIds" />
<option name="locations">
<list>
<ConfigurationLocation id="bundled-sun-checks" type="BUNDLED" scope="All" description="Sun Checks">(bundled)</ConfigurationLocation>
<ConfigurationLocation id="bundled-google-checks" type="BUNDLED" scope="All" description="Google Checks">(bundled)</ConfigurationLocation>
</list>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/main/main.iml" filepath="$PROJECT_DIR$/main/main.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/src.iml" filepath="$PROJECT_DIR$/.idea/src.iml" />
<module fileurl="file://$PROJECT_DIR$/test/test.iml" filepath="$PROJECT_DIR$/test/test.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
package no.ntnu.idatt1002.demo;
import no.ntnu.idatt1002.demo.view.MyWindow;
/**
* Use this class to start the application
* @author nilstes
*/
public class MyApp {
/**
* Main method for my application
*/
public static void main(String[] args) throws Exception {
MyWindow window = new MyWindow("The Window");
window.setVisible(true);
}
}
package no.ntnu.idatt1002.demo.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Budget.BudgetItem;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
public class AddBudgetController {
BudgetItem newBudgetItem = null;
BudgetItem chosenBudgetItem = null;
@FXML
private ComboBox<ExpenseCategory> categoryVariable;
@FXML
private TextField amountVariable;
@FXML
private TextField descriptionVariable;
@FXML
private Button addButton;
@FXML
private Button cancelButton;
@FXML
public void initialize(){
ObservableList<ExpenseCategory> expenseCategories = FXCollections.observableArrayList(
ExpenseCategory.values());
categoryVariable.setItems(expenseCategories);
categoryVariable.setPromptText("Category");
}
public ExpenseCategory getCategory(){
return categoryVariable.getValue();
}
public BudgetItem getNewBudgetItem(){
return this.newBudgetItem;
}
@FXML
public void addBudget(ActionEvent event) {
if(newBudgetItem == null){
ExpenseCategory category = getCategory();
double amount = Double.parseDouble(amountVariable.getText());
String description = descriptionVariable.getText();
newBudgetItem = new BudgetItem(amount, description, category);
}
if(chosenBudgetItem != null){
chosenBudgetItem.setBudgetAmount(Double.parseDouble(amountVariable.getText()));
chosenBudgetItem.setBudgetDescription(descriptionVariable.getText());
chosenBudgetItem.setBudgetCategory(categoryVariable.getValue());
}
final Node source = (Node) event.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
@FXML
public void setBudget(BudgetItem item){
chosenBudgetItem = new BudgetItem(item.getBudgetAmount(), item.getBudgetDescription(), item.getBudgetCategory());
chosenBudgetItem.getAmountProperty().bindBidirectional(item.getAmountProperty());
chosenBudgetItem.getDescriptionProperty().bindBidirectional(item.getDescriptionProperty());
chosenBudgetItem.getCategoryProperty().bindBidirectional(item.getCategoryProperty());
amountVariable.textProperty().set(String.valueOf(item.getBudgetAmount()));
descriptionVariable.textProperty().set(item.getBudgetDescription());
categoryVariable.setValue(item.getBudgetCategory());
}
public void closeButton(ActionEvent actionEvent) {
final Node source = (Node) actionEvent.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
}
package no.ntnu.idatt1002.demo.controller;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
public class AddExpenseController {
@FXML
private TextField dateField;
@FXML
private TextField descriptionField;
@FXML
private TextField amountField;
@FXML
public void initialize() {
}
}
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import no.ntnu.idatt1002.demo.view.DialogMode;
import no.ntnu.idatt1002.demo.view.ExpenseRepository;
public class ExpensesController {
@FXML
private Button add;
@FXML
private Button edit;
@FXML
private Button delete;
@FXML
private ComboBox<?> show;
@FXML
private TableColumn<Expense, Double> amount;
@FXML
private TableColumn<Expense, ExpenseCategory> category;
@FXML
private TableColumn<Expense, String> date;
@FXML
private TableColumn<Expense, String> description;
@FXML
private TableColumn<Expense, Boolean> recurring;
@FXML
private TableView<Expense> expenseTableView;
ObservableList<Expense> expenses = FXCollections.observableArrayList(ExpenseRepository.getExpenses());
@FXML
public void initialize() {
date.setCellValueFactory(new PropertyValueFactory<Expense, String>("date"));
amount.setCellValueFactory(new PropertyValueFactory<Expense, Double>("amount"));
category.setCellValueFactory(new PropertyValueFactory<Expense, ExpenseCategory>("category"));
description.setCellValueFactory(new PropertyValueFactory<Expense, String>("description"));
recurring.setCellValueFactory(new PropertyValueFactory<Expense, Boolean>("recurring"));
expenseTableView.setItems(expenses);
}
@FXML
private void add(ActionEvent event) throws IOException {
// Load the FXML file for the "Add Expense" dialog box
/*FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/AddExpense.fxml"));
DialogPane dialogPane = loader.load();
// Create a new Dialog instance with the loaded AnchorPane
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setTitle("Add Expense");
dialog.getDialogPane().setContent(dialogPane);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
// Get the values from the dialog and create a new Expense object
String description = descriptionField.getText();
double amount = Double.parseDouble(amountField.getText());
ExpenseCategory category = ExpenseCategory.FOOD;//categoryComboBox.getValue();
boolean isRecurring = true;// recurringComboBox.getValue();
String date = dateField.getText();
Expense newExpense = new Expense(description, amount, isRecurring, category, date);
// Add the new expense to the table view
System.out.println(expenses.size());
expenses.add(newExpense);
System.out.println(expenses.size());
expenseTableView.setItems(expenses);
}*/
}
public void edit(ActionEvent event) throws IOException {
/*
Expense expense = null;
String dialogTitle = "";
DialogMode mode;
if (event.getSource().equals((edit))) {
mode = DialogMode.EDIT;
dialogTitle = "Edit expense";
expense = expenseTableView.getSelectionModel().getSelectedItem();
} else if (event.getSource().equals(add)) {
mode = DialogMode.ADD;
dialogTitle = "Add expense";
expense = new Expense(0, false, ExpenseCategory.FOOD, "1/1/23");
} else {
return;
}
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(SceneController.class.getResource("/view/addIncome.fxml"));
DialogPane addExpenseDialog = loader.load();
ExpenseController expenseController = loader.getController();
expenseController.setExpense(expense);
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setDialogPane(addExpenseDialog);
dialog.setTitle(dialogTitle);
Optional<ButtonType> clickedButton = dialog.showAndWait();
if (clickedButton.get() == ButtonType.OK && mode == DialogMode.ADD) {
expenses.add(expense);
}
} catch (IOException e) {
e.printStackTrace();
}*/
}
public void delete(ActionEvent event) throws IOException {
}
public void switchIncome(ActionEvent event) {
}
public void switchOverview(ActionEvent event) {
}
}
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class MainMenuController {
@FXML
private Button addExpenseButton;
@FXML
private Button foodButton;
@FXML
private Button overviewButton;
@FXML
private ProgressBar progressbar;
@FXML
private DatePicker date;
@FXML
private ImageView progressMarker;
@FXML
private Label today;
//ExpenseRepository expenseRepository;
@FXML
public void initialize() {
progressbar.setProgress(0.5);
//progressbar.setProgress((ExpenseRepository.getSum())/5000);
System.out.println(progressbar.getProgress());
progressMarker.setTranslateX(-275 + progressbar.getProgress());
today.setTranslateX(-275 + progressbar.getProgress());
}
@FXML
public void switchExpenses(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Expenses.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@FXML
public void switchOverview(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Overview.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
package no.ntnu.idatt1002.demo.controller;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ProgressBar;
import javafx.stage.Stage;
public class MenuController implements Initializable {
@FXML
private Button addExpense;
@FXML
private DatePicker date;
@FXML
private Button food;
@FXML
private Button overview;
@FXML
private ProgressBar progressbar;
@FXML
public void switchStartMenu(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/FirstMenu.fxml"));
Parent root = loader.load();
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@FXML
public void switchExpenses(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Expenses.fxml"));
Parent root = loader.load();
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class SceneController {
private Stage stage;
private Scene scene;
public void switchStartMenu(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/FirstMenu.fxml"));
Parent root = loader.load();
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchNewBudget(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/NewBudget.fxml"));
Parent root = loader.load();
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchIncome(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Income.fxml"));
Parent root = loader.load();
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchExpenses(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Expenses.fxml"));
Parent root = loader.load();
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchOverview(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Overview.fxml"));
Parent root = loader.load();
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchMainMenu(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/MainMenu.fxml"));
Parent root = loader.load();
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void addIncome(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/addIncome.fxml"));
Scene newScene = new Scene(loader.load());
Stage newStage = new Stage();
newStage.setScene(newScene);
newStage.setResizable(false);
newStage.initModality(Modality.APPLICATION_MODAL);
newStage.show();
}
public void underProgress(ActionEvent event) throws IOException{
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/underProgress.fxml"));
Scene newScene = new Scene(loader.load());
Stage newStage = new Stage();
newStage.setScene(newScene);
newStage.setResizable(false);
newStage.initModality(Modality.APPLICATION_MODAL);
newStage.show();
}
public void closeButton(ActionEvent actionEvent) {
final Node source = (Node) actionEvent.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
}
package no.ntnu.idatt1002.demo.data.Budget;
import javafx.beans.property.*;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import java.util.Objects;
/**
* This class represents a budgetItem
*
* @author Adele
*/
public class BudgetItem {
private DoubleProperty budgetAmount;
private ObjectProperty<ExpenseCategory> budgetCategory;
private StringProperty budgetDescription;
/**
* The constructor of a new Budgetitem.
*
* @param budgetAmount The amount of budget as a double
* @param description A description of the budget as a String
* @param category A category from ExpenseCategory
*
*/
public BudgetItem(double budgetAmount, String description, ExpenseCategory category){
this.budgetAmount = new SimpleDoubleProperty(budgetAmount);
this.budgetDescription = new SimpleStringProperty(description);
this.budgetCategory = new SimpleObjectProperty<ExpenseCategory>(category);
}
/**
* This method gets the BudgetAmount.
*
* @return the budgetAmount as a double
*
*/
public double getBudgetAmount() {
return budgetAmount.get();
}
public DoubleProperty getAmountProperty(){ return budgetAmount;}
public void setBudgetAmount(double amount){
this.budgetAmount.set(amount);
}
/**
* This method gets the category.
*
* @return the category as one of the categories in ExpenseCategory
*
*/
public ExpenseCategory getBudgetCategory() {
return budgetCategory.get();
}
public ObjectProperty<ExpenseCategory> getCategoryProperty(){ return budgetCategory; }
public void setBudgetCategory(ExpenseCategory category){
this.budgetCategory.set(category);
}
/**
* This method gets the description.
*
* @return the description as a String
*
*/
public String getBudgetDescription() {
return budgetDescription.get();
}
public StringProperty getDescriptionProperty(){ return budgetDescription; }
public void setBudgetDescription(String description){
this.budgetDescription.set(description);
}
@Override
public String toString() {
return "budgetAmount=" + getBudgetAmount() +
"\nbudgetCategory=" + getBudgetCategory() +
"\nbudgetDescription=" + getBudgetDescription();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BudgetItem that)) return false;
return Objects.equals(budgetAmount.get(), that.budgetAmount.get()) && Objects.equals(budgetCategory.get(), that.budgetCategory.get()) && Objects.equals(budgetDescription.get(), that.budgetDescription.get());
}
@Override
public int hashCode() {
return Objects.hash(budgetAmount, budgetCategory, budgetDescription);
}
}
package no.ntnu.idatt1002.demo.data.Budget;
import no.ntnu.idatt1002.demo.data.Economics.*;
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
/**
* FileHandling is a class for writing and reading
* Budgets to and from a file.
*
* @author andreas
*/
public class FileHandlingBudget {
private static final String filePath = "src/main/resources/Budget/";
private static final String fileType = ".budget";
private static final String budgetPeriod = "budgetPeriod=";
private static final String maxAmount = "maxAmount=";
private static final String budgetAmount = "budgetAmount=";
private static final String budgetCategory = "budgetCategory=";
private static final String budgetDescription = "budgetDescription=";
/**
* Method for writing (adding) a budget to a file.
*
* @param generalBudget the budget 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 void writeGeneralBudgetToFile(String fileTitle, GeneralBudget generalBudget) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) {
bw.write(generalBudget.toString());
} catch (IOException ex) {
throw new IOException("Error writing story to file: " + ex.getMessage());
}
}
/**
* Method for reading (getting) a Budget from a file.
*
* @param fileTitle the name of the file you want to read from.
* @return the GeneralBudget from the file.
* @throws IOException if an input or output exception occurred.
*/
public GeneralBudget readGeneralBudgetFromFile(String fileTitle) throws IOException {
GeneralBudget generalBudget = null;
int budgetPeriod = 0;
double maxAmount = 0;
ArrayList<BudgetItem> budgetItems = new ArrayList<>();
double budgetAmount = 0;
ExpenseCategory expenseCategory = null;
String budgetDescription = null;
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(FileHandlingBudget.budgetPeriod)) {
budgetPeriod = Integer.parseInt(line.replace(FileHandlingBudget.budgetPeriod, ""));
} else if (line.startsWith(FileHandlingBudget.maxAmount)) {
maxAmount = Double.parseDouble(line.replace(FileHandlingBudget.maxAmount,""));
} else if (line.startsWith(FileHandlingBudget.budgetAmount)) {
budgetAmount = Double.parseDouble(line.replace(FileHandlingBudget.budgetAmount,""));
} else if (line.startsWith(FileHandlingBudget.budgetCategory)) {
line = line.replace(FileHandlingBudget.budgetCategory, "");
expenseCategory = switch (line) {
case "FOOD" -> ExpenseCategory.FOOD;
case "CLOTHES" -> ExpenseCategory.CLOTHES;
case "BOOKS" -> ExpenseCategory.BOOKS;
default -> ExpenseCategory.OTHER;
};
} else if (line.startsWith(FileHandlingBudget.budgetDescription)) {
budgetDescription = line.replace(FileHandlingBudget.budgetDescription,"");
}
if ((line.isEmpty() || (nextLine == null)) && (expenseCategory!=null)) {
if(generalBudget == null){
generalBudget = new GeneralBudget(budgetPeriod,maxAmount);
generalBudget.addToBudget(budgetAmount,budgetDescription,expenseCategory);
} else{
generalBudget.addToBudget(budgetAmount,budgetDescription,expenseCategory);
}
}
}
}
return generalBudget;
}
}
package no.ntnu.idatt1002.demo.data.Budget;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import java.sql.Date;
import java.time.Duration;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
/**
* Class that represents GeneralBudget.
*
* @author Adele
*/
public class GeneralBudget {
private final int budgetPeriod;
private final List<BudgetItem> budgetItems;
private final double maxAmount;
/**
* Class constructor.
*
* @param budgetPeriod the period the budget is going to last for.
* @param budgetItems a List of BudgetItems.
* @param maxAmount the max amount of the budget.
*
*/
public GeneralBudget(int budgetPeriod, List<BudgetItem> budgetItems, double maxAmount) {
if (budgetPeriod <= 0) {
throw new IllegalArgumentException("The period can´t be under zero days");
} else if (maxAmount < 0) {
throw new IllegalArgumentException("The max amount of the budget cant be less than zero");
} else {
this.budgetPeriod = budgetPeriod;
this.maxAmount = maxAmount;
this.budgetItems = budgetItems;
}
}
/**
* Class constructor that creates an empty
* ArrayList for storing BudgetItems
*
* @param budgetPeriod the period the budget is going to last for.
* @param maxAmount the max amount of the budget.
*/
public GeneralBudget(int budgetPeriod, double maxAmount) {
if (budgetPeriod <= 0) {
throw new IllegalArgumentException("The period can´t be under zero days");
}
if (maxAmount < 0) {
throw new IllegalArgumentException("The max amount of the budget cant be less than zero");
} else {
this.budgetPeriod = budgetPeriod;
this.maxAmount = maxAmount;
this.budgetItems = new ArrayList<>();
}
}
/**
* This method returns the amount of days the budget will last for.
*
* @return amount of days the budget lasts.
*
*/
public int getBudgetPeriod() {
return budgetPeriod;
}
/**
* Method for getting the maxAmount of the GeneralBudget
* @return the GeneralBudget´s
*/
public double getMaxAmount() {
return maxAmount;
}
/**
* Method for getting budgetItems
*
* @return an ArrayList of every BudgetItem
* @throws if budgetItems is empty
*/
public List<BudgetItem> getBudgetItems() throws IllegalArgumentException {
return budgetItems;
}
/**
* Method for getting a Specific BudgetItem
* from budgetItems
* @param expenseCategory the ExpenseCategory of the BudgetItem you want to get
* @throws IllegalArgumentException if there is no BudgetItem with the given ExpenseCategory
* @return the BudgetItem with the expenseCategory
*/
public BudgetItem getBudgetItem(ExpenseCategory expenseCategory) throws IllegalArgumentException{
for(BudgetItem budgetItem1 : budgetItems){
if (budgetItem1.getBudgetCategory().equals(expenseCategory)) {
return budgetItem1;
}
}
throw new IllegalArgumentException("No BudgetItem for this ExpenseCategory");
}
/**
* This method returns the amount of days left in the budget period as a string.
*
* @return the amount of days left in the budget period
*
*/
public long getDaysLeftOfBudgetPeriod() {
LocalDate today = LocalDate.now();
LocalDate end = today.plus(Duration.ofDays(getBudgetPeriod()));
Date todaysDate = (Date) Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
Date endDate = (Date) Date.from(end.atStartOfDay(ZoneId.systemDefault()).toInstant());
Calendar cStart = Calendar.getInstance(); cStart.setTime(todaysDate);
Calendar cEnd = Calendar.getInstance(); cEnd.setTime(endDate);
return ChronoUnit.DAYS.between((Temporal) cStart, (Temporal) cEnd);
}
/**
* This method adds a budgetItem to the list of budgetItems in the generalBudget
*
* @param budgetItem the budgetItem you want to add
*
*/
public void addToBudgetBudgetItem(BudgetItem budgetItem) {
if (totalSum() + budgetItem.getBudgetAmount() > maxAmount) {
throw new IllegalArgumentException("Amount to be added goes over the max value of the budget");
}
if (!hasBudgetCategory(budgetItem.getBudgetCategory())) {
budgetItems.add(budgetItem);
} else {
throw new IllegalArgumentException("There is already a budget with this category");
}
}
/**
* This method takes in a double budgetAmount, String description
* and a String category to create and add a BudgetItem to budgetItems
*
* @param budgetAmount The amount of budget as a double
* @param description A description of the budget as a String
* @param category A category from ExpenseCategory
*/
public void addToBudget(double budgetAmount, String description, ExpenseCategory category) {
if ((totalSum() + budgetAmount) > maxAmount) {
throw new IllegalArgumentException("Amount to be added goes over the max value of the budget");
}
if (!hasBudgetCategory(category)) {
budgetItems.add(new BudgetItem(budgetAmount, description, category));
} else {
throw new IllegalArgumentException("There is already a budget with this category");
}
}
/**
* This method checks if the list in the generalBudget already contains a budgetItem with a specified category.
*
* @param category A category from ExpenseCategory
* @return True if the list contains a budgetItem with this category and false if it does not
*
*/
public boolean hasBudgetCategory(ExpenseCategory category) {
return budgetItems.stream().anyMatch(budgetItem -> budgetItem.getBudgetCategory().equals(category));
}
/**
* This method returns the totalSum of all budgetItems in the generalBudget
*
* @return the sum of the budgetsItems as a double
*
*/
public double totalSum() {
double sum = 0;
for (BudgetItem budgetItem : budgetItems) {
sum += budgetItem.getBudgetAmount();
}
return sum;
}
/**
* This method deletes a budgetItem from the list in the GeneralBudget.
*
* @param category A category from ExpenseCategory
*
*/
public void deleteItemFromBudget(ExpenseCategory category) {
budgetItems.removeIf(item -> item.getBudgetCategory() == category);
}
@Override
public String toString() {
StringBuilder budgetItemsString = new StringBuilder();
for(BudgetItem budgetItem : budgetItems){
budgetItemsString.append(budgetItem+"\n\n");
}
if(budgetItemsString.isEmpty()) {
return "budgetPeriod="+getBudgetPeriod()+"\nmaxAmount="+getMaxAmount()+"\n\n";
} else {
return "budgetPeriod="+getBudgetPeriod()+"\nmaxAmount="+getMaxAmount()+"\n\n"+budgetItemsString;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GeneralBudget that)) return false;
return budgetPeriod == that.budgetPeriod && Double.compare(that.maxAmount, maxAmount) == 0 && Objects.equals(budgetItems, that.budgetItems);
}
@Override
public int hashCode() {
return Objects.hash(budgetPeriod, budgetItems, maxAmount);
}
/**
* Check if there is a BudgetItem for
* a specific category
*
* @param category the category you want to check
* @return true or false depending on if there is a BudgetItem for category
*/
/**
public boolean hasBudgetCategory(ExpenseCategory category){
for (BudgetItem item : listOfItems) {
if (item.getCategory() == category) {
return true;
}
}
return false;
}
*/
}
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;
public class Expense extends Item{
private ObjectProperty<ExpenseCategory> 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 Expense object as a double.
* @param recurring True if the current Expense repeats at regular intervals.
* @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory.
* @param date The date of the Expense at format "dd.mm.yy".
*/
public Expense(double amount, boolean recurring, ExpenseCategory 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 Expense as a String.
* @param amount The amount of the current Expense object as a double.
* @param recurring True if the current income repeats at regular intervals.
* @param category The category to which the Expense belongs to, provided as a value of ExpenseCategory
* @param date The date of the Expense at format "dd.mm.yy".
*/
public Expense(String description, double amount, boolean recurring, ExpenseCategory 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);
}
/**
* The method returns the category to which the Expense belongs as a value of the ExpenseCategory enum class.
* @return The category the Expense belongs to as a value of the ExpenseCategory enum class.
*/
public ExpenseCategory getCategory() {
return category.get();
}
/**
* The method sets the category of an expense to a value of the ExpenseCategory enum class.
* @param expenseCategory The category to which the expense belongs as a value of the ExpenseCategory enum.
*/
public void setCategory(ExpenseCategory expenseCategory) {
if(expenseCategory == null) {
throw new IllegalArgumentException("The income must belong to a category.");
}
this.category.set(expenseCategory);
}
/**
* Returns a String that represents the Expense.
* Also includes the ExpenseCategory
* @return The Expense 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 Expense expense)) return false;
if (!super.equals(o)) return false;
return category.get() == expense.category.get();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), category);
}
}