Lab 02
Lab 02
Lab Assignment 02
2020 – 2021 Spring, CMPE 211 Fundamentals of Programming II
SHOPPING CART
In this lab, you are going to complete a class that implements a shopping cart as an array of items. You are
asked to implement the following tasks:
The file Item.java contains the definition of a class named Item that models an item one would
purchase. An item has a name, price, and quantity (the quantity purchased).
The file ShoppingCart.java implements the shopping cart as an array of Item objects. Complete the
ShoppingCart class at least by doing the following:
a. Declare an instance variable cart to be an array of Items and instantiate cart in the constructor
to be an array holding capacity Items.
b. Fill in the code for the increaseSize method by increasing the capacity of the shopping cart by 1
element. You can make use of available copyOf method from imported util.Arrays java
library to enlarge instance cart array.
c. Fill in the code for the addToCart method. This method should add the item to the cart and
update the totalPrice instance variable (note this variable takes into account the quantity).
However, it needs to check whether total number of items (itemCount) reached the cart capacity.
If that is the case, cart capacity should be increased by using increaseSize method after
printing “Capacity has increased for 1 more item...” message.
File ShoppingCartTest.java contains a Java program that provides menu-driven testing for the
ShoppingCart class. Complete it for a program that simulates shopping. The program should have a loop that
continues as long as the user wants to shop. Each time through the loop read in the name, price, and
quantity of the item the user wants to add to the cart. After adding an item to the cart, the cart contents
should be printed. After the loop print a “Please pay ...” message with the total price of the items in the cart.
1
Computer Engineering Department
You can see a sample program run (demo) on the following link:
https://asciinema.org/a/5AIdjcYxPvHxMH99Lyk9eMqz3
Submission
You need to implement all the required tasks above and upload your Java source files (.java) as a single Zip
file into the available Moodle submission.