Freightage Computation

 

Problem

A B2C website needs to compute the price of freightage for orders. In most cases, the freightage is computed by the total weight of the package, but free delivery is provided for orders over $300. Detailed rules are given in the mailCharge table below:

field minVal maxVal Charge
cost 300 1000000 0
weight 0 1 10
weight 1 5 20
weight 5 10 25
weight 10 1000000 40

The table above records the freightages for various value ranges. For example, the first record indicates that the freightage is zero (deliver for free) if the value of cost field is between 300 and 1,000,000.The second record indicates that when the value of weight field is between 0 and 1 kg, the freightage postage is 10 dollars.

The table below is some orders placed at this website:

id cost weight(kg)
Josh1 150 6
Drake 100 3
Megan 100 1
Josh2 200 3
Josh3 500 1

Please compute the freightages for these orders.

Tip

General steps: Respectively find the records whose values of field field are the cost and the weight. Then cycle all order records, during which first determine if the cost value in the order record is up to the free freightage threshold. If not satisfied, then determine the freightage level according to its weight.

Code

A B C D
1 =file("C:\\txt\\mailCharge.txt").import@t() /Select from mailCharge table
2 =file("C:\\txt\\testOrder.txt").import@t() /Select from testOrder table
3 =A1.select(FIELD=="COST") /Retrieve the record of free postage
4 =A1.select(FIELD=="WEIGHT").sort(-MINVAL) /Retrieve the record of charging according to postage
5 =A2.derive(POSTAGE) /Add postage record to the order table
6 for A5
7 if A3.MINVAL < A6.COST /When order price exceeds the level of free
8 >A6. POSTAGE= A3.CHARGE /Free of postage
9 next
10 for A4
11 if A6.WEIGHT > B10.MINVAL /Determine the postage level according to the weight
12 >A6. POSTAGE= B10.CHARGE
13 next A6

Result

imagepng