Advertisement
jelledebock

Oefening4Package

Dec 19th, 2014
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 5.77 KB | None | 0 0
  1. CREATE OR REPLACE
  2. PACKAGE OEFENING4PACKAGE AS
  3.   TYPE order_product_rec_type IS RECORD
  4.   -- %%%%%%%%%%%%DATATYPES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5.   -- Step 2 of exercise
  6.   (
  7.     product_id        OE.product_information.product_id%TYPE,
  8.     product_name      OE.product_information.product_name%TYPE,
  9.     category_id       OE.product_information.category_id%TYPE,
  10.     product_status    OE.product_information.product_status%TYPE,
  11.     list_price        OE.product_information.list_price%TYPE,
  12.     min_price         OE.product_information.min_price%TYPE
  13.   );
  14.   -- Step 3 of exercise
  15.   TYPE order_product_tab_type IS TABLE OF order_product_rec_type;
  16.   -- %%%%%%%%%%%%%FUNCTIONS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17.   -- Step 4 of exercise
  18.   FUNCTION checkProduct(pid OE.product_information.product_id%TYPE) RETURN BOOLEAN;
  19.   -- Step 5 of exercise
  20.   FUNCTION checkCategory(cid OE.product_information.category_id%TYPE) RETURN BOOLEAN;
  21.   -- Step 6 of exercise
  22.   FUNCTION checkProductPrice
  23.     (
  24.       cid OE.product_information.category_id%TYPE,
  25.       lpri OE.product_information.list_price%TYPE,
  26.       mpri OE.product_information.min_price%TYPE
  27.     )
  28.     RETURN BOOLEAN;
  29.   -- %%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  30.   PROCEDURE addProduct(prod IN order_product_tab_type);
  31.   PROCEDURE printrecord(rec IN order_product_tab_type);
  32.   -- %%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  33.   no_category_found EXCEPTION;
  34.   product_bestaat EXCEPTION;
  35.   categorie_bestaat_niet EXCEPTION;
  36.   prijs_te_hoog EXCEPTION;
  37. END OEFENING4PACKAGE;
  38. /
  39. CREATE OR REPLACE
  40. PACKAGE BODY OEFENING4PACKAGE AS
  41.  FUNCTION checkProduct(pid OE.product_information.product_id%TYPE) RETURN BOOLEAN AS
  42.   occurences NUMBER;
  43.  BEGIN
  44.   SELECT COUNT(*) INTO occurences FROM OE.product_information
  45.   WHERE product_id = pid;
  46.  
  47.   IF occurences>0 THEN
  48.     RETURN TRUE;
  49.   ELSE
  50.     RETURN FALSE;
  51.   END IF;
  52.  END;
  53.   -- Step 5 of exercise
  54. FUNCTION checkCategory(cid OE.product_information.category_id%TYPE) RETURN BOOLEAN AS
  55.   occurences NUMBER;
  56.  BEGIN
  57.   SELECT COUNT(*) INTO occurences FROM OE.categories
  58.   WHERE category_id = cid;
  59.  
  60.   IF occurences>0 THEN
  61.     RETURN TRUE;
  62.   ELSE
  63.     RETURN FALSE;
  64.   END IF;
  65.  END;
  66.   -- Step 6 of exercise
  67. FUNCTION checkProductPrice
  68.     (
  69.       cid OE.product_information.category_id%TYPE,
  70.       lpri OE.product_information.list_price%TYPE,
  71.       mpri OE.product_information.min_price%TYPE
  72.     )
  73. RETURN BOOLEAN AS
  74.   clearflag BOOLEAN;
  75.   avg_listprice NUMBER;
  76.   avg_minprice NUMBER;
  77.  
  78.   perc1 NUMBER;
  79.   perc2 NUMBER;
  80. BEGIN
  81.   clearflag := checkCategory(cid);
  82.   IF clearflag THEN
  83.     SELECT AVG(list_price), AVG(min_price)
  84.     INTO avg_listprice, avg_minprice FROM OE.product_information
  85.     WHERE category_id = cid;
  86.    
  87.     perc1 := (lpri-avg_listprice)/avg_listprice;
  88.     perc2 := (mpri-avg_minprice)/avg_minprice;
  89.    
  90.     IF perc1 <= 0.2 AND perc2 <= 0.2 THEN
  91.       RETURN TRUE;
  92.     ELSE
  93.       RETURN FALSE;
  94.     END IF;        
  95.   ELSE
  96.     RAISE no_category_found;
  97.   END IF;
  98. END;
  99.   -- %%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  100. PROCEDURE addProduct(prod IN order_product_tab_type) IS
  101.  i INTEGER;
  102.  product order_product_rec_type;
  103. BEGIN
  104.   FOR i IN 1 .. prod.COUNT
  105.   LOOP
  106.     product := prod(i);
  107.     IF(checkProduct(product.product_id))
  108.     THEN
  109.       DBMS_OUTPUT.put_line('Bij product met id '||product.product_id);
  110.       RAISE product_bestaat;
  111.     ELSIF(checkCategory(product.category_id)!=TRUE)
  112.     THEN
  113.       DBMS_OUTPUT.put_line('Bij product met id '||product.product_id);
  114.       RAISE categorie_bestaat_niet;
  115.     ELSIF(checkProductPrice(product.category_id, product.list_price, product.min_price)!=TRUE)
  116.     THEN
  117.       DBMS_OUTPUT.put_line('Bij product met id '||product.product_id);
  118.       RAISE prijs_te_hoog;
  119.     ELSE
  120.       INSERT INTO OE.product_information(product_id, product_name, category_id,product_status, list_price, min_price)
  121.       VALUES(product.product_id, product.product_name, product.category_id,product.product_status,
  122.       product.list_price, product.min_price);
  123.       COMMIT;
  124.     END IF;
  125.   END LOOP;
  126. END;
  127. PROCEDURE printrecord(rec IN order_product_tab_type) IS
  128.   i INTEGER;
  129. BEGIN
  130.   FOR i IN 1 .. rec.COUNT
  131.   LOOP
  132.     DBMS_OUTPUT.put_line
  133.     (
  134.       '(' ||
  135.           rec(i).product_id || ', ' ||
  136.           rec(i).product_name || ', ' ||
  137.           rec(i).category_id || ', ' ||
  138.           rec(i).product_status || ', ' ||
  139.           rec(i).list_price || ', ' ||
  140.           rec(i).min_price  ||
  141.       ')'
  142.     );
  143.   END LOOP;
  144. END;
  145. END;
  146. /
  147. /********Test application **********************/
  148. DECLARE
  149.   producten oefening4package.order_product_tab_type;
  150.   entry1 oefening4package.order_product_rec_type;
  151.   entry2 oefening4package.order_product_rec_type;
  152. BEGIN
  153.   entry1.product_id := 4203;
  154.   entry1.product_name := 'test_product_1';
  155.   entry1.category_id := 11;
  156.   entry1.product_status := 'planned';
  157.   entry1.list_price := 1510;
  158.   entry1.min_price := 480;
  159.  
  160.   entry2.product_id := 4201;
  161.   entry2.product_name := 'test_product_2';
  162.   entry2.category_id := 11;
  163.   entry2.product_status := 'obsolete';
  164.   entry2.list_price := 520;
  165.   entry2.min_price := 470;
  166.  
  167.   producten := NEW oefening4package.order_product_tab_type();
  168.   producten.extend;
  169.   producten(1) := entry1;
  170.   producten.extend;
  171.   producten(2) := entry2;
  172.  
  173.   oefening4package.printrecord(producten);
  174.   BEGIN
  175.   oefening4package.addproduct(producten);
  176.   EXCEPTION
  177.     WHEN oefening4package.no_category_found THEN DBMS_OUTPUT.put_line('Opgegeven categorie bestaat niet');
  178.     WHEN oefening4package.product_bestaat THEN DBMS_OUTPUT.put_line('Opgegeven product bestaat al');
  179.     WHEN oefening4package.prijs_te_hoog THEN DBMS_OUTPUT.put_line('Opgegeven min en verkoopsprijs verschillen te veel');
  180.   END;
  181. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement