KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db > FortuneGeneratorImpl


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 1997-@year@ by Per Nyfelt. All rights reserved.
5
//
6
// $Id: FortuneGeneratorImpl.java,v 1.2 2003/11/29 16:19:28 per_nyfelt Exp $
7
package db;
8
9 import java.util.*;
10
11 import org.ozoneDB.OzoneInterface;
12 import org.ozoneDB.OzoneObject;
13
14
15 public class FortuneGeneratorImpl extends OzoneObject implements FortuneGenerator {
16
17     public static final long serialVersionUID = 1L;
18     private List fortunes;
19     Random randomizer;
20
21     public static FortuneGenerator create(OzoneInterface db) {
22         return (FortuneGenerator) db.createObject(FortuneGeneratorImpl.class,
23                                                   OzoneInterface.Public,
24                                                   FortuneGenerator.class.getName());
25     }
26
27     public FortuneGeneratorImpl() {
28         init();
29     }
30
31     public String JavaDoc getFortune() {
32         System.out.println("[FortuneGeneratorImpl] getting fortune");
33         return (String JavaDoc) fortunes.get(randomizer.nextInt(fortunes.size()));
34     }
35
36     public void addFortune(String JavaDoc fortune) {
37         System.out.println("[FortuneGeneratorImpl] adding fortune");
38         fortunes.add(fortune);
39     }
40
41     public void removeFortunes() {
42         System.out.println("[FortuneGeneratorImpl] removing fortunes");
43         init();
44     }
45
46     public List getAllFortunes() {
47         System.out.println("[FortuneGeneratorImpl] getting all fortunes");
48         return fortunes;
49     }
50
51     private void init() {
52         fortunes = new ArrayList();
53         randomizer = new Random();
54         fortunes.add("I like work ... I can sit and watch it for hours.");
55         fortunes.add("Nihilism should commence with oneself.");
56         fortunes.add("Ah say, son, you're about as sharp as a bowlin' ball.");
57         fortunes.add("Yield to Temptation ... it may not pass your way again.");
58     }
59 }
60
Popular Tags