-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpense.java
More file actions
35 lines (29 loc) · 1.14 KB
/
Expense.java
File metadata and controls
35 lines (29 loc) · 1.14 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
public class Expense {
String description;
double amount;
String category;
public Expense(String description, double amount, String category) {
this.description = description;
this.amount = amount;
this.category = category;
}
// 1. Converts this object into a String for the file
// Example: "Coffee,5.0,Food"
public String toFileString() {
return description + "," + amount + "," + category;
}
// 2. Creates an Expense object FROM a file string
public static Expense fromFileString(String fileLine) {
String[] parts = fileLine.split(","); // Splits by comma
// parts[0] is description, parts[1] is amount, parts[2] is category
return new Expense(parts[0], Double.parseDouble(parts[1]), parts[2]);
}
@Override
public String toString() {
return description + " | $" + amount + " | (" + category + ")";
}
// --- NEW: Getters for the Table ---
public String getDescription() { return description; }
public double getAmount() { return amount; }
public String getCategory() { return category; }
}