Created
March 10, 2020 18:14
-
-
Save jessestuart/5294f42ba19294692f20694a4e2a5c9e to your computer and use it in GitHub Desktop.
stackoverflow-60622844
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate | |
public class AmountModel { | |
private Integer id; | |
private Double amount; | |
// Not defined in OP | |
// private CategoryModel categoryModel; | |
private LocalDate localDate; | |
} | |
/* ******************** */ | |
public class MyBudget { | |
// Map "amounts" by ID for easy O(1) lookup. | |
Map<Integer, AmountModel> amountMap | |
// Load some dummy data. | |
MyBudget() { | |
List<AmountModel> models = (1..10).collect { i -> new AmountModel(id: i, amount: i) } | |
this.amountMap = models.collectEntries(model -> [(model.id): model]) | |
} | |
public void deleteAmount(Integer id) { | |
if (!amountMap.containsKey(id)) { | |
// (Handle invalid input) | |
throw new Exception() | |
} | |
amountMap.remove(id) | |
return | |
} | |
} | |
/* ******************** */ | |
MyBudget budget = new MyBudget() | |
assert budget.amountMap.containsKey(1) | |
budget.deleteAmount(1) | |
assert !budget.amountMap.containsKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment