recent

Titulo

Decision Structure

Who does not like the cheap gas price of Missouri? I always check the gas price whenever I pass by the gas station. Today (9/1/2015), the price of gas went down to $2.21 for regular, $2.41 for mid-grade and $2.61 for premium gasoline.  How does the gas pump know what to charge for the price of gas. Did you know the gas pump uses the simple IF logic to decide the gas price.
We don't know the programming language used behind the pump. Whatever the language maybe one thing is for sure, it uses the decision structure like IF or CASE. Assume the language used behind the pump is PL/SQL, how would your write the code for IF or CASE to use in the pump to determine the price of gas. Below, we will list all the possible IF and CASE structure to support the above pricing structure.

IF...THEN

SET serveroutput ON;
DECLARE
  lv_total_gas_price_num NUMBER NOT NULL := 1.00;
  lv_gas_type_char       VARCHAR2(10)DEFAULT 'Regular';
  lv_regular_gas_price   CONSTANT NUMBER(3,2) := 2.21;
  lv_midgrade_gas_price  CONSTANT NUMBER(3,2) :=2.41;
  lv_premium_gas_price   CONSTANT NUMBER(3,2) := 2.61;
  lv_num_of_gallon_num   NUMBER(4,2) NOT NULL :=1.00;

BEGIN
  lv_gas_type_char         := 'Premium';
  lv_num_of_gallon_num     := 20.00;
  IF lv_gas_type_char       = 'Regular' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_regular_gas_price;
  END IF;
  IF lv_gas_type_char       = 'MidGrade' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_midgrade_gas_price;
  END IF;
  IF lv_gas_type_char       = 'Premium' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_premium_gas_price;
  END IF;
  DBMS_output.put_line(lv_total_gas_price_num);
END;
/

PL/SQL procedure successfully completed.

52.2

IF...THEN...ELSE

SET serveroutput ON;
DECLARE
  lv_total_gas_price_num NUMBER NOT NULL := 1.00;
  lv_gas_type_char       VARCHAR2(10)DEFAULT 'Regular';
  lv_regular_gas_price   CONSTANT NUMBER(3,2) := 2.21;
  lv_midgrade_gas_price  CONSTANT NUMBER(3,2) :=2.41;
  lv_premium_gas_price   CONSTANT NUMBER(3,2) := 2.61;
  lv_num_of_gallon_num   NUMBER(4,2) NOT NULL :=1.00;
BEGIN
  lv_gas_type_char         := 'MidGrade';
  lv_num_of_gallon_num     := 20.00;
  IF lv_gas_type_char       = 'MidGrade' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_midgrade_gas_price;
  ELSE
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_premium_gas_price;
  END IF;
  DBMS_output.put_line(lv_total_gas_price_num);
END;
/

PL/SQL procedure successfully completed.

48.2

Note: The above decision can only support two gasoline types therefore I removed regular gasoline from the business logic.This code can be used in a gas station where they only sell 2 grades of gas.

IF...THEN...ELSIF

SET serveroutput ON;

DECLARE
  lv_total_gas_price_num NUMBER NOT NULL := 1.00;
  lv_gas_type_char       VARCHAR2(10)DEFAULT 'Regular';
  lv_regular_gas_price   CONSTANT NUMBER(3,2) := 2.21;
  lv_midgrade_gas_price  CONSTANT NUMBER(3,2) :=2.41;
  lv_premium_gas_price   CONSTANT NUMBER(3,2) := 2.61;
  lv_num_of_gallon_num   NUMBER(4,2) NOT NULL :=1.00;
BEGIN
  lv_gas_type_char         := 'Premium';
  lv_num_of_gallon_num     := 20.00;
  IF lv_gas_type_char       = 'Regular' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_regular_gas_price;
  ELSIF lv_gas_type_char       = 'MidGrade' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_midgrade_gas_price;
  ELSIF lv_gas_type_char       = 'Premium' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_premium_gas_price;
  END IF;
  DBMS_output.put_line(lv_total_gas_price_num);
END;
/

PL/SQL procedure successfully completed.

52.2

CASE

SET serveroutput ON;
 lv_total_gas_price_num NUMBER NOT NULL := 0.00;
  lv_gas_type_char       VARCHAR2(10)DEFAULT 'Regular';
  lv_regular_gas_price   CONSTANT NUMBER(3,2) := 2.21;
  lv_midgrade_gas_price  CONSTANT NUMBER(3,2) :=2.41;
  lv_premium_gas_price   CONSTANT NUMBER(3,2) := 2.61;
  lv_num_of_gallon_num   NUMBER(4,2) NOT NULL :=1.00;
BEGIN
  lv_gas_type_char     := 'Premium';
  lv_num_of_gallon_num := 20.00;
  CASE lv_gas_type_char
  WHEN 'Regular' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_regular_gas_price;
  WHEN 'MidGrade' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_midgrade_gas_price;
  WHEN 'Premium' THEN
    lv_total_gas_price_num := lv_num_of_gallon_num * lv_premium_gas_price;
  ELSE
    dbms_output.put_line('No Such Grade');
  END CASE;
  DBMS_output.put_line(lv_total_gas_price_num);
END;
/

PL/SQL procedure successfully completed.

52.2


Interested in working with me? I can be reached at pbaniya04[at]gmail.com for any questions, consulting opportunities or you may drop a line to say HELLO. Thank your again for visiting my blog and looking forward to serving you more.

Have a Database-ious Day!

No comments

Powered by Blogger.