blob: ca091d4e1eeb487276682423a1646394c2a07902 [file] [log] [blame]
/*
* Copyright (c) 2004, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package cart;
import java.util.*;
import database.BookDetails;
public class ShoppingCart {
HashMap items = null;
int numberOfItems = 0;
public ShoppingCart() {
items = new HashMap();
}
public synchronized void add(String bookId, BookDetails book) {
if (items.containsKey(bookId)) {
ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
scitem.incrementQuantity();
System.out.println("in add, quantity is " + scitem.getQuantity());
} else {
ShoppingCartItem newItem = new ShoppingCartItem(book);
items.put(bookId, newItem);
System.out.println("in add, quantity is " + newItem.getQuantity());
}
// numberOfItems++;
}
public synchronized void remove(String bookId) {
if (items.containsKey(bookId)) {
ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
scitem.decrementQuantity();
if (scitem.getQuantity() <= 0) {
items.remove(bookId);
}
numberOfItems--;
}
}
public synchronized List getItems() {
List results = new ArrayList();
Iterator items = this.items.values()
.iterator();
while (items.hasNext()) {
results.add(items.next());
}
return (results);
}
protected void finalize() throws Throwable {
items.clear();
}
public synchronized int getNumberOfItems() {
numberOfItems = 0;
for (Iterator i = getItems()
.iterator(); i.hasNext();) {
ShoppingCartItem item = (ShoppingCartItem) i.next();
numberOfItems += item.getQuantity();
System.out.println("number of items is " + numberOfItems);
}
return numberOfItems;
}
public synchronized double getTotal() {
double amount = 0.0;
for (Iterator i = getItems()
.iterator(); i.hasNext();) {
ShoppingCartItem item = (ShoppingCartItem) i.next();
BookDetails bookDetails = (BookDetails) item.getItem();
amount += (item.getQuantity() * bookDetails.getPrice());
}
return roundOff(amount);
}
private double roundOff(double x) {
long val = Math.round(x * 100); // cents
return val / 100.0;
}
public synchronized void clear() {
System.err.println("Clearing cart.");
items.clear();
numberOfItems = 0;
}
}