Listing the cart
- Dart course
- Beginner
- Lesson 51/116
- 6 min
Write the method that prints the cart formatted.
Listing the cart
To print the products in the cart we will write a printCart method. This method walks the names and prices lists by index and writes each product on a single line.
To walk by index we use a classic for loop: for (var i = 0; i < names.length; i++). On each iteration i goes from 0 to names.length - 1. With names[i] and prices[i] we reach the i-th elements of the parallel lists.
Formatted output
In printCart, while each product is written on a single line, name and price are separated by a space. print("${names[i]} ${prices[i]}"); uses interpolation to embed both fields into one string and ends with a new line.
Commas or dashes could be used instead; the formatting choice is yours. In this project we use spaces. Writing two fields on one line lets you scan the cart quickly. The ${names[i]} braced interpolation is required to embed an expression (index access).
Practice Write the printCart method
Add a printCart method to the Cart class. Its signature is void printCart(). In the body walk indices with for (var i = 0; i < names.length; i++) and print with print("${names[i]} ${prices[i]}");. main already adds two products and calls printCart. The output must be two lines: Notebook 25.0 and Pen 10.0.
Starter code
class Cart {
List<String> names = [];
List<double> prices = [];
void addItem(String name, double price) {
names.add(name);
prices.add(price);
}
double getSubtotal() {
double sum = 0;
for (var p in prices) {
sum += p;
}
return sum;
}
}
// Write the printCart method here
}
void main() {
var cart = Cart();
cart.addItem("Notebook", 25.0);
cart.addItem("Pen", 10.0);
cart.printCart();
}Sample solution
class Cart {
List<String> names = [];
List<double> prices = [];
void addItem(String name, double price) {
names.add(name);
prices.add(price);
}
double getSubtotal() {
double sum = 0;
for (var p in prices) {
sum += p;
}
return sum;
}
void printCart() {
for (var i = 0; i < names.length; i++) {
print("${names[i]} ${prices[i]}");
}
}
}
void main() {
var cart = Cart();
cart.addItem("Notebook", 25.0);
cart.addItem("Pen", 10.0);
cart.printCart();
}Expected output
Notebook 25.0 Pen 10.0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the printCart method do?
cart.printCart();- Computes the total
- Prints the cart to the screen Correct answer
- Removes a product
- Adds a product
Explanation: printCart writes the products in the cart to the screen.
2 Match each part to its job
| Term | Meaning |
|---|---|
names.length |
Element count |
names[i] |
I-th name |
prices[i] |
I-th price |
print(...) |
Prints to screen |
3 Write the print line
void printCart() {
for (var i = 0; i < names.length; i++) {
print(____);
}
}
void printCart() {
for (var i = 0; i < names.length; i++) {
print("${names[i]} ${prices[i]}");
}
}
Explanation: Both fields are embedded into one string with interpolation.