We I have this college assignment where I have to Read a file with a list of names and add up to 3 presents to each one. I can do it but the presents are repeating and some people in the list are getting the same present more than once. How can I stop it so each person receives different variety of present each time?
Here is my code:
public static void main(String[] args) throws IOException {
String path = "Christmas.txt";
String line = "";
ArrayList<String> kids = new ArrayList<>();
FileWriter fw = new FileWriter("Deliveries.txt");
SantasFactory sf = new SantasFactory();
try (Scanner s = new Scanner(new FileReader("Christmas.txt"))) {
while (s.hasNext()) {
kids.add(s.nextLine());
}
}
for (String boys : kids) {
ArrayList<String> btoys = new ArrayList<>();
int x = 0;
while (x < 3) {
if (!btoys.contains(sf.getRandomBoyToy().equals(sf.getRandomBoyToy()))) {
btoys.add(sf.getRandomBoyToy());
x++;
}
}
if (boys.endsWith("M")) {
fw.write(boys + " (" + btoys + ")\n\n");
}
}
fw.close();
}
}
Aucun commentaire:
Enregistrer un commentaire