Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added Lecture 11/.DS_Store
Binary file not shown.
Binary file added Lecture 11/Java Code/.DS_Store
Binary file not shown.
97 changes: 97 additions & 0 deletions Lecture 11/Java Code/Tomato/TomatoApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Restaurant } from './models/Restaurant.js';
import { MenuItem } from './models/MenuItem.js';
import { RestaurantManager } from './managers/RestaurantManager.js';
import { OrderManager } from './managers/OrderManager.js';
import { NowOrderFactory } from './factories/NowOrderFactory.js';
import { ScheduledOrderFactory } from './factories/ScheduledOrderFactory.js';
import { NotificationService } from './services/NotificationService.js';

export class TomatoApp {
constructor() {
this.initializeRestaurants();
}

initializeRestaurants() {
const restaurant1 = new Restaurant("Bikaner", "Delhi");
restaurant1.addMenuItem(new MenuItem("P1", "Chole Bhature", 120));
restaurant1.addMenuItem(new MenuItem("P2", "Samosa", 15));

const restaurant2 = new Restaurant("Haldiram", "Kolkata");
restaurant2.addMenuItem(new MenuItem("P1", "Raj Kachori", 80));
restaurant2.addMenuItem(new MenuItem("P2", "Pav Bhaji", 100));
restaurant2.addMenuItem(new MenuItem("P3", "Dhokla", 50));

const restaurant3 = new Restaurant("Saravana Bhavan", "Chennai");
restaurant3.addMenuItem(new MenuItem("P1", "Masala Dosa", 90));
restaurant3.addMenuItem(new MenuItem("P2", "Idli Vada", 60));
restaurant3.addMenuItem(new MenuItem("P3", "Filter Coffee", 30));

const restaurantManager = RestaurantManager.getInstance();
restaurantManager.addRestaurant(restaurant1);
restaurantManager.addRestaurant(restaurant2);
restaurantManager.addRestaurant(restaurant3);
}

searchRestaurants(location) {
return RestaurantManager.getInstance().searchByLocation(location);
}

selectRestaurant(user, restaurant) {
const cart = user.getCart();
cart.setRestaurant(restaurant);
}

addToCart(user, itemCode) {
const restaurant = user.getCart().getRestaurant();
if (!restaurant) {
console.log("Please select a restaurant first.");
return;
}
for (const item of restaurant.getMenu()) {
if (item.getCode() === itemCode) {
user.getCart().addItem(item);
break;
}
}
}

checkoutNow(user, orderType, paymentStrategy) {
return this.checkout(user, orderType, paymentStrategy, new NowOrderFactory());
}

checkoutScheduled(user, orderType, paymentStrategy, scheduleTime) {
return this.checkout(user, orderType, paymentStrategy, new ScheduledOrderFactory(scheduleTime));
}

checkout(user, orderType, paymentStrategy, orderFactory) {
if (user.getCart().isEmpty()) return null;

const userCart = user.getCart();
const orderedRestaurant = userCart.getRestaurant();
const itemsOrdered = userCart.getItems();
const totalCost = userCart.getTotalCost();

const order = orderFactory.createOrder(user, userCart, orderedRestaurant, itemsOrdered, paymentStrategy, totalCost, orderType);
OrderManager.getInstance().addOrder(order);
return order;
}

payForOrder(user, order) {
const isPaymentSuccess = order.processPayment();

if (isPaymentSuccess) {
NotificationService.notify(order);
user.getCart().clear();
}
}

printUserCart(user) {
console.log("Items in cart:");
console.log("------------------------------------");
for (const item of user.getCart().getItems()) {
console.log(`${item.getCode()} : ${item.getName()} : ₹${item.getPrice()}`);
}
console.log("------------------------------------");
console.log(`Grand total : ₹${user.getCart().getTotalCost()}`);
}
}
Binary file added Lecture 11/Java Code/Tomato/UML.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions Lecture 11/Java Code/Tomato/factories/NowOrderFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { OrderFactory } from './OrderFactory.js';
import { DeliveryOrder } from '../models/DeliveryOrder.js';
import { PickupOrder } from '../models/PickupOrder.js';
import { TimeUtils } from '../utils/TimeUtils.js';

export class NowOrderFactory extends OrderFactory {
createOrder(user, cart, restaurant, menuItems, paymentStrategy, totalCost, orderType) {
let order = null;

if (orderType === "Delivery") {
const deliveryOrder = new DeliveryOrder();
deliveryOrder.setUserAddress(user.getAddress());
order = deliveryOrder;
} else {
const pickupOrder = new PickupOrder();
pickupOrder.setRestaurantAddress(restaurant.getLocation());
order = pickupOrder;
}

order.setUser(user);
order.setRestaurant(restaurant);
order.setItems(menuItems);
order.setPaymentStrategy(paymentStrategy);
order.setScheduled(TimeUtils.getCurrentTime());
order.setTotal(totalCost);
return order;
}
}
5 changes: 5 additions & 0 deletions Lecture 11/Java Code/Tomato/factories/OrderFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class OrderFactory {
createOrder(user, cart, restaurant, menuItems, paymentStrategy, totalCost, orderType) {
throw new Error("Method 'createOrder()' must be implemented.");
}
}
32 changes: 32 additions & 0 deletions Lecture 11/Java Code/Tomato/factories/ScheduledOrderFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { OrderFactory } from './OrderFactory.js';
import { DeliveryOrder } from '../models/DeliveryOrder.js';
import { PickupOrder } from '../models/PickupOrder.js';

