Scenario Our team is developing an app for a retail client who wants users/custo
ID: 3684027 • Letter: S
Question
Scenario
Our team is developing an app for a retail client who wants users/customers to be able to shop online. Your part of the project is to model an online shopping cart using classes and objects in Java.
Mental Model
First we want to understand what we want to model. Go to your favorite online store and add some items into a shopping cart, then view your cart. Once in your cart, try updating the quantity of one of the items. Notice that:
• A shopping cart is a collection of line items
• Each line item has a name/description, quantity, and price
• The line item manages/calculates the individual item costs
• The shopping cart manages/calculates the total cost
Design and Code
Write a class named ShoppingCart (in a file named ShoppingCart.java) that represents a list of items that a shopper will buy from the store, and another class named LineItem (in a file named LineItem.java) that represents a request to purchase a particular item in a given quantity (examples: four bottles of water, two beach towels, one volleyball).
The ShoppingCart object users/contains a list of LineItem objects. Which class should we write first?
LineItem class
The LineItem class should store the name, quantity, and price of the item. A LineItem object should have the following methods:
• public LineItem(String name, int quantity, double pricePerUnit) : constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
• public double getCost() : returns the total cost of the item it its given quantity. For example, four boxes of cookies that cost 2.30 per unit will have a total cost of 9.20.
• public void setQuantity(int newQuantity) : updates this item’s quantity to be the given value.
ShoppingCart class
The ShoppingCart class should use an array field to store the line items and to keep track of its size (number of items in the list so far). Assume that a shopping cart will have no more than 10 items. A ShoppingCart object should have the following methods:
• public ShoppingCart() : constructs a new empty grocery list.
• public void add(LineItem newItem) : adds the given item order to this list if the list has fewer than 10 items.
• public double getTotalCost() : returns the total sum cost of all grocery item orders in the shopping cart.
Testing
Now that you have developed the ShoppingCart and LineItem classes, you will need to test them to make sure they work correctly. Write a class named ShoppingCartTester to perform the tests.
We want to be able to test the shopping cart for multiple cases to see if ShoppingCart handles each one gracefully:
• ShoppingCart containing one LineItem, a typical case
• ShoppingCart containing five LineItems, a typical case
• ShoppingCart containing 10 LineItems, a full cart (legal)
• ShoppingCart containing 11 LineItems, an overflowing cart (error case)
• ShoppingCart containing zero LineItems, an empty cart (legal)
The ShoppingCartTester class should have at least one method, but you may split each test case into its own method if you prefer.
• public static void main(String[] args) : creates several shopping carts and populates them with items, and checking the totals calculated by the cart. o Create a new ShoppingCart object named singleItemCart § Create a new LineItem object § Add the LineItem object into singleItemCart § Obtain and print the total cost of the singleItemCart o Create a new ShoppingCart object named typicalCart § Create and add five new LineItem objects into typicalCart § Obtain and print the total cost of the typicalCart o Create a new ShoppingCart object named fullCart § Create and add 10 new LineItem objects into fullCart § Obtain and print the total cost of the fullCart o Create a new ShoppingCart object named overflowCart § Create and add 11 new LineItem objects into overflowCart (should not add the 11th item into the cart) § Obtain and print the total cost of the overflowCart (should be the total of 10 items in the cart, not 11) o Create a new ShoppingCart object named emptyCart § Obtain and print the total cost of the emptyCart (should be 0.0)
Unit Testing
Each test case is called a unit test. Which cases worked? Which cases did not work? For the cases that did not work, what did you have to change to make the cases work?
Regression Testing
After making changes/corrections, did you go back and re-test everything to make sure your changes/corrections did not break anything that was previously working? This is called regression testing
Explanation / Answer
ShoppingCartTester.java
public class ShoppingCartTester {
// Unit test 1
// Single item
public static void main(String[] args)
{
ShoppingCart singleItemCart = new ShoppingCart();
LineItem coat = new LineItem("coat", 1, 39.99);
singleItemCart.add(coat);
System.out.println("Single item total: " + singleItemCart.getTotalCost());
System.out.println();
// Unit test 2
// Five items
ShoppingCart typicalCart = new ShoppingCart();
LineItem socks = new LineItem("socks", 4, 14.99);
LineItem hat = new LineItem("hats", 2, 10.00);
LineItem pants = new LineItem("pants", 1, 29.95);
LineItem sweater = new LineItem("sweaters", 2, 49.50);
LineItem belt = new LineItem("belt", 1, 17.99);
typicalCart.add(socks);
typicalCart.add(hat);
typicalCart.add(pants);
typicalCart.add(sweater);
typicalCart.add(belt);
System.out.println();
System.out.println("Five item total: " + typicalCart.getTotalCost());
System.out.println();
// Unit test 3
// Ten items
ShoppingCart fullCart = new ShoppingCart();
LineItem watch = new LineItem("watch", 1, 70.00);
LineItem gps = new LineItem("gps", 2, 199.99);
LineItem fan = new LineItem("fan", 1, 20.00);
LineItem radio = new LineItem("radio", 1, 15.00);
LineItem tent = new LineItem("tent", 1, 120.00);
LineItem flashlight = new LineItem("flashlight", 1, 12.00);
LineItem batteries = new LineItem("batteries", 4, 9.99);
LineItem backpack = new LineItem("backpack", 2, 79.99);
LineItem compass = new LineItem("compass", 1, 40.00);
LineItem boots = new LineItem("boots", 1, 65.00);
fullCart.add(watch);
fullCart.add(gps);
fullCart.add(fan);
fullCart.add(radio);
fullCart.add(tent);
fullCart.add(flashlight);
fullCart.add(batteries);
fullCart.add(backpack);
fullCart.add(compass);
fullCart.add(boots);
System.out.println();
System.out.println("Ten item total: " + fullCart.getTotalCost());
System.out.println();
// Unit test 4
// Overflow
ShoppingCart overflowCart = new ShoppingCart();
LineItem towels = new LineItem("towels", 2, 30.00);
LineItem water = new LineItem("water", 24, 4.00);
LineItem grill = new LineItem("grill", 1, 19.99);
LineItem charcoal = new LineItem("charcoal", 2, 15.00);
LineItem net = new LineItem("badminton", 1, 90.00);
LineItem rackets = new LineItem("rackets", 2, 8.00);
LineItem cooler = new LineItem("cooler", 1, 24.75);
LineItem ice = new LineItem("ice", 8, 13.00);
LineItem chairs = new LineItem("chairs", 3, 18.00);
LineItem sunscreen = new LineItem("sunscreen", 1, 12.99);
LineItem sandals = new LineItem("sandals", 1, 65.00);
overflowCart.add(towels);
overflowCart.add(water);
overflowCart.add(grill);
overflowCart.add(charcoal);
overflowCart.add(net);
overflowCart.add(rackets);
overflowCart.add(cooler);
overflowCart.add(ice);
overflowCart.add(chairs);
overflowCart.add(sunscreen);
overflowCart.add(sandals);
System.out.println();
System.out.println("Overflow item total: " + overflowCart.getTotalCost());
System.out.println();
// Unit test 5
// Empty cart
ShoppingCart emptyCart = new ShoppingCart();
System.out.println("Empty cart total: " + emptyCart.getTotalCost());
}
}
LineItem.java
public class LineItem
{
// Member variables set to private and can only be accessed in this class.
private String name;
private int quantity;
private double pricePerUnit;
// Constructor used to create an item order.
public LineItem(String name, int quantity, double pricePerUnit)
{
this.name = name;
this.quantity = quantity;
this.pricePerUnit = pricePerUnit;
}
// getCost returns the total cost of an item given the quantity.
public double getCost()
{
return quantity * pricePerUnit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(double pricePerUnit) {
this.pricePerUnit = pricePerUnit;
}
public int getQuantity() {
return quantity;
}
// setQuantity updates the item's quantity.
public void setQuantity(int newQuantity) {
this.quantity = newQuantity;
}
}
ShoppingCart.java
public class ShoppingCart {
LineItem[] list;
// Default constructor that accepts no parameters and initializes an empty array of line items
public ShoppingCart() {
// Zero-length array -- empty shopping cart
list = new LineItem[0];
}
// add takes the new item and adds it to the list if the cart has fewer than 10 items
public void add(LineItem newItem) {
if (list.length > 9) {
System.out.println("The cart has reached its maximum limit.");
} else {
LineItem[] tempList = new LineItem[list.length + 1];
int count = 0;
for (LineItem item : list) {
tempList[count] = item;
count++;
}
tempList[count] = newItem;
list = tempList;
System.out.println(newItem.getQuantity() + " " + newItem.getName() + " added to cart.");
}
}
// getTotalCost returns the total sum of all line items
public double getTotalCost() {
double total = 0.0;
for (LineItem item : list) {
total += item.getCost();
}
return total;
}
}
sample output
1 coat added to cart.
Single item total: 39.99
4 socks added to cart.
2 hats added to cart.
1 pants added to cart.
2 sweaters added to cart.
1 belt added to cart.
Five item total: 226.90000000000003
1 watch added to cart.
2 gps added to cart.
1 fan added to cart.
1 radio added to cart.
1 tent added to cart.
1 flashlight added to cart.
4 batteries added to cart.
2 backpack added to cart.
1 compass added to cart.
1 boots added to cart.
Ten item total: 941.9200000000001
2 towels added to cart.
24 water added to cart.
1 grill added to cart.
2 charcoal added to cart.
1 badminton added to cart.
2 rackets added to cart.
2 towels added to cart.
24 water added to cart.
1 grill added to cart.
2 charcoal added to cart.
1 badminton added to cart.
2 rackets added to cart.
1 cooler added to cart.
8 ice added to cart.
3 chairs added to cart.
1 sunscreen added to cart.
The cart has reached its maximum limit.
Overflow item total: 507.73
Empty cart total: 0.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.