I have a function that takes an object as a JSON body and returns a returns the list as a JSON body.
Here is the function:
public List<Event> executeRule(Rule rule) {
List<Transaction> userTransaction = transactionsService.latestTransactionsForUser(rule.getUserId());
List<Rule> ruleList = activeRulesForUser(rule.getUserId());
return eventList;
}
From the object passed as a parameter I get userId
and find all transactions and rules for a particular user. Now I have to return those transactions and rules of single user as new list of type Event
.
What would be the best practice for it as I have two list with different types and third list that I have to return with also different type?
I supposed you want to return the user transactions and rules, if you are encountering these together many times, maybe it's a sign that you should wrap them in a class, as follows:
class UserEvents {
private List<Transaction> transactions;
private List<Rule> rules;
public UserEvents(List<Transaction> transactions, List<Rule> rules) {
this.transactions = transactions;
this.rules = rules;
}
public UserEvents() {
this(new ArrayList<>(), new ArrayList<>());
}
public List<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}
public List<Rule> getRules() {
return rules;
}
public void setRules(List<Rule> rules) {
this.rules = rules;
}
}
Then your function will look something like this:
public UserEvents executeRule(Rule rule) {
List<Transaction> transactions = transactionsService.latestTransactionsForUser(rule.getUserId());
List<Rule> rules = activeRulesForUser(rule.getUserId());
return new UserEvents(transactions, rules);
}
I would like to let you know that a function with 'execute' in it's name does not really need to return something, it should execute a task or something similar.
You should give descriptive and correct names to your functions and methods, one should understand only from the method decleration what the method does.
I found a working solution.
public List<Event> executeRule(Rule rule) {
List<Transaction> userTransaction = transactionsService.latestTransactionsForUser(rule.getUserId());
List<Rule> ruleList = activeRulesForUser(rule.getUserId());
List<Event> eEventList = new ArrayList<>();
Event event = new Event();
event.setUserId(rule.getUserId());
savingsEvent.setTransactions(userTransaction);
savingsEvent.setSavingsRules(savingsRuleList);
savingsEventList.add(savingsEvent);
return savingsEventList;
}
So the solution was to create a new object Event and populate it with data that I need from the two lists, then add that object to Event list.
In Event object I added both lists:
class Event {
private Long id;
private Long userId;
private List<Transaction> transactions;
private List<Rule> rules;
//Getters, Setters, Constructors
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.