Skip to content

Instantly share code, notes, and snippets.

@tbanj
Created June 24, 2019 09:56
Show Gist options
  • Save tbanj/552918f69e8ed7b16eb7649a25c62281 to your computer and use it in GitHub Desktop.
Save tbanj/552918f69e8ed7b16eb7649a25c62281 to your computer and use it in GitHub Desktop.
Role Functional Assessment K001
let shoppingingcart = require("./ShoppingCart.js");
let items = function(name, price) {
this.name = name;
this.price = price;
};
let Oranges = new items("oranges", 10);
let Bananas = new items("Bananas", 10);
let Rice = new items("Rice", 10);
let Moimoi = new items("moimoi", 150);
const blog = new shoppingingcart(1000);
class CouponShoppingCart extends shoppingingcart {
constructor(totalCost) {
super(totalCost);
this.totalCost = totalCost;
}
subtractDiscount(coupon, paid_amount) {
console.log(goods.list);
console.log("delivery fee " + this.delivery());
console.log("net Total " + goods.netTotal());
console.log(`Coupon of ${coupon} is applied`);
console.log("subTotal " + goods.grossTotal(coupon));
console.log("Total delivery fee " + goods.calculateDeliveryFee());
try {
let finalPayment = goods.checkout(coupon, paid_amount);
if (finalPayment > 0) {
console.log("Your change is " + finalPayment);
}
} catch (error) {
console.log("Error encounter during " + finalPayment + "computation");
}
}
}
goods = new CouponShoppingCart();
goods.addItem(Oranges, 3);
goods.addItem(Bananas, 2);
goods.addItem(Rice, 12);
goods.subtractDiscount(1000, 10);
var items = function(name, price) {
this.name = name;
this.price = price;
};
class ShoppingCart {
constructor(totalCost) {
this.list = [];
this.totalCost = totalCost;
}
// additem
addItem(item, quantity) {
var element = {};
element.name = item.name;
element.price = item.price;
element.quantity = quantity;
this.list.push(element);
console.log(
"Add " +
quantity +
" " +
item.name +
" to the cart at a price of " +
item.price
);
return this.list;
}
delivery() {
var deliveryCost = 50;
return deliveryCost;
}
netTotal() {
var total = 0;
for (var i = 0; i < this.list.length; i++) {
total += this.list[i].price * this.list[i].quantity;
}
this.totalCost = total;
return this.totalCost;
}
grossTotal(coupon) {
var totalgross = 0;
totalgross = this.netTotal() + this.calculateDeliveryFee() - coupon;
return totalgross;
}
calculateDeliveryFee() {
var deliver = 0;
for (var i = 0; i < this.list.length; i++) {
deliver += this.list[i].quantity * this.delivery();
}
return deliver;
}
removeItem(item, quantity) {
try {
if (isNaN(item)) {
if (!isNaN(quantity)) {
var index = this.list.findIndex(x => x.name == na);
if (this.list[index].quantity > quantity) {
this.list[index].quantity -= quantity;
console.log("goods" + index);
console.log("=== Removing " + item + ". The new price is: ===");
console.log(this.netTotal());
// console.log( "mm " + this.list);
return this.netTotal();
} else if (this.list[index].quantity === quantity) {
this.list.splice(index, 1);
console.log("=== Removing " + item + ". The new price is: ===");
console.log("value = " + index);
return this.netTotal();
} else {
console.log(
"We only have " + this.list[index].quantity + " unit(s) currently"
);
console.log("quantity in cart is " + quantity);
return this.netTotal();
}
} else {
console.log(" Unacceptable value inputted for quantity ");
quantity = 0;
}
} else {
console.log(" Unacceptable value inputted ");
quantity = 0;
}
console.log(this.list);
return this.netTotal();
} catch (error) {
console.log(" bad ");
}
return this.netTotal();
}
checkout(coupon, paid_amount) {
var paidAmount = 0;
try {
if (!isNaN(paid_amount)) {
if (paid_amount > this.grossTotal(coupon)) {
paidAmount = paid_amount - this.grossTotal(coupon);
if (paidAmount >= 1) {
console.log(
"Cost of goods is: " +
this.netTotal() +
"\n" +
"delivery fee is: " +
this.calculateDeliveryFee()
);
console.log(
"Amount paid is: " +
paid_amount +
"\n" +
"Your balance is " +
paidAmount
);
console.log("Thanks for patronage");
return paidAmount;
} else {
console.log("Thanks for patronage");
return paidAmount;
}
} else {
this.list = [];
console.log("insufficient fund");
console.log(this.list);
console.log(paidAmount);
return paidAmount;
}
} else {
console.log(" Unable to compute checkout");
}
} catch (error) {
console.log("Error encounter unable to compute checkout");
}
return paidAmount;
}
}
shopping = new ShoppingCart();
// shopping.addItem({ name: "Oranges", price: 100 }, 3);
module.exports = ShoppingCart;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment