Commit 2f47a546 authored by ElaFHNW's avatar ElaFHNW
Browse files

shopping cart from Italy

parent 3f9372a7
package Testing.ShoppingCart;
public class Product {
private String title;
private double price;
public Product (String t, double p) {
this.title = t;
this.price = p;
public String getTitle() {
return title;
public double getPrice() {
return price;
public boolean equals(Object o) {
if (o instanceof Product) {
Product p = (Product)o;
return p.getTitle().equals(title);
return false;
package Testing.ShoppingCart;
public class ProductNotFoundException extends Exception {
public ProductNotFoundException() {
package Testing.ShoppingCart;
import java.util.*;
public class ShoppingCart {
private ArrayList items;
public ShoppingCart() {
items = new ArrayList();
public double getBalance() {
double balance = 0.00;
for (Iterator i = items.iterator(); i.hasNext();){
Product item = (Product);
balance += item.getPrice();
return balance;
public void addItem(Product item) {
public void removeItem(Product item)
throws ProductNotFoundException {
if (!items.remove(item)) {
throw new ProductNotFoundException();}
public int getItemCount() {
return items.size();
public void empty() {
- When created, the cart has 0 items
- When empty, the cart has 0 items
- When a new product is added, the number of items must be incremented
- When a new product is added, the new balance must be the sum of the
previous balance plus the cost of the new product
- When an item is removed, the number of items must be decreased
- When a product not in the cart is removed, a
ProductNotFoundException must be thrown
- Hint: insert the call in a try block and put a fail() after the call to
- credits: By Filippo Ricca DISI, Università di Genova, Italy
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment