-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDonationService.java
More file actions
52 lines (45 loc) · 1.43 KB
/
DonationService.java
File metadata and controls
52 lines (45 loc) · 1.43 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
47
48
49
50
51
52
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class DonationService
{
private static final String fileName = "donations.txt";
public static void saveDonation(Donation donation)
{
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)))
{
writer.write(donation.getCharity() + "|" + donation.getAmount());
writer.newLine();
}
catch (IOException e)
{
System.err.println("Error saving donation: " + e.getMessage());
}
}
public static List<Donation> getAllDonations()
{
List<Donation> donations = new ArrayList<>();
File file = new File(fileName);
if (!file.exists())
{
return donations;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = reader.readLine()) != null)
{
String[] parts = line.split("\\|");
if (parts.length == 2)
{
donations.add(new Donation(parts[0], Double.parseDouble(parts[1])));
}
}
}
catch (IOException e)
{
System.err.println("Error loading donations: " + e.getMessage());
}
return donations;
}
}