001package scpc.model;
002
003import java.io.Serializable;
004import java.util.Objects;
005import scpc.Calculator;
006
007/**
008 * 購物車內的單一物品<br>
009 * Present a single item of shopping cart.
010 *
011 * @author Kent Yeh
012 * @param <T> type of real cart item.
013 */
014public class SingleItem<T> implements IItem<T> {
015
016    private final IItem<T> item;
017    private final int sequenceNum;
018    private final int serialNum;
019    private boolean serialLast = false;
020    private boolean exclusiveLock = false;
021
022    public SingleItem(IItem<T> item, int sequenceNum, int serialNum) {
023        this.item = item;
024        this.sequenceNum = sequenceNum;
025        this.serialNum = serialNum;
026    }
027
028    /**
029     * @see IItem#getIdentity()
030     * @return
031     */
032    @Override
033    public Serializable getIdentity() {
034        return item.getIdentity();
035    }
036
037    /**
038     * @see IItem#as()
039     * @return
040     */
041    @Override
042    public T as() {
043        return item.as();
044    }
045
046    /**
047     * @see IItem#getQuantity()
048     * @return
049     */
050    @Override
051    public long getQuantity() {
052        return 1;
053    }
054
055    /**
056     * @see IItem#setQuantity(long)
057     * @param quantity
058     */
059    @Override
060    public void setQuantity(long quantity) {
061
062    }
063
064    /**
065     * @see IItem#getSalePrice()
066     * @return
067     */
068    @Override
069    public double getSalePrice() {
070        return item.getSalePrice();
071    }
072
073    /**
074     * @see IItem#getOriginalPrice()
075     * @return
076     */
077    @Override
078    public double getOriginalPrice() {
079        return item.getOriginalPrice();
080    }
081
082    /**
083     * 購物車裡的物品<br>
084     *
085     * @return a Shopping cart item.
086     */
087    public IItem<T> getItem() {
088        return item;
089    }
090
091    /**
092     * 排序編號
093     *
094     * @return ordered sequence number of item.
095     */
096    public int getSequenceNum() {
097        return sequenceNum;
098    }
099
100    /**
101     * 同品項物品的編號(從1起算)
102     *
103     * @return n'th item of same serial items.(Starting from 1)
104     */
105    public int getSerialNum() {
106        return serialNum;
107    }
108
109    /**
110     * 是否為同品項物品的最後一項
111     *
112     * @return the last item of same serial items.
113     */
114    public boolean isSerialLast() {
115        return serialLast;
116    }
117
118    /**
119     * Detect whether the current visit item is the last item of the same series
120     * or not.
121     * <br>當下走访品項是否為同系列中的最後一個
122     *
123     * @param serialLast
124     */
125    public void setSerialLast(boolean serialLast) {
126        this.serialLast = serialLast;
127    }
128
129    /**
130     * for {@link Calculator} use only.
131     *
132     * @return
133     */
134    public boolean isExclusiveLock() {
135        return exclusiveLock;
136    }
137
138    /**
139     * for {@link Calculator} use only.
140     *
141     * @param exclusiveLock
142     * @return
143     */
144    public SingleItem<T> setExclusiveLock(boolean exclusiveLock) {
145        this.exclusiveLock = exclusiveLock;
146        return this;
147    }
148
149    @Override
150    public int hashCode() {
151        int hash = 7;
152        hash = 73 * hash + this.sequenceNum;
153        return hash;
154    }
155
156    @Override
157    public boolean equals(Object obj) {
158        if (obj == null) {
159            return false;
160        }
161        if (getClass() != obj.getClass()) {
162            return false;
163        }
164        final SingleItem<?> other = (SingleItem<?>) obj;
165        if (!Objects.equals(this.item, other.item)) {
166            return false;
167        }
168        return this.sequenceNum == other.sequenceNum;
169    }
170
171    @Override
172    public String toString() {
173        return "singularize \"" + sequenceNum + "." + item.toString() + "\"";
174    }
175
176}