nfe-simulator-java
Java
// FASE 1 – Simulador Console
// Emitente Destinatario Produto ItemNFe NFe GeradorXML
// Ambiente: 1 = Produção | 2 = Homologação
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
class Emitente {
private String nome;
private String cnpj;
public Emitente(String nome, String cnpj) {
this.nome = nome;
this.cnpj = cnpj;
}
public String getNome() { return nome; }
public String getCnpj() { return cnpj; }
}
class Destinatario {
private String nome;
private String documento;
public Destinatario(String nome, String documento) {
this.nome = nome;
this.documento = documento;
}
public String getNome() { return nome; }
public String getDocumento() { return documento; }
}
class Produto {
private String nome;
private double valor;
private String ncm;
private String cfop;
private String cst;
public Produto(String nome, double valor, String ncm, String cfop, String cst) {
this.nome = nome;
this.valor = valor;
this.ncm = ncm;
this.cfop = cfop;
this.cst = cst;
}
public String getNome() { return nome; }
public double getValor() { return valor; }
public String getNcm() { return ncm; }
public String getCfop() { return cfop; }
public String getCst() { return cst; }
public double calcularICMS() {
return valor * 0.18; // exemplo 18%
}
}
class NFe {
private Emitente emitente;
private Destinatario destinatario;
private List<Produto> produtos = new ArrayList<>();
private int ambiente; // 1 = Produção | 2 = Homologação
public NFe(Emitente emitente, Destinatario destinatario, int ambiente) {
this.emitente = emitente;
this.destinatario = destinatario;
this.ambiente = ambiente;
}
public void adicionarProduto(Produto p) {
produtos.add(p);
}
public double calcularTotal() {
return produtos.stream()
.mapToDouble(Produto::getValor)
.sum();
}
public double calcularTotalICMS() {
return produtos.stream()
.mapToDouble(Produto::calcularICMS)
.sum();
}
private String gerarChaveAcesso() {
String uf = "35"; // SP
String anoMes = "2502"; // Fev 2025
String cnpj = emitente.getCnpj();
String modelo = "55";
String serie = "001";
String numero = String.format("%09d", (int)(Math.random() * 100000));
String codigoNumerico = String.format("%08d", (int)(Math.random() * 100000));
String chaveParcial = uf + anoMes + cnpj + modelo + serie + numero + codigoNumerico;
return chaveParcial; // 43 ou 44 dependendo do ajuste
}
public String gerarXML() {
StringBuilder xml = new StringBuilder();
xml.append("<NFe>\n");
xml.append(" <infNFe Id=\"").append(gerarChaveAcesso()).append("\">\n");
xml.append(" <ide>\n");
xml.append(" <tpAmb>").append(ambiente).append("</tpAmb>\n");
xml.append(" <natOp>Venda de Mercadoria</natOp>\n");
xml.append(" </ide>\n");
xml.append(" <emit>\n");
xml.append(" <xNome>").append(emitente.getNome()).append("</xNome>\n");
xml.append(" <CNPJ>").append(emitente.getCnpj()).append("</CNPJ>\n");
xml.append(" </emit>\n");
xml.append(" <dest>\n");
xml.append(" <xNome>").append(destinatario.getNome()).append("</xNome>\n");
xml.append(" <CPF>").append(destinatario.getDocumento()).append("</CPF>\n");
xml.append(" </dest>\n");
int i = 1;
for (Produto p : produtos) {
xml.append(" <det nItem=\"").append(i++).append("\">\n");
xml.append(" <prod>\n");
xml.append(" <xProd>").append(p.getNome()).append("</xProd>\n");
xml.append(" <NCM>").append(p.getNcm()).append("</NCM>\n");
xml.append(" <CFOP>").append(p.getCfop()).append("</CFOP>\n");
xml.append(" <vProd>").append(String.format("%.2f", p.getValor())).append("</vProd>\n");
xml.append(" </prod>\n");
xml.append(" <imposto>\n");
xml.append(" <ICMS>\n");
xml.append(" <CST>").append(p.getCst()).append("</CST>\n");
xml.append(" <vICMS>").append(String.format("%.2f", p.calcularICMS())).append("</vICMS>\n");
xml.append(" </ICMS>\n");
xml.append(" </imposto>\n");
xml.append(" </det>\n");
}
xml.append(" <total>\n");
xml.append(" <vNF>").append(String.format("%.2f", calcularTotal())).append("</vNF>\n");
xml.append(" <vICMS>").append(String.format("%.2f", calcularTotalICMS())).append("</vICMS>\n");
xml.append(" </total>\n");
xml.append(" </infNFe>\n");
xml.append("</NFe>");
return xml.toString();
}
}
public class Main {
public static void main(String[] args) {
Emitente emitente = new Emitente("Empresa João LTDA", "12345678000100");
Destinatario destinatario = new Destinatario("Cliente Teste", "12345678900");
NFe nfe = new NFe(emitente, destinatario, 2);
nfe.adicionarProduto(new Produto("Produto A", 100.00, "22030000", "5102", "00"));
nfe.adicionarProduto(new Produto("Produto B", 200.00, "22030000", "5102", "00"));
System.out.println("===== XML GERADO =====");
System.out.println(nfe.gerarXML());
}
}
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.