0% found this document useful (0 votes)
11 views4 pages

Saol 5.

jawaban coding pembuatan e camers

Uploaded by

Fajar Arip Usba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Saol 5.

jawaban coding pembuatan e camers

Uploaded by

Fajar Arip Usba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

ArrayList;

class Product {

private String id;

private String name;

private double price;

private int stock;

public Product(String id, String name, double price, int stock) {

this.id = id;

this.name = name;

this.price = price;

this.stock = stock;

public String getDetails() {

return "ID: " + id + ", Name: " + name + ", Price: " + price + ", Stock: " + stock;

public double getPrice() {

return price;

public int getStock() {

return stock;

public void reduceStock(int quantity) {


stock -= quantity;

class ShoppingCart {

private ArrayList<Product> products;

private double totalPrice;

public ShoppingCart() {

products = new ArrayList<>();

totalPrice = 0.0;

public void addProduct(Product product) {

products.add(product);

totalPrice += product.getPrice();

public void removeProduct(String productId) {

for (Product product : products) {

if (product.getDetails().contains(productId)) {

totalPrice -= product.getPrice();

products.remove(product);

break;

}
public double calculateTotal() {

return totalPrice;

public void displayProducts() {

for (Product product : products) {

System.out.println(product.getDetails());

class User {

private String username;

private String password;

private ShoppingCart shoppingCart;

public User(String username, String password) {

this.username = username;

this.password = password;

this.shoppingCart = new ShoppingCart();

public boolean login(String username, String password) {

return this.username.equals(username) && this.password.equals(password);

public void checkout() {

System.out.println("Total Price: " + shoppingCart.calculateTotal());


}

public ShoppingCart getShoppingCart() {

return shoppingCart;

public class Main {

public static void main(String[] args) {

// Create products

Product product1 = new Product("P001", "Laptop", 15000000, 10);

Product product2 = new Product("P002", "Smartphone", 5000000, 20);

// Create user

User user = new User("user1", "password123");

// Add products to cart

user.getShoppingCart().addProduct(product1);

user.getShoppingCart().addProduct(product2);

// Display products in cart

System.out.println("Products in Cart:");

user.getShoppingCart().displayProducts();

// Checkout

user.checkout();

You might also like