KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > example > ajax > catalog > TestCatalog


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4 package com.opensymphony.webwork.example.ajax.catalog;
5
6 import java.util.*;
7
8 /**
9  * TestCatalog
10  *
11  * @author Jason Carreira <jcarreira@eplus.com>
12  */

13 public class TestCatalog implements Catalog {
14     private static Map categories = new HashMap();
15     private static Map products = new HashMap();
16
17     public static Category CATEGORY_ELECTRONICS = new TestCategory(5001, "Electronics", null);
18     public static Category CATEGORY_CLOTHING = new TestCategory(5002, "Clothing", null);
19     public static Category CATEGORY_TELEVISIONS = new TestCategory(5003, "Televisions", CATEGORY_ELECTRONICS);
20     public static Category CATEGORY_STEREOS = new TestCategory(5004, "Stereos", CATEGORY_ELECTRONICS);
21     public static Category CATEGORY_SHIRTS = new TestCategory(5005, "Shirts", CATEGORY_CLOTHING);
22     public static Category CATEGORY_SLACKS = new TestCategory(5006, "Slacks", CATEGORY_CLOTHING);
23
24     public static Product PRODUCT_HD_TV = new TestProduct(7001, "High Definition Television", "54\" High Definition Plasma Television", 5445.00, CATEGORY_TELEVISIONS);
25     public static Product PRODUCT_STEREO = new TestProduct(7002, "All-in-one stereo", "All-in-one stereo with AM/FM tuner, 6 CD changer, 2x300 Watt speakers", 99.99, CATEGORY_STEREOS);
26
27     public TestCatalog() {
28     }
29
30     public int getId() {
31         return 1001;
32     }
33
34     public String JavaDoc getName() {
35         return "Test Catalog";
36     }
37
38     public Set findAllCategories() {
39         return new HashSet(categories.values());
40     }
41
42     public Set findAllProducts() {
43         return new HashSet(products.values());
44     }
45
46     public Category findCategoryForId(Integer JavaDoc id) {
47         return (Category) categories.get(id);
48     }
49
50     public Product findProductById(Integer JavaDoc productId) {
51         return (Product) products.get(productId);
52     }
53
54     public Set findProductsByCategory(Category category) {
55         Set categoryProducts = new HashSet();
56         for (Iterator iterator = products.values().iterator(); iterator.hasNext();) {
57             Product product = (Product) iterator.next();
58             if (product.getCategory().equals(category)) {
59                 categoryProducts.add(product);
60             }
61         }
62         return categoryProducts;
63     }
64
65     public String JavaDoc toString() {
66         return "Test Catalog";
67     }
68
69     private static class TestCategory implements Category {
70         private int id;
71         private String JavaDoc name;
72         private Category parent;
73
74         private TestCategory(int id, String JavaDoc name, Category parent) {
75             this.id = id;
76             this.name = name;
77             this.parent = parent;
78
79             categories.put(new Integer JavaDoc(id), this);
80         }
81
82         public int getId() {
83             return id;
84         }
85
86         public String JavaDoc getName() {
87             return name;
88         }
89
90         public Category getParent() {
91             return parent;
92         }
93
94         public boolean equals(Object JavaDoc o) {
95             if (this == o) return true;
96             if (o == null || getClass() != o.getClass()) return false;
97
98             final TestCategory testCategory = (TestCategory) o;
99
100             return id == testCategory.id;
101
102         }
103
104         public int hashCode() {
105             return id;
106         }
107
108
109         public String JavaDoc toString() {
110             return "TestCategory{" +
111                     "id='" + id + "'" +
112                     ", name='" + name + "'" +
113                     ", parent=" + parent +
114                     "}";
115         }
116     }
117
118     private static class TestProduct implements Product {
119         private int id;
120         private String JavaDoc name;
121         private String JavaDoc description;
122         private double price;
123         private Category category;
124
125         public TestProduct(int id, String JavaDoc name, String JavaDoc description, double price, Category category) {
126             this.id = id;
127             this.name = name;
128             this.description = description;
129             this.price = price;
130             this.category = category;
131
132             products.put(new Integer JavaDoc(id), this);
133         }
134
135         public int getId() {
136             return id;
137         }
138
139         public String JavaDoc getName() {
140             return name;
141         }
142
143         public String JavaDoc getDescription() {
144             return description;
145         }
146
147         public double getPrice() {
148             return price;
149         }
150
151         public Category getCategory() {
152             return category;
153         }
154
155         public boolean equals(Object JavaDoc o) {
156             if (this == o) return true;
157             if (o == null || getClass() != o.getClass()) return false;
158
159             final TestProduct testProduct = (TestProduct) o;
160
161             return id == testProduct.id;
162
163         }
164
165         public int hashCode() {
166             return id;
167         }
168
169
170         public String JavaDoc toString() {
171             return "TestProduct{" +
172                     "id='" + id + "'" +
173                     ", name='" + name + "'" +
174                     ", description='" + description + "'" +
175                     ", price=" + price +
176                     ", category=" + category +
177                     "}";
178         }
179     }
180 }
181
Popular Tags