Skip to content

Instantly share code, notes, and snippets.

@udnisap
Last active February 9, 2017 05:46
Show Gist options
  • Save udnisap/9f5b6865cb3317e5b208e36f07170512 to your computer and use it in GitHub Desktop.
Save udnisap/9f5b6865cb3317e5b208e36f07170512 to your computer and use it in GitHub Desktop.
// main function
customers = readFile('customer.dat');
prefCustomers = readFile('preffrred.dat')
orders = readFile('orders.dat')
for (order in orders){
customer = order.getCustomer();
subtotal = order.calcuateTotal();
currentAmountSpent = customer.getAmountSpent;
discount = getEligibleDiscount(customer);
currentAmountSpent += subtotal * (1 - discount);
updatePreferedStatus(customer, currentAmountSpent)
}
writeToFile(customers, 'customer.dat');
writeToFile(prefCustomers, 'preffrred.dat');
// support functions
// this will read the given filename and split the text for new line and then split by comma
function readFile(filename) {
text = readTextFromFile(filename)
lines = text.split('\n');
data = []
for each line in lines:
line.split(',');
return data;
}
// this will calculate the total given an order
function calculateTotal(order){
return (
(order.getOunces() * order.getOuncePrice() ) +
order.getSquareInchPrice() * 2 * PI * order.getRadius() * order.getRadius() * order.getHeight()
) * order.getQuantity();
}
// this will return the eligible discount the customer is entitled to
function getEligibleDiscount(customer){
if (isPreffered(customer)){
return customer.discount;
} else {
return 0;
}
}
// this will update the amountspent to the customer and the discount
function updatePreferedStatus ( customer, currentAmountSpent){
if (currentAmountSpent > 350) {
customer.amountSpent = currentAmountSpent;
customer.discount = .1;
makePrefered(customer);
} else if ( currentAmountSpent > 200){
customer.amountSpent = currentAmountSpent;
customer.discount = .07;
makePrefered(customer);
} else if ( currentAmountSpent > 150){
customer.amountSpent = currentAmountSpent;
customer.discount = .05;
makePrefered(customer);
} else {
//not yet prefered
customer.amountSpent = currentAmountSpent;
updateInArray(customer, customers);
}
}
// make customer preffered if not already preffered
function makePrefered (customer){
if (!prefCustomers.contains(customer)){
prefCustomers.add(customer);
customers.remove(customer);
} else {
updateInArray(customer, customers);
}
}
// update in an array in place
function updateInArray(item, array){
index = array.find(item);
array[index] = item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment