Putting it together
- Java course
- Beginner
- Lesson 52/116
- 6 min
Combine the cart class with a menu loop.
Putting the cart together
In the final lesson we combine the cart. Add a third item, list all items with for-each, add a third price and compute the total:
cart.add("Milk");
cart.set(0, "Apple x2");
cart.remove(1);
for (String item : cart) {
System.out.println(item);
}
// ... prices and totalThis is the skeleton of the cart app: adding, listing and total. Listing each item shows the cart to the customer.
Practice List all items
Add to the previous code: add a third item with cart.add("Milk") (after the first add calls). Update with cart.set(0, "Apple x2") and cart.remove(1). List all items with for (String item : cart) { System.out.println(item); }. Add a third price with prices.add(15.0). Keep the total line.
Starter code
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>();
cart.add("Apple");
cart.add("Bread");
cart.set(0, "Apple x2");
cart.remove(1);
ArrayList<Double> prices = new ArrayList<>();
prices.add(10.0);
prices.add(20.0);
double subtotal = 0;
for (double price : prices) {
subtotal += price;
}
double tax = subtotal * 0.10;
double total = subtotal + tax;
System.out.println(cart);
System.out.println("Subtotal: " + subtotal);
System.out.println("Total: " + total);
// List every item one by one
}
}Sample solution
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>();
cart.add("Apple");
cart.add("Bread");
cart.add("Milk");
cart.set(0, "Apple x2");
cart.remove(1);
for (String item : cart) {
System.out.println(item);
}
ArrayList<Double> prices = new ArrayList<>();
prices.add(10.0);
prices.add(20.0);
prices.add(15.0);
double subtotal = 0;
for (double price : prices) {
subtotal += price;
}
double tax = subtotal * 0.10;
double total = subtotal + tax;
System.out.println("Subtotal: " + subtotal);
System.out.println("Total: " + total);
}
}Expected output
Apple x2 Milk Subtotal: 45.0 Total: 49.5
Practice Full cart (3 items)
Add to the previous code: add a third item with cart.add("Milk"), update with cart.set(0, "Apple x2") and cart.remove(1). List all items with for (String item : cart) { System.out.println(item); }. Add a third price with prices.add(15.0). Print "Subtotal: " + subtotal and "Total: " + total with println.
Starter code
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>();
cart.add("Apple");
cart.add("Bread");
cart.set(0, "Apple x2");
cart.remove(1);
ArrayList<Double> prices = new ArrayList<>();
prices.add(10.0);
prices.add(20.0);
double subtotal = 0;
for (double price : prices) {
subtotal += price;
}
double tax = subtotal * 0.10;
double total = subtotal + tax;
System.out.println(cart);
System.out.println("Subtotal: " + subtotal);
System.out.println("Total: " + total);
// List every item one by one
}
}Sample solution
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>();
cart.add("Apple");
cart.add("Bread");
cart.add("Milk");
cart.set(0, "Apple x2");
cart.remove(1);
for (String item : cart) {
System.out.println(item);
}
ArrayList<Double> prices = new ArrayList<>();
prices.add(10.0);
prices.add(20.0);
prices.add(15.0);
double subtotal = 0;
for (double price : prices) {
subtotal += price;
}
double tax = subtotal * 0.10;
double total = subtotal + tax;
System.out.println("Subtotal: " + subtotal);
System.out.println("Total: " + total);
}
}Expected output
Apple x2 Milk Subtotal: 45.0 Total: 49.5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What are the basic steps of the cart app?
- Add, list, total Correct answer
- Open, read, close
- Print, wait, exit
- Input, process, output
Explanation: The cart has adding, listing and total steps.
2 Match the cart pieces
| Term | Meaning |
|---|---|
add |
Add an item |
for-each |
List items |
+= |
Addition |
println |
3 Complete the for-each
for (String item : ____) {
System.out.println(item);
}
for (String item : cart) {
System.out.println(item);
}
Explanation: The cart variable is cart.