(2) When the Window First Opens When the window first opens, it should appear (1
ID: 3797896 • Letter: #
Question
(2) When the Window First Opens When the window first opens, it should appear (1)with all buttons disabled, (2) the Products list populated with items, (3) the Shopping Cart and Contents lists should be empty and (4) the Total Price should appear as $0.00, aligned to the right side of the text field and property formatted You MUST create an update0 method that properly adjusts the contents/view ofeach list, button and the text field whenever it is called. You should call this at the end of the start( method to populate all the window components. The array below should be used as the contents of the Products list. Grocery Item products new FreezerIten ("Smart-Ones Frozen Entrees 1.39 r, 0.311E) new Grocery tem ("SnackPack Pudding C.99 0.336 new Freezer Item ("Breyers Chocolate Icecream" 2.39 f, 2.27 new Grocery tem Nabob Coffee 3.99 f, 326f) new Grocery ("Gold Seal salmon 1.99t, 0.213 new Grocery Item ("Ocean Spray Cranberry Cocktail 2.99 f 26f) new Grocery sena ("Heinz Beans original 0.791, 9.4 I new Refrigerator Item n Ground Beef 4.94f, 0.75f new FreezerItera 5-Alive Frozen Juice 0.75E, 0.426f) new Grocery Item ("Coca-Cola 12-pack 3.49 112t) new Grocerylten ("Toilet Paper 48 packs 10.9 10.89E) new Rerrigerator Itemt"2L Sealt est Milk", 2.991, 2.061) new Refrigerator Itea Extra-Large Eggs 1.79f, 0.77f new RetrigeratorIteaa "Yoplait Yogurt 6-pack 4.741, 1.02 new FreezerItem "Mega-S ed Chocolate Icecream" 67.93 f, 1 Here is what the window should look like upon opening:Explanation / Answer
package cart;
import entity.Product;
import java.util.*;
public class ShoppingCart {
List<ShoppingCartItem> items;
int numberOfItems;
double total;
public ShoppingCart() {
items = new ArrayList<ShoppingCartItem>();
numberOfItems = 0;
total = 0;
}
public synchronized void addItem(Product product) {
boolean newItem = true;
for (ShoppingCartItem scItem : items) {
if (scItem.getProduct().getId() == product.getId()) {
newItem = false;
scItem.incrementQuantity();
}
}
if (newItem) {
ShoppingCartItem scItem = new ShoppingCartItem(product);
items.add(scItem);
}
}
public synchronized void update(Product product, String quantity) {
short qty = -1;
qty = Short.parseShort(quantity);
if (qty >= 0) {
ShoppingCartItem item = null;
for (ShoppingCartItem scItem : items) {
if (scItem.getProduct().getId() == product.getId()) {
if (qty != 0) {
scItem.setQuantity(qty);
} else {
item = scItem;
break;
}
}
}
if (item != null) {
items.remove(item);
}
}
}
public synchronized List<ShoppingCartItem> getItems() {
return items;
}
public synchronized int getNumberOfItems() {
numberOfItems = 0;
for (ShoppingCartItem scItem : items) {
numberOfItems += scItem.getQuantity();
}
return numberOfItems;
}
public synchronized double getSubtotal() {
double amount = 0;
for (ShoppingCartItem scItem : items) {
Product product = (Product) scItem.getProduct();
amount += (scItem.getQuantity() * product.getPrice().doubleValue());
}
return amount;
}
public synchronized void calculateTotal(String surcharge) {
double amount = 0;
double s = Double.parseDouble(surcharge);
amount = this.getSubtotal();
amount += s;
total = amount;
}
public synchronized double getTotal() {
return total;
}
public synchronized void clear() {
items.clear();
numberOfItems = 0;
total = 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.