Create a Shopping Cart using the List Data Structure
The Item Class
Create an Item
class that models an Item to be purchased. It should contain instance data, constructor(s), and the following properties: name, price and quantity.
The ShopApp Class
Create a List
to hold Item
instances. Call your List items
. Here is an example of an List
declaration that holds string
instances:
List<String> ingredients = new List<String>();
The ShopApp
class should be designed to model shopping in similar fashion to Amazon and other online sites (where you can add items to a cart and you will see your total increase with each item added). Use a loop to simulate shopping, asking the user to enter the item, quantity and price. (You will need variables for these.) Store the items in a List
and update the total due at checkout.
Stretch task: Provide an opportunity to remove an item from the shopping cart if a user no longer desires to make that particular purchase.
Examples:
Enter the name of the item: Shoes
Enter the unit price: 25
Enter the quantity: 2
Current Cart
Item: Shoes Price: 25.0 Quantity: 2
Total Price: \$50.00
Continue shopping (y/n)? y
Enter the name of the item: Shirt
Enter the unit price: 15
Enter the quantity: 3
Current Cart
Item: Shoes Price: 25.0 Quantity: 2
Item: Shirt Price: 15.0 Quantity: 3
Total Price: \$95.00
Continue shopping (y/n)? n
Stretchier task: Create a Cart
class that is responsible for managing items
.
ShopApp
should only manipulate the contents of the cart by calling methods on Cart
. It should not talk directly to Item
instances.