package type.lib; import java.util.Date; /** * This class implements only part of type.lib.CreditCard. * It is used as an example to illustrate composition. * * @author Franck van Breugel */ public class CreditCard { private int number; private String name; private Date issueDate; private Date expiryDate; //@ invariant this.issueDate != null && this.expiryDate != null /** * Initializes this credit card with the given name, number, issue date * and expiry date. * * @param number the number of this credit card. * @param name the name of this credit card. * @param issueDate the issue date of this credit card. * @pre. issueDate != null * @param expiryDate the expiry date of this credit card. * @pre. expiryDate != null */ public CreditCard(int number, String name, Date issueDate, Date expiryDate) { this.setNumber(number); this.setName(name); this.setIssueDate(issueDate); // makes a copy this.setExpiryDate(expiryDate); // makes a copy } /** * Returns the number of this credit card. * * @return the number of this credit card. */ public int getNumber() { return this.number; } /** * Sets the number of this credit card to the given number. * * @param number the new number of this credit card. */ private void setNumber(int number) { this.number = number; } /** * Returns the name of this credit card. * * @return the name of this credit card. */ public String getName() { return this.name; } /** * Sets the name of this credit card to the given name. * * @param name the new name of this credit card. */ private void setName(String name) { this.name = name; } /** * Returns the issue date of this credit card. * * @return the issue date of this credit card. */ public Date getIssueDate() { return new Date(this.issueDate.getTime()); } /** * Sets the issue date of this credit card to the given issue date. * * @param issueDate the new issue date of this credit card. * @pre. issueDate != null */ private void setIssueDate(Date issueDate) { this.issueDate = new Date(issueDate.getTime()); // does not throw a NullPointException } /** * Returns the expiry date of this credit card. * * @return the expiry date of this credit card. */ public Date getExpiryDate() { return new Date(this.expiryDate.getTime()); } /** * Sets the expiry date of this credit card to the given expiry date. * * @param expiryDate the new expiry date of this credit card. * @pre. expiryDate != null */ public void setExpiryDate(Date expiryDate) { this.expiryDate = new Date(expiryDate.getTime()); // does not throw a NullPointException } // other methods such as equals, hashCode and toString should be implemented as well }