forked from CSEdgeOfficial/Java-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcommerce.java
More file actions
166 lines (143 loc) · 4.86 KB
/
Ecommerce.java
File metadata and controls
166 lines (143 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Product {
private int id;
private String name;
private double price;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class CartItem {
private Product product;
private int quantity;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
}
class ShoppingCart {
private List<CartItem> items;
public ShoppingCart() {
this.items = new ArrayList<>();
}
public void addItem(CartItem item) {
items.add(item);
}
public List<CartItem> getItems() {
return items;
}
public double calculateTotal() {
double total = 0;
for (CartItem item : items) {
total += item.getProduct().getPrice() * item.getQuantity();
}
return total;
}
}
public class ECommercePlatform {
private static List<Product> products = new ArrayList<>();
private static ShoppingCart cart = new ShoppingCart();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
initializeProducts();
while (true) {
System.out.println("1. Browse Products");
System.out.println("2. Add to Cart");
System.out.println("3. View Cart");
System.out.println("4. Checkout");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (option) {
case 1:
browseProducts();
break;
case 2:
addToCart();
break;
case 3:
viewCart();
break;
case 4:
checkout();
break;
case 5:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option!");
}
}
}
private static void initializeProducts() {
products.add(new Product(1, "Product 1", 10.0));
products.add(new Product(2, "Product 2", 20.0));
// Add more products here
}
private static void browseProducts() {
System.out.println("Available Products:");
for (Product product : products) {
System.out.println(product.getId() + ". " + product.getName() + " - $" + product.getPrice());
}
}
private static void addToCart() {
System.out.print("Enter product ID: ");
int productId = scanner.nextInt();
scanner.nextLine(); // Consume newline character
Product selectedProduct = null;
for (Product product : products) {
if (product.getId() == productId) {
selectedProduct = product;
break;
}
}
if (selectedProduct != null) {
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline character
cart.addItem(new CartItem(selectedProduct, quantity));
System.out.println("Item added to cart!");
} else {
System.out.println("Product not found!");
}
}
private static void viewCart() {
List<CartItem> items = cart.getItems();
if (items.isEmpty()) {
System.out.println("Cart is empty.");
} else {
System.out.println("Cart:");
for (CartItem item : items) {
System.out.println(item.getProduct().getName() + " - Quantity: " + item.getQuantity());
}
System.out.println("Total: $" + cart.calculateTotal());
}
}
private static void checkout() {
System.out.println("Checkout - Total: $" + cart.calculateTotal());
System.out.println("Thank you for shopping with us!");
// Logic for completing the checkout process (e.g., payment, order creation, etc.) would go here
// This is a simplified version without payment integration or order tracking
cart = new ShoppingCart(); // Clear the cart after checkout
}
}