Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active April 19, 2024 15:36
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 mcsee/87977580c06f4816c48ca9c6f9d10b23 to your computer and use it in GitHub Desktop.
Save mcsee/87977580c06f4816c48ca9c6f9d10b23 to your computer and use it in GitHub Desktop.
class CreditCard {
private String number;
private MonthOfYear expiryDate;
// expiryDate is the role
// MonthOfYear is the type
}
class MonthOfYear {
private Month month;
private Year year;
// These are other small objects
public MonthOfYear(Month month, Year year) {
// You don't need to add validations since
// month is a valid month
// year is a valid year
this.month = month;
this.year = year;
}
public boolean isBeforeEndOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return (calendar.get(Calendar.YEAR) < year.value()) ||
(calendar.get(Calendar.YEAR) == year.value() &&
calendar.get(Calendar.MONTH) < month.value())
// Notice there are no days involved
}
// This protocol is just for MonthOfYears
public Day[] getDaysInMonth() { }
public boolean isLeapYear() { } // ...
public void iterateDays() { } // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment