Task - TypeScript - Restaurant Management System #153
Replies: 5 comments
-
|
CODE interface Ingredient {
id: number;
name: string;
}
interface Dish {
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
addIngredient(ingredient: Ingredient): void;
removeIngredient(ingredientId: number): void;
}
class DishImpl implements Dish {
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
constructor(id: number, name: string, cuisine: string) {
this.id = id;
this.name = name;
this.cuisine = cuisine;
this.ingredients = [];
}
addIngredient(ingredient: Ingredient): void {
this.ingredients.push(ingredient);
}
removeIngredient(ingredientId: number): void {
const index = this.ingredients.findIndex(
(ingredient) => ingredient.id === ingredientId
);
if (index !== -1) {
this.ingredients.splice(index, 1);
}
}
}
class Order {
id: number;
dishes: Dish[];
status: "placed" | "completed";
constructor(id: number) {
this.id = id;
this.dishes = [];
this.status = "placed";
}
addDish(dish: Dish): void {
this.dishes.push(dish);
}
removeDish(dishId: number): void {
const index = this.dishes.findIndex((dish) => dish.id === dishId);
if (index !== -1) {
this.dishes.splice(index, 1);
}
}
completeOrder(): void {
this.status = "completed";
}
}
class Restaurant {
private dishes: Dish[] = [];
private orders: Order[] = [];
addDish(dish: Dish): void {
this.dishes.push(dish);
}
removeDish(dishId: number): void {
const index = this.dishes.findIndex((dish) => dish.id === dishId);
if (index !== -1) {
this.dishes.splice(index, 1);
}
}
filterDishesByCuisine(cuisine: string): Dish[] {
return this.dishes.filter((dish) => dish.cuisine === cuisine);
}
filterDishesByMainIngredient(ingredientId: number): Dish[] {
return this.dishes.filter((dish) =>
dish.ingredients.some((ingredient) => ingredient.id === ingredientId)
);
}
placeOrder(order: Order): void {
this.orders.push(order);
}
getOrder(orderId: number): Order | null {
const order = this.orders.find((order) => order.id === orderId);
return order || null;
}
}
// Usage example
const restaurant = new Restaurant();
// Add some ingredients
const ingredient1: Ingredient = { id: 1, name: "Tomato" };
const ingredient2: Ingredient = { id: 2, name: "Cheese" };
// Add some dishes
const pizza = new DishImpl(1, "Pizza", "Italian");
pizza.addIngredient(ingredient1);
pizza.addIngredient(ingredient2);
restaurant.addDish(pizza);
const curry = new DishImpl(2, "Curry", "Indian");
curry.addIngredient(ingredient1);
restaurant.addDish(curry);
// Filter dishes by cuisine
const italianDishes = restaurant.filterDishesByCuisine("Italian");
console.log(italianDishes); // [Pizza]
|
Beta Was this translation helpful? Give feedback.
-
CODEinterface Ingredient {
id: number;
name: string;
}
interface Dish {
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
addIngredient: (ingredient: Ingredient) => void;
removeIngredient: (ingredientId: number) => void;
}
class Order {
id: number;
dishes: Dish[];
status: "placed" | "completed";
constructor(id: number) {
this.id = id;
this.dishes = [];
this.status = "placed";
}
addDish(dish: Dish) {
this.dishes.push(dish);
}
removeDish(dishId: number) {
const index = this.dishes.findIndex(dish => dish.id === dishId);
if (index !== -1) {
this.dishes.splice(index, 1);
}
}
completeOrder() {
this.status = "completed";
}
}
class Restaurant {
menu(menu: any) {
throw new Error("Method not implemented.");
}
private dishes: Dish[];
private orders: Order[];
constructor() {
this.dishes = [];
this.orders = [];
}
addDish(dish: Dish) {
this.dishes.push(dish);
}
removeDish(dishId: number) {
const index = this.dishes.findIndex(dish => dish.id === dishId);
if (index !== -1) {
this.dishes.splice(index, 1);
}
}
filterDishesByCuisine(cuisine: string) {
return this.dishes.filter(dish => dish.cuisine === cuisine);
}
filterDishesByMainIngredient(ingredientId: number) {
return this.dishes.filter(dish => dish.ingredients.some(ingredient => ingredient.id === ingredientId));
}
placeOrder(order: Order) {
this.orders.push(order);
}
getOrder(orderId: number) {
return this.orders.find(order => order.id === orderId) || null;
}
}
const tomato: Ingredient = { id: 1, name: "Tomato" };
const cheese: Ingredient = { id: 2, name: "Cheese" };
const pepperoni: Ingredient = { id: 3, name: "Pepperoni" };
const pizza: Dish = {
id: 1, name: "Pizza", cuisine: "Italian", ingredients: [],
addIngredient: function (ingredient: Ingredient): void {
throw new Error("Function not implemented.");
},
removeIngredient: function (ingredientId: number): void {
throw new Error("Function not implemented.");
}
};
const omelette: Dish = {
id: 2, name: "Omelette", cuisine: "French", ingredients: [],
addIngredient: function (ingredient: Ingredient): void {
throw new Error("Function not implemented.");
},
removeIngredient: function (ingredientId: number): void {
throw new Error("Function not implemented.");
}
};
const restaurant: Restaurant = new Restaurant();
restaurant.addDish(pizza);
restaurant.addDish(omelette);
console.log(restaurant.menu);
restaurant.removeDish(omelette.id);
console.log(restaurant.menu);
// Test filtering dishes
pizza.ingredients.push(tomato, cheese, pepperoni);
omelette.ingredients.push(cheese);
console.log(restaurant.filterDishesByCuisine("Italian"));
console.log(restaurant.filterDishesByMainIngredient(2));
const order: Order = new Order(1);
order.addDish(pizza);
order.addDish(omelette);
console.log(order.status);
restaurant.placeOrder(order);
console.log(order.status);
console.log(restaurant.getOrder(order.id));
console.log(restaurant.getOrder(999)); OUTPUT |
Beta Was this translation helpful? Give feedback.
-
interface Ingredient {
id: number;
name: string;
}
interface Dish {
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
addIngredient(ingredient: Ingredient): void;
removeIngredient(ingredientId: number): void;
}
class DishItems implements Dish {
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
constructor(id: number, name: string, cuisine: string) {
this.id = id;
this.name = name;
this.cuisine = cuisine;
this.ingredients = [];
}
addIngredient(ingredient: Ingredient): void {
this.ingredients.push(ingredient);
}
removeIngredient(ingredientId: number): void {
this.ingredients = this.ingredients.filter(
(ingredient) => ingredient.id !== ingredientId
);
}
}
class Order {
id: number;
dishes: Dish[];
status: "placed" | "completed";
constructor(id: number) {
this.id = id;
this.dishes = [];
this.status = "placed";
}
addDish = (dish: Dish): void => {
this.dishes.push(dish);
};
removeDish = (dishId: number): void => {
this.dishes = this.dishes.filter((dish) => dishId === dish.id);
};
completeOrder = (): void => {
this.status = "completed";
};
}
class Restaurant {
dishes: Dish[];
orders: Order[];
constructor(dishes: Dish[] = [], orders: Order[] = []) {
this.dishes = dishes;
this.orders = orders;
}
addDish = (dish: Dish): void => {
this.dishes.push(dish);
};
removeDish = (dishId: number): void => {
this.dishes = this.dishes.filter((dish) => dishId !== dish.id);
};
filterDishesByCuisine = (cuisine: string): Dish[] => {
return this.dishes.filter((dish) => dish.cuisine === cuisine);
};
filterDishesByMainIngredient = (ingredientId: number): Dish[] => {
return this.dishes.filter((dish) =>
dish.ingredients.some((ingredient) => ingredient.id === ingredientId)
);
};
placeOrder = (order: Order): void => {
this.orders.push(order);
};
getOrder = (orderId: number): Order | null => {
return this.orders.find((order) => order.id === orderId) || null;
};
}
// Create a Restaurant object
const restaurant = new Restaurant();
// Ingredients
const ingredient1: Ingredient = { id: 1, name: "Anchovies" };
const ingredient2: Ingredient = { id: 2, name: "Ham" };
const ingredient3: Ingredient = { id: 3, name: "Butter" };
// Add some dishes
const dish1 = new DishItems(1, "Build Your Own Pizza", "Italian");
dish1.addIngredient(ingredient1);
dish1.addIngredient(ingredient2);
restaurant.addDish(dish1);
const dish2 = new DishItems(2, "Paneer Tikka", "Indian");
dish2.addIngredient(ingredient1);
dish2.addIngredient(ingredient3);
restaurant.addDish(dish2);
// Order Place
const order: Order = new Order(1);
order.addDish(dish1);
order.addDish(dish2);
console.log(order.status);
restaurant.placeOrder(order);
console.log(order.status);
// Filer dishes by cusine
console.log(restaurant.filterDishesByCuisine("indian"));
// Filer dishes by ingredients
console.log(restaurant.filterDishesByMainIngredient(3));
// Order Complete
const order1 = new Order(1);
order1.completeOrder(); |
Beta Was this translation helpful? Give feedback.
-
Codeinterface Ingredient {
id: number;
name: string;
}
interface Dish {
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
}
class Dishes implements Dish{
id: number;
name: string;
cuisine: string;
ingredients: Ingredient[];
constructor (id: number, name:string, cuisine: string, ingredients: Ingredient[] = []) {
this.id = id;
this.name= name;
this.cuisine = cuisine;
this.ingredients = ingredients;
}
addIngredient = (ingredient: Ingredient): void => {
const ingredients = this.ingredients.find((i) => i.id === ingredient.id);
if (ingredients) {
console.log (`Ingredient ${ingredient.id}-${ingredient.name} has been already found in dish ${this.id}-${this.name}`);
}
else {
this.ingredients.push(ingredient)
console.log("Ingredient " + ingredient.id + "-" + ingredient.name + " has been added to " + this.id + "-" + this.name);
}
};
removeIngredient(ingredientId: number): void {
const index = this.ingredients.findIndex((i) => i.id === ingredientId);
if(index > -1) {
this.ingredients.splice(index,1);
console.log(`Ingredient ${ingredientId} has been removed from dish ${this.id}-${this.name}`);
}
else {
console.log(`Could not find Ingredient ${ingredientId} is the dish ${this.id}-${this.name}`);
}
}
}
class Order {
id: number;
dishes: Dish[];
status: "placed" | "completed";
constructor(id: number, dishes: Dish[]=[]) {
this.id = id;
this.dishes = dishes;
this.status = "placed";
}
addDish(dish: Dish): void {
if (this.status == "placed") {
this.dishes.push(dish);
console.log(`Dish ${dish.id}-${dish.name} has been added to order ${this.id}`);
}
else if (this.status == "completed") {
console.log ("This Order is complete, place a new order");
}
}
removeDish(dishId: number): void {
const index = this.dishes.findIndex((dish) => dish.id === dishId);
if (index > -1) {
this.dishes.splice(index,1);
console.log (`Dish ${dishId} has been removed from the order ${this.id}`);
}
else {
console.log ("Dish " + dishId + " is not present in the order "+ this.id);
}
}
completeOrder = (): void => {
this.status = "completed";
console.log("Order " + this.id + " is complete!")
}
}
class Restaurant {
menu: Dish[];
orders: Order[];
constructor(menu: Dish[]=[], orders: Order[]=[]) {
this.menu = menu;
this.orders = orders;
}
addDish = (dish:Dish):void => {
const foundDish = this.menu.find((i) => i.id === dish.id);
if(foundDish) {
console.log("Dish " + dish.id + " is already present")
}
else {
this.menu.push(dish);
console.log(`Dish ${dish.id}-${dish.name} has been added to the menu`)
}
}
removeDish = (dishId:number) => this.menu = this.menu.filter((dish) => dish.id != dishId);
filterDishesByCuisine = (cuisine: string): Dish[] => this.menu.filter((dish) => dish.cuisine === cuisine);
filterDishesByMainIngredient = (ingredientId: number): Dish[] => { return this.menu.filter((dish) =>
dish.ingredients.find((i) => i.id === ingredientId))};
placeOrder = (order: Order) => this.orders.push(order);
getOrder = (orderId: number): Order | null => {
const findorder = this.orders.find((order) => order.id === orderId);
if(findorder) {
return findorder;
}
else {
return null;
}
}
}
const ingredient1: Ingredient = {id: 1, name: "Butter"};
const ingredient2: Ingredient = {id: 2, name: "Garlic"};
const ingredient3: Ingredient = {id:3, name: "Tomato"};
const ingredient4: Ingredient = {id:4, name: "Cheese"};
const ingredient5: Ingredient = {id:5, name: 'Milk'};
const ingredient6: Ingredient = {id:6, name: "Coffee"};
const dish1 = new Dishes(101,"Tomato Soup","Indo-Chinese");
dish1.addIngredient(ingredient2);
dish1.addIngredient(ingredient3);
dish1.addIngredient(ingredient3);
const dish2 = new Dishes(102,"Spaghetti","Italian");
dish2.addIngredient(ingredient1);
dish2.addIngredient(ingredient2);
dish2.addIngredient(ingredient3);
dish2.addIngredient(ingredient4);
dish2.removeIngredient(3);
dish2.removeIngredient(5);
const dish3 = new Dishes(103,"Cheese Pizza","Italian");
dish3.addIngredient(ingredient4);
const dish4 = new Dishes(104,"Indian Coffee","Indian");
dish4.addIngredient(ingredient5);
dish4.addIngredient(ingredient6);
const myRestaurant = new Restaurant();
myRestaurant.addDish(dish1);
myRestaurant.addDish(dish2);
myRestaurant.addDish(dish3);
myRestaurant.addDish(dish4);
myRestaurant.removeDish(103);
myRestaurant.removeDish(100);
myRestaurant.addDish(dish3);
console.log(myRestaurant.filterDishesByCuisine('Italian'));
console.log(myRestaurant.filterDishesByMainIngredient(4));
const order1 = new Order(1);
order1.addDish(dish1);
order1.addDish(dish2);
order1.addDish(dish3);
order1.removeDish(103);
const order2 = new Order(2,[dish2,dish4]);
myRestaurant.placeOrder(order1);
myRestaurant.placeOrder(order2);
console.log((myRestaurant.getOrder(2)));
order1.completeOrder();Screenshot |
Beta Was this translation helpful? Give feedback.
-
|
interface Ingredient { interface Dish { class DishImpl implements Dish { constructor(id: number, name: string, cuisine: string) { addIngredient(ingredient: Ingredient): void { removeIngredient(ingredientId: number): void { class Order { constructor(id: number) { addDish(dish: Dish): void { removeDish(dishId: number): void { completeOrder(): void { class Restaurant { addDish(dish: Dish): void { removeDish(dishId: number): void { filterDishesByCuisine(cuisine: string): Dish[] { filterDishesByMainIngredient(ingredientId: number): Dish[] { placeOrder(order: Order): void { getOrder(orderId: number): Order | null { // Usage example // Add some ingredients // Add some dishes const curry = new DishImpl(2, "Curry", "Indian"); // Filter dishes by cuisine |
Beta Was this translation helpful? Give feedback.









Uh oh!
There was an error while loading. Please reload this page.
-
You have been asked to develop a restaurant management system that allows you to manage dishes, ingredients, and orders. The system should let you add dishes and their ingredients, filter dishes by cuisine or main ingredient, and let users place and complete orders.
Create the following interfaces and classes:
Interface
Ingredientwith the following properties:Interface
Dishwith the following properties and methods:Class
Orderwith the following properties and methods:Class
Restaurantwith the following methods:Make use of arrow functions and array methods such as
push,find,filter, andsplicein the implementation.This task involves managing dishes, ingredients, and orders in a restaurant. The classes and interfaces represent different aspects of the restaurant management system, and you will need to implement methods to add and remove dishes and ingredients, filter dishes, and handle orders.
Beta Was this translation helpful? Give feedback.
All reactions