Source of StudentEntry.java


  1: 
  2: package adapter;
  3: 
  4: import java.util.Comparator;
  5: 
  6: /**
  7:  *  Adapter design pattern
  8:  */
  9: public class StudentEntry extends Student implements TableEntry { 
 10: 
 11:   // position of each column 
 12:   public static final int ID_COLUMN             = 0; 
 13:   public static final int FIRST_NAME_COLUMN     = 1; 
 14:   public static final int LAST_NAME_COLUMN      = 2; 
 15:   public static final int STREET_ADDRESS_COLUMN = 3; 
 16:   public static final int STATE_COLUMN          = 4; 
 17:   public static final int CITY_COLUMN           = 5; 
 18:   public static final int COUNTRY_COLUMN        = 6; 
 19:   public static final int POSTAL_CODE_COLUMN    = 7; 
 20:   public static final int TELEPHONE_COLUMN      = 8; 
 21:   public static final int GPA_COLUMN            = 9; 
 22:   public static final int TOTAL_CREDITS_COLUMN  = 10; 
 23: 
 24:   public static final String[] columnNames = { 
 25:     "ID", 
 26:     "First Name", 
 27:     "Last Name", 
 28:     "Street Address", 
 29:     "State", 
 30:     "City", 
 31:     "Country", 
 32:     "Postal Code", 
 33:     "Telephone", 
 34:     "GPA", 
 35:     "Total Credits", 
 36:   };
 37: 
 38:   public static final String[] columnTips = { 
 39:     "ID", 
 40:     "First Name", 
 41:     "Last Name", 
 42:     "Street Address", 
 43:     "State", 
 44:     "City", 
 45:     "Country", 
 46:     "Postal Code", 
 47:     "Telephone", 
 48:     "GPA", 
 49:     "Total Credits", 
 50:   }; 
 51: 
 52:   public static final Comparator[] comparators = {
 53:     new StudentEntryComparator(ID_COLUMN),
 54:     new StudentEntryComparator(FIRST_NAME_COLUMN),
 55:     new StudentEntryComparator(LAST_NAME_COLUMN),
 56:     new StudentEntryComparator(STREET_ADDRESS_COLUMN),
 57:     new StudentEntryComparator(STATE_COLUMN),
 58:     new StudentEntryComparator(CITY_COLUMN),
 59:     new StudentEntryComparator(COUNTRY_COLUMN),
 60:     new StudentEntryComparator(POSTAL_CODE_COLUMN),
 61:     new StudentEntryComparator(TELEPHONE_COLUMN),
 62:     new StudentEntryComparator(GPA_COLUMN),
 63:     new StudentEntryComparator(TOTAL_CREDITS_COLUMN),
 64:   };
 65: 
 66:   public StudentEntry(String ID, 
 67:                       String firstName, 
 68:                       String lastName, 
 69:                       String streetAddress, 
 70:                       String state, 
 71:                       String city, 
 72:                       String country, 
 73:                       String postalCode, 
 74:                       String telephone, 
 75:                       float GPA, 
 76:                       int totalCredits) {
 77:     super(ID, firstName, lastName, streetAddress, state, city, country, postalCode, 
 78:           telephone, GPA, totalCredits);
 79:   }
 80: 
 81:   public StudentEntry(Student student) { 
 82:     if (student != null) {
 83:       this.ID = student.ID;
 84:       this.firstName = student.firstName;
 85:       this.lastName = student.lastName;
 86:       this.streetAddress = student.streetAddress;
 87:       this.state = student.state;
 88:       this.city = student.city;
 89:       this.country = student.country;
 90:       this.postalCode = student.postalCode;
 91:       this.telephone = student.telephone;
 92:       this.GPA = student.GPA;
 93:       this.totalCredits = student.totalCredits;
 94:     }
 95:   }
 96: 
 97:   public int getColumnCount() {
 98:     return columnNames.length;
 99:   }
100: 
101:   public String getColumnName(int col) {
102:     if (col >= 0 && 
103:         col < columnNames.length) { 
104:       return columnNames[col]; 
105:     }
106:     return null;
107:   }
108: 
109:   public Object getColumnValue(int col) {
110:     if (col >= 0 && 
111:         col < columnNames.length) { 
112:       switch (col) { 
113:       case ID_COLUMN:             return ID;  
114:       case FIRST_NAME_COLUMN:     return firstName;
115:       case LAST_NAME_COLUMN:      return lastName;
116:       case STREET_ADDRESS_COLUMN: return streetAddress;
117:       case STATE_COLUMN:          return state;
118:       case CITY_COLUMN:           return city;
119:       case COUNTRY_COLUMN:        return country;
120:       case POSTAL_CODE_COLUMN:    return postalCode;
121:       case TELEPHONE_COLUMN:      return telephone;
122:       case GPA_COLUMN:            return new Float(GPA);
123:       case TOTAL_CREDITS_COLUMN:  return new Integer(totalCredits);
124:       } 
125:     }
126:     return null;
127:   }
128: 
129:   public String getColumnTip(int col) {
130:     if (col >= 0 && 
131:         col < columnTips.length) { 
132:       return columnTips[col]; 
133:     }
134:     return null;
135:   }
136: 
137:   public Class getColumnClass(int col) {
138:     if (col == GPA_COLUMN) { 
139:       return Float.class;
140:     } else if (col == TOTAL_CREDITS_COLUMN) {
141:       return Integer.class;
142:     } else { 
143:       return String.class;
144:     }
145:   }
146: 
147:   public Comparator getColumnComparator(int col) {
148:     if (col >= 0 && 
149:         col < comparators.length) { 
150:       return comparators[col]; 
151:     }
152:     return null;
153:   }
154: 
155:   public int getColumnWidth(int col) {
156:     return -1;
157:   }
158: 
159:   static public class StudentEntryComparator implements Comparator {
160: 
161:     public StudentEntryComparator(int col) { 
162:       this.col = col; 
163:     }
164: 
165:     public int compare(Object o1, Object o2) {
166:       if (o1 != null && 
167:           o2 != null &&
168:           o1 instanceof StudentEntry &&
169:           o2 instanceof StudentEntry) {
170:         StudentEntry e1 = (StudentEntry) o1; 
171:         StudentEntry e2 = (StudentEntry) o2; 
172:         if (col == GPA_COLUMN) { 
173:           return (int) (e1.getGPA() * 1000 - e2.getGPA() * 1000);
174:         } else if (col == TOTAL_CREDITS_COLUMN) {
175:           return (e1.getTotalCredits() - e2.getTotalCredits());
176:         } else { 
177:           return ((String) e1.getColumnValue(col)).compareTo(e2.getColumnValue(col)); 
178:         }        
179:       }
180:       return 0;
181:     }
182:    
183:     protected int col; 
184:   }
185: 
186: }