Java - Añadir elementos a un ArrayList ya existente

 
Vista:
sin imagen de perfil
Val: 40
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Añadir elementos a un ArrayList ya existente

Publicado por Fran (13 intervenciones) el 05/05/2021 02:12:41
Buenas.

Previo al trozo de codigo que voy a pegar, se añaden equipos y jueces. Lo que intento ahora es recorrer la arraylist de jueces para puntuar los equipos. Eso me sale hasta ahora (mas o menos).

Lo que me falla es la parte de realizar el ranking con esas puntuaciones. Es como si los elementos de puntos se añadieran al final de la arraylist en lugar de relacionados al equipo que se ha puntuado.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package worldskillscompetitionapp6;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
 
public class Ladder
{
    private static final Ladder INSTANCE = new Ladder();
 
    public Ladder()
    {
    }
 
    public static Ladder getInstance()
    {
        return INSTANCE;
    }
 
    Scanner sc =  new Scanner (System.in);
 
    private String teamID;
    private String judgeID;
    private int pointsScored = 0;
 
    List <Ladder> listOfSOFTeams = new ArrayList<>();
    List <Ladder> listOfSOFJudge = new ArrayList<>();
 
    List <Ladder> listOfNETTeams = new ArrayList<>();
    List <Ladder> listOfNETJudge = new ArrayList<>();
 
    List <Ladder> listOfWEBTeams = new ArrayList<>();
    List <Ladder> listOfWEBJudge = new ArrayList<>();
    String userChoice;
 
    public String getTeamID()
    {
        return teamID;
    }
 
    public void setTeamID(String teamID)
    {
        this.teamID = teamID;
    }
 
    public String getJudgeID()
    {
        return judgeID;
    }
 
    public void setJudgeID(String judgeID)
    {
        this.judgeID = judgeID;
    }
 
    public int getPointsScored()
    {
        return pointsScored;
    }
 
    public void setPointsScored(int pointsScored)
    {
        this.pointsScored = pointsScored;
    }
 
    public void ladderMenu()
    {
        do
        {
            System.out.println("\nLadder Menu:\n"
                    + "1. Score Teams\n"
                    + "2. Display Ranking\n"
                    + "3. Back to Main Menu");
            userChoice = sc.nextLine();
 
            switch(userChoice)
            {
                case "1":
                   UpdatePoints();
                   break;
 
                case "2":
                    DisplayPoints();
                    break;
 
                case "3":
                    WorldSkillsCompetitionApp6.mainMenu();
 
                default:
                    System.out.println("Please, enter a number between 1 and 3\n");
            }
        }while(userChoice != "3");
    }
 
    public void listTeams(String teamID)
    {
       Ladder tmpTeam = new Ladder();
 
       if(teamID.contains("SOF"))
       {
           tmpTeam.setTeamID(teamID);
 
           listOfSOFTeams.add(tmpTeam);
       }
       else if(teamID.contains("NET"))
       {
           tmpTeam.setTeamID(teamID);
 
           listOfNETTeams.add(tmpTeam);
       }
       else if(teamID.contains("WEB"))
       {
           tmpTeam.setTeamID(teamID);
 
           listOfWEBTeams.add(tmpTeam);
       }
 
    }
    public void SOFJudge(String judgeID)
    {
        System.out.println("Hello "+judgeID);
        Ladder tmpSOFJudge = new Ladder();
 
        tmpSOFJudge.setJudgeID(judgeID);
 
        listOfSOFJudge.add(tmpSOFJudge);
    }
    public void UpdatePoints()
    {
        Ladder tmpPoints = new Ladder();
 
        int tmpPointsScored;
        userChoice = Submenu();
 
            switch(userChoice)
            {
                case "1":
                    int judgeCounter;
                    int teamCounter;
 
                    List<Ladder> addPoints = new ArrayList<Ladder>();
 
                    for(Iterator<Ladder> judgeIterator = listOfSOFJudge.iterator();
                            judgeIterator.hasNext();)
                    {
                        System.out.println(judgeIterator.next().getJudgeID());
 
                        for(Iterator<Ladder> teamIterator = listOfSOFTeams.iterator();
                            teamIterator.hasNext();)
                        {
                            Ladder tmpTeam = teamIterator.next();
 
                            System.out.print(tmpTeam.getTeamID() +" - Points: ");
                            tmpPointsScored = sc.nextInt();
                            pointsScored += tmpPointsScored;
                            tmpPoints.setPointsScored(pointsScored);
 
                            addPoints.add(tmpPoints);
                        }
                    }
 
                    listOfSOFTeams.addAll(addPoints);
 
            }
    }
 
    public void DisplayPoints()
    {
        userChoice = Submenu();
 
        switch(userChoice)
        {
            case "1":
 
                int position;
 
               Collections.sort(listOfSOFTeams, new Comparator<Ladder>()
               {
                   @Override
                   public int compare(Ladder p1, Ladder p2)
                   {
                       return new Integer(p2.getPointsScored()).compareTo(p1.getPointsScored());
                   }
               });
 
               for(position = 0; position < listOfSOFTeams.size(); position++)
                {
                    System.out.println(position +. " +listOfSOFTeams.get(position).getTeamID()
                      +"   " +listOfSOFTeams.get(position).getPointsScored());
                }
        }
    }
 
    public String Submenu()
    {
        System.out.println("Which project?\n"
                + "1. Software Development\n"
                + "2. Networking\n"
                + "3. Web Design\n"
                + "4. Back to Ladder Menu");
        return userChoice = sc.nextLine();
    }
}

Gracias por vuestra ayuda!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder