/* 3magic Coffee Table Tutorial 10 + 11 MyDate.java - Custom Data and Sorting. © 1997-1999 3magic. All rights reserved worldwide. */ import java.util.StringTokenizer; import CoffeeTable.Grid.Comparable; /** * Here's a simple date object. */ public class MyDate implements Comparable { // The date storage. private int fDate; private int fMonth; private int fYear; // Constructor public MyDate() { this(7, 11, 1965); } // Another Constructor. public MyDate(int date, int month, int year) { fDate = date; fMonth = month; fYear = year; } // compareTo: negative if ab. public int compareTo(Object anotherObject) { MyDate bDate = (MyDate)anotherObject; if (this.fYear != bDate.fYear) { return (this.fYear - bDate.fYear); } else if (this.fMonth != bDate.fMonth) { return (this.fMonth - bDate.fMonth); } else { return (this.fDate - bDate.fDate); } } // Month to string conversion. public String monthString() { switch (fMonth) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } return ""; } // Parser. public static MyDate parseMyDate(String str) { StringTokenizer st = new StringTokenizer(str, "/"); int month = Integer.parseInt(st.nextToken()); int date = Integer.parseInt(st.nextToken()); int year = Integer.parseInt(st.nextToken()); return new MyDate(date, month, year); } // String representation. public String toString() { return monthString() + " " + fDate + ", " + fYear; } }