cami-la / collections-java-api-2023

Este repositório é referente ao curso "Collection Framework API Java" e é uma valiosa contribuição para a comunidade de desenvolvedores Java, fornecendo exemplos práticos e recursos educacionais relacionados à poderosa API de coleções da linguagem Java.
https://web.dio.me/course/conhecendo-collections-java/learning/c5d6f4e1-6d05-4eea-93d8-d292c708999f
1.05k stars 766 forks source link

Método removeItem remove todos os itens. #37

Closed Matt-Gary closed 1 year ago

Matt-Gary commented 1 year ago

Oi Cami! Tudo bom? Java parece muito complicada ;) Aqui tenho problem acom metodo removeItem: `import java.util.ArrayList; import java.util.List;

public class ShoppingCart { private List shoppingList;

public ShoppingCart() {
    this.shoppingList = new ArrayList<>();
}

public void addItem(String name, double price, int quantity) {
    shoppingList.add(new Item(name, price, quantity));
}

public void removeItem(String name) {
    List<Item> itemToRemove = new ArrayList<>();
    for (Item i : shoppingList) {
        if (i.getName().equalsIgnoreCase(name))
            ;
        {
            itemToRemove.add(i);
        }
    }
    shoppingList.removeAll(itemToRemove);
}

public double calculateTotalValue() {
    double totalValue = 0d;
    if (!shoppingList.isEmpty()) {
        for (Item item : shoppingList) {
            double itemValue = item.getPrice() * item.getQuantity();
            totalValue += itemValue; 
        }
        return totalValue;
    } else {
        throw new RuntimeException("A lista está vazia!");
    }
}

public void showItems() {
    System.out.println(shoppingList);
}

public static void main(String[] args) throws Exception {
    ShoppingCart shopCart = new ShoppingCart();
    shopCart.addItem("eggs", 2.99, 3);
    shopCart.addItem("apple", 4.20, 2);
    shopCart.addItem("banana", 5.76, 1);
    shopCart.showItems();
    shopCart.removeItem("eggs");
    System.out.println("Total value  = " + shopCart.calculateTotalValue());

}

}` Item.java:

`public class Item { private String name; private double price; private int quantity;

public Item(String name, double price, int quantity) {
    this.name = name;
    this.price = price;
    this.quantity = quantity;
}

String getName() {
    return name;
}

double getPrice() {
    return price;
}

int getQuantity() {
    return quantity;
}
@Override
public String toString() {
    return "Item{" +
            "name='" + name +
            ", price=" + price +
            ", quantity=" + quantity +
            '}';
}

}`

cami-la commented 1 year ago

Oi, amigo. Conseguiu resolver? (: Mas o Java não é difícil :') hehe

Matt-Gary commented 1 year ago

Consegue! Obrigado <3 Para me Java é muito mais complicada o que Python ou JavaScript. Até com professora maravilhosa como você, tive muita difficuldade. Para escrever coisa basica você precisa muitas linhas de codigo :D

cami-la commented 10 months ago

Sim, de fato! @Matt-Gary Quando comparado com Python e JS, no começo o Java é bem mais difícil. Mas depois você aprende, você começa a enxergar as similaridades você percebe que essas linguagens não são tão diferentes assim.

Ah, e muitíssimoo obrigada pelo feedback! Estou à disposição. <3