-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineShoppingCartSystem.java
More file actions
154 lines (128 loc) · 4.91 KB
/
OnlineShoppingCartSystem.java
File metadata and controls
154 lines (128 loc) · 4.91 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Product {
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public double getTotalPrice() {
return price * quantity;
}
}
class ShoppingCart {
private List<Product> products;
private static final double TAX_RATE = 0.08;
private static final double DISCOUNT_RATE = 0.1;
public ShoppingCart() {
this.products = new ArrayList<>();
}
public void addProduct(Product product) {
products.add(product);
System.out.println(product.getName() + " added to the cart.");
}
public void removeProduct(String productName) {
products.removeIf(product -> product.getName().equalsIgnoreCase(productName));
System.out.println(productName + " removed from the cart.");
}
public double calculateTotalCost() {
double total = 0;
for (Product product : products) {
total += product.getTotalPrice();
}
if (total > 100) {
total -= total * DISCOUNT_RATE;
}
total += total * TAX_RATE;
return total;
}
public void displayCartContents() {
if (products.isEmpty()) {
System.out.println("Your cart is empty.");
return;
}
System.out.println("\nCart Contents:");
for (Product product : products) {
System.out.println("Product: " + product.getName() + ", Price: $" + product.getPrice() + ", Quantity: " + product.getQuantity());
}
System.out.println("Total Cost (with tax and discounts): $" + calculateTotalCost());
}
}
class Customer {
private String name;
private String email;
private ShoppingCart cart;
public Customer(String name, String email) {
this.name = name;
this.email = email;
this.cart = new ShoppingCart();
}
public ShoppingCart getCart() {
return cart;
}
}
public class OnlineShoppingCartSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Customer customer = null;
try {
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your email: ");
String email = scanner.nextLine();
customer = new Customer(name, email);
while (true) {
System.out.println("\n1. Add Product\n2. Remove Product\n3. View Cart\n4. Checkout\n5. Exit");
System.out.print("Choose an option: ");
int option = Integer.parseInt(scanner.nextLine());
if (option == 1) {
System.out.print("Enter product name: ");
String productName = scanner.nextLine();
System.out.print("Enter product price: ");
double productPrice = Double.parseDouble(scanner.nextLine());
System.out.print("Enter product quantity: ");
int quantity = Integer.parseInt(scanner.nextLine());
Product product = new Product(productName, productPrice, quantity);
customer.getCart().addProduct(product);
} else if (option == 2) {
System.out.print("Enter product name to remove: ");
String productName = scanner.nextLine();
customer.getCart().removeProduct(productName);
} else if (option == 3) {
customer.getCart().displayCartContents();
} else if (option == 4) {
System.out.println("\nProceeding to checkout...");
customer.getCart().displayCartContents();
System.out.println("Thank you for shopping with us!");
break;
} else if (option == 5) {
System.out.println("Exiting the system. Thank you!");
break;
} else {
System.out.println("Invalid option. Please try again.");
}
}
} catch (NumberFormatException e) {
System.out.println("Invalid input! Please enter the correct data type.");
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}