-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionWriter.java
More file actions
46 lines (38 loc) · 1.72 KB
/
TransactionWriter.java
File metadata and controls
46 lines (38 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package personalFinanceManager.src;
import java.io.*;
// a class, which writes a file into a database.
public class TransactionWriter {
static String line; // allocate the line from 'transactions.txt' file.
// method, which writes data into 'transactions.txt' file.
public static void writeData(String month, int money) {
// first check, if the month already exists in our database and if the type is
// valid.
if (!monthExits(month) && month != null && money != -1) {
// write data into a file.
try (BufferedWriter out = new BufferedWriter(
new FileWriter("personalFinanceManager/data/transactions.txt", true))) {
out.write("date: " + month + "\n");
out.write("money: " + money + "\n");
return;
} catch (IOException exc) {
System.out.println("An unexpected error has occurred: " + exc);
}
}
}
// method, which checks if given date is already written in our database.
private static boolean monthExits(String month) {
try (BufferedReader in = new BufferedReader(
new FileReader("personalFinanceManager/data/transactions.txt"))) {
// read until the end.
while ((line = in.readLine()) != null) {
if (line.startsWith("date: ") && line.substring(6).equals(month)) {
return true;
}
}
} catch (IOException exc) {
System.out.println("An unexpected error has occurred: " + exc);
}
System.out.println(month + " is already written in our database.");
return false;
}
}