-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit deebaa3
Showing
27 changed files
with
414 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="test" value="true"/> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> | ||
<attributes> | ||
<attribute name="test" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>geradorcpf</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
activeProfiles= | ||
eclipse.preferences.version=1 | ||
resolveWorkspaceProjects=true | ||
version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>br.ce.orlando.geradorcpf</groupId> | ||
<artifactId>geradorcpf-orlando</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>gerador-cpf</name> | ||
<description>script para gerar cpf aleatórios e válidos, utilizando fórmula matemática padrão para cpf brasileiro.</description> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>br.ce.orlando.gerador.cpf.App</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package br.ce.orlando.gerador.cpf; | ||
/** | ||
* | ||
* @author orlando.junior | ||
* | ||
*/ | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
new GeradorCPFs(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/br/ce/orlando/gerador/cpf/CpfDocumentFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
src/main/java/br/ce/orlando/gerador/cpf/GeradorCPFs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: br.ce.orlando.gerador.cpf.App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
7 changes: 7 additions & 0 deletions
7
target/classes/META-INF/maven/br.ce.orlando.geradorcpf/geradorcpf-orlando/pom.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.