Random class doubt in Java

Asked

Viewed 54 times

-1

Guys I’m trying to create a system of numbers mega sena. Basically I have to have a client(id) that will generate 6 random numbers.

public class Cliente implements Serializable {
    
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String nome;
    private String email;
    private int numeros;

I created the client class that has name, email and numbers.

public class Random1 {

    public static void main(String[] args) {

        Random gerador = new Random();
     
        for (int i = 0; i < 6; i++) {
            System.out.println(gerador.nextInt(59));
        }

And I created the Random class, the doubt and the following, as I do for each game created of 6 numbers, get attached to an ID in the client table?

    1. Just so you know, Random1 is a bad name in terms of class modeling, says nothing about what the class is or does. 2) The way of generating is wrong, because it is letting repeat numbers. 3) In terms of object-relational mapping it makes no sense to have a field int numeros, would be an array or list of numbers with fixed size at 6, and indeed even this is wrong because a customer could make several bets so should have the concept of Aposta each with six numbers. Finally, by BD normalization it is not advisable to keep the six numbers in a single column of the table.
  • Anyway, object orientation is one thing, relational database another, orm is the idea of relating these two worlds (which in your case fatally will involve more than one table and foreign keys) and personally I think it should be studied after you already have a good idea of the first two. But each one is each one. Also one studied on how to raise requirements.

  • Your biggest doubt is generating the 6 random numbers or is saving them in the database?

  • @Piovezan Thanks for the tips friend, as I’m still graduating, I lack enough knowledge even.

  • @Victorstafuses that friend, I can generate the numbers with the Andom class, but I can’t link the numbers generated to a client class understand?

1 answer

0

A more didactic way to solve it is like this:

public class Cliente implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String nome;
    private String email;
    private int numero1;
    private int numero2;
    private int numero3;
    private int numero4;
    private int numero5;
    private int numero6;

    public Cliente(int[] numeros){
        int numero1 = numeros[0];
        int numero2 = numeros[1];
        int numero3 = numeros[2];
        int numero4 = numeros[3];
        int numero5 = numeros[4];
        int numero6 = numeros[5];
    }
}

public class Random1 {
    public static void main(String[] args) {

        Random gerador = new Random();
        
        int[] valoresSorteados = new int[6];
        for (int i = 0; i < 6; i++) {
            int valorRandom = gerador.nextInt(59);
            System.out.println(valorRandom);
            valoresSorteados[i] = valorRandom;
        }
        Cliente cliente = new Cliente(valoresSorteados);
    }
}

But it is worth noting the following, for a study code anything meets, in a project of truth several problems with this implementation will appear, as for example trust that will come an array of 6 positions in the constructor of the client class, so it is always good to try to follow the good practices of code of the market. If this is only to study object orientation I recommend you remove the part of the database to make it easier, after Voce solve the problem of the logic that Voce needs is only to save in the database, go by parts

Browser other questions tagged

You are not signed in. Login or sign up in order to post.