How to do so that I do not repeat the elements of a RANDOM WITH ELEMENTS OF ARRAYLIST JAVA

0

In this case when I'm going to print the product data I have put a random amount and price because when you start the program each product has different amounts and different prices, the problem I have is that I do it randomly but it repeats me prices and quantities then I do not do well, I would have to do randomly the quantity and prices but of the 8 products and not repeat them and I do not know how to do it! Thank you, the code I mention is at the bottom!

package m3.uf5.ac02;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

/**
 *
 * @author Alumno
 */
public class M3UF5AC02 {

    /**
     * @param args the command line arguments
     */




       static Stack <Alumno> alumnos = new Stack <Alumno> ();
       static Stack <Profesor> profesores = new Stack <Profesor> ();
       static Stack <Persona> profalum = new Stack <Persona> ();

    public static void main(String[] args) {
        // TODO code application logic here
       Scanner Scan = new Scanner(System.in);
       ArrayList <Integer> l = new ArrayList<Integer>();



       //Añadimos los datos a la arrayList 
       l.add(1);
       l.add(2);
       l.add(2);
       l.add(4);
       l.add(5);
       l.add(2);
       l.add(7);
       l.add(8);
       l.add(9);






       String f;

       System.out.println("1.- Eliminar Elemento de un ArrayList");
       System.out.println("2.- Datos a replazar en una pila");
       System.out.println("3.- Datos a replazar en una ArrayList");
       System.out.println("4.- Indica la posicion que quieres imprimir ");
       System.out.println("5.- Apilar los Profesor y los Alumnos en una misma pila");
       System.out.println("6.- Mostrar Productos y Sumar su Precio final");
       System.out.println("Indica la Intruccion a Realizar ");
       f = Scan.next();



       switch(f){

           case "1":
           borrarnum(l);

           break;

           case "2":

                remplazarpilas();


           case "3":
               mezcla();




           case "4":


               imprimeenteros();


           case "5":

               apilar();



           case "6":

               productos();

    }









    }

    public static void borrarnum(ArrayList <Integer> l){
       Scanner Scan = new Scanner(System.in);
       System.out.println("Indicame el Numero a Eliminar de el ArrayList");
       int n = Scan.nextInt();




       for(int i = 0; i<l.size(); i++){
          if(l.get(i)==n){ 
              l.remove(i);
               i--;
               System.out.println("Se ha eliminado");

          } 


       }
        System.out.println(l);



    }


    public static void remplazarpilas(){
        Scanner Scan = new Scanner(System.in);
       Stack <Integer> pila1 = new Stack <Integer> ();
       Stack <Integer> pila2 = new Stack <Integer> ();

       //Apilamos los datos a la pila
       pila1.push(1);
       pila1.push(2);
       pila1.push(3);
       pila1.push(4);
       pila1.push(5);
       pila1.push(6);
       pila1.push(7);
       pila1.push(8);
       pila1.push(9);

         System.out.println(pila1);



        System.out.println("Indicame el Dato a remplazar:");
        int DatoR = Scan.nextInt();

        System.out.println("Indicame el nuevo dato:");
        int DatoN = Scan.nextInt();



       while(!pila1.empty()){
           int dato = pila1.pop();

          if(dato !=DatoR){
              pila2.push(dato);
          }else{    
              pila1.push(DatoN);

          }
       }

        while(!pila2.empty()){
            pila1.push(pila2.pop());

        }  

          System.out.println(pila1);   
    }


    public static void mezcla(){

        ArrayList <Persona> Lista1 = new ArrayList<Persona>();
        ArrayList <Persona> Lista2 = new ArrayList<Persona>();
        ArrayList <Persona> mostrarPersonas = new ArrayList<Persona>();


       //Añadimos los datos a la arrayList de Lista1

       Persona p1= new Persona("pol","Aguilar");
       Lista1.add(p1);

       Persona p2= new Persona("Roger","Carreras");
       Lista1.add(p2);

       Persona p3= new Persona("Nona","Ruiz");
       Lista1.add(p3); 

       //Añadimos los datos a la arrayList de Lista2
       Persona p4 = new Persona("Juan" , "Carlos");
       Lista2.add(p4);

       Persona p5 = new Persona("Aitor" , "Carreras");
       Lista2.add(p5);

       Persona p6 = new Persona("Nuria" , "Gomez");
       Lista2.add(p6);


       for(int x=0; x<Lista1.size(); x++){

           Persona per = Lista1.get(x);
           mostrarPersonas.add(per);
       }

     for(int y=0; y<Lista1.size(); y++){

           Persona per = Lista2.get(y);
           mostrarPersonas.add(per);
       }

     for(int x=0; x<mostrarPersonas.size(); x++){
         System.out.println("---------------------------");
         System.out.println("Nombre: " + mostrarPersonas.get(x).getNombre());
         System.out.println("Apellido: " + mostrarPersonas.get(x).getApellidos());
     }  


    }

