Shopping cart codes

 Sure, here are some basic examples of code to create a shopping cart in different programming languages. I'll cover Python, JavaScript, and Java.


### Python


```python

class ShoppingCart:

    def __init__(self):

        self.items = []


    def add_item(self, item, price):

        self.items.append({'item': item, 'price': price})


    def remove_item(self, item):

        self.items = [i for i in self.items if i['item'] != item]


    def total_price(self):

        return sum(item['price'] for item in self.items)


    def list_items(self):

        return self.items


# Example usage

cart = ShoppingCart()

cart.add_item('Apple', 0.99)

cart.add_item('Banana', 0.59)

cart.remove_item('Apple')

print(cart.total_price())

print(cart.list_items())

```


### JavaScript


```javascript

class ShoppingCart {

    constructor() {

        this.items = [];

    }


    addItem(item, price) {

        this.items.push({ item, price });

    }


    removeItem(item) {

        this.items = this.items.filter(i => i.item !== item);

    }


    totalPrice() {

        return this.items.reduce((total, item) => total + item.price, 0);

    }


    listItems() {

        return this.items;

    }

}


// Example usage

const cart = new ShoppingCart();

cart.addItem('Apple', 0.99);

cart.addItem('Banana', 0.59);

cart.removeItem('Apple');

console.log(cart.totalPrice());

console.log(cart.listItems());

```


### Java


```java

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


class ShoppingCart {

    private List<Item> items;


    public ShoppingCart() {

        items = new ArrayList<>();

    }


    public void addItem(String itemName, double price) {

        items.add(new Item(itemName, price));

    }


    public void removeItem(String itemName) {

        Iterator<Item> iterator = items.iterator();

        while (iterator.hasNext()) {

            Item item = iterator.next();

            if (item.getName().equals(itemName)) {

                iterator.remove();

            }

        }

    }


    public double totalPrice() {

        double total = 0;

        for (Item item : items) {

            total += item.getPrice();

        }

        return total;

    }


    public List<Item> listItems() {

        return items;

    }


    private class Item {

        private String name;

        private double price;


        public Item(String name, double price) {

            this.name = name;

            this.price = price;

        }


        public String getName() {

            return name;

        }


        public double getPrice() {

            return price;

        }


        @Override

        public String toString() {

            return name + ": $" + price;

        }

    }



    public static void main(String[] args) {

        ShoppingCart cart = new ShoppingCart();

        cart.addItem("Apple", 0.99);

        cart.addItem("Banana", 0.59);

        cart.removeItem("Apple");

        System.out.println("Total Price: $" + cart.totalPrice());

        System.out.println("Items: " + cart.listItems());

    }

}

```


These examples cover basic functionality for adding, removing items, calculating total price, and listing items in a shopping cart. You can expand on these examples by adding more features such as quantity management, discounts, etc.

No comments: