58 lines
1.1 KiB
Java
58 lines
1.1 KiB
Java
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
* Stores a number of CartItems
|
|
*
|
|
* @author 5w7-mf4-u1
|
|
*
|
|
*/
|
|
public class ShoppingCart {
|
|
|
|
private ArrayList<CartItem> arr;
|
|
|
|
/**
|
|
* Initializes a ShoppingCart
|
|
*/
|
|
public ShoppingCart() {
|
|
arr = new ArrayList<>();
|
|
}
|
|
|
|
/**
|
|
* Adds an Item to an existing ShoppingCart
|
|
*
|
|
* @param item to be added
|
|
*/
|
|
public void addItem(CartItem item) {
|
|
arr.add(item);
|
|
}
|
|
|
|
/**
|
|
* Gets the total cost of the ShoppingCart
|
|
*
|
|
* @return total cost of all items in the ShoppingCart
|
|
*/
|
|
public double getTotalCost() {
|
|
double total = 0;
|
|
for (CartItem i : arr) {
|
|
total += i.getCost();
|
|
}
|
|
return total;
|
|
}
|
|
|
|
/**
|
|
* Returns a Recipt for All items in a Shopping cart and tallys up the cost
|
|
*/
|
|
@Override
|
|
public String toString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
for (CartItem i : arr) {
|
|
sb.append(i.toString());
|
|
}
|
|
sb.append("\n");
|
|
sb.append(String.format("Summe:%55.2f", getTotalCost()));
|
|
return sb.toString();
|
|
}
|
|
|
|
}
|