KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > convertor > book > Book


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.convertor.book;
21
22 import org.netbeans.spi.convertor.SimplyConvertible;
23 import java.util.Properties JavaDoc;
24
25
26 /**
27  *
28  * @author David Konecny
29  */

30 public class Book implements SimplyConvertible {
31
32     public int ID;
33     public String JavaDoc author;
34     public String JavaDoc title;
35     public String JavaDoc publisher;
36     public int price;
37
38     private boolean initialized = false;
39
40     private static final String JavaDoc KEY_ID = "id";
41     private static final String JavaDoc KEY_AUTHOR = "author";
42     private static final String JavaDoc KEY_TITLE = "title";
43     private static final String JavaDoc KEY_PUBLISHER = "publisher";
44     private static final String JavaDoc KEY_PRICE = "price";
45     
46     public Book() {
47     }
48     
49     public Book(int ID, String JavaDoc title, String JavaDoc author, String JavaDoc publisher, int price) {
50         this.ID = ID;
51         this.title = title;
52         this.author = author;
53         this.publisher = publisher;
54         this.price = price;
55     }
56     
57     public void read(Properties JavaDoc prop) {
58         if (initialized) {
59             throw new RuntimeException JavaDoc("Cannot initialize the object more than once!");
60         }
61         initialized = true;
62         
63         ID = Integer.parseInt(prop.getProperty(KEY_ID));
64         author = prop.getProperty(KEY_AUTHOR);
65         title = prop.getProperty(KEY_TITLE);
66         publisher = prop.getProperty(KEY_PUBLISHER);
67         price = Integer.parseInt(prop.getProperty(KEY_PRICE));
68     }
69     
70     public void write(java.util.Properties JavaDoc p) {
71         p.setProperty(KEY_ID, Integer.toString(ID));
72         p.setProperty(KEY_AUTHOR, author);
73         p.setProperty(KEY_TITLE, title);
74         p.setProperty(KEY_PUBLISHER, publisher);
75         p.setProperty(KEY_PRICE, Integer.toString(price));
76     }
77     
78     public boolean equals(Object JavaDoc o) {
79         if (!(o instanceof Book)) {
80             return false;
81         }
82         Book b = (Book)o;
83         return ID == b.ID &&
84             title.equals(b.title) &&
85             author.equals(b.author) &&
86             publisher.equals(b.publisher) &&
87             price == b.price;
88     }
89    
90     public int hashCode() {
91         // not relevant
92
return 125;
93     }
94     
95     public String JavaDoc toString() {
96         return "Book[id="+ID+", title="+title+", author="+author+", publisher="+publisher+", price="+price+"]"+super.toString();
97     }
98
99 }
100
Popular Tags