]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jshopcore/model/item/BaseItem.java
213ac7fc9f41d3620c91348ca5f63fdc0869f68a
[jcustomer-core.git] / src / org / mxchange / jshopcore / model / item / BaseItem.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jshopcore.model.item;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import org.mxchange.jcore.BaseFrameworkSystem;
22 import org.mxchange.jshopcore.model.product.Product;
23
24 /**
25  * An item (addedable to a basket) could respresent a product or a discount
26  * coupon. This depends on the type of the item.
27  *
28  * @author Roland Haeder
29  */
30 public abstract class BaseItem extends BaseFrameworkSystem implements AddableBasketItem {
31         /**
32          * Entry id (from database backend)
33          */
34         private Long id;
35
36         /**
37          * Item amount
38          */
39         private Long amount;
40
41         /**
42          * Item id number
43          */
44         private Long itemId;
45
46         /**
47          * Item type
48          */
49         private String itemType;
50
51         /**
52          * Item instance
53          */
54         private Product product;
55
56         @Override
57         public Float calculateTotalPrice () {
58                 // product should be set
59                 if (this.getProduct() == null) {
60                         // Abort here
61                         throw new NullPointerException("product is null"); //NOI18N
62                 }
63
64                 // Calculate and return it
65                 // TODO: If later other purchaseable items (other than products) are handled through this class, this needs expansion
66                 return (this.getAmount() * this.getProduct().getPrice());
67         }
68
69         @Override
70         public int compareTo (final AddableBasketItem item) {
71                 // Trace message
72                 this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
73                 
74                 // item should not be null
75                 if (null == item) {
76                         throw new NullPointerException("item is null"); //NOI18N
77                 }
78
79                 // Debug message
80                 this.getLogger().debug(MessageFormat.format("this.itemId={0},item.itemId={1}", this.getItemId(), item.getItemId())); //NOI18N
81
82                 // Is the id the same?
83                 if (Objects.equals(this.getItemId(), item.getItemId())) {
84                         // Same id, means same item
85                         return 0;
86                 } else if (this.getItemId() > item.getItemId()) {
87                         // This id is larger than compared to
88                         return -1;
89                 }
90
91                 // The other id is larger
92                 return 1;
93         }
94
95         @Override
96         public Long getAmount () {
97                 return this.amount;
98         }
99
100         @Override
101         public void setAmount (final Long amount) {
102                 this.amount = amount;
103         }
104
105         @Override
106         public Long getId () {
107                 return this.id;
108         }
109
110         @Override
111         public void setId (final Long id) {
112                 this.id = id;
113         }
114
115         @Override
116         public Long getItemId () {
117                 return this.itemId;
118         }
119
120         @Override
121         public void setItemId( final Long itemId) {
122                 this.itemId = itemId;
123         }
124
125         @Override
126         public String getItemType () {
127                 return this.itemType;
128         }
129
130         @Override
131         public void setItemType (final String itemType) {
132                 this.itemType = itemType;
133         }
134
135         /**
136          * @return the product
137          */
138         @Override
139         public Product getProduct () {
140                 return this.product;
141         }
142
143         /**
144          * @param product the product to set
145          */
146         @Override
147         public void setProduct (final Product product) {
148                 this.product = product;
149         }
150 }