before I post any code, I should probably explain the weird way we choose who buys stuff for who.
- my family is made up of three branches
- no one buys for anyone else in the same branch
- names are first given to the leader of each branch, and then one is given to each adult (except in branch three). leader keeps remaining names.
- there is a 10/8/4 split between families, but the names are drawn in a 9/9/4 split. (meaning branch two takes one extra name, and branch one takes one less)
- the leaders of each branch are not in the drawing
as you can imagine, these rules made everything extremely difficult for me, In fact, I’ve used for different methods so far, and only the last (and messiest) one works completely, so that’s the one I’ll be showing you.
package com.kestrel.test; import java.util.ArrayList; import java.util.Random; public class Main4 { public static int listLength; public static int namesUnpicked; public static int[] branchSize = {9, 9, 4}; public static boolean[] doSubSplit= {true, true, false}; public static Random rand = new Random(); public static FamilyMember member; public static void main(String[] args) { FamilyMember.setup(); listLength = FamilyMember.familyMembers.size(); namesUnpicked = listLength; ArrayList<ArrayList<String>> pools = new ArrayList<ArrayList<String>>(3); ArrayList<String> pool1 = new ArrayList<String>(); ArrayList<String> pool2 = new ArrayList<String>(); ArrayList<String> pool3 = new ArrayList<String>(); pools.add(pool1); pools.add(pool2); pools.add(pool3); for(int i = 0; i < listLength; i++) { member = FamilyMember.familyMembers.get(i); for(int ii = 0; ii < branchSize.length; ii++) { if(member.branch != ii + 1) { pools.get(ii).add(member.name); } } } int timeout = 0; while(namesUnpicked > 0 && timeout < 100) { for(int i = 0; i < 3; i++) { if(branchSize[i] > 0) { int timeout2 = 0; while(timeout2 < 15) { member = FamilyMember.familyMembers.get(rand.nextInt(listLength)); if(pools.get(i).contains(member.name) && member.drawnBy == 0) { member.drawnBy = i + 1; namesUnpicked--; branchSize[i]--; timeout = 0; break; } timeout2++; timeout++; } } } } if(branchSize[0] + branchSize[1] + branchSize[2] != 0) { System.out.println("failed"); System.exit(0); } ArrayList<FamilyMember> children = new ArrayList<FamilyMember>(); ArrayList<FamilyMember> adults = new ArrayList<FamilyMember>(); for(int i = 0; i < 3; i++) { for(int ii = 0; ii < listLength; ii++) { member = FamilyMember.familyMembers.get(ii); if(member.branch == i + 1 && member.status.equals("adult")) { adults.add(member); } else if(member.drawnBy == i + 1 && member.status.equals("child")) { children.add(member); } } for(int ii = 0; ii < adults.size(); ii++) { while(true) { FamilyMember child = children.get(rand.nextInt(children.size())); if(child.subDrawnBy == 0) { FamilyMember.familyMembers.get(child.index).subDrawnBy = adults.get(ii).index; break; } } } adults.clear(); children.clear(); } for(int i = 0; i < listLength; i++) { member = FamilyMember.familyMembers.get(i); if(member.subDrawnBy != 0) { System.out.println(member.name + " drawn by " + FamilyMember.familyMembers.get(member.subDrawnBy).name); } else { switch(member.drawnBy) { case 1: System.out.println(member.name + " drawn by Judy"); break; case 2: System.out.println(member.name + " drawn by Barbra"); break; case 3: System.out.println(member.name + " drawn by Jean"); break; } } } } }
So, as you’ve probably noticed, there’s a pretty good chance it will just fail. (mostly due to the family sizes) I could most likely fix that, but it works about 50% of the time, so its easier to just re-run it. the code runs in the same order as that list I made above the code, so I don’t think I need to explain every single bit of code, but I will mention that the first half uses nested arraylists to create a drawing pool for each branch, containing everyone in the other two branches. the second half however, I wrote on my way home yesterday, and was not very well thought out. I pass FamilyMember objects into two different arrays, and used what I suspect is a terrible way to keep tract of where they were located in the original arraylist (I saved their position to an “index” variable upon creation in FamilyMember.java, see below)
package com.kestrel.test; import java.util.ArrayList; public class FamilyMember { public final String name; public final int branch; public final String status; public int subDrawnBy = 0; public int drawnBy = 0; public int index = 0; public static ArrayList<FamilyMember> familyMembers = new ArrayList<>(); public FamilyMember(String name, int branch, String status) { this.name = name; this.branch = branch; this.status = status; this.index = familyMembers.size(); familyMembers.add(this); } public static void setup() { new FamilyMember("Jeff", 3, "child"); new FamilyMember("Lauren", 3, "child"); new FamilyMember("Julie", 3, "child"); new FamilyMember("David and Deidre", 3, "child"); new FamilyMember("Kim and Lee", 2, "adult"); new FamilyMember("Sam", 2, "child"); new FamilyMember("Jake", 2, "child"); new FamilyMember("Nathan", 2, "child"); new FamilyMember("Mike and Ashley", 2, "adult"); new FamilyMember("Jon and Meredith", 2, "adult"); new FamilyMember("ellie", 2, "child"); new FamilyMember("JD", 2, "child"); new FamilyMember("Jennifer and Wayne", 1, "adult"); new FamilyMember("Jonathon", 1, "child"); new FamilyMember("Bryant", 1, "child"); new FamilyMember("Lindsay", 1, "child"); new FamilyMember("Lisa and Brian", 1, "adult"); new FamilyMember("Pete", 1, "child"); new FamilyMember("Lucas", 1, "child"); new FamilyMember("Cathy and Lee", 1, "adult"); new FamilyMember("Elliott", 1, "child"); new FamilyMember("Russell", 1, "child"); } }
and… that’s about it. the FamilyMember class isn’t that complicated, its just an arraylist, a constructor, and a method that creates all my family members. the only other thing I can think to mention would be that the reason I started some values at 1 instead of 0 was for readability when I was debugging. sorry for any spelling errors, I’m not a great typist and stuff runs together when I’m reading plain black-on-white text.