/Users/lyon/j4p/src/collections/arrayList/PartyProcessor.java

1    /* 
2     * PartyProcessor.java 
3     * Created on December 4, 2002 
4     */ 
5     
6    package collections.arrayList; 
7     
8    import java.util.ArrayList; 
9    import java.util.List; 
10   import java.util.ListIterator; 
11    
12   /** 
13    * A Dinner Party Processor which shows how to use an ArrayList 
14    * @author  Thomas Rowland 
15    */ 
16   public class PartyProcessor { 
17    
18       private List parties = new ArrayList(); 
19    
20       public static void main(String[] args) { 
21           PartyProcessor proc = new PartyProcessor(); 
22    
23           //add some objects 
24           proc.addParty(new Party("Thomas", 4)); 
25           proc.addParty(new Party("Jason", 3)); 
26           proc.addParty(new Party("Tiffany", 5)); 
27           proc.addParty(new Party("Terry", 3)); 
28           proc.addParty(new Party("Brandy", 2)); 
29    
30           //display the objects 
31           proc.showParties(); 
32    
33           //update an object 
34           proc.updateParty(new Party("Jason", 8)); 
35           proc.showParties(); 
36    
37           //locate objects with specific criteria 
38           proc.showNextPartyOf(3); 
39           proc.showPartyInQueue("Jason"); 
40    
41           //remove an object 
42           proc.removeParty("Thomas"); 
43           proc.showParties(); 
44       } 
45    
46       // adds an element to the end of the list 
47       public void addParty(Party party) { 
48           parties.add(party); 
49           System.out.println("Added: " + party); 
50       } 
51    
52       // replaces an element with the one specified 
53       public boolean updateParty(Party party) { 
54           int index = parties.indexOf(party); 
55           if (index == -1) return false; 
56           parties.set(index, party); 
57           System.out.println("Updated party " + party + ", at index " + (index)); 
58           return true; 
59       } 
60    
61       // removes an element from the list 
62       public void removeParty(String name) { 
63           Party party = new Party(name); 
64           int index = parties.indexOf(party); 
65           if (index == -1) System.out.println("party not found."); 
66           System.out.println("\nRemoved party " + parties.remove(index) 
67                   + " at index " + index); 
68       } 
69    
70       // gets an element in the list with specified criteria 
71       public void showNextPartyOf(int partySize) { 
72           ListIterator iter = parties.listIterator(); 
73           while (iter.hasNext()) { 
74               Party party = (Party) iter.next(); 
75               if (party.getPartySize() == partySize) { 
76                   System.out.println("\nNext party of " + partySize + ": " + party); 
77                   return; 
78               } 
79           } 
80       } 
81    
82       // gets an element in the list with specified criteria 
83       public void showPartyInQueue(String partyName) { 
84           System.out.println("\nParty: " + partyName + " No. in queue: " 
85                   + (parties.indexOf(new Party(partyName)) + 1)); 
86       } 
87    
88       public void showParties() { 
89           ListIterator iter = parties.listIterator(); 
90           System.out.println("\nCurrently " + parties.size() + " parties..."); 
91           Party p; 
92           while (iter.hasNext()) { 
93               Party party = (Party) iter.next(); 
94               System.out.println(party); 
95           } 
96       } 
97   } 
98