Dart Code Contoh

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

class Item {

String name;

int quantity;

double price;

Item(this.name, this.quantity, this.price);

class ItemList {

List<Item> items = [];

void add(Item item) {

items.add(item);

void remove(Item item) {

items.remove(item);

int get totalQuantity {

int total = 0;

for (var item in items) {

total += item.quantity;

return total;
}

double get totalPrice {

double total = 0;

for (var item in items) {

total += item.price * item.quantity;

return total;

void main() {

var itemList = ItemList();

var item1 = Item("item1", 3, 10.0);

var item2 = Item("item2", 2, 20.0);

itemList.add(item1);

itemList.add(item2);

print("Total quantity: ${itemList.totalQuantity}");

print("Total price: ${itemList.totalPrice}");

You might also like