Advertisement
Zoddster

OOP Theory

Jul 1st, 2025
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. interface PaymentMethod {
  4.     void processPayment(double amount);
  5. }
  6.  
  7. class WalletPayment implements PaymentMethod {
  8.     public void processPayment(double amount) {
  9.         System.out.println("Rs. " + amount + " paid via Wallet.");
  10.     }
  11. }
  12.  
  13. class CardPayment implements PaymentMethod {
  14.     public void processPayment(double amount) {
  15.         System.out.println("Rs. " + amount + "paid via Card.");
  16.     }
  17. }
  18.  
  19. class CashPayment implements PaymentMethod {
  20.     public void processPayment(double amount) {
  21.         System.out.println("Rs. " + amount + " paid via Cash-on-Delivery.");
  22.     }
  23. }
  24.  
  25. abstract class User {
  26.     protected String name;
  27.     protected String email;
  28.     protected String password;
  29.  
  30.     public User(String name, String email, String password) {
  31.         this.name = name;
  32.         this.email = email;
  33.         this.password = password;
  34.     }
  35.     public String getName() {
  36.         return name;
  37.     }
  38.     public String getEmail() {
  39.         return email;
  40.     }
  41. }
  42.  
  43. class Customer extends User {
  44.     public Customer(String name, String email, String password) {
  45.         super(name, email, password);
  46.     }
  47.  
  48.     public void placeOrder() {
  49.         System.out.println(name + " placed an order.");
  50.     }
  51. }
  52.  
  53. class DeliveryAgent extends User {
  54.     private static int count = 0;
  55.     private Vehicle vehicle;
  56.  
  57.     public DeliveryAgent(String name, String email, String password, Vehicle vehicle) {
  58.         super(name, email, password);
  59.         this.vehicle = vehicle;
  60.         count++;
  61.     }
  62.  
  63.     public static int agentCount() {
  64.         return count;
  65.     }
  66.  
  67.     public Vehicle getVehicle() {
  68.         return vehicle;
  69.     }
  70. }
  71.  
  72. abstract class Vehicle {
  73.     protected String type;
  74.  
  75.     public String getType() {
  76.         return type;
  77.     }
  78. }
  79.  
  80. class Scooter extends Vehicle {
  81.     public Scooter() {
  82.         this.type = "Scooter";
  83.     }
  84. }
  85.  
  86. class Bicycle extends Vehicle {
  87.     public Bicycle() {
  88.         this.type = "Bicycle";
  89.     }
  90. }
  91.  
  92. class FoodItem {
  93.     private String itemName;
  94.     private double price;
  95.     private String category;
  96.  
  97.     public FoodItem(String itemName, double price, String category) {
  98.         this.itemName = itemName;
  99.         this.price = price;
  100.         this.category = category;
  101.     }
  102.  
  103.     public String getItemName() {
  104.         return itemName;
  105.     }
  106.     public double getPrice() {
  107.         return price;
  108.     }
  109.     public String getCategory() {
  110.         return category;
  111.     }
  112. }
  113.  
  114. class Order {
  115.     private List<FoodItem> foodItems;
  116.     private Customer customer;
  117.     private DeliveryAgent deliveryAgent;
  118.     private Date orderTime;
  119.     private double totalAmount;
  120.     private String deliveryAddress;
  121.  
  122.     public Order(List<FoodItem> foodItems, Customer customer, DeliveryAgent deliveryAgent, String deliveryAddress) {
  123.         this.foodItems = foodItems;
  124.         this.customer = customer;
  125.         this.deliveryAgent = deliveryAgent;
  126.         this.deliveryAddress = deliveryAddress;
  127.         this.orderTime = new Date();
  128.         calculatePrice();
  129.     }
  130.  
  131.     public void calculatePrice() {
  132.         totalAmount = 0;
  133.         for (FoodItem item : foodItems) {
  134.             totalAmount += item.getPrice();
  135.         }
  136.     }
  137.  
  138.     public double getTotalAmount() {
  139.         return totalAmount;
  140.     }
  141.  
  142.     public void printSummary() {
  143.         System.out.println("Customer: " + customer.getName());
  144.         System.out.println("Delivery Rider: " + deliveryAgent.getName() + " (" + deliveryAgent.getVehicle().getType() + ")");
  145.         System.out.println("Address: " + deliveryAddress);
  146.         System.out.println("Time: " + orderTime);
  147.         System.out.println("\n");
  148.         for (FoodItem item : foodItems) {
  149.             System.out.println("- " + item.getItemName() + " (" + item.getCategory() + ") Rs. " + item.getPrice());
  150.         }
  151.         System.out.println("Total Amount: Rs. " + totalAmount);
  152.     }
  153. }
  154.  
  155. interface OrderService {
  156.     void placeOrder();
  157.     void cancelOrder();
  158.     void completeOrder();
  159. }
  160.  
  161. class OrderManager implements OrderService {
  162.     private List<Order> orderList = new ArrayList<>();
  163.  
  164.     public void addOrder(Order order) {
  165.         orderList.add(order);
  166.         placeOrder();
  167.     }
  168.  
  169.     public void placeOrder() {
  170.         System.out.println("Order placed.");
  171.     }
  172.  
  173.     public void cancelOrder() {
  174.         System.out.println("Order cancelled.");
  175.     }
  176.  
  177.     public void completeOrder() {
  178.         System.out.println("Order delivered.");
  179.     }
  180. }
  181.  
  182. public class FoodDeliverySystem {
  183.     public static void main(String[] args) {
  184.  
  185.         Customer customer = new Customer("LightYagami", "[email protected]", "Note666");
  186.         DeliveryAgent agent = new DeliveryAgent("Saim", "[email protected]", "Zodd", new Scooter());
  187.  
  188.         List<FoodItem> items = new ArrayList<>();
  189.         items.add(new FoodItem("Cheese Burger", 350, "Fast-Food"));
  190.         items.add(new FoodItem("Mayo Fries", 150, "Side"));
  191.  
  192.         Order order = new Order(items, customer, agent, "Street 69-C, Hyderabad");
  193.         order.printSummary();
  194.  
  195.         PaymentMethod payment = new CardPayment();
  196.         payment.processPayment(order.getTotalAmount());
  197.  
  198.         OrderManager manager = new OrderManager();
  199.         manager.addOrder(order);
  200.         manager.completeOrder();
  201.     }
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement