TicketSales

Demonstration of a program with two recursive functions that sells tickets to a collection of buyers while keeping track of what bills are spent and making change.

by CommandLineDesign

HTML

<div id="view-result"></div>
<div id="result-log"></div>

JavaScript

// :::::::::::::::::::::::: Tickets To sell ::::::::::::::::::::::::
var buyers = {
    'Beth': {
        1: 5,
        5: 4,
        10: 1,
        20: 2,
        50: 1,
        100: 0
    },
    'Jerry' : {
        1: 0,
        5: 1,
        10: 0,
        20: 1,
        50: 1,
        100: 0
    },
    'Rick' : {
        1: 0,
        5: 0,
        10: 0,
        20: 0,
        50: 0,
        100: 1
    },
    'Morty' : {
        1: 5,
        5: 0,
        10: 2,
        20: 1,
        50: 1,
        100: 0
    }
}
// :::::::::::::::::::::::: Classes ::::::::::::::::::::::::
// Define Seller Class
var Seller = function () {
    this.ticketPrice = 35;
    this.bills = {
        1: 0,
        5: 0,
        10: 0,
        20: 0,
        50: 0,
        100: 0
    };
    this.billKeys = Object.keys(this.bills);
    this.billKeys = this.billKeys.sort(function (a, b) {
        return b - a;
    });
    this.getBills = function(){return JSON.parse(JSON.stringify(this.bills))}
};
// Define Transaction Class
var Transaction = function () {
    this.bills = {
        1: 0,
        5: 0,
        10: 0,
        20: 0,
        50: 0,
        100: 0
    };
    this.getBills = function(){return JSON.parse(JSON.stringify(this.bills))}
    this.currentBill = 0;
};
// Define Customer Class
var Customer = function () {
    this.bills = {
        1: 0,
        5: 0,
        10: 0,
        20: 0,
        50: 0,
        100: 0
    };
    this.getBills = function(){return JSON.parse(JSON.stringify(this.bills))}
    this.billKeys = Object.keys(this.bills);
    this.billKeys = this.billKeys.sort(function (a, b) {
        return b - a;
    });
    this.currentBill = 0;
};
// Recursive function to determine which of the user's bills to spend
Seller.prototype.getBillsForTransaction = function () {
    if (this.transaction.remainingPrice > 0) {
        for (var i = 0; i < this.customer.billKeys.length; i++) {
            if (this.customer.bills[this.customer.billKeys[i]]) {
             ...