Skip to content
Snippets Groups Projects
Commit 5c327714 authored by Harry Linrui XU's avatar Harry Linrui XU
Browse files

Created class for file handling the selected budget

parent 2c8fae1e
No related branches found
No related tags found
1 merge request!43Merging frontend-testing into master
package no.ntnu.idatt1002.demo.data.Budget;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileHandlingSelectedBudget {
private static final String path = System.getProperty("user.dir") + "/src/main/resources/";
private static final String currentFileType = ".current";
private static final String registerFileType = ".register";
private static final String budgetFileType = ".budget";
public static String readSelectedBudget() throws IOException {
BufferedReader br;
try {
System.out.print("Reading current file...");
FileReader fileReader = new FileReader(System.getProperty("user.dir") + "/src/main/resources/CurrentFile.txt");
br = new BufferedReader(fileReader);
} catch (IOException ioException) {
throw new IOException("File: CurrentFile.txt does not exist");
}
System.out.println("Successful read on current file");
return br.readLine();
}
public static void updateSelectedBudget(String budgetName) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/main/resources/CurrentFile.txt"))) {
bw.write(budgetName);
System.out.println("Current file is: " + budgetName);
} catch (IOException ex) {
throw new IOException("Error writing to file: " + "CurrentFile.txt");
}
}
public static boolean isSelectedBudgetEmpty(String fileTitle) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "src/main/resources/"
+ "CurrentFile.txt"))) {
return br.readLine() == null;
}
}
public static boolean createBudgetDirectory(String budgetId) {
System.out.println("Directory: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetId);
File f = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId);
return f.mkdir();
}
public static void createNewIncomeFile(String budgetId, String incomeFileTitle) throws IOException {
System.out.println("Income filepath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + incomeFileTitle + registerFileType);
File incomeFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + incomeFileTitle + registerFileType);
incomeFile.createNewFile();
}
public static void createNewExpenseFile(String budgetId, String expenseFileTitle) throws IOException {
System.out.println("Expense filePath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + expenseFileTitle + registerFileType);
File expenseFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + expenseFileTitle + registerFileType);
expenseFile.createNewFile();
}
public static void createNewBudgetFile(String budgetId, String budgetFileTitle) throws IOException {
System.out.println("Budget filePath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + budgetFileTitle + budgetFileType);
File budgetFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + budgetFileTitle + budgetFileType);
budgetFile.createNewFile();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment