Task - TypeScript - Online Shopping Cart Management System #152
Replies: 6 comments 1 reply
-
CODEinterface CartItem {
id: number;
name: string;
price: number;
quantity: number;
addItem(item: CartItem): void;
updateQuantity(item: CartItem, newQuantity: number): void;
removeItem(item: CartItem): void;
}
class ShoppingCart implements CartItem {
private items: CartItem[] = [];
id: number;
name: string;
price: number;
quantity: number;
addItem(item: CartItem): void {
const existingItem = this.items.find((i) => i.id === item.id);
if (existingItem) {
existingItem.quantity += item.quantity;
} else {
this.items.push(item);
}
}
updateQuantity(item: CartItem, newQuantity: number): void {
const existingItem = this.items.find((i) => i.id === item.id);
if (existingItem) {
existingItem.quantity = newQuantity;
}
}
removeItem(item: CartItem): void {
this.items = this.items.filter((i) => i.id !== item.id);
}
getItems(): CartItem[] {
return this.items;
}
getTotalCost(): number {
return this.items.reduce((total, item) => total + item.price * item.quantity, 0);
}
}
const cart = new ShoppingCart();
cart.addItem({
id: 1, name: "Product A", price: 10, quantity: 2,
addItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
},
updateQuantity: function (item: CartItem, newQuantity: number): void {
throw new Error("Function not implemented.");
},
removeItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
}
});
cart.addItem({
id: 2, name: "Product B", price: 20, quantity: 1,
addItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
},
updateQuantity: function (item: CartItem, newQuantity: number): void {
throw new Error("Function not implemented.");
},
removeItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
}
});
cart.addItem({
id: 3, name: "Product C", price: 5, quantity: 4,
addItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
},
updateQuantity: function (item: CartItem, newQuantity: number): void {
throw new Error("Function not implemented.");
},
removeItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
}
});
console.log("Items in the cart:");
console.log(cart.getItems());
cart.updateQuantity({
id: 2,
name: "",
price: 0,
quantity: 0,
addItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
},
updateQuantity: function (item: CartItem, newQuantity: number): void {
throw new Error("Function not implemented.");
},
removeItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
}
}, 2);
console.log("Items in the cart after updating quantity:");
console.log(cart.getItems());
cart.removeItem({
id: 3,
name: "",
price: 0,
quantity: 0,
addItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
},
updateQuantity: function (item: CartItem, newQuantity: number): void {
throw new Error("Function not implemented.");
},
removeItem: function (item: CartItem): void {
throw new Error("Function not implemented.");
}
});
console.log("Items in the cart after removing an item:");
console.log(cart.getItems());
const totalCost = cart.getTotalCost();
console.log("Total cost of items in the cart: $" + totalCost);
OUTPUT |
Beta Was this translation helpful? Give feedback.
-
interface CartItem { class ShoppingCart implements CartItem {
} const cart = new ShoppingCart(); cart.addItem({ console.log("Items in the cart:"); cart.updateQuantity({ console.log("Items in the cart after updating quantity:"); cart.removeItem({ console.log("Items in the cart after removing an item:"); const totalCost = cart.getTotalCost(); console.log("Total cost of items in the cart: $" + totalCost); |
Beta Was this translation helpful? Give feedback.
-
CODE interface CartItem {
id: number;
name: string;
price: number;
quantity: number;
addItem(item: CartItem): void;
updateQuantity(item: CartItem, newQuantity: number): void;
removeItem(item: CartItem): void;
}
class ShoppingCart implements CartItem {
private items: CartItem[] = [];
public addItem(item: CartItem): void {
const existingItem = this.items.find((i) => i.id === item.id);
if (existingItem) {
existingItem.quantity += item.quantity;
} else {
this.items.push(item);
}
}
public updateQuantity(item: CartItem, newQuantity: number): void {
const existingItem = this.items.find((i) => i.id === item.id);
if (existingItem) {
existingItem.quantity = newQuantity;
}
}
public removeItem(item: CartItem): void {
this.items = this.items.filter((i) => i.id !== item.id);
}
public getItems(): CartItem[] {
return this.items;
}
public getTotalCost(): number {
return this.items.reduce((total, item) => total + item.price * item.quantity, 0);
}
}
const cart = new ShoppingCart();
cart.addItem({ id: 1, name: 'Item 1', price: 10, quantity: 2 });
cart.addItem({ id: 2, name: 'Item 2', price: 5, quantity: 3 });
cart.updateQuantity({ id: 1, name: 'Item 1', price: 10, quantity: 2 }, 4);
cart.removeItem({ id: 2, name: 'Item 2', price: 5, quantity: 3 });
console.log(cart.getItems());
console.log(cart.getTotalCost()); output |
Beta Was this translation helpful? Give feedback.
-
Codeinterface CartItem {
id : number;
name : string;
price : number;
quantity : number;
addItem(item: CartItem): void;
updateQuantity(item: CartItem, newQuantity: number): void;
removeItem(item: CartItem): void;
}
class ShoppingCart implements CartItem {
id: number;
name: string;
price: number;
quantity: number;
shoppingCartList: CartItem[];
constructor (id: number, name: string, price: number, quantity: number) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
this.shoppingCartList = [];
}
addItem = (item: CartItem): void => {
const availableItem = this.shoppingCartList.find((i) => i.id === item.id);
if (availableItem) {
availableItem.quantity += item.quantity;
console.log(`item with quantity ${item.quantity}`);
} else {
this.shoppingCartList.push(item);
}
}
updateQuantity = (item: CartItem, newQuantity : number) : void => {
const index = this.shoppingCartList.find((i) => i.id === item.id);
if (index) {
index.quantity = newQuantity;
} else {
console.log(`item is not available in cart`)
}
}
removeItem = (item: CartItem): void => {
const index = this.shoppingCartList.findIndex((i) => i.id === item.id);
if (index) {
this.shoppingCartList.splice(index, 1);
}
}
getItems(): CartItem[] {
return this.shoppingCartList;
}
}
const CartItem1 = new ShoppingCart(1, "milk", 25, 1);
CartItem1.addItem(new ShoppingCart(2, "tea powder", 50, 2));
console.log(CartItem1);
CartItem1.updateQuantity(new ShoppingCart(2, "tea powder", 50, 2), 6);
console.log(CartItem1);
CartItem1.removeItem(CartItem1);
console.log(CartItem1); Output |
Beta Was this translation helpful? Give feedback.
-
interface CartItem {
id: number;
name: string;
price: number;
quantity: number;
}
interface Cart {
addItem(item: CartItem): void;
updateQuantity(item: CartItem, newQuantity: number): void;
removeItem(item: CartItem): void;
getItems(): CartItem[];
getTotalCost(): number;
}
class ShoppingCart implements Cart {
id: number;
name: string;
price: number;
quantity: number;
items: CartItem[];
constructor(items: CartItem[] = []) {
this.items = items;
}
addItem = (item: CartItem): void => {
const isExist = this.items.find((i) => i.id === item.id);
if (isExist) {
isExist.quantity += item.quantity;
} else {
this.items.push(item);
}
};
updateQuantity = (item: CartItem, newQuantity: number): void => {
const chkItem = this.items.find((i) => i.id === item.id);
if (chkItem) {
chkItem.quantity = newQuantity;
}
};
removeItem = (item: CartItem): void => {
this.items = this.items.filter(i => i.id !== item.id);
};
getItems = (): CartItem[] => this.items;
getTotalCost = (): number => {
return this.items.reduce(
(total, item) => total + item.price * item.quantity,
0
);
};
}
const cart = new ShoppingCart();
const item1: CartItem = { id: 1, name: "Porsche", price: 1000, quantity: 1 };
const item2: CartItem = { id: 1, name: "Alpha Romio", price: 1000, quantity: 1 };
cart.addItem(item1);
cart.addItem(item2);
cart.getItems();
cart.getTotalCost();
cart.updateQuantity(item2, 2);
cart.getItems();
cart.getTotalCost();
cart.removeItem(item2);
cart.getItems();
cart.getTotalCost(); |
Beta Was this translation helpful? Give feedback.
-
interface CartItem { class ShoppingCart implements CartItem {
} const cart = new ShoppingCart(); cart.addItem({ console.log("Items in the cart:"); cart.updateQuantity({ console.log("Items in the cart after updating quantity:"); cart.removeItem({ console.log("Items in the cart after removing an item:"); const totalCost = cart.getTotalCost(); console.log("Total cost of items in the cart: $" + totalCost); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
You have been asked to develop an online shopping cart management system that handles adding items to the cart, updating their quantities, and removing them from the cart. The system should also provide a summary of the total cost of the items in the cart.
Create an interface
CartItem
with the following specifications:Properties:
Methods:
Create a class
ShoppingCart
that implements theCartItem
interface and add the following methods:Methods:
Make use of arrow functions and array methods such as push, find, and filter in the implementation.
Beta Was this translation helpful? Give feedback.
All reactions