Mini Project - JS & TS - Online Store Management System #155
Replies: 7 comments
-
CODEinterface ProductCategory {
id: number;
name: string;
}
interface Product {
id: number;
name: string;
price: number;
category: ProductCategory;
}
interface OrderItem {
product: Product;
quantity: number;
}
class Customer {
id: number;
name: string;
email: string;
orders: Order[] = [];
constructor(id: number, name: string, email: string) {
this.id = id;
this.name = name;
this.email = email;
}
placeOrder(order: Order): void {
this.orders.push(order);
}
viewOrders(): Order[] {
return this.orders;
}
}
interface Order {
id: number;
customer: Customer;
items: OrderItem[];
status: "placed" | "shipped" | "delivered";
}
class OnlineStore {
products: Product[] = [];
categories: ProductCategory[] = [];
orders: Order[] = [];
addProduct(product: Product): void {
this.products.push(product);
}
removeProduct(productId: number): void {
const index = this.products.findIndex(
(product) => product.id === productId
);
if (index !== -1) {
this.products.splice(index, 1);
}
}
filterProductsByCategory(categoryId: number): Product[] {
return this.products.filter(
(product) => product.category.id === categoryId
);
}
filterProductsByPriceRange(minPrice: number, maxPrice: number): Product[] {
return this.products.filter(
(product) => product.price >= minPrice && product.price <= maxPrice
);
}
getOrder(orderId: number): Order | null {
const order = this.orders.find((order) => order.id === orderId);
if (order) {
return order;
}
return null;
}
updateOrderStatus(
orderId: number,
status: "placed" | "shipped" | "delivered"
): void {
const order = this.orders.find((order) => order.id === orderId);
if (order) {
order.status = status;
}
}
addCategory(category: ProductCategory): void {
this.categories.push(category);
}
removeCategory(categoryId: number): void {
const index = this.categories.findIndex(
(category) => category.id === categoryId
);
if (index !== -1) {
this.categories.splice(index, 1);
}
}
addOrder(order: Order): void {
this.orders.push(order);
}
}
const store = new OnlineStore();
const electronicsCategory: ProductCategory = { id: 1, name: "Electronics" };
const booksCategory: ProductCategory = { id: 2, name: "Books" };
store.addCategory(electronicsCategory);
store.addCategory(booksCategory);
const laptop: Product = {
id: 1,
name: "Laptop",
price: 999.99,
category: electronicsCategory
};
const phone: Product = {
id: 2,
name: "Phone",
price: 599.99,
category: electronicsCategory
};
const book: Product = {
id: 3,
name: "The Lord of the Rings",
price: 29.99,
category: booksCategory
};
store.addProduct(laptop);
store.addProduct(phone);
store.addProduct(book);
store.removeProduct(2);
const electronicsProducts = store.filterProductsByCategory(1);
const affordableProducts = store.filterProductsByPriceRange(0, 100);
const customer = new Customer(1, "Alice", "alice@example.com");
const order: Order = {
id: 1,
customer: customer,
items: [
{ product: laptop, quantity: 1 },
{ product: book, quantity: 2 }
],
status: "placed"
};
store.addOrder(order);
store.updateOrderStatus(1, "shipped");
const orders = customer.viewOrders();
const clothingCategory: ProductCategory = { id: 3, name: "Clothing" };
store.addCategory(clothingCategory);
store.removeCategory(2);
console.log(store);
console.log(customer);
console.log(orders);
console.log(store.filterProductsByCategory(1));
console.log(store.filterProductsByPriceRange(0, 100));
OUTPUT |
Beta Was this translation helpful? Give feedback.
-
|
interface ProductCategory { interface Product { interface OrderItem { class Customer { constructor(id: number, name: string, email: string) { placeOrder(order: Order): void { viewOrders(): Order[] { interface Order { class OnlineStore { addProduct(product: Product): void { removeProduct(productId: number): void { filterProductsByCategory(categoryId: number): Product[] { filterProductsByPriceRange(minPrice: number, maxPrice: number): Product[] { getOrder(orderId: number): Order | null { updateOrderStatus( addCategory(category: ProductCategory): void { removeCategory(categoryId: number): void { addOrder(order: Order): void { const store = new OnlineStore(); const electronicsCategory: ProductCategory = { id: 1, name: "Electronics" }; const laptop: Product = { store.removeProduct(2); const electronicsProducts = store.filterProductsByCategory(1); const affordableProducts = store.filterProductsByPriceRange(0, 100); const customer = new Customer(1, "Alice", "alice@example.com"); const order: Order = { store.addOrder(order); store.updateOrderStatus(1, "shipped"); const orders = customer.viewOrders(); const clothingCategory: ProductCategory = { id: 3, name: "Clothing" }; store.removeCategory(2); console.log(store); |
Beta Was this translation helpful? Give feedback.
-
// Create a Product Category
interface CategoryInt {
id: number;
name: string;
}
// Create a Product Class With Implements Product Interface
class Category implements CategoryInt {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
// Create a Product Interface
interface ProductInt {
id: number;
name: string;
price: number;
category: CategoryInt;
}
// Create a Product Class With Implements Product Interface
class Product implements ProductInt {
id: number;
name: string;
price: number;
category: CategoryInt;
constructor(id: number, name: string, price: number, category: CategoryInt) {
this.id = id;
this.name = name;
this.price = price;
this.category = category;
}
}
// Create a Order Item Interface
interface OrderItemInt {
product: ProductInt;
quantity: number;
}
// Create a Order Item Class
class OrderItem implements OrderItemInt {
product: ProductInt;
quantity: number;
constructor(product: ProductInt, quantity: number) {
this.product = product;
this.quantity = quantity;
}
}
// Create a Order Interface
interface OrderInt {
id: number;
customer: Customer;
items: OrderItemInt[];
status: "placed" | "shipped" | "delivered";
addItem(item: OrderItemInt): void;
removeItem(productId: number): void;
}
// Create a Custom Class
class CustomOrder implements OrderInt {
id: number;
customer: Customer;
status: "placed" | "shipped" | "delivered";
items: OrderItemInt[] = [];
constructor(
id: number,
customer: Customer,
status: "placed" | "shipped" | "delivered"
) {
this.id = id;
this.customer = customer;
this.status = status;
}
addItem = (item: OrderItemInt): void => {
this.items.push(item);
console.log(`You item(${item.product.name}) has been added to order.`);
};
removeItem = (productId: number): void => {
const item = this.items.find((item) => item.product.id === productId);
if (item) {
this.items.splice(this.items.indexOf(item), 1);
console.log(`You item(${item.product.name}) has been removed`);
}
};
}
// Create a Customer Class
class Customer {
id: number;
name: string;
email: string;
orders: OrderInt[] = [];
constructor(id: number, name: string, email: string) {
this.id = id;
this.name = name;
this.email = email;
}
placeOrder = (order: OrderInt): void => {
this.orders.push(order);
};
viewOrders = (): OrderInt[] => this.orders;
}
// Create a Online Store Class
class OnlineStore {
products: ProductInt[] = [];
orders: OrderInt[] = [];
addProduct = (product: ProductInt): void => {
this.products.push(product);
};
removeProduct = (productId: number): void => {
this.products.splice(
this.products.findIndex((product) => product.id === productId),
1
);
};
filterProductsByCategory = (categoryId: number): ProductInt[] => {
return this.products.filter(
(product) => product.category.id === categoryId
);
};
filterProductsByPriceRange = (
minPrice: number,
maxPrice: number
): ProductInt[] => {
return this.products.filter(
(product) => product.price >= minPrice && product.price <= maxPrice
);
};
getOrder = (orderId: number): OrderInt | null => {
return this.orders.find((order) => order.id === orderId) || null;
};
updateOrderStatus = (
orderId: number,
status: "placed" | "shipped" | "delivered"
): void => {
const order = this.orders.find((order) => order.id === orderId);
if (order) {
order.status = status;
console.log(`Your order has been ${order.status} successfully.`);
}
};
}
// Create a Online Store
const os = new OnlineStore();
// Create a Category
const category1 = new Category(1, "Mens Footear");
const category2 = new Category(2, "Shorts and Trousers");
// Create a Product
const product1 = new Product(1, "Air Max 270 Mens Trainers", 135, category1);
const product2 = new Product(2, "Jersey Dress", 50, category2);
os.addProduct(product1);
os.addProduct(product2);
// Create a order item
const item1 = new OrderItem(product1, 1);
const item2 = new OrderItem(product1, 2);
// Create a Customer
const customer1 = new Customer(1, "Steve", "steve@example.com");
const customer2 = new Customer(2, "Nick", "nick@example.com");
// Create a Order
const order = new CustomOrder(1, customer1, "placed");
order.addItem(item1);
order.addItem(item2);
// Customer can place the order or order itself
customer1.placeOrder(order);
customer1.viewOrders();
// Remove product
os.removeProduct(1);
os.removeProduct(2);
// Filter products by category and price range
os.filterProductsByCategory(1);
os.filterProductsByPriceRange(50, 100);
// Get order by order id
os.getOrder(1);
// Update the oder status
os.updateOrderStatus(1, "shipped");
os.updateOrderStatus(1, "delivered"); |
Beta Was this translation helpful? Give feedback.
-
|
interface ProductCategory { interface Product { interface OrderItem { interface Order1 { class Customer { placeOrder = (order: Order1): void => { viewOrders = (): Order1[] => this.orders; class OnlineStore { constructor( addItem = (item: OrderItem): void => { removeItem = (productId: number): void => { addProduct = (product: Product): void => { removeProduct = (productId: number): void => { filterProductsByCategory = (categoryId: number): Product[] => { filterProductsByPriceRange = ( getOrder = (orderId: number): Order1 | null => { updateOrderStatus = ( |
Beta Was this translation helpful? Give feedback.
-
|
CODE interface ProductCategory {
id: number;
name: string;
}
interface Product {
id: number;
name: string;
price: number;
category: ProductCategory;
}
interface OrderItem {
product: Product;
quantity: number;
}
class Customer {
id: number;
name: string;
email: string;
orders: Order[] = [];
constructor(id: number, name: string, email: string) {
this.id = id;
this.name = name;
this.email = email;
}
placeOrder(order: Order): void {
this.orders.push(order);
}
viewOrders(): Order[] {
return this.orders;
}
}
interface Order {
id: number;
customer: Customer;
items: OrderItem[];
status: "placed" | "shipped" | "delivered";
}
class OnlineStore {
products: Product[] = [];
categories: ProductCategory[] = [];
orders: Order[] = [];
addProduct(product: Product): void {
this.products.push(product);
}
removeProduct(productId: number): void {
const index = this.products.findIndex(p => p.id === productId);
if (index !== -1) {
this.products.splice(index, 1);
}
}
filterProductsByCategory(categoryId: number): Product[] {
return this.products.filter(p => p.category.id === categoryId);
}
filterProductsByPriceRange(minPrice: number, maxPrice: number): Product[] {
return this.products.filter(p => p.price >= minPrice && p.price <= maxPrice);
}
getOrder(orderId: number): Order | null {
return this.orders.find(o => o.id === orderId) || null;
}
updateOrderStatus(orderId: number, status: "placed" | "shipped" | "delivered"): void {
const order = this.getOrder(orderId);
if (order) {
order.status = status;
}
}
}
// create some categories
const electronics: ProductCategory = { id: 1, name: "Electronics" };
const books: ProductCategory = { id: 2, name: "Books" };
const clothing: ProductCategory = { id: 3, name: "Clothing" };
// create some products
const laptop: Product = { id: 1, name: "Laptop", price: 1000, category: electronics };
const phone: Product = { id: 2, name: "Phone", price: 500, category: electronics };
const novel: Product = { id: 3, name: "Novel", price: 10, category: books };
const tshirt: Product = { id: 4, name: "T-Shirt", price: 20, category: clothing };
// create an online store
const store = new OnlineStore();
// add the products and categories to the store
store.categories.push(electronics, books, clothing);
store.products.push(laptop, phone, novel, tshirt);
// create some customers
const alice = new Customer(1, "Alice", "alice@example.com");
const bob = new Customer(2, "Bob", "bob@example.com");
// create an order for Alice
const order1: Order = {
id: 1,
customer: alice,
items: [
{ product: laptop, quantity: 1 },
{ product: phone , quantity: 2 }
],
status: "placed",
addItem(item: OrderItem): void {
this.items.push(item);
},
removeItem(productId: number): void {
const index = this.items.findIndex((item) => item.product.id === productId);
if (index !== -1) {
this.items.splice(index, 1);
}
}
};
// create another order for Bob
const order2: Order = {
id: 2,
customer: bob,
items: [
{ product: novel, quantity: 3 },
{ product: tshirt, quantity: 2 }
],
status: "placed",
addItem(item: OrderItem): void {
this.items.push(item);
},
removeItem(productId: number): void {
const index = this.items.findIndex((item) => item.product.id === productId);
if (index !== -1) {
this.items.splice(index, 1);
}
}
};
// place the orders
alice.placeOrder(order1);
bob.placeOrder(order2);
// view the orders placed by Alice and Bob
console.log(alice.viewOrders());
console.log(bob.viewOrders());
// update the status of Alice's order
store.updateOrderStatus(1, "shipped");
// get Bob's order by ID
const order = store.getOrder(2);
if (order) {
console.log(order);
} else {
console.log("Order not found.");
}
// filter products by category and price range
const electronicsProducts = store.filterProductsByCategory(1);
const affordableProducts = store.filterProductsByPriceRange(0, 50);
console.log(electronicsProducts);
console.log(affordableProducts);
|
Beta Was this translation helpful? Give feedback.
-
Codeinterface ProductCategory {
id: number;
name: string;
}
interface Product {
id: number;
name:string;
price: number;
category: ProductCategory;
}
interface OrderItem {
product: Product;
quantity: number;
}
class Customer {
id: number;
name: string;
email: string;
orders: Order[] =[];
constructor(id: number,name: string,email: string) {
this.id = id;
this.name = name;
this.email = email;
}
placeOrder(order: Order): void {
this.orders.push(order);
}
viewOrders = (): Order[] => this.orders;
}
interface Order {
id: number;
customer: Customer;
items: OrderItem[];
status: "placed"|"shipped"|"delivered";
addItem(item: OrderItem): void;
removeItem(productId: number): void;
}
class NewOrder implements Order {
id: number;
customer: Customer;
items: OrderItem[];
status: "placed" | "shipped" | "delivered";
constructor(id: number,customer: Customer,items: OrderItem[]=[]) {
this.id = id;
this.customer = customer;
this.items = items;
this.status="placed";
}
addItem(item: OrderItem): void {
this.items.push(item);
}
removeItem(productId: number): void {
this.items = this.items.filter((product) => product.product.id != productId);
}
}
class OnlineStore {
products: Product[] = [];
orders: Order[] = [];
addProduct(product: Product): void {
this.products.push(product)
console.log(`Product ${product.id}-${product.name} has been added to the online store.`)
};
removeProduct(productId: number): void {
this.products = this.products.filter((product) => product.id != productId);
}
addOrder(order: Order): void {
this.orders.push(order);
}
filterProductsByCategory(categoryId: number): Product[] {
return this.products.filter((product) => product.category.id === categoryId);
}
filterProductsByPriceRange(minPrice: number, maxPrice: number): Product[] {
return this.products.filter((product) => (product.price >= minPrice && product.price <= maxPrice));
}
getOrder = (orderId: number): Order | null => this.orders.filter((order) => order.id === orderId);
updateOrderStatus(orderId: number, status: "placed" | "shipped" | "delivered"): void {
const findOrder = this.orders.find((order) => order.id === orderId);
if (findOrder) {
findOrder.status = status;
console.log(`Order ${findOrder.id} status has been updated to ${findOrder.status}`);
}
else {
console.log(`Could not find order ${orderId}`);
}
}
}
const myStore = new OnlineStore();
const elctronics: ProductCategory = {id:1, name: "electronics"};
const books: ProductCategory = {id:2, name: "books"};
const laptop: Product={id:101,name:"DELL laptop",price:600,category:elctronics};
const book1: Product={id:102,name:"The Wings of fire",price:50,category:books};
const user1 = new Customer(1,"Aiden","aiden@example.com");
const user2 = new Customer(2,"Krishna","krishna@example.com");
myStore.addProduct(laptop);
myStore.addProduct(book1);
const order1 = new NewOrder(1,user1);
order1.addItem({product:laptop,quantity:1});
order1.addItem({product:book1,quantity:5});
user1.placeOrder(order1);
myStore.addOrder(order1);
console.log(user1.viewOrders());
console.log((myStore.filterProductsByCategory(1)));
console.log((myStore.filterProductsByPriceRange(500,600)));
console.log((myStore.getOrder(1)));
myStore.updateOrderStatus(1,"shipped");
console.log(user1.viewOrders());
myStore.updateOrderStatus(1,"delivered");
console.log(user1.viewOrders());Screenshot |
Beta Was this translation helpful? Give feedback.
-
|
interface ProductCategory { interface Product { interface OrderItem { class Customer { constructor(id: number, name: string, email: string) { placeOrder(order: Order): void { viewOrders(): Order[] { interface Order { class OnlineStore { addProduct(product: Product): void { removeProduct(productId: number): void { filterProductsByCategory(categoryId: number): Product[] { filterProductsByPriceRange(minPrice: number, maxPrice: number): Product[] { getOrder(orderId: number): Order | null { updateOrderStatus( addCategory(category: ProductCategory): void { removeCategory(categoryId: number): void { addOrder(order: Order): void { const store = new OnlineStore(); const electronicsCategory: ProductCategory = { id: 1, name: "Electronics" }; const laptop: Product = { store.removeProduct(2); const electronicsProducts = store.filterProductsByCategory(1); const affordableProducts = store.filterProductsByPriceRange(0, 100); const customer = new Customer(1, "Alice", "alice@example.com"); const order: Order = { store.addOrder(order); store.updateOrderStatus(1, "shipped"); const orders = customer.viewOrders(); const clothingCategory: ProductCategory = { id: 3, name: "Clothing" }; store.removeCategory(2); console.log(store); |
Beta Was this translation helpful? Give feedback.









Uh oh!
There was an error while loading. Please reload this page.
-
Task: Online Store Management System
You have been asked to develop an online store management system that allows you to manage products, product categories, and customers' orders. The system should let you add products and their categories, filter products by category or price range, and let customers place and view their orders.
Create the following interfaces and classes using TypeScript:
Interface
ProductCategorywith the following properties:id: number - A unique identifier for the product category.name: string - The name of the product category.Interface
Productwith the following properties and methods:id: number - A unique identifier for the product.name: string - The name of the product.price: number - The price of the product.category: ProductCategory - The category of the product.Interface
OrderItemwith the following properties:product: Product - The product in the order item.quantity: number - The quantity of the product in the order item.Class
Customerwith the following properties and methods:id: number - A unique identifier for the customer.name: string - The name of the customer.email: string - The email of the customer.orders: Order[] - An array of orders placed by the customer.placeOrder(order: Order): void - A method to place an order.viewOrders(): Order[] - A method to view all orders placed by the customer.Interface
Orderwith the following properties and methods:id: number - A unique identifier for the order.customer: Customer - The customer who placed the order.items: OrderItem[] - An array of order items in the order.status: "placed" | "shipped" | "delivered" - The status of the order.addItem(item: OrderItem): void - A method to add an item to the order.removeItem(productId: number): void - A method to remove an item from the order by product ID.Class
OnlineStorewith the following methods:addProduct(product: Product): void - A method to add a product to the store.removeProduct(productId: number): void - A method to remove a product from the store by its ID.filterProductsByCategory(categoryId: number): Product[] - A method to filter products by category ID.filterProductsByPriceRange(minPrice: number, maxPrice: number): Product[] - A method to filter products by price range.getOrder(orderId: number): Order | null - A method to get an order by its ID.updateOrderStatus(orderId: number, status: "placed" | "shipped" | "delivered"): void - A method to update the status of an order by its ID.Make use of arrow functions, classes, interfaces, and array methods such as
push,find,filter, andsplicein the implementation. This task requires a combination of logic and creativity, as it involves managing products, product categories, and customers' orders in an online store management system. The classes and interfaces represent different aspects of the online store management system, and you will need to implement methods to add and remove products and product categories, filter products, handle customer orders, and manage order statuses.Beta Was this translation helpful? Give feedback.
All reactions