Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

write a java code to create an online shopping system

+16 votes
asked Oct 17, 2019 by anonymous

2 Answers

+1 vote
answered Jun 14, 2024 by Sunkara Chanikya kumar (160 points)
class Product {
  private String name;
  private double price;
  private int quantity;

  // Getters and setters omitted for brevity
}

class Cart {
  private List<Product> items;

  public void addToCart(Product product) {
    items.add(product);
  }

  public double getTotalPrice() {
    double total = 0;
    for (Product product : items) {
      total += product.getPrice();
    }
    return total;
  }

  // ... other methods for removing items, managing quantities
}

public class OnlineShop {
  private List<Product> products;

  public OnlineShop() {
    // Initialize product list with sample data
    products = new ArrayList<>();
    products.add(new Product("T-Shirt", 19.99, 10));
    products.add(new Product("Laptop", 799.99, 5));
  }

  public void displayProducts() {
    for (int i = 0; i < products.size(); i++) {
      Product product = products.get(i);
      System.out.println((i + 1) + ". " + product.getName() + " - $" + product.getPrice());
    }
  }

  public void addToCart(int productIndex, Cart cart) {
    if (productIndex > 0 && productIndex <= products.size()) {
      Product product = products.get(productIndex - 1);
      cart.addToCart(product);
    } else {
      System.out.println("Invalid product selection.");
    }
  }

  public static void main(String[] args) {
    OnlineShop shop = new OnlineShop();
    Cart cart = new Cart();

    shop.displayProducts();

    // User interaction loop (simulated here)
    int choice = 1; // Sample user choice
    shop.addToCart(choice, cart);

    System.out.println("Cart Total: $" + cart.getTotalPrice());
  }
}
0 votes
answered Jun 25, 2024 by Mr Srikanth (190 points)
import java.util.*;

public class OnlineShoppingSystem {
    private static List<Product> products = new ArrayList<>();
    private static ShoppingCart cart = new ShoppingCart();
    private static User currentUser;

    public static void main(String[] args) {
        // Initialize some products
        products.add(new Product("Laptop", "High-performance laptop", 999.99));
        products.add(new Product("Smartphone", "Latest smartphone model", 499.99));
        products.add(new Product("Headphones", "Noise-cancelling headphones", 199.99));

        // Simulate user actions
        currentUser = new User("John Doe", "[email protected]", "123 Main St");

        addToCart(products.get(0));
        addToCart(products.get(1));
        viewCart();
        checkout();
    }

    private static void addToCart(Product product) {
        cart.addItem(product);
        System.out.println(product.getName() + " added to cart.");
    }

    private static void viewCart() {
        System.out.println("\nShopping Cart:");
        for (Product item : cart.getItems()) {
            System.out.println(item.getName() + " - $" + item.getPrice());
        }
        System.out.println("Total: $" + cart.calculateTotal());
    }

    private static void checkout() {
        System.out.println("\nChecking out...");
        // Simulate payment processing
        Payment paymentProcessor = new Payment();
        boolean paymentSuccessful = paymentProcessor.processPayment(new Order(cart.getItems(), currentUser, LocalDateTime.now(), "Pending"), new PaymentDetails("John Doe", "1234 5678 9012", "123", "12/24"));
        if (paymentSuccessful) {
            System.out.println("Payment successful. Order placed!");
            cart.clearCart(); // Clear cart after successful checkout
        } else {
            System.out.println("Payment failed. Please try again.");
        }
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...