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