Postage calculation –SPL Programming Practice

A certain B2C website needs to calculate the shipping cost of an order. In most cases, the shipping cost is determined by the total weight of the package. However, when the price of the order exceeds $300, free shipping is provided. The detailed rules …


This content originally appeared on DEV Community and was authored by Judy

A certain B2C website needs to calculate the shipping cost of an order. In most cases, the shipping cost is determined by the total weight of the package. However, when the price of the order exceeds $300, free shipping is provided. The detailed rules are shown in the mailCharge table below:


This table records the postage for each field within various value ranges. For example, the first record indicates that when the COST field value is between 300 and 1000000, the postage is 0 (free shipping); The second record indicates that when the value of the WEIGHT field is between 0 and 1 (kg), the postage is 10 (USD).

Here are some testOrders from the website:

Please calculate the detailed postage for these orders.

Find the records with COST and WEIGHT field values in the mailCharge records separately, and then loop through the entire order records. First, check whether the COST value in the order record meets the free standard. If it does not meet the standard, determine the postage level based on the weight.


Try.DEMO
A1 reads the mailCharge table, B1 reads the order data.

A2 selects free standard data, and since this is a single record, adds the @1 option; B2 Selects the weight billing standard and sort it in ascending order by weight range:

A3 adds a postage field to the order data, and when setting it, first determine whether the discount amount has been reached based on COST, and set the corresponding discount postage (free in this example); Otherwise, use SEGP to find the interval where WEIGHT is located in the postage standard and obtain the corresponding postage to complete the setting. Since the interval used is a left open and right closed interval, the @r option is added. The final result is as follows:

SPL is open-source. You can obtain the source code from GitHub .

Try it free~~


This content originally appeared on DEV Community and was authored by Judy


Print Share Comment Cite Upload Translate Updates
APA

Judy | Sciencx (2025-09-05T05:59:33+00:00) Postage calculation –SPL Programming Practice. Retrieved from https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/

MLA
" » Postage calculation –SPL Programming Practice." Judy | Sciencx - Friday September 5, 2025, https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/
HARVARD
Judy | Sciencx Friday September 5, 2025 » Postage calculation –SPL Programming Practice., viewed ,<https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/>
VANCOUVER
Judy | Sciencx - » Postage calculation –SPL Programming Practice. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/
CHICAGO
" » Postage calculation –SPL Programming Practice." Judy | Sciencx - Accessed . https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/
IEEE
" » Postage calculation –SPL Programming Practice." Judy | Sciencx [Online]. Available: https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/. [Accessed: ]
rf:citation
» Postage calculation –SPL Programming Practice | Judy | Sciencx | https://www.scien.cx/2025/09/05/postage-calculation-spl-programming-practice/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.