FileDocCategorySizeDatePackage
Schedule.javaAPI DocExample2225Sun Sep 02 14:59:02 BST 2001chap4

Schedule

public class Schedule extends Object

Fields Summary
private static Schedule
instance
static String[]
titles
Constructors Summary
private Schedule()

    
Methods Summary
public static chap4.SchedulegetInstance()



        
        return instance;
    
public Show[]getTodaysShows()

        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 6);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        int duration = 30;

        List shows = new ArrayList();
        Random rand = new Random();

        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();

        // make up some random shows
        for (int channel=2; channel<6; channel++) {
            startTime.setTime(today.getTime());
            endTime.setTime(today.getTime());

            do {
                duration = (1 + rand.nextInt(4)) * 30;
                endTime.add(Calendar.MINUTE, duration);

                String curTitle = titles[rand.nextInt(titles.length)];

                shows.add(new Show(curTitle, channel,
                        startTime.getTime(), endTime.getTime()));
                startTime.setTime(endTime.getTime());
            } while (endTime.get(Calendar.DATE) == today.get(Calendar.DATE));
        }

        Collections.sort(shows);
        return (Show[]) shows.toArray(new Show[0]);
    
public static voidmain(java.lang.String[] args)

        Show[] shows = Schedule.getInstance().getTodaysShows();
        for (int i=0; i<shows.length; i++) {
            System.out.println(shows[i]);
        }