-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingCart3.java
More file actions
55 lines (50 loc) · 1.46 KB
/
ShoppingCart3.java
File metadata and controls
55 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.text.NumberFormat;
import java.util.ArrayList;
public class ShoppingCart3
{
private double totalPrice;
private ArrayList<Item> cart;
/**
* Constructor method
* Creates a shopping cart with a capacity of five items.
*/
public ShoppingCart3()
{
totalPrice = 0.0;
cart = new ArrayList<Item>();
}
/**
* Adds an item to the shopping cart.
* @param itemName name of item
* @param price price of item
* @quantity how much of one item
*/
public void addToCart(String itemName, double price, int quantity)
{
cart.add(new Item (itemName, price, quantity));
totalPrice += price * quantity;
}
/**
* Creates a string representation of the cart.
* @return string representation
*/
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String contents = "\nShopping Cart\n";
contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";
for (int i = 0; i < cart.size(); i++)
contents += cart.get(i).toString() + "\n";
contents += "\nTotal Price: " + fmt.format(totalPrice);
contents += "\n";
return contents;
}
/**
* Accessor method for the total price.
* @return totalPrice total price of the cart's contents.
*/
public double getTotalPrice()
{
return totalPrice;
}
}