java – Clases con atributos recíprocos

Tengo que crear dos clases, una clase llamada Cliente y otra Pedido.

La primera tiene entre sus atributos un array dinámico de elementos de la clase Pedido que almacena el historial del cliente.

A su vez, La clase Pedido tiene entre sus atributos uno de clase Cliente.

¿Qué debería instanciar primero? ¿Debería tener dos constructores con sobrecarga de Cliente para cuando es cliente nuevo y cuando no? ¿Alguna sugerencia? Gracias de antemano.

java – Registro não apaga e IDE não informa erro

Bom dia galera, estou com um erro inusitado aqui e a IDE não informa o erro, o que está me deixando perdido, também não acho o erro, então resolvi pedir ajuda ao mais experientes. Enxuguei o código para facilitar o entendimento.

Resumindo: ao executar a ação, nada acontece. Nenhum erro aparece e nenhum registro selecionado é apagado.

O que eu estou tentando apagar é um registro de pagamento de uma venda que está relacionada a uma ordem de serviço. O relacionamento no MySql é de muitos para muitos entre a também VENDA e FORMA_DE_PAGAMENTO, originando a tabela VENDA_has_FORMA_DE_PAGAMENTO. Nenhum erro de restrição ocorre.

Desde já, agradeço a ajuda.

Botão de apagar um registro

Cliente c = new Cliente();

OrdemDeServico ods = new OrdemDeServico();

Venda v = new Venda();

FormaDePagamento fdp = new FormaDePagamento();
int linha = jTableVendaTemFormaDePagamento.getSelectedRow();
fdp.setId((int) jTableVendaTemFormaDePagamento.getValueAt(linha, 2));

VendaTemFormaDePagamento vtfdp = new VendaTemFormaDePagamento();

