Principal,java
import java.io.IOException;
import java.util.Scanner;
public class Principal {
public static void main(String[] args) {
Scanner find = new Scanner(System.in);
SearchCep searchCep = new SearchCep();
System.out.println("Digite seu CEP para obtermos seu endereço.");
var cep = find.nextLine();
try {
Address newAddress = searchCep.searchAddress(cep);
System.out.println(newAddress);
FilesCreator creator = new FilesCreator();
creator.salvaJson(newAddress);
} catch (RuntimeException | IOException e) {
System.out.println(e.getMessage());
System.out.println("Operações encerradas.");
}
}
}
Address.java
public record Address(String cep, String logradouro,
String bairro,
String localidade, String uf) {
}
FilesCreator.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
public class FilesCreator {
public void salvaJson(Address address) throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
FileWriter escrita = new FileWriter(address.cep() + ".json");
escrita.write(gson.toJson(address));
escrita.close();
}
}
SearchCep.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
public class FilesCreator {
public void salvaJson(Address address) throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
FileWriter escrita = new FileWriter(address.cep() + ".json");
escrita.write(gson.toJson(address));
escrita.close();
}
}