export class ScheduledOrderFactory extends OrderFactory {
constructor(scheduleTime) {
super();
this.scheduleTime = scheduleTime;
}

createOrder(user, cart, restaurant, menuItems, paymentStrategy, totalCost, orderType) {
let order = null;

if (orderType === "Delivery") {
const deliveryOrder = new DeliveryOrder();
deliveryOrder.setUserAddress(user.getAddress());
order = deliveryOrder;
} else {
const pickupOrder = new PickupOrder();
pickupOrder.setRestaurantAddress(restaurant.getLocation());
order = pickupOrder;
}

order.setUser(user);
order.setRestaurant(restaurant);
order.setItems(menuItems);
order.setPaymentStrategy(paymentStrategy);
order.setScheduled(this.scheduleTime);
order.setTotal(totalCost);
return order;
}
}
46 changes: 46 additions & 0 deletions Lecture 11/Java Code/Tomato/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TomatoApp } from './TomatoApp.js';
import { User } from './models/User.js';
import { UpiPaymentStrategy } from './strategies/UpiPaymentStrategy.js';

function main() {
// Simulating a happy flow
// Create TomatoApp Object
const tomato = new TomatoApp();

// Simulate a user coming in (Happy Flow)
const user = new User(101, "Aditya", "Delhi");
console.log(`User: ${user.getName()} is active.`);

// User searches for restaurants by location
const restaurantList = tomato.searchRestaurants("Delhi");

if (restaurantList.length === 0) {
console.log("No restaurants found!");
return;
}

console.log("Found Restaurants:");
for (const restaurant of restaurantList) {
console.log(" - " + restaurant.getName());
}

// User selects a restaurant
tomato.selectRestaurant(user, restaurantList[0]);
console.log("Selected restaurant: " + restaurantList[0].getName());

// User adds items to the cart
tomato.addToCart(user, "P1");
tomato.addToCart(user, "P2");

tomato.printUserCart(user);

// User checkout the cart
const order = tomato.checkoutNow(user, "Delivery", new UpiPaymentStrategy("1234567890"));

// User pays for the cart. If payment is successful, notification is sent.
if (order) {
tomato.payForOrder(user, order);
}
}

main();
27 changes: 27 additions & 0 deletions Lecture 11/Java Code/Tomato/managers/OrderManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class OrderManager {
constructor() {
if (OrderManager.instance) {
return OrderManager.instance;
}
this.orders = [];
OrderManager.instance = this;
}

static getInstance() {
if (!OrderManager.instance) {
OrderManager.instance = new OrderManager();
}
return OrderManager.instance;
}

addOrder(order) {
this.orders.push(order);
}

listOrders() {
console.log("\n--- All Orders ---");
for (const order of this.orders) {
console.log(`${order.getType()} order for ${order.getUser().getName()} | Total: ₹${order.getTotal()} | At: ${order.getScheduled()}`);
}
}
}
32 changes: 32 additions & 0 deletions Lecture 11/Java Code/Tomato/managers/RestaurantManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export class RestaurantManager {
constructor() {
if (RestaurantManager.instance) {
return RestaurantManager.instance;
}
this.restaurants = [];
RestaurantManager.instance = this;
}

static getInstance() {
if (!RestaurantManager.instance) {
RestaurantManager.instance = new RestaurantManager();
}
return RestaurantManager.instance;
}

addRestaurant(r) {
this.restaurants.push(r);
}

searchByLocation(loc) {
const result = [];
loc = loc.toLowerCase();
for (const r of this.restaurants) {
const rl = r.getLocation().toLowerCase();
if (rl === loc) {
result.push(r);
}
}
return result;
}
}
43 changes: 43 additions & 0 deletions Lecture 11/Java Code/Tomato/models/Cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export class Cart {
constructor() {
this.restaurant = null;
this.items = [];
}

addItem(item) {
if (this.restaurant === null) {
console.error("Cart: Set a restaurant before adding items.");
return;
}
this.items.push(item);
}

getTotalCost() {
let sum = 0;
for (const item of this.items) {
sum += item.getPrice();
}
return sum;
}

isEmpty() {
return this.restaurant === null || this.items.length === 0;
}

clear() {
this.items = [];
this.restaurant = null;
}

setRestaurant(r) {
this.restaurant = r;
}

getRestaurant() {
return this.restaurant;
}

getItems() {
return this.items;
}
}
20 changes: 20 additions & 0 deletions Lecture 11/Java Code/Tomato/models/DeliveryOrder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Order } from './Order.js';

export class DeliveryOrder extends Order {
constructor() {
super();
this.userAddress = "";
}

getType() {
return "Delivery";
}

setUserAddress(addr) {
this.userAddress = addr;
}

getUserAddress() {
return this.userAddress;
}
}
31 changes: 31 additions & 0 deletions Lecture 11/Java Code/Tomato/models/MenuItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class MenuItem {
constructor(code, name, price) {
this.code = code;
this.name = name;
this.price = price;
}

getCode() {
return this.code;
}

setCode(c) {
this.code = c;
}

getName() {
return this.name;
}

setName(n) {
this.name = n;
}

getPrice() {
return this.price;
}

setPrice(p) {
this.price = p;
}
}
Loading