package tut;
import com.ibm.telephony.beans.media.*; import com.ibm.telephony.wvr.*; /** * Subcomponent that allows the caller to enter their credit card * details and validates them */ class CardChecker {
//Define the category for all the voice segments in this class private static final String CATEGORY = "Tutorials";
// Create the Voice Segment objects private static final VoiceSegment VS_INVALID_CARD = new VoiceSegment(CATEGORY, "InvalidCard"); private static final VoiceSegment VS_CARD_NUMBER = new VoiceSegment(CATEGORY, "CardNumber"); private static final VoiceSegment VS_EXPIRY_DATE = new VoiceSegment(CATEGORY, "ExpiryDate"); private static final VoiceSegment VS_DATE_FORMAT = new VoiceSegment(CATEGORY, "DateFormat");
// Create an array to store two of the voice segments so they can be played in sequence private static final MediaType[] VS_EXPIRY = { VS_EXPIRY_DATE, VS_DATE_FORMAT };
// Create attribute objects for getting the cardnumber from the caller private PlayAttributes cardNumPlayAtts = new PlayAttributes(); private InputAttributes cardNumInputAtts = new InputAttributes(); private DTMFAttributes cardNumDTMFAtts = new DTMFAttributes(); private RecoAttributes cardNumRecoAtts = null; // Create attribute objects for getting the expiry date from the caller private PlayAttributes expiryPlayAtts = new PlayAttributes(); private InputAttributes expiryInputAtts = new InputAttributes(); private DTMFAttributes expiryDTMFAtts = new DTMFAttributes(); private RecoAttributes expiryRecoAtts = null;
// Create a constructor method to set up various attributes public CardChecker() { // Set the attributes for getting the credit card number cardNumInputAtts.setMessage(VS_CARD_NUMBER); cardNumInputAtts.setTimeout(20); cardNumDTMFAtts.setMaximumKeys(16); cardNumDTMFAtts.setDelimiterKeys(""); // Set the attributes for getting the expiry date expiryInputAtts.setMessage(VS_EXPIRY); expiryInputAtts.setTimeout(10); expiryDTMFAtts.setMaximumKeys(4); expiryDTMFAtts.setDelimiterKeys(""); } //CardChecker()
// Validate the caller's credit card details public boolean takeCardDetails(Call call) throws WVRException {
String cardNumber = null; String expiryDate = null; int year = 0; int firstNumber = 0;
int numberOfTries = 0; while(numberOfTries < 3) { numberOfTries++;
// Get the card number InputResult result = call.playAndGetInput(cardNumPlayAtts, cardNumInputAtts, cardNumDTMFAtts, cardNumRecoAtts); cardNumber = result.getValue(); // Get the expiry date result = call.playAndGetInput(expiryPlayAtts, expiryInputAtts, expiryDTMFAtts, expiryRecoAtts); expiryDate = result.getValue();
// Extract the year and the first digit of the card number year = Integer.parseInt(expiryDate.substring(2, 4)); firstNumber = Integer.parseInt(cardNumber.substring(0, 1));
// Validate the card number if (year >= 3 && year <= 5 && year == firstNumber) { return true; // Returns that the card number was valid } else { call.play(VS_INVALID_CARD); } //if }//while
return false; // Returns that the card number was not valid }//takeCardDetails() }//CardChecker