Skip to content

Commit

Permalink
online order problem statement
Browse files Browse the repository at this point in the history
  • Loading branch information
PrasanthVijayy committed Oct 17, 2024
1 parent c71bd0e commit ef73c9c
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class OrderManager {
constructor() {
this.history = [];
this.undoStack = [];
}

executeCommand(command) {
command.execute();
this.history.push(command);
this.undoStack = []; // Reset the undo stack after a new command
}

undo() {
const command = this.history.pop();
if (command) {
command.undo();
this.undoStack.push(command);
} else {
console.log("No commands to undo.");
}
}

redo() {
const command = this.undoStack.pop();
if (command) {
command.execute();
this.history.push(command);
} else {
console.log("No commands to redo.");
}
}
}

module.exports = OrderManager;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Order = require("./receiver");
const OrderManager = require("./ManagerInvoker");
const {
AddItemCommand,
RemoveItemCommand,
ProcessOrderCommand,
CancelOrderCommand,
} = require("./orderCommand");

// Client code
const myOrder = new Order();
const orderManager = new OrderManager();

// Add items
const addItem1 = new AddItemCommand(myOrder, "Burger");
const addItem2 = new AddItemCommand(myOrder, "Fries");

orderManager.executeCommand(addItem1); // Adds "Burger"
orderManager.executeCommand(addItem2); // Adds "Fries"

// Show current items
myOrder.showItems();

// Remove an item
const removeItem = new RemoveItemCommand(myOrder, "Fries");
orderManager.executeCommand(removeItem); // Removes "Fries"

// Undo remove
orderManager.undo(); // Restores "Fries"

// Show current items
myOrder.showItems();

// Process order
const processOrder = new ProcessOrderCommand(myOrder);
orderManager.executeCommand(processOrder); // Processes the order

// Cancel the order
const cancelOrder = new CancelOrderCommand(myOrder);
orderManager.executeCommand(cancelOrder); // Cancels the order
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class OrderCommand {
execute() {
throw new Error("execute() is not implemented");
}

undo() {
throw new Error("undo() is not implemented");
}
}

// Concrete class
class AddItemCommand extends OrderCommand {
constructor(order, item) {
super();
this.order = order;
this.item = item;
}

execute() {
this.order.addItem(this.item);
}

undo() {
this.order.removeItem(this.item);
}
}

class RemoveItemCommand extends OrderCommand {
constructor(order, item) {
super();
this.order = order;
this.item = item;
}

execute() {
this.order.removeItem(this.item);
}

undo() {
this.order.addItem(this.item);
}
}

class ProcessOrderCommand extends OrderCommand {
constructor(order) {
super();
this.order = order;
}

execute() {
this.order.processOrder();
}

undo() {
console.log("Cannot undo after order is processed.");
}
}

class CancelOrderCommand extends OrderCommand {
constructor(order) {
super();
this.order = order;
}

execute() {
this.order.cancelOrder();
}

undo() {
console.log("Cannot undo after order is cancelled.");
}
}

module.exports = {
AddItemCommand,
RemoveItemCommand,
ProcessOrderCommand,
CancelOrderCommand,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Order {
constructor() {
this.items = [];
}

addItem(item) {
this.items.push(item);
}

removeItem(item) {
const index = this.items.indexOf(item);
if (index > -1) {
this.items.splice(index, 1);
console.log(`Removed ${item} from the order.`);
} else {
console.log(`${item} is not in the order.`);
}
}

processOrder() {
if (this.items.length > 0) {
console.log(`Order processed: ${this.items.join(", ")}`);
this.items = [];
} else {
console.log("Add products to the order before processing it.");
}
}

cancelOrder() {
console.log("Order cancelled.");
this.items = [];
}

showItems() {
console.log(
`Items in the order: ${this.items.join(", ") || "Empty, no items found."}`
);
}
}

module.exports = Order;

0 comments on commit ef73c9c

Please sign in to comment.