Wednesday February 25, 2015, 16:00-17:15 Lab 01 Version E
Programming question
Implement the SINCard class. A skeleton can be found
here.
You have to put your SINCard class in the test3 package.
You have to implement the multiton design pattern.
You should not add any public features to the class.
package test3;
import java.util.HashMap;
import java.util.Map;
/**
* A Social Insurance Number card has a number.
*
* @author Franck van Breugel
*/
public class SINCard
{
private int number;
private static Map<Integer, SINCard> instance = new HashMap<Integer, SINCard>();
/**
* Initializes this card with the given number
*
* @param number the number of this card.
* @pre. number >= 1000000000 && number <= 999999999
*/
private SINCard(int number)
{
this.setNumber(number);
}
/**
* Returns the number of this card.
*
* @return the number of this card.
*/
public int getNumber()
{
return this.number;
}
/**
* Sets the number of this card to the given number.
*
* @param number the new number of this card.
* @pre. number >= 1000000000 && number <= 999999999
*/
private void setNumber(int number)
{
this.number = number;
}
/**
* Returns a string representation of this card.
* This string consists of the number of the card.
*
* @return a string representation of this card.
*/
public String toString()
{
return "" + this.getNumber();
}
/**
* Tests whether the number of this card is even.
*
* @return true if the number of this card is even,
* false otherwise.
*/
public boolean isEven()
{
return this.getNumber() % 2 == 0;
}
/**
* Returns a SIN card with the given number.
*
* @param number the new number of this card.
* @pre. number >= 1000000000 && number <= 999999999
* @return a SIN card with the given number.
*/
public static SINCard getInstance(int number)
{
if (!SINCard.instance.containsKey(number))
{
SINCard.instance.put(number, new SINCard(number));
}
return SINCard.instance.get(number);
}
/**
* Tests whether a card with the given number has been created.
*
* @param number a number.
* @return true if a card with the given number has been created,
* false otherwise.
*/
public static boolean exists(int number)
{
return SINCard.instance.containsKey(number);
}
}