Java Programming: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Sample Output\\\\\\\\\\\\\
ID: 3690378 • Letter: J
Question
Java Programming:
\\\\\\\\\Sample Output\\\\\\\\\\\\\\\\
\\\\\\\\\\\\ShoppingCartDriver\\\\\\\\\\\\\\\\
public class ShoppingCartDriver {
public static void main(String[] args) {
//Create a shopping cart
ShoppingCart cart = new ShoppingCart();
//create different items
Item item1 = new Item("butter", 4, 2);
Item item2 = new Item("milk", 5, 3);
Item item3 = new Item("eggs", 10, 3);
Item item4 = new Item("cheese", 20, 2);
//add items to the cart
cart.addItem(item1);
cart.addItem(item2);
cart.addItem(item3);
cart.addItem(item4);
//Display content of the cart
System.out.println(cart);
//Delete item4 from the cart
cart.deleteItem(item4);
//Display content of the cart
System.out.println(cart);
//Modify item1's name, when you this method
//item1's name will be set to UNKNOW
cart.modifyItem(item1);
//Display content of the cart
System.out.println(cart);
}
}
Explanation / Answer
package cart;
010.
011.import entity.Product;
012.import java.util.*;
013.
014./**
015. *
016. * @author tgiunipero
017. */
018.public class ShoppingCart {
019.
020. List<ShoppingCartItem> items;
021. int numberOfItems;
022. double total;
023.
024. public ShoppingCart() {
025. items = new ArrayList<ShoppingCartItem>();
026. numberOfItems = 0;
027. total = 0;
028. }
029.
030. /**
031. * Adds a <code>ShoppingCartItem</code> to the <code>ShoppingCart</code>'s
032. * <code>items</code> list. If item of the specified <code>product</code>
033. * already exists in shopping cart list, the quantity of that item is
034. * incremented.
035. *
036. * @param product the <code>Product</code> that defines the type of shopping cart item
037. * @see ShoppingCartItem
038. */
039. public synchronized void addItem(Product product) {
040.
041. boolean newItem = true;
042.
043. for (ShoppingCartItem scItem : items) {
044.
045. if (scItem.getProduct().getId() == product.getId()) {
046.
047. newItem = false;
048. scItem.incrementQuantity();
049. }
050. }
051.
052. if (newItem) {
053. ShoppingCartItem scItem = new ShoppingCartItem(product);
054. items.add(scItem);
055. }
056. }
057.
058. /**
059. * Updates the <code>ShoppingCartItem</code> of the specified
060. * <code>product</code> to the specified quantity. If '<code>0</code>'
061. * is the given quantity, the <code>ShoppingCartItem</code> is removed
062. * from the <code>ShoppingCart</code>'s <code>items</code> list.
063. *
064. * @param product the <code>Product</code> that defines the type of shopping cart item
065. * @param quantity the number which the <code>ShoppingCartItem</code> is updated to
066. * @see ShoppingCartItem
067. */
068. public synchronized void update(Product product, String quantity) {
069.
070. short qty = -1;
071.
072. // cast quantity as short
073. qty = Short.parseShort(quantity);
074.
075. if (qty >= 0) {
076.
077. ShoppingCartItem item = null;
078.
079. for (ShoppingCartItem scItem : items) {
080.
081. if (scItem.getProduct().getId() == product.getId()) {
082.
083. if (qty != 0) {
084. // set item quantity to new value
085. scItem.setQuantity(qty);
086. } else {
087. // if quantity equals 0, save item and break
088. item = scItem;
089. break;
090. }
091. }
092. }
093.
094. if (item != null) {
095. // remove from cart
096. items.remove(item);
097. }
098. }
099. }
100.
101. /**
102. * Returns the list of <code>ShoppingCartItems</code>.
103. *
104. * @return the <code>items</code> list
105. * @see ShoppingCartItem
106. */
107. public synchronized List<ShoppingCartItem> getItems() {
108.
109. return items;
110. }
111.
112. /**
113. * Returns the sum of quantities for all items maintained in shopping cart
114. * <code>items</code> list.
115. *
116. * @return the number of items in shopping cart
117. * @see ShoppingCartItem
118. */
119. public synchronized int getNumberOfItems() {
120.
121. numberOfItems = 0;
122.
123. for (ShoppingCartItem scItem : items) {
124.
125. numberOfItems += scItem.getQuantity();
126. }
127.
128. return numberOfItems;
129. }
130.
131. /**
132. * Returns the sum of the product price multiplied by the quantity for all
133. * items in shopping cart list. This is the total cost excluding the surcharge.
134. *
135. * @return the cost of all items times their quantities
136. * @see ShoppingCartItem
137. */
138. public synchronized double getSubtotal() {
139.
140. double amount = 0;
141.
142. for (ShoppingCartItem scItem : items) {
143.
144. Product product = (Product) scItem.getProduct();
145. amount += (scItem.getQuantity() * product.getPrice().doubleValue());
146. }
147.
148. return amount;
149. }
150.
151. /**
152. * Calculates the total cost of the order. This method adds the subtotal to
153. * the designated surcharge and sets the <code>total</code> instance variable
154. * with the result.
155. *
156. * @param surcharge the designated surcharge for all orders
157. * @see ShoppingCartItem
158. */
159. public synchronized void calculateTotal(String surcharge) {
160.
161. double amount = 0;
162.
163. // cast surcharge as double
164. double s = Double.parseDouble(surcharge);
165.
166. amount = this.getSubtotal();
167. amount += s;
168.
169. total = amount;
170. }
171.
172. /**
173. * Returns the total cost of the order for the given
174. * <code>ShoppingCart</code> instance.
175. *
176. * @return the cost of all items times their quantities plus surcharge
177. */
178. public synchronized double getTotal() {
179.
180. return total;
181. }
182.
183. /**
184. * Empties the shopping cart. All items are removed from the shopping cart
185. * <code>items</code> list, <code>numberOfItems</code> and
186. * <code>total</code> are reset to '<code>0</code>'.
187. *
188. * @see ShoppingCartItem
189. */
190. public synchronized void clear() {
191. items.clear();
192. numberOfItems = 0;
193. total = 0;
194. }
195.
196.}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.