Help in project using ArrayLists

1

Hello I need help for a project I could not finish because I do not know how to do that.

Well the problem is that the project is Arraylist that arraylist full of information on "projects" for that I ask the user the number of proy, name, budget and percentage of progress of the proy, what happens is that if I ask but what happens if the user wants to put another project, it does the same but it would be a project with different data.

In a menu I have an option that says "Consult specific project" so I ask for the proy number and automatically the prog pulls me the info of said project depending on the number.

and when I try to do that I get all the info that I got, that is, if the number of the first project is 1 and the second is 13 and the user wants to print only the info of 1 project and type 1 but also the proy 13 info comes out because everything was printed.

Help please!

Here the code that I have written:

public static void main(String[] args) {
  ArrayList P1 = new ArrayList();

  Scanner sc = new Scanner(System.in);

  int op = 0, num, sn, x = 01, i, n;

  String nom;

  double pt, pr = 0;

  do {
   op = Integer.parseInt(JOptionPane.showInputDialog(null, " REGISTRO DE PROYECTOS\n" 
    + "\n 1 - Registrar datos de proyecto\n"

    + "2 - Actualizar porcentaje de avance de proyectos\n"

    + "3 - Consultar un proyecto en especifico\n"

    + "4 - Mostrar datos de todos los proyectos\n"

    + "5 - Eliminar un registro de preyecto en especifico\n"

    + "6 - Eliminar todos los registros de proyecto\n "));



   switch (op) {

    case 1:
     System.out.println("*************DATOS DEL PROYECTO *************");
     System.out.println("\n");
     System.out.println("Numero del proyecto:");
     num = sc.nextInt();
     P1.add(num);
     System.out.println("Nombre del proyecto:");
     nom = sc.next();
     P1.add(nom);
     System.out.println("Presupuesto para el proyecto:");
     pt = sc.nextDouble();
     P1.add(pt);
     System.out.println("Porcentaje de avance del proyecto:");
     pr = sc.nextDouble();
     P1.add(pr);
     break;


    case 2:

     System.out.println("Nuevo porcentaje de proyecto:");
     pr = sc.nextInt();
     if (pr == 100) {
      JOptionPane.showMessageDialog(null, "*****ACTUALIZACION*****\n" + "PROYECTO CONCLUIDO");
     } else {
      JOptionPane.showMessageDialog(null, "*****ACTUALIZACION*****\n" + "PROYECTO EN EJECUCION");
     }

     System.out.println("\n");

     System.out.println("***************************************************************************");
     System.out.println("***************************************************************************");
     System.out.println("\n");

    case 3:
     System.out.println("*****CONSULTA DE PROYECTO*****");


     for (i = 0; i <= P1.size() - 1; i++) {
      System.out.println(P1.get(i));

     }

   }
   System.out.println("Desea volver al menu (SI = 1 / NO = 2)");
   sn = sc.nextInt();
  } while (sn == 1);
    
asked by Jose Moreno Gomez 04.07.2018 в 19:45
source

2 answers

1

One of the options that I could find was doing it POO, creating a project class:

public class proyecto {

    protected int num_proj;
    protected String name_proj;
    protected double estimate_proj;
    protected double percentage_proj;

    public proyecto(){}

    public proyecto(int num_proj, String name_proj, double estimate_proj, double percentage_proj) {
        this.num_proj = num_proj;
        this.name_proj = name_proj;
        this.estimate_proj = estimate_proj;
        this.percentage_proj = percentage_proj;
    }

    public int getNum_proj() {
        return num_proj;
    }

    public void setNum_proj(int num_proj) {
        this.num_proj = num_proj;
    }

    public String getName_proj() {
        return name_proj;
    }

    public void setName_proj(String name_proj) {
        this.name_proj = name_proj;
    }

    public double getEstimate_proj() {
        return estimate_proj;
    }

    public void setEstimate_proj(double estimate_proj) {
        this.estimate_proj = estimate_proj;
    }

    public double getPercentage_proj() {
        return percentage_proj;
    }

    public void setPercentage_proj(double percentage_proj) {
        this.percentage_proj = percentage_proj;
    }
}

Then In the main class it would be nothing else to modify the steps:

public static void main(String[] args){

        ArrayList<proyecto> Proyectos = new ArrayList();

        Scanner sc = new Scanner(System.in);

        int op = 0, num, sn, x = 01, i, n;

        String nom;

        double pt, pr = 0;

        do {
            op = Integer.parseInt(JOptionPane.showInputDialog(null, " REGISTRO DE PROYECTOS\n"
                    + "\n 1 - Registrar datos de proyecto\n"

                    + "2 - Actualizar porcentaje de avance de proyectos\n"

                    + "3 - Consultar un proyecto en especifico\n"

                    + "4 - Mostrar datos de todos los proyectos\n"

                    + "5 - Eliminar un registro de preyecto en especifico\n"

                    + "6 - Eliminar todos los registros de proyecto\n "));



            switch (op) {

                case 1:
                    proyecto p = new proyecto();
                    System.out.println("*************DATOS DEL PROYECTO *************");
                    System.out.println("\n");
                    System.out.println("Numero del proyecto:");
                    num = sc.nextInt();
                    p.setNum_proj(num);
                    System.out.println("Nombre del proyecto:");
                    nom = sc.next();
                    p.setName_proj(nom);
                    System.out.println("Presupuesto para el proyecto:");
                    pt = sc.nextDouble();
                    p.setEstimate_proj(pt);
                    System.out.println("Porcentaje de avance del proyecto:");
                    pr = sc.nextDouble();
                    p.setPercentage_proj(pr);
                    Proyectos.add(p);
                    break;


                case 2:

                    System.out.println("Digite numero del proyecto al que quiere editar el porcentaje");
                    proyecto ps = new proyecto();
                    int find = sc.nextInt();
                    int pos = 0;
                    for (int j = 0; j < Proyectos.size(); j++){
                        if(Proyectos.get(j).getNum_proj()==find){
                            ps = Proyectos.get(j);
                            pos = j;
                            break;
                        }
                    }
                    System.out.println("Nuevo porcentaje de proyecto:");
                    pr = sc.nextInt();
                    if (pr == 100) {
                        ps.setPercentage_proj(pr);
                        Proyectos.set(pos, ps);
                        JOptionPane.showMessageDialog(null, "*****ACTUALIZACION*****\n" + "PROYECTO CONCLUIDO");
                    } else {
                        ps.setPercentage_proj(pr);
                        Proyectos.set(pos, ps);
                        JOptionPane.showMessageDialog(null, "*****ACTUALIZACION*****\n" + "PROYECTO EN EJECUCION");
                    }

                    System.out.println("\n");

                    System.out.println("***************************************************************************");
                    System.out.println("***************************************************************************");
                    System.out.println("\n");
                    break;
                case 3:
                    System.out.println("*****CONSULTA DE PROYECTO*****");
                    System.out.println("Digite numero de proyecto a consultar");
                    int search = sc.nextInt();
                    for (int j = 0; j < Proyectos.size(); j++){
                        if(Proyectos.get(j).getNum_proj()==search){
                            System.out.println("Numero: " + Proyectos.get(j).getNum_proj());
                            System.out.println("Nombre: " + Proyectos.get(j).getName_proj());
                            System.out.println("Presupuesto: " + Proyectos.get(j).getEstimate_proj());
                            System.out.println("Avance: " + Proyectos.get(j).getPercentage_proj() + "%");

                            break;
                        }
                    }


            }
            System.out.println("Desea volver al menu (SI = 1 / NO = 2)");
            sn = sc.nextInt();
        } while (sn == 1);

    }

That would be everything

    
answered by 05.07.2018 / 20:21
source
0

In the case of taking out a specific project I see that it is option 3 which is

case 3:
     System.out.println("*****CONSULTA DE PROYECTO*****");
     for (i = 0; i <= P1.size() - 1; i++) {
      System.out.println(P1.get(i));
     }
   }

There, what you are doing is printing a line of text and then going through array P1 and showing each and every one of the results. To catch only 1 result it should be

 case 3:
         System.out.println("*****CONSULTA DE PROYECTO*****");
         /* variable 'i' será el ID introducido por el usuario, por lo que se deberá de recoger anteriormente su respuesta. */
          System.out.println(P1.get(i));
       }
    
answered by 05.07.2018 в 09:28