Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- /**
- * Create cycling groups.
- */
- public class RidingGroup implements Iterable<HashSet<String>>
- {
- private List<HashSet<String>> groups;
- /** Constructor for Riding Group */
- public RidingGroup()
- {
- // initialize the category map
- groups = new ArrayList<HashSet<String>>();
- // Create an entry in the map for each skill
- for (Distance dist: Distance.values())
- {
- // Each group is associated with a set of cyclist names
- groups.add(new HashSet<String>());
- }
- }
- /** Read the cyclist data from an input stream and create the list.
- * @return List of cyclists from whom to form groups.
- */
- public void load(Scanner input)
- {
- // Read all input
- while(input.hasNext())
- {
- //the first token will be the name
- String name = input.next();
- Distance distance = Distance.valueOf(input.next());
- // categorize the riders by distance
- groups.get(distance.ordinal()).add(name);
- }
- }
- /** Accessor to an iterator over the groups */
- public Iterator<HashSet<String>> iterator()
- {
- return groups.iterator();
- }
- /** Local main for testing */
- public static void main(String[] args) throws java.io.IOException
- {
- // Open standard input for reading the text
- RidingGroup rg = new RidingGroup();
- rg.load(new Scanner(System.in));
- //rg.createGroups(list);
- Iterator<HashSet<String>> iter = rg.iterator();
- for (Distance dist: Distance.values())
- {
- System.out.println(dist);
- System.out.println(iter.next());
- }
- }
- }
- /** Enumerated type for riding distances */
- enum Distance
- {
- easy, moderate, lengthy, century, centuryplus;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement