DECLARE TYPE CartItem IS RECORD ( item_name VARCHAR2(50), item_price NUMBER ); TYPE Cart IS TABLE OF CartItem; shopping_cart Cart := Cart(); total_cost NUMBER := 0; user_choice VARCHAR2(10); item_name VARCHAR2(50); item_price NUMBER; PROCEDURE AddItemToCart(i_name VARCHAR2, i_price NUMBER) IS BEGIN shopping_cart.EXTEND; shopping_cart(shopping_cart.LAST).item_name := i_name; shopping_cart(shopping_cart.LAST).item_price := i_price; total_cost := total_cost + i_price; DBMS_OUTPUT.PUT_LINE('Item added: ' || i_name || ' - $' || i_price); END AddItemToCart; PROCEDURE ViewCart IS BEGIN IF shopping_cart.COUNT = 0 THEN DBMS_OUTPUT.PUT_LINE('Your cart is empty.'); ELSE DBMS_OUTPUT.PUT_LINE('Items in your cart:'); FOR i IN 1..shopping_cart.COUNT LOOP DBMS_OUTPUT.PUT_LINE('- ' || shopping_cart(i).item_name || ': $' || shopping_cart(i).item_price); END LOOP; DBMS_OUTPUT.PUT_LINE('Total cost: $' || total_cost); END IF; END ViewCart; BEGIN DBMS_OUTPUT.PUT_LINE('Welcome to the shopping cart program!'); LOOP DBMS_OUTPUT.PUT_LINE('Choose an option:'); DBMS_OUTPUT.PUT_LINE('1: Add item to cart'); DBMS_OUTPUT.PUT_LINE('2: View cart'); DBMS_OUTPUT.PUT_LINE('3: Exit'); DBMS_OUTPUT.PUT('Enter your choice: '); -- Simulate user input -- (In a real application, you would use DBMS_INPUT or another method to get user input) user_choice := '1'; -- Replace this with actual user input retrieval IF user_choice = '1' THEN DBMS_OUTPUT.PUT('Enter item name: '); -- Simulate user input item_name := 'Sample Item'; -- Replace this with actual user input retrieval DBMS_OUTPUT.PUT('Enter item price: '); -- Simulate user input item_price := 19.99; -- Replace this with actual user input retrieval AddItemToCart(item_name, item_price); ELSIF user_choice = '2' THEN ViewCart; ELSIF user_choice = '3' THEN DBMS_OUTPUT.PUT_LINE('Thank you for shopping with us!'); EXIT; ELSE DBMS_OUTPUT.PUT_LINE('Invalid choice, please try again.'); END IF; -- Add a delay or wait for user input (not needed in this simulation) END LOOP; END;
Write, Run & Share PL/SQL code online using OneCompiler's Oracle PL/SQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for Oracle PL/SQL running on latest version 23c (23.3.0.0). Getting started with the OneCompiler's Oracle PL/SQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'PL/SQL' and start writing code to learn and test online without worrying about tedious process of installation.
PL/SQL is procedural extension for SQL created by Oracle. It is by default embedded into the Oracle Database. PL/SQL program units are compiled and stored inside the Oracle Database which results in optimal execution times as the PL/SQL and SQL run within the same server process.
Following is the syntax structure for the PL/SQL code blocks
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
DECLARE
message varchar2(100):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
CREATE OR REPLACE FUNCTION
hello_user
(user_name IN VARCHAR2)
RETURN VARCHAR2
IS
BEGIN
RETURN 'Hello ' || user_name;
END hello_user;
/
BEGIN
dbms_output.put_line(hello_user('Peter'));
END;
/
BEGIN
DBMS_OUTPUT.put_line (1/0);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('error is: ' || SQLERRM);
END;