Commit 7a188c06 authored by michael lundquist's avatar michael lundquist
Browse files

Adding hands on assignment for week 8.

parent 27dcf4b2
# ch2 hands on assignment part 1
## 2-1: Using Scalar variables
- Create a PL/SQL block containing the following variables:
| Name | Data Type | Option | Initial Value |
|--------------|--------------|----------|-------------------|
| lv_test_date | DATE | | December 10, 2012 |
| lv_test_num | NUMBER(3) | CONSTANT | 10 |
| lv_test_txt | VARCHAR2(10) | | |
- Assign your last name as the value of the text variable in the executable section of the block. Include statements in the block to display each variable's value onscreen.
```plsql
DECLARE
lv_test_date DATE := '10-DEC-2012';
lv_test_num CONSTANT NUMBER(3) := 10;
lv_test_txt VARCHAR2(10);
begin
lv_test_txt := 'Lundquist';
DBMS_OUTPUT.put_line('lv_test_date ' || lv_test_date);
DBMS_OUTPUT.put_line('lv_test_num ' || lv_test_num);
DBMS_OUTPUT.put_line('lv_test_txt ' || lv_test_txt);
end;
/
```
![PROBLEM1](./pics/PROBLEM1.PNG)
## 2-2: Creating a Flowchart
The Brewbean's application needs a block that determines whether a customer is rated high, mid, or low based on his or her total purchases. The block needs to determine the rating and then display the results onscreen. The code rates the customer high if total purchases are greater than $200, mid if greater than $100, and low if $100 or lower. Develop a flowchart to outline the conditional processing steps needed for this book.
```graphviz
digraph {
rankdir=TB;
h [label = "rating = high"];
m [label = "rating = medium"];
l [label = "rating = low"];
d [label = "purchases > $200" shape="diamond"];
d2 [label = "purchases > $100" shape="diamond"];
d->h [label="yes"];
d->d2 [label="no"];
d2->m [label="yes"];
d2->l [label="no"];
}
```
## 2-3 Using IF Statements
- Create a block using an IF statement to perform the actions described in Assignment 2-2. Use a scalar variable for the total purchase amount, and initialize this variable to different values to test your block.
```plsql
DECLARE
lv_purchases_num NUMBER(5, 2) := 100.00;
lv_rating_txt VARCHAR2(20) := 'low';
begin
IF lv_purchases_num > 200 THEN
lv_rating_txt := 'high';
elsif lv_purchases_num > 100 then
lv_rating_txt := 'medium';
end if;
dbms_output.put_line( 'the customer is rated ' || lv_rating_txt );
end;
/
```
![2-3](./pics/PROBLEM3.PNG)
## 2-4 Using CASE Statements
- Create a block using a CASE statement to perform the actions described in Assignment 2-2. Use a scalar variable for the total purchase amount, and initialize this variable to different values to test your block.
```plsql
DECLARE
lv_purchases_num NUMBER(5, 2) := 100.00;
lv_rating_txt VARCHAR2(20) := 'low';
begin
case
when lv_purchases_num > 200 THEN
lv_rating_txt := 'high';
when lv_purchases_num > 100 then
lv_rating_txt := 'medium';
else
lv_rating_txt := 'low';
end case;
dbms_output.put_line( 'the customer is rated ' || lv_rating_txt );
end;
/
```
![PROBLEM 4](./pics/PROBLEM4.PNG)
## 2-5 Using a Boolean Variable
- Brewbean's needs program code to indicate whether an amount is still due on an account when a payment is received. Create a PL/SQL block using a Boolean variable to indicate whether an amount is still due. Declare and initialize two variables to provide input for the account balance and the payment amount received. A TRUE Boolean value should indicate an amount is till owed, and a FALSE value should indicate the amount is paid in full. Use output statements to confirm the boolean variable is working correctly.
```plsql
Declare
lv_due_bool BOOLEAN;
lv_balance_num NUMBER(6,2) := 100;
lv_payment_num number(6,2) := 50;
begin
IF lv_balance_num > lv_payment_num THEN
lv_due_bool := TRUE;
ELSE
lv_due_bool := FALSE;
END IF;
dbms_output.put_line( 'balance is due? ' || sys.diutil.bool_to_int(lv_due_bool));
end;
/
```
![problem 5](./pics/PROBLEM5.PNG)
## 2-6 Using Looping Statements
- Create a block using a loop that determines the number of items that can be purchased based on the item prices and the total available to spend. (You could solve it with division, but you need to practice using loop structure.) The block should include statements to display the total number of items that can be purchased and the total amount spent.
```plsql
Declare
lv_cost_num NUMBER(6,2) := 10.0;
lv_balance_num NUMBER(6,2) := 122.3;
lv_items_num NUMBER(3,0) := 0;
begin
while lv_balance_num > lv_cost_num loop
lv_balance_num := lv_balance_num - lv_cost_num;
lv_items_num := lv_items_num + 1;
end loop;
dbms_output.put_line( 'You can purchase ' || lv_items_num || ' items');
end;
/
```
![problem 6](./pics/PROBLEM6.PNG)
## 2-7: Creating a Flowchart
- Brewbean's determines shipping costs based on the number of items ordered and club membership status. The applicable rates are shown in the following chart. Develop a flowchart to outline the condition-processing steps needed to handle this calculation.
| quantity of items | Nonmember shipping costs | Member Shipping Cost |
|-------------------|--------------------------|----------------------|
| up to 3 | $5.00 | $3.00 |
| 4-6 | $7.50 | $5.00 |
| 7-10 | $10.00 | $7.00 |
| More than 10 | $12.00 | $9.00 |
```graphviz
digraph{
d1 [label="more than 10 items?" shape="diamond"];
d2[label = "7-10 items?" shape="diamond"];
d3[label = "4-6 items?" shape="diamond"];
d4[label = "less than 4 items." shape="box"];
p1[label = "$9 member, $12 non-member"];
p2[label = "$7 member, $10 non-member"];
p3[label = "$5 member, $7.5 non-member"];
p4[label = "$3 member, $5 non-member"];
d1->p1 [label="yes"];
d1->d2 [label="no"];
d2->p2 [label="yes"];
d2->d3 [label="no"];
d3->p3 [label="yes"];
d3->d4 [label="no"];
d4->p4;
}
```
## Assignment 2-8: Using IF statements
- Create a block to accomplish the task outlined in Assignment 2-7. Include a variable containing a Y or N to indicate membership status and a variable to represent the number of items purchased. Test with a variety of values.
```plsql
Declare
lv_items_num NUMBER(3) := 6;
lv_membership_char CHAR(1) := 'Y';
lv_cost_num NUMBER(10,2) := 0;
begin
case
when lv_items_num > 10 then
if lv_membership_char = 'Y' then lv_cost_num := 9;
else lv_cost_num := 12;
end if;
when lv_items_num > 6 then
if lv_membership_char = 'Y' then lv_cost_num := 7;
else lv_cost_num := 10;
end if;
when lv_items_num > 3 then
if lv_membership_char = 'Y' then lv_cost_num := 5;
else lv_cost_num := 7.5;
end if;
else
if lv_membership_char = 'Y' then lv_cost_num := 5;
else lv_cost_num := 3;
end if;
end case;
dbms_output.put_line( 'Your cost will be $' || lv_cost_num );
end;
/
```
![PROBLEM1](./pics/PROBLEM8.PNG)
\ No newline at end of file
--SET SERVEROUTPUT ON;
/*
## 2-1: Using Scalar variables
- Create a PL/SQL block containing the following variables:
| Name | Data Type | Option | Initial Value |
|--------------|--------------|----------|-------------------|
| lv_test_date | DATE | | December 10, 2012 |
| lv_test_num | NUMBER(3) | CONSTANT | 10 |
| lv_test_txt | VARCHAR2(10) | | |
- Assign your last name as the value of the text variable in the executable section of the block. Include statements in the block to display each variable's value onscreen.
*/
DECLARE
lv_test_date DATE := '10-DEC-2012';
lv_test_num CONSTANT NUMBER(3) := 10;
lv_test_txt VARCHAR2(10);
begin
lv_test_txt := 'Lundquist';
DBMS_OUTPUT.put_line('lv_test_date ' || lv_test_date);
DBMS_OUTPUT.put_line('lv_test_num ' || lv_test_num);
DBMS_OUTPUT.put_line('lv_test_txt ' || lv_test_txt);
end;
/
/*
Create a block using an IF statement to perform the actions described in Assignment 2-2. Use a scalar variable for the total purchase amount, and initialize this variable to different values to test your block.
*/
DECLARE
lv_purchases_num NUMBER(5, 2) := 100.00;
lv_rating_txt VARCHAR2(20) := 'low';
begin
case
when lv_purchases_num > 200 THEN
lv_rating_txt := 'high';
when lv_purchases_num > 100 then
lv_rating_txt := 'medium';
else
lv_rating_txt := 'low';
end case;
dbms_output.put_line( 'the customer is rated ' || lv_rating_txt );
end;
/
/*
problem 5
Brewbean's needs program code to indicate whether an amount is still due on an account when a payment is received. Create a PL/SQL block using a Boolean variable to indicate whether an amount is still due. Declare and initialize two variables to provide input for the account balance and the payment amount received. A TRUE Boolean value should indicate an amount is till owed, and a FALSE value should indicate the amount is paid in full. Use output statements to confirm the boolean variable is working correctly.
*/
Declare
lv_due_bool BOOLEAN;
lv_balance_num NUMBER(6,2) := 100;
lv_payment_num number(6,2) := 50;
begin
IF lv_balance_num > lv_payment_num THEN
lv_due_bool := TRUE;
ELSE
lv_due_bool := FALSE;
END IF;
dbms_output.put_line( 'balance is due? ' || sys.diutil.bool_to_int(lv_due_bool));
end;
/
/*
problem 6
Create a block using a loop that determines the number of items that can be purchased based on the item prices and the total available to spend. (You could solve it with division, but you need to practice using loop structure.) The block should include statements to display the total number of items that can be purchased and the total amount spent.
*/
Declare
lv_cost_num NUMBER(6,2) := 10.0;
lv_balance_num NUMBER(6,2) := 122.3;
lv_items_num NUMBER(3,0) := 0;
begin
while lv_balance_num > lv_cost_num loop
lv_balance_num := lv_balance_num - lv_cost_num;
lv_items_num := lv_items_num + 1;
end loop;
dbms_output.put_line( 'You can purchase ' || lv_items_num || ' items');
end;
/
/*
Create a block to accomplish the task outlined in Assignment 2-7. Include a variable containing a Y or N to indicate membership status and a variable to represent the number of items purchased. Test with a variety of values.
*/
Declare
lv_items_num NUMBER(3) := 6;
lv_membership_char CHAR(1) := 'Y';
lv_cost_num NUMBER(10,2) := 0;
begin
case
when lv_items_num > 10 then
if lv_membership_char = 'Y' then lv_cost_num := 9;
else lv_cost_num := 12;
end if;
when lv_items_num > 6 then
if lv_membership_char = 'Y' then lv_cost_num := 7;
else lv_cost_num := 10;
end if;
when lv_items_num > 3 then
if lv_membership_char = 'Y' then lv_cost_num := 5;
else lv_cost_num := 7.5;
end if;
else
if lv_membership_char = 'Y' then lv_cost_num := 5;
else lv_cost_num := 3;
end if;
end case;
dbms_output.put_line( 'Your cost will be $' || lv_cost_num );
end;
/
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment