Created
December 18, 2012 09:35
-
-
Save aeris/4326631 to your computer and use it in GitHub Desktop.
Java refactoring
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
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