Food ordering
unknown
java
a month ago
8.4 kB
1
Indexable
Never
import java.util.*; // Interfaces interface User { String getName(); void placeOrder(Order order); void rateOrder(Order order, int rating); List<Order> getOrderHistory(); } interface Restaurant { String getName(); List<MenuItem> getMenu(); void addMenuItem(MenuItem item); void removeMenuItem(MenuItem item); Order createOrder(User user, List<MenuItem> items); } interface MenuItem { String getName(); double getPrice(); } interface Order { String getOrderId(); User getUser(); Restaurant getRestaurant(); List<MenuItem> getItems(); double getTotalPrice(); void completeOrder(); void rateOrder(int rating); int getRating(); boolean cancelOrder(); } interface Rating { int getRating(); void setRating(int rating); } interface Payment { boolean processPayment(double amount); boolean refundPayment(double amount); } // Concrete Implementations class Customer implements User { private String name; private List<Order> orderHistory; public Customer(String name) { this.name = name; this.orderHistory = new ArrayList<>(); } @Override public String getName() { return name; } @Override public void placeOrder(Order order) { orderHistory.add(order); System.out.println("Order placed successfully."); } @Override public void rateOrder(Order order, int rating) { order.rateOrder(rating); System.out.println("Order rated successfully."); } @Override public List<Order> getOrderHistory() { return orderHistory; } } class FoodRestaurant implements Restaurant { private String name; private List<MenuItem> menu; public FoodRestaurant(String name) { this.name = name; this.menu = new ArrayList<>(); } @Override public String getName() { return name; } @Override public List<MenuItem> getMenu() { return menu; } @Override public void addMenuItem(MenuItem item) { menu.add(item); System.out.println("Menu item added: " + item.getName()); } @Override public void removeMenuItem(MenuItem item) { menu.remove(item); System.out.println("Menu item removed: " + item.getName()); } @Override public Order createOrder(User user, List<MenuItem> items) { Order order = new FoodOrder(UUID.randomUUID().toString(), user, this, items); user.placeOrder(order); return order; } } class FoodItem implements MenuItem { private String name; private double price; public FoodItem(String name, double price) { this.name = name; this.price = price; } @Override public String getName() { return name; } @Override public double getPrice() { return price; } } class FoodOrder implements Order, Rating { private String orderId; private User user; private Restaurant restaurant; private List<MenuItem> items; private double totalPrice; private boolean isCompleted; private boolean isCancelled; private int rating; private Payment paymentMethod; public FoodOrder(String orderId, User user, Restaurant restaurant, List<MenuItem> items) { this.orderId = orderId; this.user = user; this.restaurant = restaurant; this.items = items; this.isCompleted = false; this.isCancelled = false; this.rating = 0; calculateTotalPrice(); } private void calculateTotalPrice() { totalPrice = items.stream().mapToDouble(MenuItem::getPrice).sum(); } @Override public String getOrderId() { return orderId; } @Override public User getUser() { return user; } @Override public Restaurant getRestaurant() { return restaurant; } @Override public List<MenuItem> getItems() { return items; } @Override public double getTotalPrice() { return totalPrice; } public void setPaymentMethod(Payment paymentMethod) { this.paymentMethod = paymentMethod; } public boolean processPayment() { if (paymentMethod != null) { return paymentMethod.processPayment(totalPrice); } System.out.println("Payment method not set."); return false; } public boolean refundPayment() { if (paymentMethod != null) { return paymentMethod.refundPayment(totalPrice); } System.out.println("Payment method not set."); return false; } @Override public void completeOrder() { if (isCancelled) { System.out.println("Cannot complete a cancelled order."); return; } if (processPayment()) { isCompleted = true; System.out.println("Order completed: " + orderId); } else { System.out.println("Order could not be completed due to payment failure."); } } @Override public boolean cancelOrder() { if (isCompleted) { System.out.println("Cannot cancel a completed order."); return false; } if (isCancelled) { System.out.println("Order has already been cancelled."); return false; } if (processPayment()) { refundPayment(); } isCancelled = true; System.out.println("Order cancelled: " + orderId); return true; } @Override public void rateOrder(int rating) { if (isCancelled) { System.out.println("Cannot rate a cancelled order."); return; } if (isCompleted) { this.rating = rating; System.out.println("Order rated with: " + rating); } else { System.out.println("Order not completed yet."); } } @Override public int getRating() { return rating; } @Override public void setRating(int rating) { this.rating = rating; } } class CreditCardPayment implements Payment { private String cardNumber; private String cardHolderName; public CreditCardPayment(String cardNumber, String cardHolderName) { this.cardNumber = cardNumber; this.cardHolderName = cardHolderName; } @Override public boolean processPayment(double amount) { // Simulate payment processing logic System.out.println("Processing credit card payment of $" + amount + " for " + cardHolderName); // Assuming payment is always successful for simplicity return true; } @Override public boolean refundPayment(double amount) { // Simulate refund logic System.out.println("Refunding credit card payment of $" + amount + " for " + cardHolderName); // Assuming refund is always successful for simplicity return true; } } // Main Class to Test the System public class FoodOrderingSystem { public static void main(String[] args) { // Create restaurant Restaurant restaurant = new FoodRestaurant("The Gourmet Kitchen"); // Create menu items MenuItem pizza = new FoodItem("Pizza", 12.99); MenuItem burger = new FoodItem("Burger", 8.99); MenuItem salad = new FoodItem("Salad", 5.99); // Add menu items to the restaurant restaurant.addMenuItem(pizza); restaurant.addMenuItem(burger); restaurant.addMenuItem(salad); // Create a user User user = new Customer("John Doe"); // User places an order List<MenuItem> orderItems = Arrays.asList(pizza, salad); FoodOrder order = (FoodOrder) restaurant.createOrder(user, orderItems); // Set payment method for the order Payment paymentMethod = new CreditCardPayment("1234-5678-9101-1121", "John Doe"); order.setPaymentMethod(paymentMethod); // Cancel the order before completion order.cancelOrder(); // Attempt to complete the cancelled order order.completeOrder(); // View order history for (Order o : user.getOrderHistory()) { System.out.println("Order ID: " + o.getOrderId() + ", Rating: " + o.getRating()); } } }
Leave a Comment