Source code: datas/Item.java
1 package datas;
2
3 import java.util.NoSuchElementException;
4 import java.util.StringTokenizer;
5 /**
6 * Class Item
7 * @invariant ((theTitle != null) && (theTitle.length() != 0)) // theTitle must be set
8 */
9 public class Item {
10 // Attributs
11 private final String SEPARATEUR = "#";
12 private static int lastItemId = 0;
13 private int itemId;
14 private Title theTitle;
15 private boolean borrowed;
16
17 // Constructeurs
18 /**
19 * Method Item construct an item.
20 * @param data data of the item
21 * @throws NoSuchElementException if the data is not good
22 *
23 * @pre ((data != null) && (data.length() != 0)) // data must be set
24 */
25 public Item(String data) throws NoSuchElementException {
26 this.parseData(data);
27 }
28
29 /**
30 * Method Item create an item of a title.
31 * @param theTitle title of the item
32 */
33 public Item(Title theTitle) {
34 this.itemId = lastItemId++;
35 this.theTitle = theTitle;
36 }
37
38 /**
39 * Method getItemId get the id of the item.
40 * @return int return the id of the item
41 */
42 public int getItemId() {
43 return this.itemId;
44 }
45
46 /**
47 * Method getTitle get the title of the item.
48 * @return Title the title of the item
49 */
50 public Title getTitle() {
51 return this.theTitle;
52 }
53
54 /**
55 * Method isBorrowed the item is borrowed or not.
56 * @return boolean true if the item is borrowed
57 */
58 public boolean isBorrowed() {
59 return this.borrowed;
60 }
61
62 /**
63 * Method setItemId modify the itemId.
64 * @param itemId the new itemId
65 */
66 public void setItemId(int itemId) {
67 this.itemId = itemId;
68 }
69
70 /**
71 * Method setTitle modify the title of an item.
72 * @param title the new title
73 * @return boolean true
74 */
75 public boolean setTitle(Title title) {
76 this.theTitle = title;
77 return true;
78 }
79
80 /**
81 * Method becomeBorrowed set if the item is borrowed.
82 * @param borrowed true if the item is borrowed
83 */
84 public void becomeBorrowed(boolean borrowed) {
85 this.borrowed = borrowed;
86 }
87
88 /**
89 * Put the item data in a string
90 * @see Object#toString()
91 */
92 public String toString() {
93
94 StringBuffer sb = new StringBuffer();
95 sb.append(this.theTitle);
96 sb.append(SEPARATEUR);
97 sb.append(this.borrowed);
98 return sb.toString();
99 }
100
101 /**
102 * Compares this Item to the specified object. The result is true if and only if the argument is not null
103 * and is a Item object that represents the same Item as this object.
104 * @param o the object to compare this Item against.
105 * @return boolean true if the Item are equal; false otherwise.
106 * @see Object#equals(Object)
107 */
108 public boolean equals(Object o) {
109 boolean ret = false;
110 if (o != null) {
111 if (o.getClass().getName().equals(this.getClass().getName())) {
112 if (((Item) o).getTitle().equals(this.getTitle())
113 && ((Item) o).getItemId() == (this.getItemId())) {
114 ret = true;
115 }
116 }
117 }
118 return ret;
119 }
120
121 /**
122 * Method parseData.
123 * @param data
124 * @throws NoSuchElementException
125 */
126 private void parseData(String data) throws NoSuchElementException {
127 StringTokenizer st = new StringTokenizer(data, SEPARATEUR);
128 this.itemId = lastItemId++;
129 this.theTitle = new Title(st.nextToken());
130 this.borrowed = (new Boolean(st.nextToken())).booleanValue();
131 }
132 }