-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBudget.java
More file actions
79 lines (72 loc) · 2.39 KB
/
Budget.java
File metadata and controls
79 lines (72 loc) · 2.39 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import com.google.gson.annotations.Expose;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a budget with an amount and category, supporting observer notifications
* for changes. Uses Gson annotations for serialization and maintains a list of observers
* to implement the observer pattern.
*/
public class Budget {
/** The monetary amount allocated to this budget, exposed for serialization. */
@Expose
private double amount;
/** The category of this budget (e.g., food, travel), exposed for serialization. */
@Expose
private String category;
/** List of observers to be notified of budget updates. */
private List<FinancialObserver> observers = new ArrayList<>();
/**
* Initializes a new budget with a specified amount and category.
*
* @param amount The budget’s monetary value.
* @param category The type or purpose of the budget.
*/
public Budget(double amount, String category) {
this.amount = amount;
this.category = category;
}
/**
* Registers a new observer to receive updates when the budget changes.
*
* @param observer The observer to add to the notification list.
*/
public void addObserver(FinancialObserver observer) {
// Append the observer to the list
observers.add(observer);
}
/**
* Unregisters an observer, stopping it from receiving further budget updates.
*
* @param observer The observer to remove from the notification list.
*/
public void removeObserver(FinancialObserver observer) {
// Remove the specified observer
observers.remove(observer);
}
/**
* Alerts all registered observers about changes to this budget, triggering their
* update logic.
*/
public void notifyObservers() {
// Loop through observers and call their update method
for (FinancialObserver observer : observers) {
observer.update(this);
}
}
/**
* Retrieves the monetary amount of this budget.
*
* @return The budget’s amount.
*/
public double getAmount() {
return amount;
}
/**
* Retrieves the category of this budget.
*
* @return The budget’s category.
*/
public String getCategory() {
return category;
}
}