Advertisement
jdalbey

Create cycling groups.

May 5th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. /**
  4. * Create cycling groups.
  5. */
  6. public class RidingGroup implements Iterable<HashSet<String>>
  7. {
  8. private List<HashSet<String>> groups;
  9.  
  10. /** Constructor for Riding Group */
  11. public RidingGroup()
  12. {
  13. // initialize the category map
  14. groups = new ArrayList<HashSet<String>>();
  15. // Create an entry in the map for each skill
  16. for (Distance dist: Distance.values())
  17. {
  18. // Each group is associated with a set of cyclist names
  19. groups.add(new HashSet<String>());
  20. }
  21. }
  22.  
  23.  
  24. /** Read the cyclist data from an input stream and create the list.
  25. * @return List of cyclists from whom to form groups.
  26. */
  27. public void load(Scanner input)
  28. {
  29. // Read all input
  30. while(input.hasNext())
  31. {
  32. //the first token will be the name
  33. String name = input.next();
  34. Distance distance = Distance.valueOf(input.next());
  35. // categorize the riders by distance
  36. groups.get(distance.ordinal()).add(name);
  37. }
  38. }
  39. /** Accessor to an iterator over the groups */
  40. public Iterator<HashSet<String>> iterator()
  41. {
  42. return groups.iterator();
  43. }
  44. /** Local main for testing */
  45. public static void main(String[] args) throws java.io.IOException
  46. {
  47. // Open standard input for reading the text
  48. RidingGroup rg = new RidingGroup();
  49. rg.load(new Scanner(System.in));
  50. //rg.createGroups(list);
  51. Iterator<HashSet<String>> iter = rg.iterator();
  52. for (Distance dist: Distance.values())
  53. {
  54. System.out.println(dist);
  55. System.out.println(iter.next());
  56. }
  57.  
  58. }
  59. }
  60. /** Enumerated type for riding distances */
  61. enum Distance
  62. {
  63. easy, moderate, lengthy, century, centuryplus;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement