A Dart console app simulating a shopping cart with add, remove, view, and total calculation features, demonstrating list management and user input.
import 'dart:io';
// Class representing a product in the cart
class Prodotto {
String nome;
double prezzoUnitario;
int quantita;
Prodotto({required this.nome, required this.prezzoUnitario, required this.quantita});
// Calculates the total price for this product (unitPrice * quantity)
double prezzoTotale() => prezzoUnitario * quantita;
@override
String toString() {
return '$nome - Unit price: €${prezzoUnitario.toStringAsFixed(2)}, Quantity: $quantita, Total: €${prezzoTotale().toStringAsFixed(2)}';
}
}
// Class that manages the shopping cart
class Carrello {
final List<Prodotto> _prodotti = [];
// Adds a product to the cart or updates the quantity if already present
void aggiungiProdotto(Prodotto prodotto) {
for (var p in _prodotti) {
if (p.nome.toLowerCase() == prodotto.nome.toLowerCase()) {
p.quantita += prodotto.quantita;
print('Quantity updated for product "${p.nome}".');
return;
}
}
_prodotti.add(prodotto);
print('Product "${prodotto.nome}" added to the cart.');
}
// Removes a product from the cart by name
bool rimuoviProdotto(String nome) {
return _prodotti.removeWhere((p) => p.nome.toLowerCase() == nome.toLowerCase()) > 0;
}
// Modifies the quantity of an existing product
bool modificaQuantita(String nome, int nuovaQuantita) {
for (var p in _prodotti) {
if (p.nome.toLowerCase() == nome.toLowerCase()) {
p.quantita = nuovaQuantita;
return true;
}
}
return false;
}
// Calculates the overall total of the cart
double totale() {
double somma = 0.0;
for (var p in _prodotti) {
somma += p.prezzoTotale();
}
return somma;
}
// Shows all products in the cart
void mostraProdotti() {
if (_prodotti.isEmpty) {
print('The cart is empty.');
return;
}
print('Products in the cart:');
for (var p in _prodotti) {
print('- ${p.toString()}');
}
print('Cart total: €${totale().toStringAsFixed(2)}');
}
}
// Function to read a line from console with prompt
String leggiInput(String prompt) {
stdout.write(prompt);
return stdin.readLineSync() ?? '';
}
// Function to read a positive double from console
double leggiDoublePositivo(String prompt) {
while (true) {
String input = leggiInput(prompt);
try {
double valore = double.parse(input.replaceAll(',', '.'));
if (valore > 0) {
return valore;
} else {
print('Please enter a positive number.');
}
} catch (e) {
print('Invalid input. Try again.');
}
}
}
// Function to read a positive int from console
int leggiIntPositivo(String prompt) {
while (true) {
String input = leggiInput(prompt);
try {
int valore = int.parse(input);
if (valore > 0) {
return valore;
} else {
print('Please enter a positive integer.');
}
} catch (e) {
print('Invalid input. Try again.');
}
}
}
void main() {
Carrello carrello = Carrello();
bool uscita = false;
print('Welcome to the Shopping Cart Simulator!');
while (!uscita) {
print('\nMenu:');
print('1 - Add product');
print('2 - Remove product');
print('3 - Modify product quantity');
print('4 - Show cart');
print('5 - Exit');
String scelta = leggiInput('Choose an option (1-5): ');
switch (scelta) {
case '1':
String nome = leggiInput('Product name: ').trim();
if (nome.isEmpty) {
print('Product name cannot be empty.');
break;
}
double prezzo = leggiDoublePositivo('Unit price (€): ');
int quantita = leggiIntPositivo('Quantity: ');
carrello.aggiungiProdotto(Prodotto(nome: nome, prezzoUnitario: prezzo, quantita: quantita));
break;
case '2':
String nome = leggiInput('Name of product to remove: ').trim();
if (carrello.rimuoviProdotto(nome)) {
print('Product "$nome" successfully removed.');
} else {
print('Product "$nome" not found in the cart.');
}
break;
case '3':
String nome = leggiInput('Name of product to modify: ').trim();
int nuovaQuantita = leggiIntPositivo('New quantity: ');
if (carrello.modificaQuantita(nome, nuovaQuantita)) {
print('Quantity updated for "$nome".');
} else {
print('Product "$nome" not found in the cart.');
}
break;
case '4':
carrello.mostraProdotti();
break;
case '5':
uscita = true;
print('Thank you for using the simulator. Goodbye!');
break;
default:
print('Invalid option. Try again.');
}
}
}Software: Dart SDK (version 2.12 or higher for null safety)
Software: No external dependencies required
Configuration: Run from terminal/console with command 'dart run filename.dart'
Software components: