Here's a simple Java program for a shopping mall:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Product {
String name;
double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
}
public class ShoppingMall {
static Map<String, Product> products = new HashMap<>();
static Map<String, Integer> cart = new HashMap<>();
public static void main(String[] args) {
addProducts();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. View 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("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
viewProducts();
break;
case 2:
addToCart(scanner);
break;
case 3:
viewCart();
break;
case 4:
checkout();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
static void addProducts() {
products.put("P1", new Product("Shirt", 500.0));
products.put("P2", new Product("Pants", 700.0));
products.put("P3", new Product("Shoes", 1000.0));
products.put("P4", new Product("Watch", 5000.0));
products.put("P5", new Product("Sunglasses", 2000.0));
}
static void viewProducts() {
System.out.println("Available Products:");
for (Map.Entry<String, Product> entry : products.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue().name + " - " + entry.getValue().price);
}
}
static void addToCart(Scanner scanner) {
System.out.print("Enter product code: ");
String code = scanner.next();
if (products.containsKey(code)) {
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
if (cart.containsKey(code)) {
cart.put(code, cart.get(code) + quantity);
} else {
cart.put(code, quantity);
}
System.out.println("Product added to cart successfully.");
} else {
System.out.println("Product not found.");
}
}
static void viewCart() {
System.out.println("Your Cart:");
double total = 0;
for (Map.Entry<String, Integer> entry : cart.entrySet()) {
Product product = products.get(entry.getKey());
double price = product.price * entry.getValue();
System.out.println(entry.getKey() + ": " + product.name + " x " + entry.getValue() + " = " + price);
total += price;
}
System.out.println("Total: " + total);
}
static void checkout() {
System.out.println("Checkout successful. Thank you for shopping with us!");
cart.clear();
}
}
This program allows users to:
1. View available products
2. Add products to their cart
3. View their cart
4. Checkout and clear their cart
Note that this is a simplified example and a real-world shopping mall program would require more features, such as user authentication, payment processing, and inventory management.
No comments:
Post a Comment