VendaTemFormaDePagamentoDao vtfdpdao = new VendaTemFormaDePagamentoDao();
if (vtfdpdao.delete(vtfdp)) {
     JOptionPane.showMessageDialog(null, "apagado.");
} else {
     JOptionPane.showMessageDialog(null, "não apagou.");

Classe Dao

public boolean delete(VendaTemFormaDePagamento vendaTemFormaDePagamento) {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("DELETE FROM `VENDA_has_FORMA_DE_PAGAMENTO` WHERE `VENDA_ID`=? AND "
                    + "`VENDA_ORDEM_DE_SERVICO_NUMERO_OS`=? AND "
                    + "`VENDA_ORDEM_DE_SERVICO_CLIENTE_CPF`=? AND "
                    + "`FORMA_DE_PAGAMENTO_ID`=?");
            ps.setInt(1, vendaTemFormaDePagamento.getVendaId().getID());
            ps.setInt(2, vendaTemFormaDePagamento.getVendaOrdemDeServicoNumeroOs().getOrdemDeServicoNumeroOS().getNumeroOS());
            ps.setString(3, vendaTemFormaDePagamento.getVendaOrdemDeServicoClienteCpf().getOrdemDeServicoClienteCpf().getCPF());
            ps.setInt(4, vendaTemFormaDePagamento.getFormaDePagamentoId().getId());
            return true;
        } catch (SQLException ex) {
            System.err.println("Erro " + ex);
            return false;
        } finally {
            ConnectionFactory.closeConnection(conn, ps);

A classe bean

package model.bean;

import java.sql.Time;
import java.util.Date;

public class VendaTemFormaDePagamento extends FormaDePagamento {

    private Venda vendaId;
    private Venda vendaOrdemDeServicoNumeroOs;
    private Venda vendaOrdemDeServicoClienteCpf;
    private FormaDePagamento formaDePagamentoId;
    private Date dataDoPagamento;
    private Time horarioDoPagamento;
    private String transacao;
    private double valorPago;

    public VendaTemFormaDePagamento() {

    public Venda getVendaId() {
        return vendaId;

    public void setVendaId(Venda vendaId) {
        this.vendaId = vendaId;

    public Venda getVendaOrdemDeServicoNumeroOs() {
        return vendaOrdemDeServicoNumeroOs;

    public void setVendaOrdemDeServicoNumeroOs(Venda vendaOrdemDeServicoNumeroOs) {
        this.vendaOrdemDeServicoNumeroOs = vendaOrdemDeServicoNumeroOs;

    public Venda getVendaOrdemDeServicoClienteCpf() {
        return vendaOrdemDeServicoClienteCpf;

    public void setVendaOrdemDeServicoClienteCpf(Venda vendaOrdemDeServicoClienteCpf) {
        this.vendaOrdemDeServicoClienteCpf = vendaOrdemDeServicoClienteCpf;

    public FormaDePagamento getFormaDePagamentoId() {
        return formaDePagamentoId;

    public void setFormaDePagamentoId(FormaDePagamento formaDePagamentoId) {
        this.formaDePagamentoId = formaDePagamentoId;

    public Date getDataDoPagamento() {
        return dataDoPagamento;

    public void setDataDoPagamento(Date dataDoPagamento) {
        this.dataDoPagamento = dataDoPagamento;

    public Time getHorarioDoPagamento() {
        return horarioDoPagamento;

    public void setHorarioDoPagamento(Time horarioDoPagamento) {
        this.horarioDoPagamento = horarioDoPagamento;

    public String getTransacao() {
        return transacao;

    public void setTransacao(String transacao) {
        this.transacao = transacao;

    public double getValorPago() {
        return valorPago;

    public void setValorPago(double valorPago) {
        this.valorPago = valorPago;


java – recreate fragment android

tengo un problema al tratar de actualizar la vista de un fragment.

Tengo un fragment que muestra los datos obtenidos desde firestore, desde el abro una activity nueva para realizar algunas updates en la db ( esta se abre en modo dialog ), quiero que al cerrar la ventana ( activity ) se vuelva a recargar el fragment mostrando los datos nuevos.

Probé con OnRestart onResume pero ambos también se ejecutan cuando inicio el fragment, yo solo quiero que se recargue cuando cierre la actividad que inicie.

java – Offline audio comparison for Android

I have two audio clips:

  1. Source of truth
  2. Recording of user

I want to compare the two, testing if they are similar enough, removing accents, etc. Any idea how I could do this on Android?

To add more detail, I want to record the user reciting some Arabic and then compare it to the correct pronunciation. The idea would be test their pronunciation and give them feedback on where they need to improve. I’m thinking of doing this offline (vs online) for faster response times to the user.

java – How do i efficiently read random lines from a TXT or CSV file?

Context of the problem:

  1. I have made chess GUI (Java)
  2. The GUI is capable of loading chess puzzles/problems to solve
  3. Of said puzzles, i have gotten my hands on a database which is just shy of a million entries

The problem/question:
How does one most efficiently go about getting a random puzzle from this database?

Obviously keeping the database in memory is not an option, despite it already being compressed. Stripped all data that isn’t needed from it and in the end converted it into byte-arrays. All to no avail. The full database always ends up taking up somewhere between 100 and 200 MB of memory. A tenth of that would be acceptable (for my purposes).

Even worse: When processing the entire database (in attempts to keep it all in memory), the process file->memory took upwards of 700 MB memory.

Let’s say i have the following:

  • The database as either a txt or csv file
  • The amount of lines in said file (which is equal to the amount of puzzles)

Am i with that, in some way, capable of grabbing either a random or specific puzzle from the file (albeit async, that doesn’t matter) without having to load the entire database into memory?

Thanks for your time!

java – How to integrate Firebase to Libgdx?

I have an android game built with libgdx and what I’d like to do is to add some kind of authentication so I can connect the game with user data to firebase database.

I have some experience with firebase using it in native android apps, but I have no idea on how to connect “native code” with libgdx.

Researching I found these third party libs and

But as it does not provide any example it didn’t help me much.

So what’s the best way to “mix” native android code and libgdx? I want to be able to achieve things like storing data on db, implementing in-app-purchases, authenticating users. If I’m looking the wrong way please let me know too.

java – help me solve it i want to find the occurrence of number in an array and a combination of same element into an arraylist

I am trying to find the combination of same numbers in an array in java e.g i have number in an array e.g numbers{1,2,2,2,1,1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1} .So i want the program to check all combinations of number that occurred in a sequence of order only like 1, or 2,2,2 or 1,1,1 or 2,2,2 then it will check how many times did 2 or 1 occur so the occurred number will be stored in an arraylist. In that case first 1 occurred 1 time then 2 occured 3 times then 1 occurred 3 times then 2 occurred 3 times and so on it will show how elements are arranged in an array .i have wrote a code but it is not working in java as i want it to

public class Main {

private static ArrayListPlistItem=new ArrayList();

private static ArrayListNlistItem=new ArrayList();

private static ArrayListPwillhappen=new ArrayList();

private static ArrayListNNhappen=new ArrayList();

private static ArrayList LhappenList=new ArrayList();

private static ArrayList NhappenList=new ArrayList();

private static ArrayList LhappenListTotal=new ArrayList();

private static ArrayList NhappenListTotal=new ArrayList();

private static int Lhappen=0;
private static int Lhappen2=0;

private static int Nhappen=0;
private static int Nhappen2=0;
private static int () items ={1,2,2,2,1,1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1};
private static int () Nhappenitems =new int(1);
private static int () Lhappenitems =new int(1);

public static void main(String ()args){


//comparing elements from an array which with an arraylist number
for(int y=0; y<items.length; y++){

//counter to count how many times 1 occurred
//counter to count how many times 1 occurred
//adding the counter in arraylist
Pwillhappen.add(0, Lhappen);

//adding the numbers how many times it occured in arraylist

//adding in total number it occur in an arraylist
//setting the counter into 0

}else if(NlistItem.get(0)==items(y)){
//counter to count how many times 2 occurred
//counter to count how many times 2 occurred
//adding the counter in arraylist
//adding the numbers how it many times it occurred in arraylist

//adding in total number it occur in an arraylist
NhappenListTotal.add(0, NNhappen.get(0));
//setting the counter into 0



System.out.println(” RESULTS ” );
System.out.println(“1 occurrence total “+ Nhappenitems(0) +” times”);
System.out.println(“2 occurence total “+ Lhappenitems(0) +” times”);
for(int i=0; i<LhappenListTotal.size(); i++){
System.out.println(” combination of 1’s occurred “+ LhappenListTotal.get(i) +” times”);


for(int i=0; i<NhappenListTotal.size(); i++){

System.out.println(“combination of 2’s occurred “+ NhappenListTotal.get(i)+” times”);



1 occurrence total 11 times

2 occurence total 10 times

combination of 1’s occurred 1 times

combination of 1’s occurred 3 times

combination of 1’s occurred 2 times

combination of 1’s occurred 1 times

combination of 1’s occurred 2 times

combination of 1’s occurred 1 times

combination of 1’s occurred 3 times

combination of 1’s occurred 2 times

combination of 1’s occurred 1 times

combination of 1’s occurred 1 times

combination of 2’s occurred 2 times

combination of 2’s occurred 1 times

combination of 2’s occurred 3 times

combination of 2’s occurred 2 times

combination of 2’s occurred 1 times

combination of 2’s occurred 3 times

combination of 2’s occurred 2 times

combination of 2’s occurred 1 times

combination of 2’s occurred 3 times

combination of 2’s occurred 2 times

combination of 2’s occurred 1 times

my code when i run it is showIng as above results instead i want my results to be as below so help me to solve the code to produce the results as below it show produce the result same as the elements how they are arranged in an array.. items ={1,2,2,2,1,1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1};


combination of 1’s occurred 1 times

combination of 1’s occurred 3 times

combination of 1’s occurred 2 times

combination of 1’s occurred 3 times

combination of 1’s occurred 1 times

combination of 2’s occurred 3 times

combination of 2’s occurred 3 times

combination of 2’s occurred 3 times

combination of 2’s occurred 2 times

google sheets – VBA Syntax into Java

anyone can help me translating this VBA code so that I can use it for google sheets? Would be awesome.

​Sub test()

 Dim row As Integer
 Dim row2 As Integer

 row = 1
 row2 = 1
 Do While (Cells(row, 1) <> "")
    If Cells(row, 2) = "x" Then
        Sheets("Table2").Cells(row2, 1) = Cells(row, 1)
        row2 = row2 + 1
    End If
    row = row + 1
End Sub

spring boot – CE_CLASS_ENVY Method excessively uses methods of another class Java

Spotbug is giving the violation for the below code

    class LocationServiceTest {
    LocationService locationService;
    void shouldFetchAllLocation_whenQueried() {
       when(locationService.findAll(any(),any()).thenReturn(List.of(location("LOC1"), location("LOC2"))
   private Location location(String id) {
        return Location.builder()

This method(location) makes extensive use of methods from another class over methods of its own, but the method location(String id) calling in the same class only not in any other Test class but still gives this spotbug violation .

Thanks for your help !!