From: Roland Häder Date: Tue, 26 Apr 2016 07:43:24 +0000 (+0200) Subject: Continued with customer number generator: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f1420b57788761412f2cfa147065158633e6abbf;p=jcustomer-core.git Continued with customer number generator: - the customer number generator can generated numbers like XXXXX-XXXXX-XXX (10 characters total length, number "block" of 5 characters with leading zeros and dashes as separator) - added common-lang3 jar as an updated StringUtils is needed - ignored strings for i18n --- diff --git a/lib/commons-lang3-3.4.jar b/lib/commons-lang3-3.4.jar new file mode 100644 index 0000000..8ec91d4 Binary files /dev/null and b/lib/commons-lang3-3.4.jar differ diff --git a/nbproject/project.properties b/nbproject/project.properties index c7f1952..66db306 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,103 +1,106 @@ -annotation.processing.enabled=true -annotation.processing.enabled.in.editor=true -annotation.processing.processors.list= -annotation.processing.run.all.processors=true -annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.title=jcustomer-core -application.vendor=Roland Haeder -auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml -build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form -# This directory is removed when the project is cleaned: -build.dir=build -build.generated.dir=${build.dir}/generated -build.generated.sources.dir=${build.dir}/generated-sources -# Only compile against the classpath explicitly listed here: -build.sysclasspath=ignore -build.test.classes.dir=${build.dir}/test/classes -build.test.results.dir=${build.dir}/test/results -# Uncomment to specify the preferred debugger connection transport: -#debug.transport=dt_socket -debug.classpath=\ - ${run.classpath} -debug.test.classpath=\ - ${run.test.classpath} -# Files in build.classes.dir which should be excluded from distribution jar -dist.archive.excludes= -# This directory is removed when the project is cleaned: -dist.dir=dist -dist.jar=${dist.dir}/jcustomer-core.jar -dist.javadoc.dir=${dist.dir}/javadoc -endorsed.classpath= -excludes= -file.reference.commons-codec-1.10.jar=lib/commons-codec-1.10.jar -file.reference.jcontacts-core.jar=lib/jcontacts-core.jar -file.reference.jcore.jar=lib/jcore.jar -file.reference.jcoreee.jar=lib/jcoreee.jar -includes=** -jar.archive.disabled=${jnlp.enabled} -jar.compress=false -jar.index=${jnlp.enabled} -javac.classpath=\ - ${file.reference.jcore.jar}:\ - ${file.reference.jcoreee.jar}:\ - ${file.reference.jcontacts-core.jar}:\ - ${file.reference.commons-codec-1.10.jar}:\ - ${libs.javaee-api-7.0.classpath} -# Space-separated list of extra javac options -javac.compilerargs=-Xlint:unchecked -Xlint:deprecation -javac.deprecation=true -javac.external.vm=false -javac.processorpath=\ - ${javac.classpath} -javac.source=1.7 -javac.target=1.7 -javac.test.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -javac.test.processorpath=\ - ${javac.test.classpath} -javadoc.additionalparam= -javadoc.author=true -javadoc.encoding=${source.encoding} -javadoc.noindex=false -javadoc.nonavbar=false -javadoc.notree=false -javadoc.private=true -javadoc.splitindex=true -javadoc.use=true -javadoc.version=true -javadoc.windowtitle=Java Shop Core -jnlp.codebase.type=no.codebase -jnlp.descriptor=application -jnlp.enabled=false -jnlp.mixed.code=default -jnlp.offline-allowed=false -jnlp.signed=false -jnlp.signing= -jnlp.signing.alias= -jnlp.signing.keystore= -# Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed -manifest.custom.codebase= -# Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions) -manifest.custom.permissions= -meta.inf.dir=${src.dir}/META-INF -mkdist.disabled=true -platform.active=default_platform -project.license=gpl30 -run.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -# Space-separated list of JVM arguments used when running the project. -# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. -# To set system properties for unit tests define test-sys-prop.name=value: -run.jvmargs= -run.test.classpath=\ - ${javac.test.classpath}:\ - ${build.test.classes.dir} -source.encoding=UTF-8 -source.reference.jcontacts-core.jar=../jcontacts-core/src/ -source.reference.jcore.jar=../jcore/src/ -source.reference.jcoreee.jar=../jcoreee/src/ -src.dir=src -test.src.dir=test +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=true +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=jcustomer-core +application.vendor=Roland Haeder +auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/jcustomer-core.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.commons-codec-1.10.jar=lib/commons-codec-1.10.jar +file.reference.commons-lang3-3.4.jar=lib/commons-lang3-3.4.jar +file.reference.jcontacts-core.jar=lib/jcontacts-core.jar +file.reference.jcore.jar=lib/jcore.jar +file.reference.jcoreee.jar=lib/jcoreee.jar +includes=** +jar.archive.disabled=${jnlp.enabled} +jar.compress=false +jar.index=${jnlp.enabled} +javac.classpath=\ + ${file.reference.jcore.jar}:\ + ${file.reference.jcoreee.jar}:\ + ${file.reference.jcontacts-core.jar}:\ + ${file.reference.commons-codec-1.10.jar}:\ + ${file.reference.commons-lang3-3.4.jar}:\ + ${libs.javaee-api-7.0.classpath} +# Space-separated list of extra javac options +javac.compilerargs=-Xlint:unchecked -Xlint:deprecation +javac.deprecation=true +javac.external.vm=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=true +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=true +javadoc.reference.commons-lang3-3.4.jar=https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/ +javadoc.splitindex=true +javadoc.use=true +javadoc.version=true +javadoc.windowtitle=Java Shop Core +jnlp.codebase.type=no.codebase +jnlp.descriptor=application +jnlp.enabled=false +jnlp.mixed.code=default +jnlp.offline-allowed=false +jnlp.signed=false +jnlp.signing= +jnlp.signing.alias= +jnlp.signing.keystore= +# Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed +manifest.custom.codebase= +# Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions) +manifest.custom.permissions= +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=true +platform.active=default_platform +project.license=gpl30 +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +source.reference.jcontacts-core.jar=../jcontacts-core/src/ +source.reference.jcore.jar=../jcore/src/ +source.reference.jcoreee.jar=../jcoreee/src/ +src.dir=src +test.src.dir=test diff --git a/src/org/mxchange/jcustomercore/utils/CustomerUtils.java b/src/org/mxchange/jcustomercore/utils/CustomerUtils.java index c26bccd..be29c8d 100644 --- a/src/org/mxchange/jcustomercore/utils/CustomerUtils.java +++ b/src/org/mxchange/jcustomercore/utils/CustomerUtils.java @@ -17,7 +17,9 @@ package org.mxchange.jcustomercore.utils; import java.security.SecureRandom; +import java.text.MessageFormat; import java.util.Random; +import org.apache.commons.lang3.StringUtils; /** * Customer utilities @@ -42,12 +44,58 @@ public class CustomerUtils { /** * Generates a random customer number with some dashes in it *

- * @param length Length of the number + * @param totalLength Length of the number + * @param blockSize Block size + * @param separator Seperator *

* @return Generated customer number */ - public static String generateCustomerNumber (final long length) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) { + // All parameters must be set + if (totalLength < 5) { + // Total length is to short + throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N + } else if (blockSize < 3) { + // 3 charcters is minimum + throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N + } + + // Init number + StringBuilder customerNumber = new StringBuilder(totalLength); + + // Calculate total blockas + long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5); + + // Generate CN + for (int i = 0; i < totalBlocks; i++) { + // Fill it up with leading zeros and append it + separator character + customerNumber.append(genrateBlock(blockSize)).append(separator); + } + + // Calculate remaining charcters + long remain = totalLength - blockSize * totalBlocks; + + // Generate new block and append it + customerNumber.append(genrateBlock((short) remain)); + + // Return finished number + return customerNumber.toString(); + } + + /** + * Generates a block of numbers with leading zeros, if the random number is + * shorter than block size + *

+ * @param blockSize Block size + *

+ * @return Generated block + */ + private static String genrateBlock (final short blockSize) { + // Generate random number + int num = RANDOM_NUMBER_GENERATOR.nextInt(10 ^ blockSize); + + // Generate "block" and return it + return StringUtils.leftPad(String.valueOf(num), blockSize, '0'); } /**