0% found this document useful (0 votes)
4 views7 pages

Solid Example

The document outlines a food delivery application with classes for items, orders, users, payment methods, notifications, delivery services, and database management. It includes functionalities for adding items to an order, processing payments, delivering orders, and saving orders to a database. The application supports multiple payment methods and delivery options, adhering to principles of object-oriented design such as interfaces and dependency inversion.

Uploaded by

senthilanandhibe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Solid Example

The document outlines a food delivery application with classes for items, orders, users, payment methods, notifications, delivery services, and database management. It includes functionalities for adding items to an order, processing payments, delivering orders, and saving orders to a database. The application supports multiple payment methods and delivery options, adhering to principles of object-oriented design such as interfaces and dependency inversion.

Uploaded by

senthilanandhibe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

class Item {

private String name;


private double price;

public Item(String name, double price) {


[Link] = name;
[Link] = price;
}

public String getName() { return name; }


public double getPrice() { return price; }
}
=======================

import [Link];
import [Link];

class Order {
private List<Item> items = new ArrayList<>();
private double totalPrice;

public void addItem(String name, double price) {


Item item = new Item(name, price);
[Link](item);
}

public double calculateTotal() {


totalPrice = 0;
for (Item item : items) {
totalPrice += [Link]();
}
return totalPrice;
}

public List<Item> getItems() {


return items;
}

public double getTotalPrice() {


return totalPrice;
}
}
====================
class User {
private String name;
private String email;

public User(String name, String email) {


[Link] = name;
[Link] = email;
}

public String getName() { return name; }


public String getEmail() { return email; }
}
==============================
interface PaymentMethod {
void pay(double amount);
}

class CreditCardPayment implements PaymentMethod {


public void pay(double amount) {
[Link]("Paid ₹" + amount + " using Credit Card");
}
}

class PayPalPayment implements PaymentMethod {


public void pay(double amount) {
[Link]("Paid ₹" + amount + " using PayPal");
}
}
========================

class NotificationService {
public void sendOrderConfirmation(User user, Order order) {
[Link]("Sending confirmation to " + [Link]());
[Link]("Order Total: ₹" + [Link]());
}
}
=====================
class PaymentProcessor {
public void processPayment(PaymentMethod method, double amount) {
[Link](amount);
}
}
===================
interface DeliveryService {
void deliverOrder(Order order);
}

class DeliveryPerson implements DeliveryService {


public void deliverOrder(Order order) {
[Link]("Order assigned to delivery person.");
}
}
class DroneDelivery implements DeliveryService {
public void deliverOrder(Order order) {
[Link]("Order delivered via drone.");
}
}

=========================
public class FoodDeliveryApp {
public static void main(String[] args) {

// Create user
User user = new User("john", "john@[Link]");

// Create order
Order order = new Order();
[Link]("Pizza", 250);
[Link]("Burger", 150);

double total = [Link]();


[Link]("Total Price: ₹" + total);

// Payment
PaymentMethod paymentMethod = new CreditCardPayment();
PaymentProcessor paymentProcessor = new PaymentProcessor();
[Link](paymentMethod, total);

// Delivery
DeliveryService deliveryService = new DeliveryPerson();
[Link](order);

// Notification
NotificationService notificationService = new NotificationService();
[Link](user, order);

// Database
Database db = new MySQLDatabase();
OrderService orderService = new OrderService(db);
[Link](order);
}
}

=====================

import [Link];
import [Link];

// MENU MANAGEMENT (ISP)

interface MenuManagement {
void addMenuItem(Item item);
void updateStock(Item item, int quantity);
}

interface Reporting {
void generateMonthlyReport();
}

class SmallRestaurant implements MenuManagement {


private Map<String, Integer> stock = new HashMap<>();
public void addMenuItem(Item item) {
[Link]([Link](), 10); // default stock
[Link]("Added item: " + [Link]());
}

public void updateStock(Item item, int quantity) {


[Link]([Link](), quantity);
[Link]("Updated stock for " + [Link]() + ": " +
quantity);
}
}
========================================
======================
//DATABASE (DIP)
interface Database {
void saveOrder(Order order);
}

class MySQLDatabase implements Database {


public void saveOrder(Order order) {
[Link]("Order saved in MySQL DB");
}
}

class MongoDBDatabase implements Database {


public void saveOrder(Order order) {
[Link]("Order saved in MongoDB");
}
}

=========================================
// ORDER SERVICE (DIP)
class OrderService {
private Database database;

public OrderService(Database database) {


[Link] = database;
}

public void placeOrder(Order order) {


[Link](order);
}
}

You might also like