Skip to content

Instantly share code, notes, and snippets.

@aeris
Created December 18, 2012 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeris/4326631 to your computer and use it in GitHub Desktop.
Save aeris/4326631 to your computer and use it in GitHub Desktop.
Java refactoring
public class DateTimeRange {
private final DateTime start;
private final DateTime end;
public DateTimeRange(final DateTime start, final DateTime end) {
this.start = start;
this.end = end;
}
public DateTime getStart() {
return this.start;
}
public DateTime getEnd() {
return this.end;
}
public boolean include(final DateTime date) {
return this.start.isBefore(date) && this.end.isAfter(date);
}
}
public class Order {
private final DateTime date;
private final int amount;
public Order(final DateTime date, final int amount) {
this.date = date;
this.amount = amount;
}
public DateTime getDate() {
return this.date;
}
public int getAmount() {
return this.amount;
}
public boolean isInRange(final DateTimeRange range) {
return range.include(this.date);
}
}
public class OrderReport {
private final Collection<Order> orders;
private final DateTimeRange range;
public OrderReport(final Collection<Order> orders, final DateTimeRange range) {
this.orders = orders;
this.range = range;
}
public int getTotal() {
int sum = 0;
for (final Order order : this.getApplicable()) {
sum += order.getAmount();
}
return sum;
}
private Collection<Order> getApplicable() {
final Collection<Order> applicable = new LinkedList<>();
for (final Order order : this.orders) {
if (order.isInRange(this.range)) {
applicable.add(order);
}
}
return applicable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment