diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..a61aebb --- /dev/null +++ b/.classpath @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..6871b53 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + geradorcpf + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7251e5a --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1dc8b3e --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + 4.0.0 + br.ce.orlando.geradorcpf + geradorcpf-orlando + 0.0.1-SNAPSHOT + gerador-cpf + script para gerar cpf aleatórios e válidos, utilizando fórmula matemática padrão para cpf brasileiro. + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + br.ce.orlando.gerador.cpf.App + + + + + + + \ No newline at end of file diff --git a/src/main/java/br/ce/orlando/gerador/cpf/App.java b/src/main/java/br/ce/orlando/gerador/cpf/App.java new file mode 100644 index 0000000..5e24223 --- /dev/null +++ b/src/main/java/br/ce/orlando/gerador/cpf/App.java @@ -0,0 +1,12 @@ +package br.ce.orlando.gerador.cpf; +/** + * + * @author orlando.junior + * + */ +public class App { + + public static void main(String[] args) { + new GeradorCPFs(); + } +} diff --git a/src/main/java/br/ce/orlando/gerador/cpf/CpfDocumentFilter.java b/src/main/java/br/ce/orlando/gerador/cpf/CpfDocumentFilter.java new file mode 100644 index 0000000..0c6ee7b --- /dev/null +++ b/src/main/java/br/ce/orlando/gerador/cpf/CpfDocumentFilter.java @@ -0,0 +1,53 @@ +package br.ce.orlando.gerador.cpf; +/** + * + * @author orlando.junior + * + */ +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; +import javax.swing.text.DocumentFilter; + +public class CpfDocumentFilter extends DocumentFilter { + + @Override + public void insertString(FilterBypass fb, int offset, String string, + AttributeSet attr) throws BadLocationException { + + // Remove caracteres que não sejam números + string = string.replaceAll("[^0-9]", ""); + + StringBuilder builder = new StringBuilder(fb.getDocument().getText(0, fb.getDocument().getLength())); + builder.insert(offset, string); + + // Adiciona os pontos e traço + if (builder.length() == 11) { + builder.insert(3, "."); + builder.insert(7, "."); + builder.insert(11, "-"); + } + + super.insertString(fb, offset, string, attr); + } + + @Override + public void replace(FilterBypass fb, int offset, int length, String string, + AttributeSet attrs) throws BadLocationException { + + // Remove caracteres que não sejam números + string = string.replaceAll("[^0-9]", ""); + + StringBuilder builder = new StringBuilder(fb.getDocument().getText(0, fb.getDocument().getLength())); + builder.replace(offset, offset + length, string); + + // Adiciona os pontos e traço + if (builder.length() == 11) { + builder.insert(3, "."); + builder.insert(7, "."); + builder.insert(11, "-"); + } + + super.replace(fb, offset, length, string, attrs); + } +} + diff --git a/src/main/java/br/ce/orlando/gerador/cpf/GeradorCPF.java b/src/main/java/br/ce/orlando/gerador/cpf/GeradorCPF.java new file mode 100644 index 0000000..1c78cbc --- /dev/null +++ b/src/main/java/br/ce/orlando/gerador/cpf/GeradorCPF.java @@ -0,0 +1,39 @@ +package br.ce.orlando.gerador.cpf; +import java.util.Random; +/** + * + * @author orlando.junior + * + */ +public class GeradorCPF { + + public String geraCPF() { + + int soma1, soma2, resto1, resto2; + + StringBuilder cpf = new StringBuilder(); + Random random = new Random(); + + int[] digitos = new int[11]; + for (int i = 0; i < 9; i++) { + digitos[i] = random.nextInt(10); + } + + soma1 = digitos[8] * 2 + digitos[7] * 3 + digitos[6] * 4 + digitos[5] * 5 + digitos[4] * 6 + digitos[3] * 7 + digitos[2] * 8 + digitos[1] * 9 + digitos[0] * 10; + resto1 = soma1 % 11; + + digitos[9] = resto1 < 2 ? 0 : 11 - resto1; + + soma2 = digitos[9] * 2 + digitos[8] * 3 + digitos[7] * 4 + digitos[6] * 5 + digitos[5] * 6 + digitos[4] * 7 + digitos[3] * 8 + digitos[2] * 9 + digitos[1] * 10 + digitos[0] * 11; + resto2 = soma2 % 11; + + digitos[10] = resto2 < 2 ? 0 : 11 - resto2; + + cpf.append(digitos[0]).append(digitos[1]).append(digitos[2]).append(".") + .append(digitos[3]).append(digitos[4]).append(digitos[5]).append(".") + .append(digitos[6]).append(digitos[7]).append(digitos[8]).append("-") + .append(digitos[9]).append(digitos[10]); + + return cpf.toString(); + } +} diff --git a/src/main/java/br/ce/orlando/gerador/cpf/GeradorCPFs.java b/src/main/java/br/ce/orlando/gerador/cpf/GeradorCPFs.java new file mode 100644 index 0000000..6256f93 --- /dev/null +++ b/src/main/java/br/ce/orlando/gerador/cpf/GeradorCPFs.java @@ -0,0 +1,127 @@ +package br.ce.orlando.gerador.cpf; +/** + * + * @author orlando.junior + * + */ +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.text.PlainDocument; + +public class GeradorCPFs extends JFrame { + + private static final long serialVersionUID = 1L; + + PlainDocument docQuantidadeCpf = new PlainDocument(); + PlainDocument docValidaCpf = new PlainDocument(); + + private final JPanel panel; + private final JLabel labelQuantidadeCpf; + private final JTextField inputQuantidadeCpf; + private final JButton gerarCpfBtn; + private final JButton validarCpfBtn; + + + public GeradorCPFs() { + super("Gerador de CPFs"); + + panel = new JPanel(); + + labelQuantidadeCpf = new JLabel("Quantidade de CPFs:"); + inputQuantidadeCpf = new JTextField(10); + inputQuantidadeCpf.setDocument(docQuantidadeCpf); + docQuantidadeCpf.setDocumentFilter(new CpfDocumentFilter()); + + gerarCpfBtn = new JButton("Gerar CPFs"); + validarCpfBtn = new JButton("Validar CPF"); + + panel.add(labelQuantidadeCpf); + panel.add(inputQuantidadeCpf); + panel.add(gerarCpfBtn); + panel.add(validarCpfBtn); + + + gerarCpfBtn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int quantidade = Integer.parseInt(inputQuantidadeCpf.getText()); + gerarCPFs(quantidade); + } + }); + + validarCpfBtn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + String cpf = JOptionPane.showInputDialog(panel, "Digite o CPF a ser validado:"); + boolean valido = ValidadorCPF.validaCPF(cpf); + if (valido) { + JOptionPane.showMessageDialog(panel, "CPF válido!"); + } else { + JOptionPane.showMessageDialog(panel, "CPF inválido!"); + } + } + }); + + add(panel, BorderLayout.CENTER); + + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(300, 100); + setVisible(true); + } + + private void gerarCPFs(int quantidade) { + + GeradorCPF gerador = new GeradorCPF(); + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < quantidade; i++) { + stringBuilder.append(gerador.geraCPF()).append(",\n"); + } + + String cpfString = stringBuilder.toString().substring(0, stringBuilder.length() - 1); + + // Cria um seletor de arquivos para o usuário escolher o diretório e o nome do arquivo + JFileChooser fileChooser = new JFileChooser(); + + FileNameExtensionFilter filter = new FileNameExtensionFilter("Arquivo de Texto (*.txt)", "txt"); + + fileChooser.setFileFilter(filter); + fileChooser.setSelectedFile(new File("cpfs.txt")); + + int result = fileChooser.showSaveDialog(panel); + + if (result == JFileChooser.APPROVE_OPTION) { + + // Se o usuário escolher um arquivo, salva os CPFs nele + File file = fileChooser.getSelectedFile(); + + if (!file.getName().endsWith(".txt")) { + file = new File(file.getPath() + ".txt"); + } + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write(cpfString); + writer.close(); + JOptionPane.showMessageDialog(panel, "CPFs gerados com sucesso!"); + } catch (IOException ex) { + JOptionPane.showMessageDialog(panel, "Erro ao gerar CPFs: " + ex.getMessage()); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/br/ce/orlando/gerador/cpf/ValidadorCPF.java b/src/main/java/br/ce/orlando/gerador/cpf/ValidadorCPF.java new file mode 100644 index 0000000..1a6ec0e --- /dev/null +++ b/src/main/java/br/ce/orlando/gerador/cpf/ValidadorCPF.java @@ -0,0 +1,37 @@ +package br.ce.orlando.gerador.cpf; +/** + * + * @author orlando.junior + * + */ +public class ValidadorCPF { + + private static final int[] PESO = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2}; + + private static int calcularDigito(String str, int[] peso) { + int soma = 0; + for (int indice=str.length()-1, digito; indice >= 0; indice-- ) { + digito = Integer.parseInt(str.substring(indice,indice+1)); + soma += digito*peso[peso.length-str.length()+indice]; + } + soma = 11 - soma % 11; + return soma > 9 ? 0 : soma; + } + + public static boolean validaCPF(String cpf) { + if (cpf == null) { + return false; + } + + cpf = cpf.replaceAll("\\D", ""); + + if (cpf.length() != 11) { + return false; + } + + int digito1 = calcularDigito(cpf.substring(0, 9), PESO); + int digito2 = calcularDigito(cpf.substring(0, 9) + digito1, PESO); + return cpf.equals(cpf.substring(0, 9) + digito1 + digito2); + } +} + diff --git a/src/main/resources/META-INF/manifest.mf b/src/main/resources/META-INF/manifest.mf new file mode 100644 index 0000000..f68a6cd --- /dev/null +++ b/src/main/resources/META-INF/manifest.mf @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Main-Class: br.ce.orlando.gerador.cpf.App diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..424a627 --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,6 @@ +Manifest-Version: 1.0 +Built-By: hiper +Build-Jdk: 17.0.5 +Main-Class: br.ce.orlando.gerador.cpf.App +Created-By: Maven Integration for Eclipse + diff --git a/target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.properties b/target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.properties new file mode 100644 index 0000000..1348070 --- /dev/null +++ b/target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Fri Apr 07 15:49:04 BRT 2023 +m2e.projectLocation=T\:\\eclipse-workspace\\geradorcpf +m2e.projectName=geradorcpf +groupId=br.ce.orlando.geradorcpf +artifactId=geradorcpf-orlando +version=0.0.1-SNAPSHOT diff --git a/target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.xml b/target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.xml new file mode 100644 index 0000000..1dc8b3e --- /dev/null +++ b/target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.xml @@ -0,0 +1,24 @@ + + 4.0.0 + br.ce.orlando.geradorcpf + geradorcpf-orlando + 0.0.1-SNAPSHOT + gerador-cpf + script para gerar cpf aleatórios e válidos, utilizando fórmula matemática padrão para cpf brasileiro. + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + br.ce.orlando.gerador.cpf.App + + + + + + + \ No newline at end of file diff --git a/target/classes/br/ce/orlando/gerador/cpf/App.class b/target/classes/br/ce/orlando/gerador/cpf/App.class new file mode 100644 index 0000000..b155ea2 Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/App.class differ diff --git a/target/classes/br/ce/orlando/gerador/cpf/CpfDocumentFilter.class b/target/classes/br/ce/orlando/gerador/cpf/CpfDocumentFilter.class new file mode 100644 index 0000000..3a4947d Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/CpfDocumentFilter.class differ diff --git a/target/classes/br/ce/orlando/gerador/cpf/GeradorCPF.class b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPF.class new file mode 100644 index 0000000..61ce5f0 Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPF.class differ diff --git a/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs$1.class b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs$1.class new file mode 100644 index 0000000..128ece3 Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs$1.class differ diff --git a/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs$2.class b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs$2.class new file mode 100644 index 0000000..03a61bc Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs$2.class differ diff --git a/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs.class b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs.class new file mode 100644 index 0000000..1133a4f Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/GeradorCPFs.class differ diff --git a/target/classes/br/ce/orlando/gerador/cpf/ValidadorCPF.class b/target/classes/br/ce/orlando/gerador/cpf/ValidadorCPF.class new file mode 100644 index 0000000..53a4c96 Binary files /dev/null and b/target/classes/br/ce/orlando/gerador/cpf/ValidadorCPF.class differ diff --git a/target/geradorcpf-orlando-0.0.1-SNAPSHOT.jar b/target/geradorcpf-orlando-0.0.1-SNAPSHOT.jar new file mode 100644 index 0000000..d52982d Binary files /dev/null and b/target/geradorcpf-orlando-0.0.1-SNAPSHOT.jar differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..a202986 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,4 @@ +#Created by Apache Maven 3.8.7 +version=0.0.1-SNAPSHOT +groupId=br.ce.orlando.geradorcpf +artifactId=geradorcpf-orlando diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..29e8003 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,5 @@ +T:\eclipse-workspace\geradorcpf-orlando\src\main\java\br\ce\orlando\gerador\cpf\App.java +T:\eclipse-workspace\geradorcpf-orlando\src\main\java\br\ce\orlando\gerador\cpf\ValidadorCPF.java +T:\eclipse-workspace\geradorcpf-orlando\src\main\java\br\ce\orlando\gerador\cpf\model\GeradorCPF.java +T:\eclipse-workspace\geradorcpf-orlando\src\main\java\br\ce\orlando\gerador\cpf\GeradorCPFs.java +T:\eclipse-workspace\geradorcpf-orlando\src\main\java\br\ce\orlando\gerador\cpf\CpfDocumentFilter.java diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..e69de29