Search code examples
asp-classicparallel-arrays

need help to write this code to parallel arrays in classic ASP


if anyone can please help to convert this if else statements in to parallel array to find out calculate the discounts.

Parallel Arrays

is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory.
For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.

'discount calculations of total cost with GST

if TotGST >= 5000 AND TotGST <= 9999 then 
discount = (TotGST * 0.05)
else 
    if TotGST >= 10000 AND TotGST <= 49999 then
    discount = (TotGST * 0.08)
    else 
        if TotGST >= 50000 then 
        else        
        discount = (TotGST * 0.1)
        end if 
    end if 
end if 

Solution

  • First of all, set up the parallel arrays. Then loop through the arrays in a loop to find the matching range. If it matches, apply the discount.

    See sample code below

    <%       
    Function CalcDiscount(nAmount)
        ' Set up the arrays
        Amin = Array(5000,10000,50000)
        Amax = Array(9999,49999,-1)
        Adiscount = Array(0.05,0.08,0.10)
    
        ' Initialise other variables
        nUpper = uBound(Adiscount) 
        i = 0
        bDiscount = false
        CalcDiscount = 0
    
        ' Loop through the array to find a matching amount range
        do until (i > nUpper or bDiscount = true) 
            If (nAmount >= Amin(i) and (nAmount <= Amax(i) or Amax(i) = -1)) Then
                ' Apply discount
                CalcDiscount = nAmount * Adiscount(i)
                bDiscount = true
            End If
            i = i + 1
        loop
    
    End Function 
    
    ' Run some test cases
    TotGST = 1000
    nDiscount=CalcDiscount(TotGST)
    response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
    
    
    TotGST = 5000
    nDiscount=CalcDiscount(TotGST)
    response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
    
    
    TotGST = 5500
    nDiscount=CalcDiscount(TotGST)
    response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
    
    TotGST = 9999
    nDiscount=CalcDiscount(TotGST)
    response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
    
    
    TotGST = 10000
    nDiscount=CalcDiscount(TotGST)
    response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
    
    TotGST = 50000
    nDiscount=CalcDiscount(TotGST)
    response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
    
    %>
    

    Output

    TotGST=1000 Discount = 0
    TotGST=5000 Discount = 250
    TotGST=5500 Discount = 275
    TotGST=9999 Discount = 499.95
    TotGST=10000 Discount = 800
    TotGST=50000 Discount = 5000