     public static void imprimeenteros(){

          ArrayList <Integer> enteros = new ArrayList<Integer>();
          Scanner Scan = new Scanner(System.in);


           enteros.add(1);
           enteros.add(2);
           enteros.add(3);
           enteros.add(4);
           enteros.add(5);

          int dato;


          System.out.println("Indicame la Posicion que quieres comenzar a imprimir:");
          dato = Scan.nextInt();




          for(int i=dato; i>=0; i--){

              System.out.println(enteros.get(i));



          }



     }


     public static void apilar(){
        boolean salir = false;   
    while(!salir){    
       Scanner Scan = new Scanner(System.in);
       System.out.println("1.- Alumno");
       System.out.println("2.- Profesor");
       System.out.println("3.- Mostrar Alumnos y profesores");

       int opcion = Scan.nextInt();

       Alumno alum = new Alumno();
       Profesor prof = new Profesor();

      switch(opcion){

          case 1:

           System.out.println("Nombre Alumno: ");
           String nombrealum = Scan.next() ;
           alum.setNombre(nombrealum);

           System.out.println("Edad Alumno: ");
           int edadalum = Scan.nextInt();
           alum.setEdad(edadalum);


           alumnos.push(alum);

           Alumno datosalumnos = (Alumno) alumnos.pop();
           profalum.push(datosalumnos);


          System.out.println("Se ha apilado correctamente.");  

           break;




          case 2:

              System.out.println("Nombre Profesor:");
              String nombreprof = Scan.next();
              prof.setNombre(nombreprof);

              System.out.println("Edad Profesor:");
              int edadprof = Scan.nextInt();
              prof.setEdad(edadprof);

              profesores.push(prof);


              Profesor datosprofesores = (Profesor) profesores.pop();
              profalum.push(datosprofesores); 


              System.out.println("Se ha apilado correctamente.");

              break;



          case 3:

                  for(int x = 0; x<profalum.size(); x++){

                      System.out.println("Nombre: " + profalum.get(x).getNombre());
                      System.out.println("Edad: " + profalum.get(x).getEdad());



                  }

          case 4: 

                salir = true;

                 break;

           }









      }

     }

     public static void productos(){

         String espacio="    ";
         String espacio1 ="        ";
         String espacio2 ="            ";

         Random ran = new Random();

         ArrayList <Productos> p = new ArrayList<Productos>();



         Productos Productos1 = new Productos("Producto1", 5, 2.6);
         p.add(Productos1);

         Productos Productos2 = new Productos("Producto2",8,3.5);
         p.add(Productos2);

         Productos Productos3 = new Productos("Producto3",2,5.4);
         p.add(Productos3);

         Productos Productos4 = new Productos("Producto4",10,9.5);
         p.add(Productos4);

         Productos Productos5 = new Productos("Producto5",3,4.5);
         p.add(Productos5);

         Productos Productos6 = new Productos("Producto6",1,7.2);
         p.add(Productos6);

         Productos Productos7 = new Productos("Producto7",7,20.5);
         p.add(Productos7);


         Productos Productos8 = new Productos("Producto8",10,8.5);
         p.add(Productos8);

         double preciototal;

         preciototal = Productos1.precioFinal() + Productos2.precioFinal() + Productos3.precioFinal() + Productos4.precioFinal() + Productos5.precioFinal() + Productos6.precioFinal() + Productos7.precioFinal() + Productos8.precioFinal();



        for(int x=0; x<p.size(); x++) {

         int randomIndex = ran.nextInt(p.size());
         System.out.println("Nombre" + "----"  + "Cantidad" +"----" +"Precio" +"----" + "----" + "Total") ;
         System.out.println(p.get(x).getNombres()+ espacio + p.get(randomIndex).getCantidad() + espacio1+  p.get(randomIndex).getPrecio()+ espacio2 + p.get(x).precioFinal());



        }
          System.out.println("Precio TOTAL: " + preciototal);

     }





 }
    
asked by KratosDofus 18.12.2018 в 11:11
source

1 answer

1

If you're looking to print the products in a pseudo-random order without repeating them, you can use an auxiliary list, use random to select the item, and then remove it from the list so it can not be chosen again.

ArrayList<Productos> aux = new ArrayList<Productos>(p);
int cantItems = aux.size();
for(int x=0; x < cantItems ; x++) {

     int randomIndex = ran.nextInt(aux.size());
     Productos productoRandom = aux.remove(randomIndex);
     System.out.println("Nombre" + "----"  + "Cantidad" +"----" +"Precio" +"----" + "----" + "Total") ;
     System.out.println(productoRandom.get(x).getNombres()+ espacio + productoRandom.getCantidad() + espacio1+  productoRandom.getPrecio()+ espacio2 + productoRandom.precioFinal());

    }
      System.out.println("Precio TOTAL: " + preciototal);

Thanks @JDev for the correction in the number of loops!

    
answered by 18.12.2018 / 12:45
source