⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 creditcardconverter.java

📁 售车网站
💻 JAVA
字号:
/* * $Id: CreditCardConverter.java,v 1.3 2004/11/14 07:33:07 tcfujii Exp $ *//* * Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved. * Use is subject to license terms. */package carstore;import javax.faces.application.FacesMessage;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.convert.Converter;import javax.faces.convert.ConverterException;/** * CreditCardConverter Class accepts a Credit Card Number of type String * and strips blanks and <oode>"-"</code> if any from it. It also formats the * CreditCardNumber such a blank space separates every four characters. * Blanks and <oode>"-"</code> characters are the expected demiliters * that could be used as part of a CreditCardNumber. */public class CreditCardConverter implements Converter {    /**     * <p>The message identifier of the Message to be created if     * the conversion fails.  The message format string for this     * message may optionally include a <code>{0}</code> and     * <code>{1}</code> placeholders, which     * will be replaced by the object and value.</p>     */    public static final String CONVERSION_ERROR_MESSAGE_ID =        "carstore.Conversion_Error";    /**     * Parses the CreditCardNumber and strips any blanks or <oode>"-"</code>     * characters from it.     */    public Object getAsObject(FacesContext context, UIComponent component,                              String newValue) throws ConverterException {        String convertedValue = null;        if (newValue == null) {            return newValue;        }        // Since this is only a String to String conversion, this conversion         // does not throw ConverterException.        convertedValue = newValue.trim();        if (((convertedValue.indexOf("-")) != -1) ||            ((convertedValue.indexOf(" ")) != -1)) {            char[] input = convertedValue.toCharArray();            StringBuffer buffer = new StringBuffer(50);            for (int i = 0; i < input.length; ++i) {                if (input[i] == '-' || input[i] == ' ') {                    continue;                } else {                    buffer.append(input[i]);                }            }            convertedValue = buffer.toString();        }        // System.out.println("Converted value " + convertedValue);        return convertedValue;    }    /**     * Formats the value by inserting space after every four characters     * for better readability if they don't already exist. In the process     * converts any <oode>"-"</code> characters into blanks for consistency.     */    public String getAsString(FacesContext context, UIComponent component,                              Object value) throws ConverterException {        String inputVal = null;        if (value == null) {            return null;        }        // value must be of the type that can be cast to a String.        try {            inputVal = (String) value;        } catch (ClassCastException ce) {            FacesMessage errMsg = MessageFactory.getMessage(                CONVERSION_ERROR_MESSAGE_ID,                (new Object[]{value, inputVal}));            throw new ConverterException(errMsg.getSummary());        }        // insert spaces after every four characters for better            // readability if it doesn't already exist.           char[] input = inputVal.toCharArray();        StringBuffer buffer = new StringBuffer(50);        for (int i = 0; i < input.length; ++i) {            if ((i % 4) == 0 && i != 0) {                if (input[i] != ' ' || input[i] != '-') {                    buffer.append(" ");                    // if there any "-"'s convert them to blanks.                } else if (input[i] == '-') {                    buffer.append(" ");                }            }            buffer.append(input[i]);        }        String convertedValue = buffer.toString();        // System.out.println("Formatted value " + convertedValue);        return convertedValue;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -